|
| 1 | +import { describe, it, expect, vi } from 'vitest'; |
| 2 | +import { render, screen } from '@testing-library/react'; |
| 3 | +import userEvent from '@testing-library/user-event'; |
| 4 | +import { DateInput } from './DateInput'; |
| 5 | + |
| 6 | +describe('DateInput', () => { |
| 7 | + it('renders a wrapper div', () => { |
| 8 | + const { container } = render(<DateInput data-testid="input" />); |
| 9 | + expect( |
| 10 | + container.querySelector('.dsn-date-input-wrapper') |
| 11 | + ).toBeInTheDocument(); |
| 12 | + }); |
| 13 | + |
| 14 | + it('renders an input element inside wrapper', () => { |
| 15 | + render(<DateInput data-testid="input" />); |
| 16 | + expect(screen.getByTestId('input').tagName).toBe('INPUT'); |
| 17 | + }); |
| 18 | + |
| 19 | + it('has date type', () => { |
| 20 | + render(<DateInput data-testid="input" />); |
| 21 | + expect(screen.getByTestId('input')).toHaveAttribute('type', 'date'); |
| 22 | + }); |
| 23 | + |
| 24 | + it('always has base dsn-text-input class on input', () => { |
| 25 | + render(<DateInput data-testid="input" />); |
| 26 | + expect(screen.getByTestId('input')).toHaveClass('dsn-text-input'); |
| 27 | + }); |
| 28 | + |
| 29 | + it('always has dsn-date-input class on input', () => { |
| 30 | + render(<DateInput data-testid="input" />); |
| 31 | + expect(screen.getByTestId('input')).toHaveClass('dsn-date-input'); |
| 32 | + }); |
| 33 | + |
| 34 | + it('applies custom className to input', () => { |
| 35 | + render(<DateInput className="custom" data-testid="input" />); |
| 36 | + const el = screen.getByTestId('input'); |
| 37 | + expect(el).toHaveClass('dsn-text-input'); |
| 38 | + expect(el).toHaveClass('dsn-date-input'); |
| 39 | + expect(el).toHaveClass('custom'); |
| 40 | + }); |
| 41 | + |
| 42 | + it('forwards ref to input element', () => { |
| 43 | + const ref = { current: null as HTMLInputElement | null }; |
| 44 | + render(<DateInput ref={ref} />); |
| 45 | + expect(ref.current).toBeInstanceOf(HTMLInputElement); |
| 46 | + }); |
| 47 | + |
| 48 | + it('spreads additional HTML attributes to input', () => { |
| 49 | + render(<DateInput id="date" data-testid="input" />); |
| 50 | + const el = screen.getByTestId('input'); |
| 51 | + expect(el).toHaveAttribute('id', 'date'); |
| 52 | + }); |
| 53 | + |
| 54 | + it('accepts value prop', () => { |
| 55 | + render( |
| 56 | + <DateInput value="2025-03-15" onChange={() => {}} data-testid="input" /> |
| 57 | + ); |
| 58 | + expect(screen.getByTestId('input')).toHaveValue('2025-03-15'); |
| 59 | + }); |
| 60 | + |
| 61 | + it('wrapper always has fixed width (no width prop)', () => { |
| 62 | + const { container } = render(<DateInput />); |
| 63 | + const wrapper = container.querySelector('.dsn-date-input-wrapper'); |
| 64 | + expect(wrapper?.className).toBe('dsn-date-input-wrapper'); |
| 65 | + }); |
| 66 | + |
| 67 | + describe('calendar button', () => { |
| 68 | + it('renders a calendar button', () => { |
| 69 | + const { container } = render(<DateInput />); |
| 70 | + expect( |
| 71 | + container.querySelector('.dsn-date-input__button') |
| 72 | + ).toBeInTheDocument(); |
| 73 | + }); |
| 74 | + |
| 75 | + it('calendar button is a Button component (has dsn-button class)', () => { |
| 76 | + const { container } = render(<DateInput />); |
| 77 | + const button = container.querySelector('.dsn-date-input__button'); |
| 78 | + expect(button).toHaveClass('dsn-button'); |
| 79 | + }); |
| 80 | + |
| 81 | + it('calendar button uses subtle variant', () => { |
| 82 | + const { container } = render(<DateInput />); |
| 83 | + const button = container.querySelector('.dsn-date-input__button'); |
| 84 | + expect(button).toHaveClass('dsn-button--subtle'); |
| 85 | + }); |
| 86 | + |
| 87 | + it('calendar button uses small size', () => { |
| 88 | + const { container } = render(<DateInput />); |
| 89 | + const button = container.querySelector('.dsn-date-input__button'); |
| 90 | + expect(button).toHaveClass('dsn-button--size-small'); |
| 91 | + }); |
| 92 | + |
| 93 | + it('calendar button uses icon-only', () => { |
| 94 | + const { container } = render(<DateInput />); |
| 95 | + const button = container.querySelector('.dsn-date-input__button'); |
| 96 | + expect(button).toHaveClass('dsn-button--icon-only'); |
| 97 | + }); |
| 98 | + |
| 99 | + it('calendar button has tabIndex -1', () => { |
| 100 | + const { container } = render(<DateInput />); |
| 101 | + const button = container.querySelector('.dsn-date-input__button'); |
| 102 | + expect(button).toHaveAttribute('tabindex', '-1'); |
| 103 | + }); |
| 104 | + |
| 105 | + it('calendar button has type button', () => { |
| 106 | + const { container } = render(<DateInput />); |
| 107 | + const button = container.querySelector('.dsn-date-input__button'); |
| 108 | + expect(button).toHaveAttribute('type', 'button'); |
| 109 | + }); |
| 110 | + |
| 111 | + it('calendar button has aria-hidden', () => { |
| 112 | + const { container } = render(<DateInput />); |
| 113 | + const button = container.querySelector('.dsn-date-input__button'); |
| 114 | + expect(button).toHaveAttribute('aria-hidden', 'true'); |
| 115 | + }); |
| 116 | + |
| 117 | + it('calendar button has visually hidden label text', () => { |
| 118 | + const { container } = render(<DateInput />); |
| 119 | + const hiddenLabel = container.querySelector( |
| 120 | + '.dsn-date-input__button-label' |
| 121 | + ); |
| 122 | + expect(hiddenLabel).toBeInTheDocument(); |
| 123 | + expect(hiddenLabel).toHaveTextContent('Datumkiezer openen'); |
| 124 | + }); |
| 125 | + |
| 126 | + it('does not render calendar button when disabled', () => { |
| 127 | + const { container } = render(<DateInput disabled data-testid="input" />); |
| 128 | + expect( |
| 129 | + container.querySelector('.dsn-date-input__button') |
| 130 | + ).not.toBeInTheDocument(); |
| 131 | + }); |
| 132 | + |
| 133 | + it('does not render calendar button when readOnly', () => { |
| 134 | + const { container } = render(<DateInput readOnly data-testid="input" />); |
| 135 | + expect( |
| 136 | + container.querySelector('.dsn-date-input__button') |
| 137 | + ).not.toBeInTheDocument(); |
| 138 | + }); |
| 139 | + |
| 140 | + it('calls showPicker on button click when available', async () => { |
| 141 | + const user = userEvent.setup(); |
| 142 | + const showPicker = vi.fn(); |
| 143 | + const { container } = render(<DateInput data-testid="input" />); |
| 144 | + const input = screen.getByTestId('input') as HTMLInputElement; |
| 145 | + Object.defineProperty(input, 'showPicker', { |
| 146 | + value: showPicker, |
| 147 | + writable: true, |
| 148 | + }); |
| 149 | + const button = container.querySelector( |
| 150 | + '.dsn-date-input__button' |
| 151 | + ) as HTMLButtonElement; |
| 152 | + await user.click(button); |
| 153 | + expect(showPicker).toHaveBeenCalledOnce(); |
| 154 | + }); |
| 155 | + }); |
| 156 | + |
| 157 | + it('can be disabled', () => { |
| 158 | + render(<DateInput disabled data-testid="input" />); |
| 159 | + expect(screen.getByTestId('input')).toBeDisabled(); |
| 160 | + }); |
| 161 | + |
| 162 | + it('can be read-only', () => { |
| 163 | + render(<DateInput readOnly data-testid="input" />); |
| 164 | + expect(screen.getByTestId('input')).toHaveAttribute('readOnly'); |
| 165 | + }); |
| 166 | + |
| 167 | + it('can be required', () => { |
| 168 | + render(<DateInput required data-testid="input" />); |
| 169 | + expect(screen.getByTestId('input')).toBeRequired(); |
| 170 | + }); |
| 171 | + |
| 172 | + describe('invalid state', () => { |
| 173 | + it('sets aria-invalid when invalid prop is true', () => { |
| 174 | + render(<DateInput invalid data-testid="input" />); |
| 175 | + expect(screen.getByTestId('input')).toHaveAttribute( |
| 176 | + 'aria-invalid', |
| 177 | + 'true' |
| 178 | + ); |
| 179 | + }); |
| 180 | + |
| 181 | + it('does not set aria-invalid when invalid prop is false', () => { |
| 182 | + render(<DateInput invalid={false} data-testid="input" />); |
| 183 | + expect(screen.getByTestId('input')).not.toHaveAttribute('aria-invalid'); |
| 184 | + }); |
| 185 | + |
| 186 | + it('does not set aria-invalid by default', () => { |
| 187 | + render(<DateInput data-testid="input" />); |
| 188 | + expect(screen.getByTestId('input')).not.toHaveAttribute('aria-invalid'); |
| 189 | + }); |
| 190 | + }); |
| 191 | + |
| 192 | + describe('accessibility', () => { |
| 193 | + it('can have aria-describedby', () => { |
| 194 | + render(<DateInput aria-describedby="help-text" data-testid="input" />); |
| 195 | + expect(screen.getByTestId('input')).toHaveAttribute( |
| 196 | + 'aria-describedby', |
| 197 | + 'help-text' |
| 198 | + ); |
| 199 | + }); |
| 200 | + |
| 201 | + it('can have aria-labelledby', () => { |
| 202 | + render(<DateInput aria-labelledby="label-id" data-testid="input" />); |
| 203 | + expect(screen.getByTestId('input')).toHaveAttribute( |
| 204 | + 'aria-labelledby', |
| 205 | + 'label-id' |
| 206 | + ); |
| 207 | + }); |
| 208 | + }); |
| 209 | +}); |
0 commit comments