Skip to content

Commit

Permalink
Fix open election no description (#22)
Browse files Browse the repository at this point in the history
* Fix open election with no description

* Make code more readable

* Add validation
  • Loading branch information
Georges Mainville authored Aug 17, 2022
1 parent 52b5890 commit 0921a81
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
36 changes: 26 additions & 10 deletions src/lib/belenios/admin/__tests__/openElection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,30 +32,23 @@ describe('Tests createElection', () => {
});

it('Should return an election id. Election with no description', () => {
const election = {
const election1 = {
name: 'Name of the election',
questions: [{
answers: ['Answer 1', 'Answer 2'], min: 0, max: 1, question: 'Question 1?',
}, {
answers: ['Answer 1', 'Answer 2'], blank: true, min: 1, max: 1, question: 'Question 2?',
}],
};

const electionId = openElection(DEFAULT_VOTERS, election);
deleteElection(electionId);

const election2 = {
description: '',
description: null,
name: 'Name of the election',
questions: [{
answers: ['Answer 1', 'Answer 2'], min: 0, max: 1, question: 'Question 1?',
}, {
answers: ['Answer 1', 'Answer 2'], blank: true, min: 1, max: 1, question: 'Question 2?',
}],
};

const electionId2 = openElection(DEFAULT_VOTERS, election2);
deleteElection(electionId2);

const election3 = {
description: { fr: undefined, en: undefined },
name: 'Name of the election',
Expand All @@ -65,8 +58,31 @@ describe('Tests createElection', () => {
answers: ['Answer 1', 'Answer 2'], blank: true, min: 1, max: 1, question: 'Question 2?',
}],
};
const election4 = {
name: 'Name of the election',
description: '',
questions: [{
answers: ['Answer 1', 'Answer 2'], min: 0, max: 1, question: 'Question 1?',
}, {
answers: ['Answer 1', 'Answer 2'], blank: true, min: 1, max: 1, question: 'Question 2?',
}],
};

const electionId1 = openElection(DEFAULT_VOTERS, election1);
deleteElection(electionId1);

const electionId2 = openElection(DEFAULT_VOTERS, election2);
deleteElection(electionId2);

const electionId3 = openElection(DEFAULT_VOTERS, election3);
deleteElection(electionId3);

const electionId4 = openElection(DEFAULT_VOTERS, election4);
deleteElection(electionId4);

expect(electionId1).toBeDefined();
expect(electionId2).toBeDefined();
expect(electionId3).toBeDefined();
expect(electionId4).toBeDefined();
});
});
2 changes: 1 addition & 1 deletion src/lib/belenios/admin/makeElection.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function stringifyTemplate(template) {
questions,
} = template;

const stringifyDescription = JSON.stringify(description);
const stringifyDescription = JSON.stringify(description) || '';
const stringifyName = JSON.stringify(name);
const stringifyQuestions = questions.map(({
answers,
Expand Down

0 comments on commit 0921a81

Please sign in to comment.