listsAndCandidates.spec.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * Copyright 2018 Scytl Secure Electronic Voting SA
  3. *
  4. * All rights reserved
  5. *
  6. * See our extended copyright notice in *file 'Copyright.txt' which is part of this source code package
  7. */
  8. /* global OV */
  9. describe('Lists And Candidates contest parser', function() {
  10. 'use strict';
  11. var lacParser = OV.ListsAndCandidatesParser;
  12. var contest = require('./mocks/listsAndCandidates.json');
  13. var candidatesOnly = require('./mocks/candidatesOnly.json');
  14. var candidatesWithWriteIns = require('./mocks/candidatesWithWriteIns.json');
  15. it('should parse the contest configuration', function() {
  16. var parsed = lacParser.parse(contest);
  17. expect(parsed.template).toBe('listsAndCandidates');
  18. expect(parsed.allowFullBlank).toBe(true);
  19. });
  20. it('should parse the lists min and max restrictions', function() {
  21. var parsed = lacParser.parse(contest);
  22. // If fails, probably is due to not being able to find the question (alias changed?)
  23. expect(parsed.listQuestion.minChoices).toBe(0);
  24. expect(parsed.listQuestion.maxChoices).toBe(1);
  25. });
  26. it('should parse the candidates min and max restrictions', function() {
  27. var parsed = lacParser.parse(contest);
  28. expect(parsed.candidatesQuestion.minChoices).toBe(0);
  29. expect(parsed.candidatesQuestion.maxChoices).toBe(2);
  30. });
  31. it('should parse the lists', function() {
  32. var parsed = lacParser.parse(contest);
  33. expect(parsed.lists.length).toBe(3);
  34. expect(parsed.lists
  35. .filter(function(l) { return l.prime === '100109' }).length).toBe(1);
  36. expect(parsed.lists
  37. .filter(function(l) { return l.prime === '100169' }).length).toBe(1);
  38. expect(parsed.lists
  39. .filter(function(l) { return l.prime === '100153' }).length).toBe(1);
  40. });
  41. it('should parse the blank list', function() {
  42. var parsed = lacParser.parse(contest);
  43. var blankList = parsed.lists.filter(function(l) {
  44. return l.prime === '100109' })[0];
  45. expect(blankList.isBlank).toBe(true);
  46. expect(blankList.candidates.length).toBe(2);
  47. expect(blankList.candidates
  48. .filter(function(c) { return c.prime === '100183' }).length).toBe(1);
  49. expect(blankList.candidates
  50. .filter(function(c) { return c.prime === '100193' }).length).toBe(1);
  51. });
  52. it('should parse the candidates', function() {
  53. var parsed = lacParser.parse(contest);
  54. var list1 = parsed.lists.filter(function(l) {
  55. return l.prime === '100169' })[0];
  56. expect(list1.isBlank).toBe(false);
  57. expect(list1.candidates.length).toBe(2);
  58. expect(list1.candidates
  59. .filter(function(c) { return c.prime === '100129' }).length).toBe(1);
  60. expect(list1.candidates
  61. .filter(function(c) { return c.prime === '100237' }).length).toBe(1);
  62. var list2 = parsed.lists.filter(function(l) {
  63. return l.prime === '100153' })[0];
  64. expect(list2.isBlank).toBe(false);
  65. expect(list2.candidates.length).toBe(2);
  66. expect(list2.candidates
  67. .filter(function(c) { return c.prime === '100207' }).length).toBe(1);
  68. expect(list2.candidates
  69. .filter(function(c) { return c.prime === '100267' }).length).toBe(1);
  70. });
  71. it('should parse the blank candidate inside a non selectable blank list', function() {
  72. var parsed = lacParser.parse(candidatesOnly);
  73. var blankList = parsed.lists.filter(function(l) {
  74. return l.isBlank === true })[0];
  75. expect(blankList.isBlank).toBe(true);
  76. expect(blankList.candidates.length).toBe(1);
  77. expect(blankList.candidates
  78. .filter(function(c) { return c.prime === '100673' }).length).toBe(1);
  79. });
  80. it('should parse the candidates with non selectable lists', function() {
  81. var parsed = lacParser.parse(candidatesOnly);
  82. var list1 = parsed.lists.filter(function(l) {
  83. return l.id === '9053f1203ecf4281b220b9c385c3c18e' })[0];
  84. expect(list1.isBlank).toBe(false);
  85. expect(list1.candidates.length).toBe(2);
  86. expect(list1.candidates
  87. .filter(function(c) { return c.prime === '10069' }).length).toBe(1);
  88. expect(list1.candidates
  89. .filter(function(c) { return c.prime === '100613' }).length).toBe(1);
  90. var list2 = parsed.lists.filter(function(l) {
  91. return l.id === '980cd86438a048e78737405f34687c05' })[0];
  92. expect(list2.isBlank).toBe(false);
  93. expect(list2.candidates.length).toBe(2);
  94. expect(list2.candidates
  95. .filter(function(c) { return c.prime === '100559' }).length).toBe(1);
  96. expect(list2.candidates
  97. .filter(function(c) { return c.prime === '100549' }).length).toBe(1);
  98. });
  99. it('should parse the candidates with writeins', function() {
  100. var parsed = lacParser.parse(candidatesWithWriteIns);
  101. var blankList = parsed.lists.find(function(l) {
  102. return l.isBlank === true });
  103. expect(blankList.candidates.length).toBe(2);
  104. expect(blankList.candidates.find(function (o) { return o.isBlank; })).toBeDefined();
  105. expect(blankList.candidates.find(function (o) { return o.isWriteIn; })).toBeDefined();
  106. });
  107. });