Skip to content
This repository has been archived by the owner on Feb 10, 2025. It is now read-only.

Commit

Permalink
remove: unnecessary test dependencies
Browse files Browse the repository at this point in the history
refactor: an old test to work without act
  • Loading branch information
joonatank committed Sep 10, 2024
1 parent ce84589 commit 18787fa
Show file tree
Hide file tree
Showing 3 changed files with 301 additions and 423 deletions.
59 changes: 30 additions & 29 deletions apps/ui/components/form/__tests__/DateRangePicker.test.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import userEvent from "@testing-library/user-event";
import { advanceTo } from "jest-date-mock";
import { get as mockGet } from "lodash";
import React from "react";
import mockTranslations from "@/public/locales/fi/dateSelector.json";
import { act, configure, render, screen } from "@/test/testUtils";
import { configure, render, screen } from "@/test/testUtils";
import DateRangePicker, { DateRangePickerProps } from "../DateRangePicker";

jest.mock("next-i18next", () => ({
Expand All @@ -27,63 +26,65 @@ const defaultProps: DateRangePickerProps = {
startDate: null,
};

beforeEach(() => {
advanceTo("2020-10-10");
beforeAll(() => {
jest.useFakeTimers({
now: new Date(2020, 10, 10, 9, 0, 0),
});
});

afterAll(() => {
jest.useRealTimers();
});

const renderComponent = (props?: Partial<DateRangePickerProps>) =>
render(<DateRangePicker {...defaultProps} {...props} />);

test("should show error start date must be before end date", async () => {
renderComponent();
const user = userEvent.setup();
const view = renderComponent();
// NOTE weird issues with the default delay causing the test to fail always
const user = userEvent.setup({ delay: null });

const startDateInput = screen.getByRole("textbox", {
name: /alkamispäivä/i,
});
await act(async () => {
await user.type(startDateInput, "23.6.2021");
});
const startDateText = view.getByText(/alkamispäivä/i);
expect(startDateText).toBeInTheDocument();

const endDateInput = screen.getByRole("textbox", {
const startDateInput = view.getByLabelText(/alkamispäivä/i);
expect(startDateInput).toBeInTheDocument();

await user.type(startDateInput, "23.6.2021");
await user.type(startDateInput, "23.6.2021");

const endDateInput = view.getByRole("textbox", {
name: /päättymispäivä/i,
});
await act(async () => {
await user.type(endDateInput, "22.6.2021");
});
await user.type(endDateInput, "22.6.2021");

screen.queryByText(/Alkamispäivän on oltava ennen päättymispäivää/i);
view.queryByText(/Alkamispäivän on oltava ennen päättymispäivää/i);

await act(async () => {
await user.clear(endDateInput);
});
await act(async () => {
await user.type(endDateInput, "24.6.2021");
});
await user.clear(endDateInput);
await user.type(endDateInput, "24.6.2021");

const start = screen.queryByText(
const start = view.queryByText(
/Alkamispäivän on oltava ennen päättymispäivää/i
);

expect(start).not.toBeInTheDocument();
});

test("should show formatting error", async () => {
advanceTo("2020-10-10");
renderComponent();
const view = renderComponent();

const startDateInput = screen.getByRole("textbox", {
const startDateInput = view.getByRole("textbox", {
name: /alkamispäivä/i,
});
userEvent.type(startDateInput, "23..2021");

expect(
screen.queryByText(/Päivämäärän on oltava muotoa pp\.kk\.vvvv/i)
view.queryByText(/Päivämäärän on oltava muotoa pp\.kk\.vvvv/i)
).not.toBeInTheDocument();

// should show error when focusing out of the element
userEvent.tab();
screen.queryByText(/Päivämäärän on oltava muotoa pp\.kk\.vvvv/i);
view.queryByText(/Päivämäärän on oltava muotoa pp\.kk\.vvvv/i);

// Error should disappear
userEvent.clear(startDateInput);
Expand Down
2 changes: 0 additions & 2 deletions apps/ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,7 @@
"axe-core": "^4.9.0",
"jest": "^29.7.0",
"jest-axe": "^8.0.0",
"jest-date-mock": "^1.0.9",
"jest-environment-jsdom": "^29.7.0",
"msw": "^1.3.2",
"postcss-styled-syntax": "^0.6.4",
"prettier": "^3.3.3",
"stylelint": "^16.2.1",
Expand Down
Loading

0 comments on commit 18787fa

Please sign in to comment.