Skip to content

Commit

Permalink
fix errors for political groups
Browse files Browse the repository at this point in the history
change tests obj compare
  • Loading branch information
lkleuver committed Jul 11, 2024
1 parent f0a719f commit 2bad3de
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { overrideOnce, render, screen } from "app/test/unit";
import { getBodyObject, overrideOnce, render, screen } from "app/test/unit";
import { userEvent } from "@testing-library/user-event";
import { describe, expect, test, vi, afterEach } from "vitest";
import {
Expand Down Expand Up @@ -151,8 +151,8 @@ describe("Test CandidatesVotesForm", () => {
expect(result).toHaveTextContent(/^Success$/);
});

describe("VotersAndVotesForm Api call", () => {
test("VotersAndVotesForm request body is equal to the form data", async () => {
describe("CandidateVotesForm Api call", () => {
test("CandidateVotesForm request body is equal to the form data", async () => {
const spy = vi.spyOn(global, "fetch");

const expectedRequest = {
Expand All @@ -176,13 +176,17 @@ describe("Test CandidatesVotesForm", () => {
const submitButton = screen.getByRole("button", { name: "Volgende" });
await user.click(submitButton);

expect(spy).toHaveBeenCalledWith("http://testhost/v1/api/polling_stations/1/data_entries/1", {
method: "POST",
body: JSON.stringify(expectedRequest),
headers: {
"Content-Type": "application/json",
},
});
// expect(spy).toHaveBeenCalledWith("http://testhost/v1/api/polling_stations/1/data_entries/1", {
// method: "POST",
// body: JSON.stringify(expectedRequest),
// headers: {
// "Content-Type": "application/json",
// },
// });
expect(spy).toHaveBeenCalled();

const obj = getBodyObject(spy.mock.calls);
expect(obj).toEqual(expectedRequest);

Check failure on line 189 in frontend/app/component/form/candidates_votes_form/CandidatesVotesForm.test.tsx

View workflow job for this annotation

GitHub Actions / Node

app/component/form/candidates_votes_form/CandidatesVotesForm.test.tsx > Test CandidatesVotesForm > CandidateVotesForm Api call > CandidateVotesForm request body is equal to the form data

AssertionError: expected { data: { …(4) } } to deeply equal { data: { …(4) } } - Expected + Received Object { "data": Object { "differences_counts": Object { "fewer_ballots_count": 0, "more_ballots_count": 0, "no_explanation_count": 0, "other_explanation_count": 0, "too_few_ballots_handed_out_count": 0, "too_many_ballots_handed_out_count": 0, "unreturned_ballots_count": 0, }, "political_group_votes": Array [ Object { "candidate_votes": Array [ Object { "number": 1, - "votes": 5, + "votes": 0, }, Object { "number": 2, - "votes": 5, + "votes": 0, }, Object { "number": 3, - "votes": 5, + "votes": 0, }, Object { "number": 4, - "votes": 5, + "votes": 0, }, Object { "number": 5, - "votes": 5, + "votes": 0, }, Object { "number": 6, - "votes": 5, + "votes": 0, }, Object { "number": 7, - "votes": 5, + "votes": 0, }, Object { "number": 8, - "votes": 5, + "votes": 0, }, Object { "number": 9, - "votes": 5, + "votes": 0, }, Object { "number": 10, - "votes": 5, + "votes": 0, }, Object { "number": 11, - "votes": 5, + "votes": 0, }, Object { "number": 12, - "votes": 5, + "votes": 0, }, Object { "number": 13, - "votes": 5, + "votes": 0, }, Object { "number": 14, - "votes": 5, + "votes": 0, }, Object { "number": 15, - "votes": 5, + "votes": 0, }, Object { "number": 16, - "votes": 5, + "votes": 0, }, Object { "number": 17, - "votes": 5, + "votes": 0, }, Object { "number": 18, - "votes": 5, + "votes": 0, }, Object { "number": 19, - "votes": 5, + "votes": 0, }, Object { "number": 20, - "votes": 5, + "votes": 0, }, Object { "number": 21, - "votes": 5, + "votes": 0, }, Object { "number": 22, - "votes": 5, + "votes": 0, }, Object { "number": 23, - "votes": 5, + "votes": 0, }, Object { "number": 24, - "votes": 5, + "votes": 0, }, Object { "number": 25, - "votes": 5, + "votes": 0, }, Object { "number": 26, - "votes": 5, + "votes": 0, }, Object { "number": 27, - "votes": 5, + "votes": 0, }, Object { "number": 28, - "votes": 5, + "votes": 0, }, Object { "number": 29, - "votes": 5, + "votes": 0, }, ], "number": 1, - "total": 100, + "total": 0, }, Object { "candidate_votes": Array [ Object { "number": 1

const result = await screen.findByTestId("result");
expect(result).toHaveTextContent(/^Success$/);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @vitest-environment jsdom
*/

import { overrideOnce, render, screen, fireEvent } from "app/test/unit";
import { overrideOnce, render, screen, fireEvent, getBodyObject } from "app/test/unit";
import { userEvent } from "@testing-library/user-event";
import { describe, expect, test, vi, afterEach } from "vitest";

Expand Down Expand Up @@ -227,13 +227,17 @@ describe("Test VotersAndVotesForm", () => {
const submitButton = screen.getByRole("button", { name: "Volgende" });
await user.click(submitButton);

expect(spy).toHaveBeenCalledWith("http://testhost/v1/api/polling_stations/1/data_entries/1", {
method: "POST",
body: JSON.stringify(expectedRequest),
headers: {
"Content-Type": "application/json",
},
});
// expect(spy).toHaveBeenCalledWith("http://testhost/v1/api/polling_stations/1/data_entries/1", {
// method: "POST",
// body: JSON.stringify(expectedRequest),
// headers: {
// "Content-Type": "application/json",
// },
// });

expect(spy).toHaveBeenCalled();
const obj = getBodyObject(spy.mock.calls);
expect(obj).toEqual(expectedRequest);

const result = await screen.findByTestId("result");
expect(result).toHaveTextContent(/^Success$/);
Expand Down
16 changes: 16 additions & 0 deletions frontend/app/test/unit/test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,19 @@ const customRender = (ui: ReactElement, options?: Omit<RenderOptions, "wrapper">

export * from "@testing-library/react";
export { customRender as render };

export function getBodyObject(
call: [input: string | URL | Request, init?: RequestInit | undefined][],
) {
if (call.length > 0) {
if (call[0] && call[0].length > 1) {
if (call[0][1]) {
if (call[0][1].body) {
const body = call[0][1].body;
return JSON.parse(body as string) as object;
}
}
}
}
return null;
}
12 changes: 8 additions & 4 deletions frontend/lib/api/form/pollingstation/usePoliticalGroups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,24 @@ export function usePoliticalGroup(political_group_number: number) {
const errors = React.useMemo(() => {
if (data) {
return data.validation_results.errors.filter((err) =>
matchValidationResultWithFormSections(err.fields, ["political_group_votes"]),
matchValidationResultWithFormSections(err.fields, [
`political_group_votes[${political_group_number}]`,
]),
);
}
return [] as ValidationResult[];
}, [data]);
}, [data, political_group_number]);

const warnings = React.useMemo(() => {
if (data) {
return data.validation_results.warnings.filter((warning) =>
matchValidationResultWithFormSections(warning.fields, ["political_group_votes"]),
matchValidationResultWithFormSections(warning.fields, [
`political_group_votes[${political_group_number}]`,
]),
);
}
return [] as ValidationResult[];
}, [data]);
}, [data, political_group_number]);

const setSectionValues = (values: PoliticalGroupVotes) => {
setValues((old) => ({
Expand Down
5 changes: 5 additions & 0 deletions frontend/lib/util/fields.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,9 @@ test("matchValidationResultWithFormSections", () => {
sections = ["test1"];

expect(matchValidationResultWithFormSections(fields, sections)).equals(false);

fields = ["data.political_group_votes[1].total"];
sections = ["political_group_votes[1]"];

expect(matchValidationResultWithFormSections(fields, sections)).equals(true);
});

0 comments on commit 2bad3de

Please sign in to comment.