|
| 1 | +import { screen } from "@testing-library/react"; |
| 2 | +import { addDays } from "date-fns"; |
| 3 | +import React from "react"; |
| 4 | +import { dateButton } from "@/test/elements"; |
| 5 | +import { render } from "@/test/render"; |
| 6 | +import { user } from "@/test/user"; |
| 7 | +import { RangeResetSelection } from "./RangeResetSelection"; |
| 8 | + |
| 9 | +const today = new Date(2022, 8, 12); |
| 10 | + |
| 11 | +beforeAll(() => jest.setSystemTime(today)); |
| 12 | +afterAll(() => jest.useRealTimers()); |
| 13 | + |
| 14 | +beforeEach(() => render(<RangeResetSelection />)); |
| 15 | + |
| 16 | +const getFrom = () => screen.getByTestId("from"); |
| 17 | +const getTo = () => screen.getByTestId("to"); |
| 18 | + |
| 19 | +test("select same day range", async () => { |
| 20 | + await user.click(dateButton(today)); |
| 21 | + expect(getFrom()).toHaveTextContent("from: 2022-09-12"); |
| 22 | + expect(getTo()).toHaveTextContent("to:"); |
| 23 | + await user.click(dateButton(today)); |
| 24 | + expect(getFrom()).toHaveTextContent("from: 2022-09-12"); |
| 25 | + expect(getTo()).toHaveTextContent("to: 2022-09-12"); |
| 26 | +}); |
| 27 | + |
| 28 | +test("start range after click on day with range selected", async () => { |
| 29 | + await user.click(dateButton(today)); |
| 30 | + expect(getFrom()).toHaveTextContent("from: 2022-09-12"); |
| 31 | + expect(getTo()).toHaveTextContent("to:"); |
| 32 | + await user.click(dateButton(addDays(today, 1))); |
| 33 | + expect(getFrom()).toHaveTextContent("from: 2022-09-12"); |
| 34 | + expect(getTo()).toHaveTextContent("to: 2022-09-13"); |
| 35 | + await user.click(dateButton(addDays(today, 4))); |
| 36 | + expect(getFrom()).toHaveTextContent("from: 2022-09-16"); |
| 37 | + expect(getTo()).toHaveTextContent("to:"); |
| 38 | + await user.click(dateButton(today)); |
| 39 | + expect(getFrom()).toHaveTextContent("from: 2022-09-12"); |
| 40 | + expect(getTo()).toHaveTextContent("to: 2022-09-16"); |
| 41 | +}); |
0 commit comments