From 0921a81fad7170568bae1b2dba6034fc28d3d4e2 Mon Sep 17 00:00:00 2001 From: Georges Mainville Date: Wed, 17 Aug 2022 13:53:13 -0400 Subject: [PATCH] Fix open election no description (#22) * Fix open election with no description * Make code more readable * Add validation --- .../admin/__tests__/openElection.test.js | 36 +++++++++++++------ src/lib/belenios/admin/makeElection.js | 2 +- 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/src/lib/belenios/admin/__tests__/openElection.test.js b/src/lib/belenios/admin/__tests__/openElection.test.js index b9dcf72..e54347e 100644 --- a/src/lib/belenios/admin/__tests__/openElection.test.js +++ b/src/lib/belenios/admin/__tests__/openElection.test.js @@ -32,7 +32,7 @@ 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?', @@ -40,22 +40,15 @@ describe('Tests createElection', () => { 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', @@ -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(); }); }); diff --git a/src/lib/belenios/admin/makeElection.js b/src/lib/belenios/admin/makeElection.js index f0c788d..e35792a 100644 --- a/src/lib/belenios/admin/makeElection.js +++ b/src/lib/belenios/admin/makeElection.js @@ -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,