diff --git a/frontend/app/component/form/candidates_votes_form/CandidatesVotesForm.test.tsx b/frontend/app/component/form/candidates_votes_form/CandidatesVotesForm.test.tsx index 1959a5de0..c4f2f5581 100644 --- a/frontend/app/component/form/candidates_votes_form/CandidatesVotesForm.test.tsx +++ b/frontend/app/component/form/candidates_votes_form/CandidatesVotesForm.test.tsx @@ -5,18 +5,25 @@ import { getUrlMethodAndBody, overrideOnce, render, screen } from "app/test/unit import { Election, + ElectionProvider, PoliticalGroup, POLLING_STATION_DATA_ENTRY_REQUEST_BODY, PollingStationFormController, } from "@kiesraad/api"; -import { electionMock, politicalGroupMock } from "@kiesraad/api-mocks"; +import { electionMock, politicalGroupMock, pollingStationMock } from "@kiesraad/api-mocks"; import { CandidatesVotesForm } from "./CandidatesVotesForm"; const Component = ( - - - + + + + + ); const rootRequest: POLLING_STATION_DATA_ENTRY_REQUEST_BODY = { @@ -59,12 +66,12 @@ describe("Test CandidatesVotesForm", () => { }); describe("CandidatesVotesForm user interactions", () => { test("hitting enter key does not result in api call", async () => { - const spy = vi.spyOn(global, "fetch"); - const user = userEvent.setup(); render(Component); - const candidate1 = screen.getByTestId("candidate_votes[0].votes"); + const spy = vi.spyOn(global, "fetch"); + + const candidate1 = await screen.findByTestId("candidate_votes[0].votes"); await user.type(candidate1, "12345"); expect(candidate1).toHaveValue("12.345"); @@ -82,7 +89,7 @@ describe("Test CandidatesVotesForm", () => { render(Component); - const candidate1 = screen.getByTestId("candidate_votes[0].votes"); + const candidate1 = await screen.findByTestId("candidate_votes[0].votes"); expect(candidate1).toHaveFocus(); await user.type(candidate1, "12345"); expect(candidate1).toHaveValue("12.345"); @@ -148,8 +155,6 @@ describe("Test CandidatesVotesForm", () => { describe("CandidatesVotesForm API request and response", () => { test("CandidateVotesForm request body is equal to the form data", async () => { - const spy = vi.spyOn(global, "fetch"); - const politicalGroupMockData: PoliticalGroup = { number: 1, name: "Lijst 1 - Vurige Vleugels Partij", @@ -171,7 +176,7 @@ describe("Test CandidatesVotesForm", () => { ], }; - const electionMockData: Election = { + const electionMockData: Required = { id: 1, name: "Gemeenteraadsverkiezingen 2026", category: "Municipal", @@ -203,13 +208,18 @@ describe("Test CandidatesVotesForm", () => { ], }; - const electionMock = electionMockData as Required; const politicalGroupMock = politicalGroupMockData as Required; const Component = ( - - - + + + + + ); const expectedRequest = { @@ -249,11 +259,12 @@ describe("Test CandidatesVotesForm", () => { }; const user = userEvent.setup(); - render(Component); + const spy = vi.spyOn(global, "fetch"); + await user.type( - screen.getByTestId("candidate_votes[0].votes"), + await screen.findByTestId("candidate_votes[0].votes"), expectedRequest.data.political_group_votes[0]?.candidate_votes[0]?.votes.toString() ?? "0", ); @@ -289,7 +300,7 @@ describe("Test CandidatesVotesForm", () => { render(Component); - const submitButton = screen.getByRole("button", { name: "Volgende" }); + const submitButton = await screen.findByRole("button", { name: "Volgende" }); await user.click(submitButton); const feedbackServerError = await screen.findByTestId("feedback-server-error"); expect(feedbackServerError).toHaveTextContent(/^Error422 error from mock$/); @@ -305,7 +316,7 @@ describe("Test CandidatesVotesForm", () => { render(Component); - const submitButton = screen.getByRole("button", { name: "Volgende" }); + const submitButton = await screen.findByRole("button", { name: "Volgende" }); await user.click(submitButton); const feedbackServerError = await screen.findByTestId("feedback-server-error"); expect(feedbackServerError).toHaveTextContent(/^Error500 error from mock$/); @@ -332,7 +343,7 @@ describe("Test CandidatesVotesForm", () => { // Since the component does not allow to input invalid values such as -3, // not inputting any values and just clicking the submit button. - const submitButton = screen.getByRole("button", { name: "Volgende" }); + const submitButton = await screen.findByRole("button", { name: "Volgende" }); await user.click(submitButton); const feedbackError = await screen.findByTestId("feedback-error"); @@ -357,7 +368,7 @@ describe("Test CandidatesVotesForm", () => { render(Component); const user = userEvent.setup(); - await user.type(screen.getByTestId("candidate_votes[0].votes"), "1"); + await user.type(await screen.findByTestId("candidate_votes[0].votes"), "1"); await user.type(screen.getByTestId("candidate_votes[1].votes"), "2"); await user.type(screen.getByTestId("total"), "10"); @@ -391,7 +402,7 @@ describe("Test CandidatesVotesForm", () => { // Since no warnings exist for the fields on this page, // not inputting any values and just clicking submit. - const submitButton = screen.getByRole("button", { name: "Volgende" }); + const submitButton = await screen.findByRole("button", { name: "Volgende" }); await user.click(submitButton); const feedbackWarning = await screen.findByTestId("feedback-warning"); diff --git a/frontend/app/component/form/differences/DifferencesForm.test.tsx b/frontend/app/component/form/differences/DifferencesForm.test.tsx index 88a78bb81..511213464 100644 --- a/frontend/app/component/form/differences/DifferencesForm.test.tsx +++ b/frontend/app/component/form/differences/DifferencesForm.test.tsx @@ -7,17 +7,24 @@ import { afterEach, describe, expect, test, vi } from "vitest"; import { getUrlMethodAndBody, overrideOnce, render, screen } from "app/test/unit"; import { + ElectionProvider, POLLING_STATION_DATA_ENTRY_REQUEST_BODY, PollingStationFormController, } from "@kiesraad/api"; -import { electionMock } from "@kiesraad/api-mocks"; +import { electionMock, pollingStationMock } from "@kiesraad/api-mocks"; import { DifferencesForm } from "./DifferencesForm"; const Component = ( - - - + + + + + ); const rootRequest: POLLING_STATION_DATA_ENTRY_REQUEST_BODY = { @@ -61,13 +68,12 @@ describe("Test DifferencesForm", () => { describe("DifferencesForm user interactions", () => { test("hitting enter key does not result in api call", async () => { - const spy = vi.spyOn(global, "fetch"); - const user = userEvent.setup(); render(Component); + const spy = vi.spyOn(global, "fetch"); - const moreBallotsCount = screen.getByTestId("more_ballots_count"); + const moreBallotsCount = await screen.findByTestId("more_ballots_count"); await user.type(moreBallotsCount, "12345"); expect(moreBallotsCount).toHaveValue("12.345"); @@ -85,7 +91,7 @@ describe("Test DifferencesForm", () => { render(Component); - const moreBallotsCount = screen.getByTestId("more_ballots_count"); + const moreBallotsCount = await screen.findByTestId("more_ballots_count"); expect(moreBallotsCount).toHaveFocus(); await user.type(moreBallotsCount, "12345"); expect(moreBallotsCount).toHaveValue("12.345"); @@ -145,8 +151,6 @@ describe("Test DifferencesForm", () => { describe("DifferencesForm API request and response", () => { test("DifferencesForm request body is equal to the form data", async () => { - const spy = vi.spyOn(global, "fetch"); - const expectedRequest = { data: { ...rootRequest.data, @@ -165,9 +169,10 @@ describe("Test DifferencesForm", () => { const user = userEvent.setup(); render(Component); + const spy = vi.spyOn(global, "fetch"); await user.type( - screen.getByTestId("more_ballots_count"), + await screen.findByTestId("more_ballots_count"), expectedRequest.data.differences_counts.more_ballots_count.toString(), ); await user.type( @@ -217,7 +222,7 @@ describe("Test DifferencesForm", () => { render(Component); - const submitButton = screen.getByRole("button", { name: "Volgende" }); + const submitButton = await screen.findByRole("button", { name: "Volgende" }); await user.click(submitButton); const feedbackServerError = await screen.findByTestId("feedback-server-error"); expect(feedbackServerError).toHaveTextContent(/^Error422 error from mock$/); @@ -232,7 +237,7 @@ describe("Test DifferencesForm", () => { render(Component); - const submitButton = screen.getByRole("button", { name: "Volgende" }); + const submitButton = await screen.findByRole("button", { name: "Volgende" }); await user.click(submitButton); const feedbackServerError = await screen.findByTestId("feedback-server-error"); expect(feedbackServerError).toHaveTextContent(/^Error500 error from mock$/); diff --git a/frontend/app/component/form/voters_and_votes/VotersAndVotesForm.test.tsx b/frontend/app/component/form/voters_and_votes/VotersAndVotesForm.test.tsx index 6d1508110..02ee73990 100644 --- a/frontend/app/component/form/voters_and_votes/VotersAndVotesForm.test.tsx +++ b/frontend/app/component/form/voters_and_votes/VotersAndVotesForm.test.tsx @@ -7,17 +7,24 @@ import { afterEach, describe, expect, test, vi } from "vitest"; import { getUrlMethodAndBody, overrideOnce, render, screen, userTypeInputs } from "app/test/unit"; import { + ElectionProvider, POLLING_STATION_DATA_ENTRY_REQUEST_BODY, PollingStationFormController, } from "@kiesraad/api"; -import { electionMock } from "@kiesraad/api-mocks"; +import { electionMock, pollingStationMock } from "@kiesraad/api-mocks"; import { VotersAndVotesForm } from "./VotersAndVotesForm"; const Component = ( - - - + + + + + ); const rootRequest: POLLING_STATION_DATA_ENTRY_REQUEST_BODY = { @@ -61,13 +68,12 @@ describe("Test VotersAndVotesForm", () => { describe("VotersAndVotesForm user interactions", () => { test("hitting enter key does not result in api call", async () => { - const spy = vi.spyOn(global, "fetch"); - const user = userEvent.setup(); render(Component); + const spy = vi.spyOn(global, "fetch"); - const pollCards = screen.getByTestId("poll_card_count"); + const pollCards = await screen.findByTestId("poll_card_count"); await user.type(pollCards, "12345"); expect(pollCards).toHaveValue("12.345"); @@ -85,7 +91,7 @@ describe("Test VotersAndVotesForm", () => { render(Component); - const pollCards = screen.getByTestId("poll_card_count"); + const pollCards = await screen.findByTestId("poll_card_count"); expect(pollCards).toHaveFocus(); await user.type(pollCards, "12345"); expect(pollCards).toHaveValue("12.345"); @@ -150,8 +156,6 @@ describe("Test VotersAndVotesForm", () => { describe("VotersAndVotesForm API request and response", () => { test("VotersAndVotesForm request body is equal to the form data", async () => { - const spy = vi.spyOn(global, "fetch"); - const expectedRequest = { data: { ...rootRequest.data, @@ -173,6 +177,7 @@ describe("Test VotersAndVotesForm", () => { const user = userEvent.setup(); render(Component); + const spy = vi.spyOn(global, "fetch"); await userTypeInputs(user, { ...expectedRequest.data.voters_counts, @@ -202,7 +207,7 @@ describe("Test VotersAndVotesForm", () => { render(Component); - const submitButton = screen.getByRole("button", { name: "Volgende" }); + const submitButton = await screen.findByRole("button", { name: "Volgende" }); await user.click(submitButton); const feedbackServerError = await screen.findByTestId("feedback-server-error"); expect(feedbackServerError).toHaveTextContent(/^Error422 error from mock$/); @@ -221,7 +226,7 @@ describe("Test VotersAndVotesForm", () => { render(Component); - const submitButton = screen.getByRole("button", { name: "Volgende" }); + const submitButton = await screen.findByRole("button", { name: "Volgende" }); await user.click(submitButton); const feedbackServerError = await screen.findByTestId("feedback-server-error"); expect(feedbackServerError).toHaveTextContent(/^Error500 error from mock$/); @@ -251,7 +256,7 @@ describe("Test VotersAndVotesForm", () => { // Since the component does not allow to input invalid values such as -3, // not inputting any values and just clicking the submit button. - const submitButton = screen.getByRole("button", { name: "Volgende" }); + const submitButton = await screen.findByRole("button", { name: "Volgende" }); await user.click(submitButton); const feedbackError = await screen.findByTestId("feedback-error"); @@ -282,7 +287,8 @@ describe("Test VotersAndVotesForm", () => { render(Component); - await user.type(screen.getByTestId("poll_card_count"), "1"); + // We await the first element to appear, so we know the page is loaded + await user.type(await screen.findByTestId("poll_card_count"), "1"); await user.type(screen.getByTestId("proxy_certificate_count"), "1"); await user.type(screen.getByTestId("voter_card_count"), "1"); await user.type(screen.getByTestId("total_admitted_voters_count"), "4"); @@ -322,7 +328,8 @@ describe("Test VotersAndVotesForm", () => { render(Component); - await user.type(screen.getByTestId("votes_candidates_counts"), "1"); + // We await the first element to appear, so we know the page is loaded + await user.type(await screen.findByTestId("votes_candidates_counts"), "1"); await user.type(screen.getByTestId("blank_votes_count"), "1"); await user.type(screen.getByTestId("invalid_votes_count"), "1"); await user.type(screen.getByTestId("total_votes_cast_count"), "4"); @@ -358,7 +365,7 @@ describe("Test VotersAndVotesForm", () => { // Since the component does not allow to input values for non-existing fields, // not inputting any values and just clicking the submit button. - const submitButton = screen.getByRole("button", { name: "Volgende" }); + const submitButton = await screen.findByRole("button", { name: "Volgende" }); await user.click(submitButton); expect(screen.queryByTestId("result")).toBeNull(); @@ -389,7 +396,8 @@ describe("Test VotersAndVotesForm", () => { render(Component); - await user.type(screen.getByTestId("votes_candidates_counts"), "0"); + // We await the first element to appear, so we know the page is loaded + await user.type(await screen.findByTestId("votes_candidates_counts"), "0"); await user.type(screen.getByTestId("blank_votes_count"), "1"); await user.type(screen.getByTestId("invalid_votes_count"), "0"); await user.type(screen.getByTestId("total_votes_cast_count"), "1"); @@ -423,7 +431,8 @@ describe("Test VotersAndVotesForm", () => { render(Component); - await user.type(screen.getByTestId("votes_candidates_counts"), "0"); + // We await the first element to appear, so we know the page is loaded + await user.type(await screen.findByTestId("votes_candidates_counts"), "0"); await user.type(screen.getByTestId("blank_votes_count"), "0"); await user.type(screen.getByTestId("invalid_votes_count"), "1"); await user.type(screen.getByTestId("total_votes_cast_count"), "1"); @@ -459,7 +468,8 @@ describe("Test VotersAndVotesForm", () => { render(Component); - await user.type(screen.getByTestId("poll_card_count"), "1"); + // We await the first element to appear, so we know the page is loaded + await user.type(await screen.findByTestId("poll_card_count"), "1"); await user.type(screen.getByTestId("proxy_certificate_count"), "0"); await user.type(screen.getByTestId("voter_card_count"), "0"); await user.type(screen.getByTestId("total_admitted_voters_count"), "1"); diff --git a/frontend/app/test/unit/test-utils.ts b/frontend/app/test/unit/test-utils.ts index 9a5580f1d..76820cb9e 100644 --- a/frontend/app/test/unit/test-utils.ts +++ b/frontend/app/test/unit/test-utils.ts @@ -40,7 +40,7 @@ export function getUrlMethodAndBody( export async function userTypeInputs(user: UserEvent, inputs: { [key: string]: string | number }) { for (const [key, value] of Object.entries(inputs)) { - const input = screen.getByTestId(key); + const input = await screen.findByTestId(key); await user.type(input, value.toString()); expect(input).toHaveValue(value.toString()); }