Skip to content

Commit

Permalink
Move testHelperFunctions.ts into test.utils.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
praseodym committed Mar 5, 2025
1 parent 3f7f97f commit 6cc4f8a
Show file tree
Hide file tree
Showing 10 changed files with 157 additions and 151 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,16 @@ import { getUrlMethodAndBody, overrideOnce, render, screen, server, within } fro

import { DataEntryProvider } from "../state/DataEntryProvider";
import { DataEntryState } from "../state/types";
import { defaultFormSection, overrideServerGetDataEntryResponse } from "../test.util";
import {
defaultFormSection,
emptyDataEntryRequest,
expectFieldsToBeInvalidAndToHaveAccessibleErrorMessage,
expectFieldsToBeValidAndToNotHaveAccessibleErrorMessage,
expectFieldsToHaveIconAndToHaveAccessibleName,
expectFieldsToNotHaveIcon,
getCandidateFullNamesFromMockData,
} from "../testHelperFunctions";
overrideServerGetDataEntryResponse,
} from "../test.util";
import { CandidatesVotesForm } from "./CandidatesVotesForm";

const defaultDataEntryState: DataEntryState = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@ import { overrideOnce, renderReturningRouter, screen, server, within } from "@ki

import { DataEntryProvider } from "../state/DataEntryProvider";
import { DataEntryState } from "../state/types";
import { defaultFormSection, overrideServerGetDataEntryResponse } from "../test.util";
import { emptyDataEntryRequest, errorWarningMocks } from "../testHelperFunctions";
import {
defaultFormSection,
emptyDataEntryRequest,
errorWarningMocks,
overrideServerGetDataEntryResponse,
} from "../test.util";
import { CheckAndSaveForm } from "./CheckAndSaveForm";

const defaultValues = emptyDataEntryRequest.data;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ import { userEvent } from "@testing-library/user-event";
import { beforeEach, describe, expect, test, vi } from "vitest";

import {
defaultFormSection,
emptyDataEntryRequest,
expectFieldsToBeInvalidAndToHaveAccessibleErrorMessage,
expectFieldsToBeValidAndToNotHaveAccessibleErrorMessage,
expectFieldsToHaveIconAndToHaveAccessibleName,
expectFieldsToNotHaveIcon,
} from "app/component/form/data_entry/testHelperFunctions";
overrideServerGetDataEntryResponse,
} from "app/component/form/data_entry/test.util";

