Skip to content

Commit

Permalink
makes tests structure across Forms tests consistent
Browse files Browse the repository at this point in the history
  • Loading branch information
jschuurk-kr committed Jul 15, 2024
1 parent 42cd324 commit 4d3a795
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 128 deletions.
124 changes: 61 additions & 63 deletions frontend/app/component/form/differences/DifferencesForm.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down

0 comments on commit 4d3a795

Please sign in to comment.