util.spec.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. describe('Util functions', function () {
  9. var ballot = require('./mocks/ballot.json');
  10. describe('getText()', function () {
  11. var getText = require('../../../src/lib/parsers/util.js').getText;
  12. it('returns an empty string if translations collection has not been specified', function () {
  13. var text = getText();
  14. expect(text).toBe('');
  15. });
  16. it('returns an empty string if translation attribute id has not been specified', function () {
  17. var text = getText(ballot.ballotTexts[0].texts);
  18. expect(text).toBe('');
  19. });
  20. it('returns an empty string if translation attribute property has not been specified', function () {
  21. var text = getText(ballot.ballotTexts[0].texts, '220e11ebf42a47088dc5a7cad00bbd9a');
  22. expect(text).toBe('');
  23. });
  24. it('returns an empty string if translation attribute property does not exist', function () {
  25. var text = getText(ballot.ballotTexts[0].texts, 'dff31b210f734fa3973c43b277d4d39d', 'missingProp');
  26. expect(text).toBe('');
  27. });
  28. it('returns an empty string if attribute property is not a string and translation key is null', function () {
  29. var text = getText(ballot.ballotTexts[0].texts, '220e11ebf42a47088dc5a7cad00bbd9a', 'data');
  30. expect(text).toBe('');
  31. });
  32. it('returns an empty string if translation key was not found', function () {
  33. var text = getText(ballot.ballotTexts[0].texts, '220e11ebf42a47088dc5a7cad00bbd9a', 'data', 'missingKey');
  34. expect(text).toBe('');
  35. });
  36. it('returns the translation for a specific attribute property', function () {
  37. var text = getText(ballot.ballotTexts[0].texts, 'dff31b210f734fa3973c43b277d4d39d', 'title');
  38. expect(text).toBe(ballot.ballotTexts[0].texts['dff31b210f734fa3973c43b277d4d39d'].title);
  39. });
  40. it('returns the translation for a specific key', function () {
  41. var text = getText(ballot.ballotTexts[0].texts, '220e11ebf42a47088dc5a7cad00bbd9a', 'data', 'firstName');
  42. expect(text).toBe(ballot.ballotTexts[0].texts['220e11ebf42a47088dc5a7cad00bbd9a'].data[0].value);
  43. });
  44. it('returns the translation for a specific key index', function () {
  45. var text = getText(ballot.ballotTexts[0].texts, '220e11ebf42a47088dc5a7cad00bbd9a', 'data', 0);
  46. expect(text).toBe(ballot.ballotTexts[0].texts['220e11ebf42a47088dc5a7cad00bbd9a'].data[0].value);
  47. });
  48. });
  49. describe('parseAttributeTranslations()', function () {
  50. const mockTranslations = {
  51. a: 2,
  52. b: { firstName: 'FIRST_NAME' },
  53. c: [
  54. { firstName: 'FIRST_NAME' },
  55. { lastName: 'LAST_NAME' },
  56. ],
  57. d: [
  58. {
  59. firstName: 'FIRST_NAME',
  60. lastName: 'LAST_NAME',
  61. },
  62. { middleName: 'MIDDLE_NAME' },
  63. ],
  64. };
  65. var parseAttributeTranslations = require('../../../src/lib/parsers/util.js').parseAttributeTranslations;
  66. it('returns an empty object if translations collection has not been specified', function () {
  67. var texts = parseAttributeTranslations();
  68. expect(texts).toEqual({});
  69. });
  70. it('returns an empty object if translation attribute id has not been specified', function () {
  71. var texts = parseAttributeTranslations(mockTranslations);
  72. expect(texts).toEqual({});
  73. });
  74. it('returns an empty object if translation attribute id does not exist', function () {
  75. var texts = parseAttributeTranslations(mockTranslations, 'NO_ID');
  76. expect(texts).toEqual({});
  77. });
  78. it('returns an empty object if translation attribute is not an array', function () {
  79. var texts = parseAttributeTranslations(mockTranslations, 'a');
  80. expect(texts).toEqual({});
  81. var texts = parseAttributeTranslations(mockTranslations, 'b');
  82. expect(texts).toEqual({});
  83. });
  84. it('returns an object representing the translations for an attribute representing an array of simple objects', function () {
  85. var texts = parseAttributeTranslations(mockTranslations, 'c');
  86. expect(texts).toEqual({
  87. firstName: 'FIRST_NAME',
  88. lastName: 'LAST_NAME',
  89. });
  90. });
  91. it('returns an object representing the translations for an attribute represents a array of more complex objects', function () {
  92. var texts = parseAttributeTranslations(mockTranslations, 'd');
  93. expect(texts).toEqual({
  94. firstName: 'FIRST_NAME',
  95. middleName: 'MIDDLE_NAME',
  96. lastName: 'LAST_NAME',
  97. });
  98. });
  99. });
  100. });