ballot.spec.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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('Ballot model', function() {
  10. 'use strict';
  11. var listsAndCandidates = require('./mocks/listsAndCandidates.json');
  12. var optionsContest = require('./mocks/options.json');
  13. it('should init a contest', function() {
  14. var c1 = new OV.ListsAndCandidates(listsAndCandidates);
  15. expect(c1.template).toBe('listsAndCandidates');
  16. expect(c1.allowFullBlank).toBe(true);
  17. expect(typeof c1.validateVoteCorrectness).toBe('function');
  18. });
  19. it('should create a simple question with two options', function() {
  20. var b1 = new OV.Ballot('001');
  21. var c1 = new OV.Options(optionsContest);
  22. var q1 = new OV.Question(optionsContest.questions[0]);
  23. var o1 = new OV.Option(optionsContest.options[0], false);
  24. var o2 = new OV.Option(optionsContest.options[1], false);
  25. b1.addContest(c1);
  26. c1.addQuestion(q1);
  27. q1.addOption(o1);
  28. q1.addOption(o2);
  29. expect(typeof c1.validateVoteCorrectness).toBe('function');
  30. expect(q1).toBeDefined();
  31. expect(q1.options.length).toBe(2);
  32. expect(o1.parent).toBe(q1);
  33. expect(o2.parent).toBe(q1);
  34. expect(b1.getQualifiedId()).toBe('001');
  35. expect(c1.getQualifiedId()).toBe('001_e133300f6a124ca8aea3fd7c935b18e0');
  36. expect(q1.getQualifiedId()).toBe('001_e133300f6a124ca8aea3fd7c935b18e0_50e2e005c2c0413a96cd6b36e659fae0');
  37. });
  38. it('should create a question with a blank option', function() {
  39. var q1 = new OV.Question(optionsContest.questions[0]);
  40. var o1 = new OV.Option(optionsContest.options[1], true);
  41. q1.addOption(o1);
  42. expect(q1.options.length).toBe(0);
  43. expect(q1.blankOption).toBe(o1);
  44. });
  45. it('should keep track of options ordering within the questions', function() {
  46. var q = new OV.Question('q');
  47. var o1 = new OV.Option({id:'1', representation: "3"});
  48. var o2 = new OV.Option({id:'1', representation: "5"});
  49. var o3 = new OV.Option({id:'1', representation: "7"});
  50. var oblank = new OV.Option({id:'1', representation: "11"}, true);
  51. q.addOption(o1);
  52. q.addOption(o2);
  53. q.addOption(o3);
  54. q.addOption(oblank);
  55. expect(o1.ordinal).toBe(1);
  56. expect(o2.ordinal).toBe(2);
  57. expect(o3.ordinal).toBe(3);
  58. expect(oblank.ordinal).toBe(0);
  59. });
  60. it('should check types of childs', function() {
  61. expect(function() {
  62. var x = new OV.Ballot('001');
  63. x.addContest({});
  64. }).toThrow(new Error('Bad argument type, need a Contest'));
  65. expect(function() {
  66. var x = new OV.Options('001', 'options');
  67. x.addQuestion({});
  68. }).toThrow(new Error('Bad argument type, need a Question'));
  69. expect(function() {
  70. var x = new OV.Question('001');
  71. x.addOption({});
  72. }).toThrow(new Error('Bad argument type, need an Option'));
  73. });
  74. it('should not accept more than one blank option in a question', function() {
  75. var q1 = new OV.Question('001');
  76. var o1 = new OV.Option('001', 3, true, true);
  77. var o2 = new OV.Option('002', 5, true, false);
  78. q1.addOption(o1);
  79. expect(function() {
  80. q1.addOption(o2);
  81. }).toThrow(new Error('Question already has a blank option'));
  82. });
  83. });