Skip to content

Commit

Permalink
No default handlers used in tests (#881)
Browse files Browse the repository at this point in the history
  • Loading branch information
oliver3 authored Jan 23, 2025
1 parent 8fa32f5 commit b9b1d38
Show file tree
Hide file tree
Showing 23 changed files with 194 additions and 59 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import * as React from "react";

import { describe, expect, test } from "vitest";
import { beforeEach, describe, expect, test } from "vitest";

import { PollingStationFormController } from "app/component/form/data_entry/PollingStationFormController";
import { usePollingStationFormController } from "app/component/form/data_entry/usePollingStationFormController";
import { errorWarningMocks } from "app/component/form/testHelperFunctions";

import { SaveDataEntryResponse } from "@kiesraad/api";
import { electionMockData } from "@kiesraad/api-mocks";
import { overrideOnce, Providers, renderHook, waitFor } from "@kiesraad/test";
import {
electionMockData,
PollingStationDataEntryGetHandler,
PollingStationDataEntrySaveHandler,
} from "@kiesraad/api-mocks";
import { overrideOnce, Providers, renderHook, server, waitFor } from "@kiesraad/test";

const Wrapper = ({ children }: { children: React.ReactNode }) => (
<Providers>
Expand All @@ -19,6 +23,9 @@ const Wrapper = ({ children }: { children: React.ReactNode }) => (
);

describe("PollingStationFormController", () => {
beforeEach(() => {
server.use(PollingStationDataEntryGetHandler, PollingStationDataEntrySaveHandler);
});
test("PollingStationFormController renderHook", async () => {
const { result } = renderHook(() => usePollingStationFormController(), {
wrapper: Wrapper,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
import { userEvent } from "@testing-library/user-event";
import { describe, expect, test, vi } from "vitest";
import { beforeEach, describe, expect, test, vi } from "vitest";

import {
Election,
PoliticalGroup,
POLLING_STATION_DATA_ENTRY_SAVE_REQUEST_BODY,
PollingStationResults,
} from "@kiesraad/api";
import { electionMockData, politicalGroupMockData } from "@kiesraad/api-mocks";
import { getUrlMethodAndBody, overrideOnce, render, screen, within } from "@kiesraad/test";
import {
electionMockData,
politicalGroupMockData,
PollingStationDataEntryGetHandler,
PollingStationDataEntrySaveHandler,
} from "@kiesraad/api-mocks";
import { getUrlMethodAndBody, overrideOnce, render, screen, server, within } from "@kiesraad/test";

import {
emptyDataEntryRequest,
Expand Down Expand Up @@ -41,6 +46,10 @@ const candidatesFieldIds = {
};

describe("Test CandidatesVotesForm", () => {
beforeEach(() => {
server.use(PollingStationDataEntryGetHandler, PollingStationDataEntrySaveHandler);
});

describe("CandidatesVotesForm renders correctly", () => {
test("Candidates with first name", async () => {
const politicalGroupMockData: PoliticalGroup = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ import { userEvent } from "@testing-library/user-event";
import { beforeEach, describe, expect, test, vi } from "vitest";

import { ElectionProvider, PollingStationResults } from "@kiesraad/api";
import { electionDetailsMockResponse, electionMockData } from "@kiesraad/api-mocks";
import {
electionMockData,
ElectionRequestHandler,
PollingStationDataEntryGetHandler,
PollingStationDataEntrySaveHandler,
} from "@kiesraad/api-mocks";
import { overrideOnce, renderReturningRouter, screen, server, within } from "@kiesraad/test";

import { defaultFormState, emptyDataEntryRequest, errorWarningMocks } from "../../testHelperFunctions";
Expand All @@ -29,7 +34,7 @@ function renderForm(defaultFormState: Partial<FormState> = {}, defaultValues?: P

describe("Test CheckAndSaveForm", () => {
beforeEach(() => {
overrideOnce("get", "/api/elections/1", 200, electionDetailsMockResponse);
server.use(ElectionRequestHandler, PollingStationDataEntryGetHandler, PollingStationDataEntrySaveHandler);
});

test("Data entry can be finalised", async () => {
Expand Down Expand Up @@ -130,6 +135,9 @@ describe("Test CheckAndSaveForm", () => {
});

describe("Test CheckAndSaveForm summary", () => {
beforeEach(() => {
server.use(ElectionRequestHandler, PollingStationDataEntryGetHandler, PollingStationDataEntrySaveHandler);
});
test("Blocking", async () => {
const formState = structuredClone(defaultFormState);
formState.sections.voters_votes_counts.errors = [errorWarningMocks.F201];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { userEvent } from "@testing-library/user-event";
import { describe, expect, test, vi } from "vitest";
import { beforeEach, describe, expect, test, vi } from "vitest";

import {
emptyDataEntryRequest,
Expand All @@ -10,8 +10,12 @@ import {
} from "app/component/form/testHelperFunctions";

import { POLLING_STATION_DATA_ENTRY_SAVE_REQUEST_BODY, PollingStationResults } from "@kiesraad/api";
import { electionMockData } from "@kiesraad/api-mocks";
import { getUrlMethodAndBody, overrideOnce, render, screen, userTypeInputs } from "@kiesraad/test";
import {
electionMockData,
PollingStationDataEntryGetHandler,
PollingStationDataEntrySaveHandler,
} from "@kiesraad/api-mocks";
import { getUrlMethodAndBody, overrideOnce, render, screen, server, userTypeInputs } from "@kiesraad/test";

import { PollingStationFormController } from "../PollingStationFormController";
import { DifferencesForm } from "./DifferencesForm";
Expand Down Expand Up @@ -40,6 +44,10 @@ const differencesFieldIds = {
};

describe("Test DifferencesForm", () => {
beforeEach(() => {
server.use(PollingStationDataEntryGetHandler, PollingStationDataEntrySaveHandler);
});

describe("DifferencesForm user interactions", () => {
test("hitting enter key does not result in api call", async () => {
const user = userEvent.setup();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import { waitFor } from "@testing-library/react";
import { userEvent } from "@testing-library/user-event";
import { describe, expect, test } from "vitest";
import { beforeEach, describe, expect, test } from "vitest";

import { PollingStationChoiceForm } from "app/component/form/data_entry/polling_station_choice/PollingStationChoiceForm";

import { ElectionProvider, ElectionStatusProvider, ElectionStatusResponse } from "@kiesraad/api";
import { electionDetailsMockResponse, electionStatusMockResponse } from "@kiesraad/api-mocks";
import { overrideOnce, render, renderReturningRouter, screen, within } from "@kiesraad/test";
import {
electionDetailsMockResponse,
ElectionRequestHandler,
electionStatusMockResponse,
ElectionStatusRequestHandler,
} from "@kiesraad/api-mocks";
import { overrideOnce, render, renderReturningRouter, screen, server, within } from "@kiesraad/test";

function renderPollingStationChoicePage() {
return renderReturningRouter(
Expand All @@ -19,6 +24,9 @@ function renderPollingStationChoicePage() {
}

describe("Test PollingStationChoiceForm", () => {
beforeEach(() => {
server.use(ElectionStatusRequestHandler);
});
describe("Polling station choice form", () => {
test("Form field entry", async () => {
overrideOnce("get", "/api/elections/1", 200, electionDetailsMockResponse);
Expand Down Expand Up @@ -305,6 +313,7 @@ describe("Test PollingStationChoiceForm", () => {

describe("Polling station in progress", () => {
test("Show polling stations as 'in progress'", async () => {
server.use(ElectionRequestHandler);
overrideOnce("get", "api/elections/1/status", 200, {
statuses: [
{ polling_station_id: 1, status: "first_entry_not_started" },
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import { userEvent } from "@testing-library/user-event";
import { describe, expect, test, vi } from "vitest";
import { beforeEach, describe, expect, test, vi } from "vitest";

import { emptyDataEntryRequest, errorWarningMocks } from "app/component/form/testHelperFunctions";

import { POLLING_STATION_DATA_ENTRY_SAVE_REQUEST_BODY, SaveDataEntryResponse } from "@kiesraad/api";
import { electionMockData } from "@kiesraad/api-mocks";
import { getUrlMethodAndBody, overrideOnce, render, screen } from "@kiesraad/test";
import {
electionMockData,
PollingStationDataEntryGetHandler,
PollingStationDataEntrySaveHandler,
} from "@kiesraad/api-mocks";
import { getUrlMethodAndBody, overrideOnce, render, screen, server } from "@kiesraad/test";

import { PollingStationFormController } from "../PollingStationFormController";
import { RecountedForm } from "./RecountedForm";
Expand All @@ -17,6 +21,9 @@ const Component = (
);

describe("Test RecountedForm", () => {
beforeEach(() => {
server.use(PollingStationDataEntryGetHandler, PollingStationDataEntrySaveHandler);
});
describe("RecountedForm user interactions", () => {
test("hitting enter key does not result in api call", async () => {
render(Component);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { userEvent } from "@testing-library/user-event";
import { describe, expect, test, vi } from "vitest";
import { beforeEach, describe, expect, test, vi } from "vitest";

import {
emptyDataEntryRequest,
Expand All @@ -10,8 +10,12 @@ import {
} from "app/component/form/testHelperFunctions";

import { POLLING_STATION_DATA_ENTRY_SAVE_REQUEST_BODY, PollingStationResults } from "@kiesraad/api";
import { electionMockData } from "@kiesraad/api-mocks";
import { getUrlMethodAndBody, overrideOnce, render, screen, userTypeInputs, waitFor } from "@kiesraad/test";
import {
electionMockData,
PollingStationDataEntryGetHandler,
PollingStationDataEntrySaveHandler,
} from "@kiesraad/api-mocks";
import { getUrlMethodAndBody, overrideOnce, render, screen, server, userTypeInputs, waitFor } from "@kiesraad/test";

import { FormState, PollingStationFormController } from "../PollingStationFormController";
import { VotersAndVotesForm } from "./VotersAndVotesForm";
Expand Down Expand Up @@ -91,6 +95,10 @@ const recountFieldIds = {
};

describe("Test VotersAndVotesForm", () => {
beforeEach(() => {
server.use(PollingStationDataEntryGetHandler, PollingStationDataEntrySaveHandler);
});

describe("VotersAndVotesForm user interactions", () => {
test("hitting enter key does not result in api call", async () => {
const user = userEvent.setup();
Expand Down Expand Up @@ -124,10 +132,6 @@ describe("Test VotersAndVotesForm", () => {
});

test("Form field entry and keybindings", async () => {
overrideOnce("post", "/api/polling_stations/1/data_entries/1", 200, {
validation_results: { errors: [], warnings: [] },
});

const user = userEvent.setup();

renderForm({ recounted: true });
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { UserEvent, userEvent } from "@testing-library/user-event";
import { describe, expect, test, vi } from "vitest";
import { beforeEach, describe, expect, test, vi } from "vitest";

import { ErrorResponse, PollingStation } from "@kiesraad/api";
import { overrideOnce, render, screen, waitFor } from "@kiesraad/test";
import { PollingStationCreateHandler, PollingStationUpdateHandler } from "@kiesraad/api-mocks";
import { overrideOnce, render, screen, server, waitFor } from "@kiesraad/test";

import { PollingStationForm } from "./PollingStationForm";

Expand Down Expand Up @@ -43,6 +44,9 @@ async function fillForm(user: UserEvent, testPollingStation: PollingStation | Om
}

describe("PollingStationForm", () => {
beforeEach(() => {
server.use(PollingStationCreateHandler, PollingStationUpdateHandler);
});
describe("PollingStationForm create", () => {
test("Successful create", async () => {
const testPollingStation: Omit<PollingStation, "id"> = {
Expand Down
10 changes: 8 additions & 2 deletions frontend/app/module/data_entry/page/DataEntryHomePage.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@ import { beforeEach, describe, expect, test } from "vitest";
import { routes } from "app/routes";

import { ElectionProvider, ElectionStatusProvider, ElectionStatusResponse } from "@kiesraad/api";
import { electionDetailsMockResponse } from "@kiesraad/api-mocks";
import { overrideOnce, Providers, render, screen, setupTestRouter, waitFor, within } from "@kiesraad/test";
import {
electionDetailsMockResponse,
ElectionListRequestHandler,
ElectionRequestHandler,
ElectionStatusRequestHandler,
} from "@kiesraad/api-mocks";
import { overrideOnce, Providers, render, screen, server, setupTestRouter, waitFor, within } from "@kiesraad/test";

import { DataEntryHomePage } from "./DataEntryHomePage";

Expand All @@ -22,6 +27,7 @@ const renderDataEntryHomePage = () =>

describe("DataEntryHomePage", () => {
beforeEach(() => {
server.use(ElectionListRequestHandler, ElectionRequestHandler, ElectionStatusRequestHandler);
overrideOnce("get", "/api/elections/1", 200, electionDetailsMockResponse);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,14 @@ import {
POLLING_STATION_DATA_ENTRY_SAVE_REQUEST_BODY,
SaveDataEntryResponse,
} from "@kiesraad/api";
import { electionDetailsMockResponse, electionMockData } from "@kiesraad/api-mocks";
import { overrideOnce, renderReturningRouter, screen, server } from "@kiesraad/test";
import {
electionMockData,
ElectionRequestHandler,
PollingStationDataEntryDeleteHandler,
PollingStationDataEntryGetHandler,
PollingStationDataEntrySaveHandler,
} from "@kiesraad/api-mocks";
import { renderReturningRouter, screen, server } from "@kiesraad/test";

import { PollingStationFormController } from "../../../component/form/data_entry/PollingStationFormController";
import { VotersAndVotesForm } from "../../../component/form/data_entry/voters_and_votes/VotersAndVotesForm";
Expand All @@ -29,7 +35,7 @@ const renderAbortDataEntryControl = () => {

describe("Test AbortDataEntryControl", () => {
beforeEach(() => {
overrideOnce("get", "/api/elections/1", 200, electionDetailsMockResponse);
server.use(ElectionRequestHandler, PollingStationDataEntryGetHandler, PollingStationDataEntrySaveHandler);
});

test("renders and toggles the modal", async () => {
Expand Down Expand Up @@ -98,7 +104,7 @@ describe("Test AbortDataEntryControl", () => {

// set up a listener to check if the delete request is made
let request_method, request_url;
overrideOnce("delete", "/api/polling_stations/1/data_entries/1", 204, null);
server.use(PollingStationDataEntryDeleteHandler);
server.events.on("request:start", ({ request }) => {
request_method = request.method;
request_url = request.url;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
import { render as rtlRender } from "@testing-library/react";
import { userEvent } from "@testing-library/user-event";
import { describe, expect, test, vi } from "vitest";
import { beforeEach, describe, expect, test, vi } from "vitest";

import { errorWarningMocks } from "app/component/form/testHelperFunctions";
import { routes } from "app/routes";

import { SaveDataEntryResponse } from "@kiesraad/api";
import { electionMockData } from "@kiesraad/api-mocks";
import {
ElectionListRequestHandler,
electionMockData,
ElectionRequestHandler,
ElectionStatusRequestHandler,
PollingStationDataEntryDeleteHandler,
PollingStationDataEntryGetHandler,
PollingStationDataEntrySaveHandler,
} from "@kiesraad/api-mocks";
import {
overrideOnce,
Providers,
Router,
screen,
server,
setupTestRouter,
userTypeInputs,
waitFor,
Expand Down Expand Up @@ -298,6 +307,16 @@ async function executeStepsForPendingChanges(router: Router) {
}

describe("Polling Station data entry integration tests", () => {
beforeEach(() => {
server.use(
ElectionListRequestHandler,
ElectionRequestHandler,
ElectionStatusRequestHandler,
PollingStationDataEntryGetHandler,
PollingStationDataEntrySaveHandler,
PollingStationDataEntryDeleteHandler,
);
});
describe("Navigation through the form", () => {
test("Fill in complete form", async () => {
const router = renderWithRouter();
Expand Down
Loading

0 comments on commit b9b1d38

Please sign in to comment.