Skip to content

Commit

Permalink
fix indeces are number - 1
Browse files Browse the repository at this point in the history
  • Loading branch information
lkleuver committed Jul 15, 2024
1 parent 9a73ce7 commit 103d233
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ describe("Test CandidatesVotesForm", () => {
const user = userEvent.setup();
render(Component);

const candidate1 = screen.getByTestId("candidate_votes-1.votes");
const candidate1 = screen.getByTestId("candidate_votes-0.votes");
await user.clear(candidate1);
await user.type(candidate1, "12345");
expect(candidate1).toHaveValue("12.345");
Expand All @@ -83,47 +83,47 @@ describe("Test CandidatesVotesForm", () => {

render(Component);

const candidate1 = screen.getByTestId("candidate_votes-1.votes");
const candidate1 = screen.getByTestId("candidate_votes-0.votes");
expect(candidate1).toHaveFocus();
await user.clear(candidate1);
await user.type(candidate1, "12345");
expect(candidate1).toHaveValue("12.345");

await user.keyboard("{enter}");

const candidate2 = screen.getByTestId("candidate_votes-2.votes");
const candidate2 = screen.getByTestId("candidate_votes-1.votes");
expect(candidate2).toHaveFocus();
await user.clear(candidate2);
await user.type(candidate2, "6789");
expect(candidate2).toHaveValue("6.789");

await user.keyboard("{enter}");

const candidate3 = screen.getByTestId("candidate_votes-3.votes");
const candidate3 = screen.getByTestId("candidate_votes-2.votes");
expect(candidate3).toHaveFocus();
await user.clear(candidate3);
await user.type(candidate3, "123");
expect(candidate3).toHaveValue("123");

await user.keyboard("{enter}");

const candidate4 = screen.getByTestId("candidate_votes-4.votes");
const candidate4 = screen.getByTestId("candidate_votes-3.votes");
expect(candidate4).toHaveFocus();
await user.clear(candidate4);
await user.paste("4242");
expect(candidate4).toHaveValue("4.242");

await user.keyboard("{enter}");

const candidate5 = screen.getByTestId("candidate_votes-5.votes");
const candidate5 = screen.getByTestId("candidate_votes-4.votes");
expect(candidate5).toHaveFocus();
await user.clear(candidate5);
await user.type(candidate5, "12");
expect(candidate5).toHaveValue("12");

await user.keyboard("{enter}");

const candidate6 = screen.getByTestId("candidate_votes-6.votes");
const candidate6 = screen.getByTestId("candidate_votes-5.votes");
expect(candidate6).toHaveFocus();
await user.clear(candidate6);
// Test if maxLength on field works
Expand All @@ -132,7 +132,7 @@ describe("Test CandidatesVotesForm", () => {

await user.keyboard("{enter}");

const candidate7 = screen.getByTestId("candidate_votes-7.votes");
const candidate7 = screen.getByTestId("candidate_votes-6.votes");
expect(candidate7).toHaveFocus();
await user.clear(candidate7);
await user.type(candidate7, "3");
Expand Down Expand Up @@ -260,14 +260,14 @@ describe("Test CandidatesVotesForm", () => {

render(Component);

const candidateOne = screen.getByTestId("candidate_votes-1.votes");
const candidateOne = screen.getByTestId("candidate_votes-0.votes");
await user.clear(candidateOne);
await user.type(
candidateOne,
expectedRequest.data.political_group_votes[0]?.candidate_votes[0]?.votes.toString() ?? "0",
);

const candidateTwo = screen.getByTestId("candidate_votes-2.votes");
const candidateTwo = screen.getByTestId("candidate_votes-1.votes");
await user.clear(candidateTwo);
await user.type(
candidateTwo,
Expand Down Expand Up @@ -335,11 +335,7 @@ describe("Test CandidatesVotesForm", () => {
validation_results: {
errors: [
{
fields: [
"data.political_group_votes[1].canidates[1].votes",
"data.political_group_votes[1].canidates[2].votes",
"data.political_group_votes[1].total",
],
fields: ["data.political_group_votes[0].total"],
code: "IncorrectTotal",
},
],
Expand All @@ -350,11 +346,11 @@ describe("Test CandidatesVotesForm", () => {
render(Component);
const user = userEvent.setup();

const candidateOne = screen.getByTestId("candidate_votes-1.votes");
const candidateOne = screen.getByTestId("candidate_votes-0.votes");
await user.clear(candidateOne);
await user.type(candidateOne, "1");

const candidateTwo = screen.getByTestId("candidate_votes-2.votes");
const candidateTwo = screen.getByTestId("candidate_votes-1.votes");
await user.clear(candidateTwo);
await user.type(candidateTwo, "2");

Expand All @@ -381,7 +377,7 @@ describe("CandidatesVotesForm warnings", () => {
errors: [],
warnings: [
{
fields: ["data.political_group_votes[1].candidates[1].votes"],
fields: ["data.political_group_votes[0].candidates[0].votes"],
code: "AboveThreshold",
},
],
Expand All @@ -392,11 +388,11 @@ describe("CandidatesVotesForm warnings", () => {

render(Component);

const candidateOne = screen.getByTestId("candidate_votes-1.votes");
const candidateOne = screen.getByTestId("candidate_votes-0.votes");
await user.clear(candidateOne);
await user.type(candidateOne, "1");

const candidateTwo = screen.getByTestId("candidate_votes-2.votes");
const candidateTwo = screen.getByTestId("candidate_votes-1.votes");
await user.clear(candidateTwo);
await user.type(candidateTwo, "2");

Expand All @@ -421,7 +417,7 @@ describe("CandidatesVotesForm warnings", () => {
errors: [],
warnings: [
{
fields: ["data.political_group_votes[1].total"],
fields: ["data.political_group_votes[0].total"],
code: "AboveThreshold",
},
],
Expand All @@ -432,11 +428,11 @@ describe("CandidatesVotesForm warnings", () => {

render(Component);

const candidateOne = screen.getByTestId("candidate_votes-1.votes");
const candidateOne = screen.getByTestId("candidate_votes-0.votes");
await user.clear(candidateOne);
await user.type(candidateOne, "1");

const candidateTwo = screen.getByTestId("candidate_votes-2.votes");
const candidateTwo = screen.getByTestId("candidate_votes-1.votes");
await user.clear(candidateTwo);
await user.type(candidateTwo, "2");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ export function CandidatesVotesForm({ group }: CandidatesVotesFormProps) {
key={`list${group.number}-candidate${index + 1}`}
field={`${index + 1}`}
name="candidatevotes[]"
id={`candidate_votes-${candidate.number}.votes`}
id={`candidate_votes-${candidate.number - 1}.votes`}
title={`${candidate.last_name}, ${candidate.initials} (${candidate.first_name})`}
errorsAndWarnings={errorsAndWarnings}
inputProps={register()}
Expand Down Expand Up @@ -176,7 +176,7 @@ function candidateNumberFromElement(el: HTMLInputElement) {
const bits = id.split("-");
const numberString = bits[bits.length - 1];
if (numberString) {
return parseInt(numberString);
return parseInt(numberString) + 1;
}
return 0;
}
6 changes: 3 additions & 3 deletions frontend/lib/api-mocks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ export const pollingStationDataEntryHandler = http.post<
if (valueOutOfRange(pg.total)) {
response.validation_results.errors.push({
code: "OutOfRange",
fields: [`data.political_group_votes[${pg.number}].total`],
fields: [`data.political_group_votes[${pg.number - 1}].total`],
});
}

Expand All @@ -143,7 +143,7 @@ export const pollingStationDataEntryHandler = http.post<
response.validation_results.errors.push({
code: "OutOfRange",
fields: [
`data.political_group_votes[${pg.number}].candidate_votes[${cv.number}].votes`,
`data.political_group_votes[${pg.number - 1}].candidate_votes[${cv.number - 1}].votes`,
],
});
}
Expand All @@ -153,7 +153,7 @@ export const pollingStationDataEntryHandler = http.post<
if (sum !== pg.total) {
response.validation_results.errors.push({
code: "IncorrectTotal",
fields: [`data.political_group_votes[${pg.number}].total`],
fields: [`data.political_group_votes[${pg.number - 1}].total`],
});
}
});
Expand Down
4 changes: 2 additions & 2 deletions frontend/lib/api/form/pollingstation/usePoliticalGroups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export function usePoliticalGroup(political_group_number: number) {
if (data) {
return data.validation_results.errors.filter((err) =>
matchValidationResultWithFormSections(err.fields, [
`political_group_votes[${political_group_number}]`,
`political_group_votes[${political_group_number - 1}]`,
]),
);
}
Expand All @@ -42,7 +42,7 @@ export function usePoliticalGroup(political_group_number: number) {
if (data) {
return data.validation_results.warnings.filter((warning) =>
matchValidationResultWithFormSections(warning.fields, [
`political_group_votes[${political_group_number}]`,
`political_group_votes[${political_group_number - 1}]`,
]),
);
}
Expand Down

0 comments on commit 103d233

Please sign in to comment.