|
| 1 | +import { render, screen } from '@testing-library/react'; |
| 2 | +import { describe, expect, it, vi } from 'vitest'; |
| 3 | +import CardContainer from '@/components/Card/CardContainer'; |
| 4 | +import '@testing-library/jest-dom'; |
| 5 | + |
| 6 | +// Mock Carbon Grid component |
| 7 | +vi.mock('@carbon/react', () => ({ |
| 8 | + Grid: ({ className, children }: any) => ( |
| 9 | + <div data-testid="carbon-grid" className={className}> |
| 10 | + {children} |
| 11 | + </div> |
| 12 | + ), |
| 13 | +})); |
| 14 | + |
| 15 | +describe('CardContainer', () => { |
| 16 | + it('renders Grid component', () => { |
| 17 | + render(<CardContainer>Content</CardContainer>); |
| 18 | + expect(screen.getByTestId('carbon-grid')).toBeInTheDocument(); |
| 19 | + }); |
| 20 | + |
| 21 | + it('renders children content', () => { |
| 22 | + render( |
| 23 | + <CardContainer> |
| 24 | + <span data-testid="child-content">Test Content</span> |
| 25 | + </CardContainer> |
| 26 | + ); |
| 27 | + expect(screen.getByTestId('child-content')).toBeInTheDocument(); |
| 28 | + }); |
| 29 | + |
| 30 | + it('applies card-container-grid class', () => { |
| 31 | + render(<CardContainer>Content</CardContainer>); |
| 32 | + expect(screen.getByTestId('carbon-grid')).toHaveClass('card-container-grid'); |
| 33 | + }); |
| 34 | + |
| 35 | + it('applies default-grid class', () => { |
| 36 | + render(<CardContainer>Content</CardContainer>); |
| 37 | + expect(screen.getByTestId('carbon-grid')).toHaveClass('default-grid'); |
| 38 | + }); |
| 39 | + |
| 40 | + it('applies custom className when provided', () => { |
| 41 | + render( |
| 42 | + <CardContainer className="custom-class"> |
| 43 | + Content |
| 44 | + </CardContainer> |
| 45 | + ); |
| 46 | + expect(screen.getByTestId('carbon-grid')).toHaveClass('custom-class'); |
| 47 | + }); |
| 48 | + |
| 49 | + it('applies both default and custom classes', () => { |
| 50 | + render( |
| 51 | + <CardContainer className="my-custom-class"> |
| 52 | + Content |
| 53 | + </CardContainer> |
| 54 | + ); |
| 55 | + const grid = screen.getByTestId('carbon-grid'); |
| 56 | + expect(grid).toHaveClass('card-container-grid'); |
| 57 | + expect(grid).toHaveClass('default-grid'); |
| 58 | + expect(grid).toHaveClass('my-custom-class'); |
| 59 | + }); |
| 60 | + |
| 61 | + it('renders multiple children', () => { |
| 62 | + render( |
| 63 | + <CardContainer> |
| 64 | + <div data-testid="child-1">First</div> |
| 65 | + <div data-testid="child-2">Second</div> |
| 66 | + <div data-testid="child-3">Third</div> |
| 67 | + </CardContainer> |
| 68 | + ); |
| 69 | + expect(screen.getByTestId('child-1')).toBeInTheDocument(); |
| 70 | + expect(screen.getByTestId('child-2')).toBeInTheDocument(); |
| 71 | + expect(screen.getByTestId('child-3')).toBeInTheDocument(); |
| 72 | + }); |
| 73 | + |
| 74 | + it('renders with no children', () => { |
| 75 | + const { container } = render(<CardContainer />); |
| 76 | + expect(container.querySelector('[data-testid="carbon-grid"]')).toBeInTheDocument(); |
| 77 | + }); |
| 78 | + |
| 79 | + it('renders with string children', () => { |
| 80 | + render(<CardContainer>Just text</CardContainer>); |
| 81 | + expect(screen.getByText('Just text')).toBeInTheDocument(); |
| 82 | + }); |
| 83 | + |
| 84 | + it('renders with no className', () => { |
| 85 | + render(<CardContainer>Content</CardContainer>); |
| 86 | + const grid = screen.getByTestId('carbon-grid'); |
| 87 | + expect(grid.className).toContain('card-container-grid'); |
| 88 | + expect(grid.className).toContain('default-grid'); |
| 89 | + }); |
| 90 | + |
| 91 | + it('combines multiple class names correctly', () => { |
| 92 | + render( |
| 93 | + <CardContainer className="first-class second-class"> |
| 94 | + Content |
| 95 | + </CardContainer> |
| 96 | + ); |
| 97 | + const grid = screen.getByTestId('carbon-grid'); |
| 98 | + expect(grid).toHaveClass('card-container-grid'); |
| 99 | + expect(grid).toHaveClass('default-grid'); |
| 100 | + expect(grid).toHaveClass('first-class'); |
| 101 | + expect(grid).toHaveClass('second-class'); |
| 102 | + }); |
| 103 | + |
| 104 | + it('renders complex component children', () => { |
| 105 | + const ComplexChild = () => <div data-testid="complex">Complex Component</div>; |
| 106 | + render( |
| 107 | + <CardContainer> |
| 108 | + <ComplexChild /> |
| 109 | + </CardContainer> |
| 110 | + ); |
| 111 | + expect(screen.getByTestId('complex')).toBeInTheDocument(); |
| 112 | + }); |
| 113 | + |
| 114 | + it('maintains children order', () => { |
| 115 | + const { container } = render( |
| 116 | + <CardContainer> |
| 117 | + <div data-testid="first">1</div> |
| 118 | + <div data-testid="second">2</div> |
| 119 | + <div data-testid="third">3</div> |
| 120 | + </CardContainer> |
| 121 | + ); |
| 122 | + const children = container.querySelectorAll('[data-testid^="second"], [data-testid^="first"], [data-testid^="third"]'); |
| 123 | + expect(children.length).toBe(3); |
| 124 | + }); |
| 125 | +}); |
0 commit comments