From 4d3a79522dc5381b72c23a085d87a77813a46e65 Mon Sep 17 00:00:00 2001 From: Joep <145749778+jschuurk-kr@users.noreply.github.com> Date: Mon, 15 Jul 2024 17:12:32 +0200 Subject: [PATCH] makes tests structure across Forms tests consistent --- .../form/differences/DifferencesForm.test.tsx | 124 +++++++++-------- .../VotersAndVotesForm.test.tsx | 128 +++++++++--------- 2 files changed, 124 insertions(+), 128 deletions(-) diff --git a/frontend/app/component/form/differences/DifferencesForm.test.tsx b/frontend/app/component/form/differences/DifferencesForm.test.tsx index 903ad194d..3dcdc4e7b 100644 --- a/frontend/app/component/form/differences/DifferencesForm.test.tsx +++ b/frontend/app/component/form/differences/DifferencesForm.test.tsx @@ -142,70 +142,68 @@ describe("Test DifferencesForm", () => { expect(result).toHaveTextContent(/^Success$/); }); - describe("DifferencesForm Api call", () => { - test("DifferencesForm request body is equal to the form data", async () => { - const spy = vi.spyOn(global, "fetch"); - - const expectedRequest = { - data: { - ...rootRequest.data, - differences_counts: { - more_ballots_count: 2, - fewer_ballots_count: 0, - unreturned_ballots_count: 0, - too_few_ballots_handed_out_count: 0, - too_many_ballots_handed_out_count: 1, - other_explanation_count: 0, - no_explanation_count: 1, - }, + test("DifferencesForm request body is equal to the form data", async () => { + const spy = vi.spyOn(global, "fetch"); + + const expectedRequest = { + data: { + ...rootRequest.data, + differences_counts: { + more_ballots_count: 2, + fewer_ballots_count: 0, + unreturned_ballots_count: 0, + too_few_ballots_handed_out_count: 0, + too_many_ballots_handed_out_count: 1, + other_explanation_count: 0, + no_explanation_count: 1, }, - }; - - const user = userEvent.setup(); - - render(Component); - - await user.type( - screen.getByTestId("more_ballots_count"), - expectedRequest.data.differences_counts.more_ballots_count.toString(), - ); - await user.type( - screen.getByTestId("fewer_ballots_count"), - expectedRequest.data.differences_counts.fewer_ballots_count.toString(), - ); - await user.type( - screen.getByTestId("unreturned_ballots_count"), - expectedRequest.data.differences_counts.unreturned_ballots_count.toString(), - ); - await user.type( - screen.getByTestId("too_few_ballots_handed_out_count"), - expectedRequest.data.differences_counts.too_few_ballots_handed_out_count.toString(), - ); - await user.type( - screen.getByTestId("too_many_ballots_handed_out_count"), - expectedRequest.data.differences_counts.too_many_ballots_handed_out_count.toString(), - ); - await user.type( - screen.getByTestId("other_explanation_count"), - expectedRequest.data.differences_counts.other_explanation_count.toString(), - ); - await user.type( - screen.getByTestId("no_explanation_count"), - expectedRequest.data.differences_counts.no_explanation_count.toString(), - ); - - const submitButton = screen.getByRole("button", { name: "Volgende" }); - await user.click(submitButton); - - expect(spy).toHaveBeenCalled(); - const { url, method, body } = getUrlMethodAndBody(spy.mock.calls); - expect(url).toEqual("http://testhost/v1/api/polling_stations/1/data_entries/1"); - expect(method).toEqual("POST"); - expect(body).toEqual(expectedRequest); - - const result = await screen.findByTestId("result"); - expect(result).toHaveTextContent(/^Success$/); - }); + }, + }; + + const user = userEvent.setup(); + + render(Component); + + await user.type( + screen.getByTestId("more_ballots_count"), + expectedRequest.data.differences_counts.more_ballots_count.toString(), + ); + await user.type( + screen.getByTestId("fewer_ballots_count"), + expectedRequest.data.differences_counts.fewer_ballots_count.toString(), + ); + await user.type( + screen.getByTestId("unreturned_ballots_count"), + expectedRequest.data.differences_counts.unreturned_ballots_count.toString(), + ); + await user.type( + screen.getByTestId("too_few_ballots_handed_out_count"), + expectedRequest.data.differences_counts.too_few_ballots_handed_out_count.toString(), + ); + await user.type( + screen.getByTestId("too_many_ballots_handed_out_count"), + expectedRequest.data.differences_counts.too_many_ballots_handed_out_count.toString(), + ); + await user.type( + screen.getByTestId("other_explanation_count"), + expectedRequest.data.differences_counts.other_explanation_count.toString(), + ); + await user.type( + screen.getByTestId("no_explanation_count"), + expectedRequest.data.differences_counts.no_explanation_count.toString(), + ); + + const submitButton = screen.getByRole("button", { name: "Volgende" }); + await user.click(submitButton); + + expect(spy).toHaveBeenCalled(); + const { url, method, body } = getUrlMethodAndBody(spy.mock.calls); + expect(url).toEqual("http://testhost/v1/api/polling_stations/1/data_entries/1"); + expect(method).toEqual("POST"); + expect(body).toEqual(expectedRequest); + + const result = await screen.findByTestId("result"); + expect(result).toHaveTextContent(/^Success$/); }); test("422 response results in display of error message", async () => { 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 b7d94367c..8443a4910 100644 --- a/frontend/app/component/form/voters_and_votes/VotersAndVotesForm.test.tsx +++ b/frontend/app/component/form/voters_and_votes/VotersAndVotesForm.test.tsx @@ -147,79 +147,77 @@ describe("Test VotersAndVotesForm", () => { expect(result).toHaveTextContent(/^Success$/); }); - describe("VotersAndVotesForm Api call", () => { - test("VotersAndVotesForm request body is equal to the form data", async () => { - const spy = vi.spyOn(global, "fetch"); - - const expectedRequest = { - data: { - ...rootRequest.data, - voters_counts: { - poll_card_count: 1, - proxy_certificate_count: 2, - voter_card_count: 3, - total_admitted_voters_count: 6, - }, - votes_counts: { - votes_candidates_counts: 4, - blank_votes_count: 5, - invalid_votes_count: 6, - total_votes_cast_count: 15, - }, + test("VotersAndVotesForm request body is equal to the form data", async () => { + const spy = vi.spyOn(global, "fetch"); + + const expectedRequest = { + data: { + ...rootRequest.data, + voters_counts: { + poll_card_count: 1, + proxy_certificate_count: 2, + voter_card_count: 3, + total_admitted_voters_count: 6, + }, + votes_counts: { + votes_candidates_counts: 4, + blank_votes_count: 5, + invalid_votes_count: 6, + total_votes_cast_count: 15, }, - }; + }, + }; - const user = userEvent.setup(); + const user = userEvent.setup(); - render(Component); + render(Component); - await user.type( - screen.getByTestId("poll_card_count"), - expectedRequest.data.voters_counts.poll_card_count.toString(), - ); - await user.type( - screen.getByTestId("proxy_certificate_count"), - expectedRequest.data.voters_counts.proxy_certificate_count.toString(), - ); - await user.type( - screen.getByTestId("voter_card_count"), - expectedRequest.data.voters_counts.voter_card_count.toString(), - ); - await user.type( - screen.getByTestId("total_admitted_voters_count"), - expectedRequest.data.voters_counts.total_admitted_voters_count.toString(), - ); - - await user.type( - screen.getByTestId("votes_candidates_counts"), - expectedRequest.data.votes_counts.votes_candidates_counts.toString(), - ); - await user.type( - screen.getByTestId("blank_votes_count"), - expectedRequest.data.votes_counts.blank_votes_count.toString(), - ); - await user.type( - screen.getByTestId("invalid_votes_count"), - expectedRequest.data.votes_counts.invalid_votes_count.toString(), - ); - await user.type( - screen.getByTestId("total_votes_cast_count"), - expectedRequest.data.votes_counts.total_votes_cast_count.toString(), - ); + await user.type( + screen.getByTestId("poll_card_count"), + expectedRequest.data.voters_counts.poll_card_count.toString(), + ); + await user.type( + screen.getByTestId("proxy_certificate_count"), + expectedRequest.data.voters_counts.proxy_certificate_count.toString(), + ); + await user.type( + screen.getByTestId("voter_card_count"), + expectedRequest.data.voters_counts.voter_card_count.toString(), + ); + await user.type( + screen.getByTestId("total_admitted_voters_count"), + expectedRequest.data.voters_counts.total_admitted_voters_count.toString(), + ); + + await user.type( + screen.getByTestId("votes_candidates_counts"), + expectedRequest.data.votes_counts.votes_candidates_counts.toString(), + ); + await user.type( + screen.getByTestId("blank_votes_count"), + expectedRequest.data.votes_counts.blank_votes_count.toString(), + ); + await user.type( + screen.getByTestId("invalid_votes_count"), + expectedRequest.data.votes_counts.invalid_votes_count.toString(), + ); + await user.type( + screen.getByTestId("total_votes_cast_count"), + expectedRequest.data.votes_counts.total_votes_cast_count.toString(), + ); - const submitButton = screen.getByRole("button", { name: "Volgende" }); - await user.click(submitButton); + const submitButton = screen.getByRole("button", { name: "Volgende" }); + await user.click(submitButton); - expect(spy).toHaveBeenCalled(); - const { url, method, body } = getUrlMethodAndBody(spy.mock.calls); + expect(spy).toHaveBeenCalled(); + const { url, method, body } = getUrlMethodAndBody(spy.mock.calls); - expect(url).toEqual("http://testhost/v1/api/polling_stations/1/data_entries/1"); - expect(method).toEqual("POST"); - expect(body).toEqual(expectedRequest); + expect(url).toEqual("http://testhost/v1/api/polling_stations/1/data_entries/1"); + expect(method).toEqual("POST"); + expect(body).toEqual(expectedRequest); - const result = await screen.findByTestId("result"); - expect(result).toHaveTextContent(/^Success$/); - }); + const result = await screen.findByTestId("result"); + expect(result).toHaveTextContent(/^Success$/); }); test("422 response results in display of error message", async () => {