|
| 1 | +import { render, screen } from '@testing-library/react'; |
| 2 | +import { describe, expect, it, vi } from 'vitest'; |
| 3 | +import { Datepicker } from './Datepicker'; |
| 4 | +import { getFormattedDate } from './helpers'; |
| 5 | +import userEvent from '@testing-library/user-event'; |
| 6 | + |
| 7 | +describe('Components / Datepicker', () => { |
| 8 | + it("should display today's date by default", () => { |
| 9 | + const todaysDateInDefaultLanguage = getFormattedDate('en', new Date()); |
| 10 | + |
| 11 | + render(<Datepicker />); |
| 12 | + |
| 13 | + expect(screen.getByDisplayValue(todaysDateInDefaultLanguage)).toBeInTheDocument(); |
| 14 | + }); |
| 15 | + |
| 16 | + it('should update date when a different day is clicked', async () => { |
| 17 | + const todaysDayOfMonth = new Date().getDate(); |
| 18 | + const anotherDay = todaysDayOfMonth === 1 ? 2 : 1; |
| 19 | + |
| 20 | + render(<Datepicker />); |
| 21 | + |
| 22 | + await userEvent.click(screen.getByRole('textbox')); |
| 23 | + await userEvent.click(screen.getAllByText(anotherDay)[0]); |
| 24 | + |
| 25 | + expect((screen.getByRole('textbox') as HTMLInputElement).value?.includes(`${anotherDay}`)).toBeTruthy(); |
| 26 | + }); |
| 27 | + |
| 28 | + it("should reset to today's date when Clear button is clicked", async () => { |
| 29 | + const todaysDateInDefaultLanguage = getFormattedDate('en', new Date()); |
| 30 | + const todaysDayOfMonth = new Date().getDate(); |
| 31 | + const anotherDay = todaysDayOfMonth === 1 ? 2 : 1; |
| 32 | + |
| 33 | + render(<Datepicker />); |
| 34 | + |
| 35 | + await userEvent.click(screen.getByRole('textbox')); |
| 36 | + await userEvent.click(screen.getAllByText(anotherDay)[0]); |
| 37 | + await userEvent.click(screen.getByRole('textbox')); |
| 38 | + await userEvent.click(screen.getByText('Clear')); |
| 39 | + |
| 40 | + expect(screen.getByDisplayValue(todaysDateInDefaultLanguage)).toBeInTheDocument(); |
| 41 | + }); |
| 42 | + |
| 43 | + it("should use today's date when Today button is clicked", async () => { |
| 44 | + const todaysDateInDefaultLanguage = getFormattedDate('en', new Date()); |
| 45 | + const todaysDayOfMonth = new Date().getDate(); |
| 46 | + const anotherDay = todaysDayOfMonth === 1 ? 2 : 1; |
| 47 | + |
| 48 | + render(<Datepicker />); |
| 49 | + |
| 50 | + await userEvent.click(screen.getByRole('textbox')); |
| 51 | + await userEvent.click(screen.getAllByText(anotherDay)[0]); |
| 52 | + await userEvent.click(screen.getByRole('textbox')); |
| 53 | + await userEvent.click(screen.getByText('Today')); |
| 54 | + |
| 55 | + expect(screen.getByDisplayValue(todaysDateInDefaultLanguage)).toBeInTheDocument(); |
| 56 | + }); |
| 57 | + |
| 58 | + it('should call `onSelectedDateChange` when a new date is selected', async () => { |
| 59 | + const onSelectedDateChange = vi.fn(); |
| 60 | + const todaysDayOfMonth = new Date().getDate(); |
| 61 | + const anotherDay = todaysDayOfMonth === 1 ? 2 : 1; |
| 62 | + |
| 63 | + render(<Datepicker onSelectedDateChanged={onSelectedDateChange} />); |
| 64 | + |
| 65 | + await userEvent.click(screen.getByRole('textbox')); |
| 66 | + await userEvent.click(screen.getAllByText(anotherDay)[0]); |
| 67 | + |
| 68 | + expect(onSelectedDateChange).toHaveBeenCalledOnce(); |
| 69 | + }); |
| 70 | + |
| 71 | + it('should close month overlay when user clicks outside of it', async () => { |
| 72 | + render(<Datepicker />); |
| 73 | + |
| 74 | + await userEvent.click(screen.getByRole('textbox')); |
| 75 | + await userEvent.click(document.body); |
| 76 | + }); |
| 77 | +}); |
0 commit comments