import { POLLING_STATION_DATA_ENTRY_SAVE_REQUEST_BODY } from "@kiesraad/api";
import {
Expand All @@ -19,7 +21,6 @@ import { getUrlMethodAndBody, overrideOnce, render, screen, server, userTypeInpu

import { DataEntryProvider } from "../state/DataEntryProvider";
import { DataEntryState } from "../state/types";
import { defaultFormSection, overrideServerGetDataEntryResponse } from "../test.util";
import { DifferencesForm } from "./DifferencesForm";

const defaultDataEntryState: DataEntryState = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { userEvent } from "@testing-library/user-event";
import { beforeEach, describe, expect, test, vi } from "vitest";

import { emptyDataEntryRequest, errorWarningMocks } from "app/component/form/data_entry/testHelperFunctions";
import { emptyDataEntryRequest, errorWarningMocks } from "app/component/form/data_entry/test.util";

import { POLLING_STATION_DATA_ENTRY_SAVE_REQUEST_BODY, SaveDataEntryResponse } from "@kiesraad/api";
import {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import { assert, describe, expect, test } from "vitest";

import { ValidationResult } from "@kiesraad/api";

import { defaultDataEntryState, initialValues } from "../test.util";
import { errorWarningMocks } from "../testHelperFunctions";
import { defaultDataEntryState, errorWarningMocks, initialValues } from "../test.util";
import {
addValidationResultToFormState,
formSectionComplete,
Expand Down
140 changes: 138 additions & 2 deletions frontend/app/component/form/data_entry/test.util.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
import { GetDataEntryResponse, PollingStationResults } from "@kiesraad/api";
import { expect } from "vitest";

import {
GetDataEntryResponse,
PoliticalGroup,
POLLING_STATION_DATA_ENTRY_SAVE_REQUEST_BODY,
PollingStationResults,
ValidationResult,
ValidationResultCode,
} from "@kiesraad/api";
import { electionMockData } from "@kiesraad/api-mocks";
import { overrideOnce } from "@kiesraad/test";
import { overrideOnce, screen, within } from "@kiesraad/test";

import { getClientState } from "./state/dataEntryUtils";
import { DataEntryState, FormSection, FormState } from "./state/types";
Expand Down Expand Up @@ -119,3 +128,130 @@ export function overrideServerGetDataEntryResponse({
validation_results: validationResults,
} satisfies GetDataEntryResponse);
}

export function expectFieldsToBeInvalidAndToHaveAccessibleErrorMessage(fields: Array<string>, feedbackMessage: string) {
fields.forEach((field) => {
const inputField = within(screen.getByTestId(`cell-${field}`)).getByRole("textbox");
expect(inputField).toBeInvalid();
expect(inputField).toHaveAccessibleErrorMessage(feedbackMessage);
});
}

export function expectFieldsToHaveIconAndToHaveAccessibleName(fields: Array<string>, accessibleName: string) {
fields.forEach((field) => {
const icon = within(screen.getByTestId(`cell-${field}`)).getByRole("img");
expect(icon).toHaveAccessibleName(accessibleName);
});
}

export function expectFieldsToBeValidAndToNotHaveAccessibleErrorMessage(fields: Array<string>) {
fields.forEach((field) => {
const inputField = within(screen.getByTestId(`cell-${field}`)).getByRole("textbox");
expect(inputField).toBeValid();
expect(inputField).not.toHaveAccessibleErrorMessage();
});
}

export function expectFieldsToNotHaveIcon(fields: Array<string>) {
fields.forEach((field) => {
const icon = within(screen.getByTestId(`cell-${field}`)).queryByRole("img");
expect(icon).toBeNull();
});
}

export const emptyDataEntryRequest: POLLING_STATION_DATA_ENTRY_SAVE_REQUEST_BODY = {
progress: 0,
data: {
voters_counts: {
poll_card_count: 0,
proxy_certificate_count: 0,
voter_card_count: 0,
total_admitted_voters_count: 0,
},
votes_counts: {
votes_candidates_count: 0,
blank_votes_count: 0,
invalid_votes_count: 0,
total_votes_cast_count: 0,
},
differences_counts: {
more_ballots_count: 0,
fewer_ballots_count: 0,
unreturned_ballots_count: 0,
too_few_ballots_handed_out_count: 0,
too_many_ballots_handed_out_count: 0,
other_explanation_count: 0,
no_explanation_count: 0,
},
political_group_votes: electionMockData.political_groups.map((group) => ({
number: group.number,
total: 0,
candidate_votes: group.candidates.map((candidate) => ({
number: candidate.number,
votes: 0,
})),
})),
},
client_state: {
test: "test",
},
};

type ErrorWarningsMap<Code extends ValidationResultCode> = {
[C in Code]: ValidationResult & { code: C };
};

export const errorWarningMocks: ErrorWarningsMap<"F101" | "F201" | "F301" | "F204" | "W301" | "W302"> = {
F101: {
fields: ["data.recounted"],
code: "F101",
},
F201: {
fields: [
"data.voters_counts.poll_card_count",
"data.voters_counts.proxy_certificate_count",
"data.voters_counts.voter_card_count",
"data.voters_counts.total_admitted_voters_count",
],
code: "F201",
},
F204: {
fields: ["data.votes_counts.votes_candidates_count", "data.political_group_votes"],
code: "F204",
},
F301: {
fields: ["data.differences_counts.more_ballots_count"],
code: "F301",
},
W301: {
fields: [
"data.differences_counts.more_ballots_count",
"data.differences_counts.too_many_ballots_handed_out_count",
"data.differences_counts.unreturned_ballots_count",
"data.differences_counts.too_few_ballots_handed_out_count",
"data.differences_counts.other_explanation_count",
"data.differences_counts.no_explanation_count",
],
code: "W301",
},
W302: {
fields: [
"data.differences_counts.fewer_ballots_count",
"data.differences_counts.unreturned_ballots_count",
"data.differences_counts.too_few_ballots_handed_out_count",
"data.differences_counts.too_many_ballots_handed_out_count",
"data.differences_counts.other_explanation_count",
"data.differences_counts.no_explanation_count",
],
code: "W302",
},
};

export function getCandidateFullNamesFromMockData(politicalGroupMockData: PoliticalGroup): string[] {
const candidateNames = politicalGroupMockData.candidates.map((candidate) => {
return candidate.first_name
? `${candidate.last_name}, ${candidate.initials} (${candidate.first_name})`
: `${candidate.last_name}, ${candidate.initials}`;
});
return candidateNames;
}
137 changes: 0 additions & 137 deletions frontend/app/component/form/data_entry/testHelperFunctions.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ import { beforeEach, describe, expect, test, vi } from "vitest";

import { mockElection } from "app/component/election/status/mockData";
import {
defaultFormSection,
emptyDataEntryRequest,
expectFieldsToBeInvalidAndToHaveAccessibleErrorMessage,
expectFieldsToBeValidAndToNotHaveAccessibleErrorMessage,
expectFieldsToHaveIconAndToHaveAccessibleName,
expectFieldsToNotHaveIcon,
} from "app/component/form/data_entry/testHelperFunctions";
overrideServerGetDataEntryResponse,
} from "app/component/form/data_entry/test.util";

import {
GetDataEntryResponse,
Expand All @@ -25,7 +27,6 @@ import { getUrlMethodAndBody, overrideOnce, render, screen, server, userTypeInpu
import { DataEntryProvider } from "../state/DataEntryProvider";
import { getClientState } from "../state/dataEntryUtils";
import { DataEntryState } from "../state/types";
import { defaultFormSection, overrideServerGetDataEntryResponse } from "../test.util";
import { VotersAndVotesForm } from "./VotersAndVotesForm";

const initialValues: PollingStationResults = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { render as rtlRender, within } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { beforeEach, describe, expect, test, vi } from "vitest";

import { errorWarningMocks } from "app/component/form/data_entry/testHelperFunctions";
import { errorWarningMocks } from "app/component/form/data_entry/test.util";
import { routes } from "app/routes";

import { SaveDataEntryResponse } from "@kiesraad/api";
Expand Down
Loading

0 comments on commit 6cc4f8a

Please sign in to comment.