|
| 1 | +import { render } from '@testing-library/react'; |
| 2 | +import userEvent from '@testing-library/user-event'; |
| 3 | +import React from 'react'; |
| 4 | +import CustomDate from '../Date'; |
| 5 | + |
| 6 | +describe('Date component', () => { |
| 7 | + it('renders with initial value', () => { |
| 8 | + const initialValue = new Date('2024-03-04'); |
| 9 | + const { asFragment, getByDisplayValue } = render( |
| 10 | + <CustomDate value={initialValue} /> |
| 11 | + ); |
| 12 | + |
| 13 | + getByDisplayValue('2024-03-04'); |
| 14 | + |
| 15 | + expect(asFragment()).toMatchSnapshot(); |
| 16 | + }); |
| 17 | + |
| 18 | + it('calls onChange handler when input value changes', () => { |
| 19 | + const onChangeMock = jest.fn(); |
| 20 | + const initialValue = new Date('2024-03-04'); |
| 21 | + const { getByLabelText, asFragment } = render( |
| 22 | + <> |
| 23 | + {/* eslint-disable-next-line jsx-a11y/label-has-associated-control */} |
| 24 | + <label htmlFor="date">Date:</label> |
| 25 | + <CustomDate |
| 26 | + onChange={onChangeMock} |
| 27 | + name="date" |
| 28 | + id="date" |
| 29 | + value={initialValue} |
| 30 | + /> |
| 31 | + </> |
| 32 | + ); |
| 33 | + const inputElement = getByLabelText('Date:') as HTMLInputElement; |
| 34 | + |
| 35 | + userEvent.type(inputElement, '2024-03-05'); |
| 36 | + |
| 37 | + expect(onChangeMock).toHaveBeenCalledWith({ |
| 38 | + value: new Date('2024-03-05'), |
| 39 | + name: 'date', |
| 40 | + id: 'date', |
| 41 | + }); |
| 42 | + |
| 43 | + expect(inputElement.value).toBe('2024-03-05'); |
| 44 | + |
| 45 | + expect(asFragment()).toMatchSnapshot(); |
| 46 | + }); |
| 47 | + |
| 48 | + it('calls onChange handler when input value changes although target value is null', () => { |
| 49 | + const initialValue = new Date('2024-03-04'); |
| 50 | + const onChangeMock = jest.fn(); |
| 51 | + const { getByLabelText, asFragment } = render( |
| 52 | + <> |
| 53 | + {/* eslint-disable-next-line jsx-a11y/label-has-associated-control */} |
| 54 | + <label htmlFor="date">Date:</label> |
| 55 | + <CustomDate |
| 56 | + onChange={onChangeMock} |
| 57 | + name="date" |
| 58 | + id="date" |
| 59 | + value={initialValue} |
| 60 | + /> |
| 61 | + </> |
| 62 | + ); |
| 63 | + const inputElement = getByLabelText('Date:') as HTMLInputElement; |
| 64 | + |
| 65 | + userEvent.clear(inputElement); |
| 66 | + |
| 67 | + expect(inputElement.value).toBe(''); |
| 68 | + |
| 69 | + expect(onChangeMock).toHaveBeenCalledWith({ |
| 70 | + value: null, |
| 71 | + name: 'date', |
| 72 | + id: 'date', |
| 73 | + }); |
| 74 | + |
| 75 | + expect(asFragment()).toMatchSnapshot(); |
| 76 | + }); |
| 77 | + |
| 78 | + it('applies class modifier', () => { |
| 79 | + const { container, asFragment } = render( |
| 80 | + <CustomDate classModifier="required" /> |
| 81 | + ); |
| 82 | + container.querySelector('.af-form__input-date--required'); |
| 83 | + |
| 84 | + expect(asFragment()).toMatchSnapshot(); |
| 85 | + }); |
| 86 | +}); |
0 commit comments