|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | +import { render, screen } from '@testing-library/react'; |
| 3 | +import { createRef } from 'react'; |
| 4 | +import { Stack } from './Stack'; |
| 5 | + |
| 6 | +describe('Stack', () => { |
| 7 | + it('renders children', () => { |
| 8 | + render( |
| 9 | + <Stack> |
| 10 | + <div>Item 1</div> |
| 11 | + <div>Item 2</div> |
| 12 | + </Stack> |
| 13 | + ); |
| 14 | + expect(screen.getByText('Item 1')).toBeInTheDocument(); |
| 15 | + expect(screen.getByText('Item 2')).toBeInTheDocument(); |
| 16 | + }); |
| 17 | + |
| 18 | + it('renders as a <div> element', () => { |
| 19 | + const { container } = render(<Stack />); |
| 20 | + expect(container.firstChild?.nodeName).toBe('DIV'); |
| 21 | + }); |
| 22 | + |
| 23 | + it('always has base dsn-stack class', () => { |
| 24 | + const { container } = render(<Stack />); |
| 25 | + expect(container.firstChild).toHaveClass('dsn-stack'); |
| 26 | + }); |
| 27 | + |
| 28 | + it('does not add space modifier class for default (md)', () => { |
| 29 | + const { container } = render(<Stack />); |
| 30 | + expect(container.firstChild).not.toHaveClass('dsn-stack--space-md'); |
| 31 | + }); |
| 32 | + |
| 33 | + it('does not add space modifier class for explicit md', () => { |
| 34 | + const { container } = render(<Stack space="md" />); |
| 35 | + expect(container.firstChild).not.toHaveClass('dsn-stack--space-md'); |
| 36 | + }); |
| 37 | + |
| 38 | + it.each(['sm', 'lg', 'xl', '2xl', '3xl', '4xl', '5xl', '6xl'] as const)( |
| 39 | + 'applies space modifier class for %s', |
| 40 | + (space) => { |
| 41 | + const { container } = render(<Stack space={space} />); |
| 42 | + expect(container.firstChild).toHaveClass(`dsn-stack--space-${space}`); |
| 43 | + } |
| 44 | + ); |
| 45 | + |
| 46 | + it('applies custom className', () => { |
| 47 | + const { container } = render(<Stack className="custom" />); |
| 48 | + expect(container.firstChild).toHaveClass('dsn-stack'); |
| 49 | + expect(container.firstChild).toHaveClass('custom'); |
| 50 | + }); |
| 51 | + |
| 52 | + it('forwards ref to the div element', () => { |
| 53 | + const ref = createRef<HTMLDivElement>(); |
| 54 | + const { container } = render(<Stack ref={ref} />); |
| 55 | + expect(ref.current).toBe(container.firstChild); |
| 56 | + }); |
| 57 | + |
| 58 | + it('passes additional HTML attributes', () => { |
| 59 | + const { container } = render(<Stack data-testid="stack" role="list" />); |
| 60 | + expect(container.firstChild).toHaveAttribute('data-testid', 'stack'); |
| 61 | + expect(container.firstChild).toHaveAttribute('role', 'list'); |
| 62 | + }); |
| 63 | +}); |
0 commit comments