utils.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import '../_setup.js';
  2. import {
  3. lastItemOf,
  4. pushUnique,
  5. stringToArray,
  6. isInRange,
  7. limitToRange,
  8. createTagRepeat,
  9. optimizeTemplateHTML
  10. } from '../../../js/lib/utils.js';
  11. describe('lib/utils', function () {
  12. describe('lastItemOf()', function () {
  13. it('returns the last item of array', function () {
  14. expect(lastItemOf([1, 2, 3]), 'to be', 3);
  15. expect(lastItemOf(['foo']), 'to be', 'foo');
  16. expect(lastItemOf([]), 'to be undefined');
  17. });
  18. });
  19. describe('pushUnique()', function () {
  20. it('add items to an array if the item is not in the array', function () {
  21. let arr = ['foo', 'bar'];
  22. pushUnique(arr, 'foo');
  23. expect(arr, 'to equal', arr);
  24. pushUnique(arr, 'baz');
  25. expect(arr, 'to equal', ['foo', 'bar', 'baz']);
  26. arr = ['foo', 'bar'];
  27. pushUnique(arr, 'bar', 'baz', 'bam', 'baz');
  28. expect(arr, 'to equal', ['foo', 'bar', 'baz', 'bam']);
  29. });
  30. it('returns the given array', function () {
  31. let arr = ['foo', 'bar'];
  32. expect(pushUnique(arr, 'baz'), 'to be', arr);
  33. });
  34. });
  35. describe('stringToArray()', function () {
  36. it('converts a string to array by spliting it with given separator', function () {
  37. expect(stringToArray('foo,bar,baz', ','), 'to equal', ['foo', 'bar', 'baz']);
  38. expect(stringToArray('abc-def', '-'), 'to equal', ['abc', 'def']);
  39. });
  40. it('return an empty array if string is empty', function () {
  41. expect(stringToArray('', ','), 'to equal', []);
  42. });
  43. });
  44. describe('isInRange()', function () {
  45. it('returns whether a value is between given min & max values', function () {
  46. expect(isInRange(0, 1, 3), 'to be false');
  47. expect(isInRange(1, 1, 3), 'to be true');
  48. expect(isInRange(2, 1, 3), 'to be true');
  49. expect(isInRange(3, 1, 3), 'to be true');
  50. expect(isInRange(4, 1, 3), 'to be false');
  51. //
  52. expect(isInRange('abb', 'abc', 'ccc'), 'to be false');
  53. expect(isInRange('bbc', 'abc', 'ccc'), 'to be true');
  54. expect(isInRange('ccb', 'abc', 'ccc'), 'to be true');
  55. expect(isInRange('ccd', 'abc', 'ccc'), 'to be false');
  56. });
  57. it('omits minimum check if min = undefined', function () {
  58. expect(isInRange(0, undefined, 3), 'to be true');
  59. expect(isInRange(4, undefined, 3), 'to be false');
  60. expect(isInRange(-Infinity, undefined, 3), 'to be true');
  61. });
  62. it('omits maximum check if max = undefined', function () {
  63. expect(isInRange(0, 1, undefined), 'to be false');
  64. expect(isInRange(4, 1, undefined), 'to be true');
  65. expect(isInRange(Infinity, 1, undefined), 'to be true');
  66. });
  67. });
  68. describe('limitToRange()', function () {
  69. it('returns min value if it\'s specified and the value < min', function () {
  70. expect(limitToRange(0, 1, undefined), 'to be', 1);
  71. expect(limitToRange(0, 1, 3), 'to be', 1);
  72. expect(limitToRange('abb', 'abc', 'ccc'), 'to be', 'abc');
  73. });
  74. it('returns max value if it\'s specified and the value > max', function () {
  75. expect(limitToRange(4, undefined, 3), 'to be', 3);
  76. expect(limitToRange(4, 1, 3), 'to be', 3);
  77. expect(limitToRange('ccd', 'abc', 'ccc'), 'to be', 'ccc');
  78. });
  79. it('returns the given value if it is within the specified min/max', function () {
  80. expect(limitToRange(1, undefined, undefined), 'to be', 1);
  81. expect(limitToRange(1, undefined, 3), 'to be', 1);
  82. expect(limitToRange(3, 1, undefined), 'to be', 3);
  83. expect(limitToRange(1, 1, 3), 'to be', 1);
  84. expect(limitToRange(3, 1, 3), 'to be', 3);
  85. expect(limitToRange('abc', 'aaa', 'ccc'), 'to be', 'abc');
  86. expect(limitToRange('aaa', 'aaa', 'ccc'), 'to be', 'aaa');
  87. expect(limitToRange('ccc', 'aaa', 'ccc'), 'to be', 'ccc');
  88. });
  89. });
  90. describe('createTagRepeat()', function () {
  91. it('returns HTML of a tag repeated specified times', function () {
  92. expect(createTagRepeat('div', 3), 'to be', '<div></div><div></div><div></div>');
  93. expect(createTagRepeat('span', 2), 'to be', '<span></span><span></span>');
  94. // returns at least 1 tag
  95. expect(createTagRepeat('p', 0), 'to be', '<p></p>');
  96. });
  97. it('adds addributes if name:value pairs are given', function () {
  98. expect(createTagRepeat('p', 2, {class: 'foo bar'}), 'to be', '<p class="foo bar"></p><p class="foo bar"></p>');
  99. expect(createTagRepeat('p', 2, {'data-x': '0', 'data-y': '1'}), 'to be', '<p data-x="0" data-y="1"></p><p data-x="0" data-y="1"></p>');
  100. });
  101. it('accepts function that takes current index as the argument for attribute value', function () {
  102. expect(createTagRepeat('li', 3, {'data-cnt': ix => ix + 1}), 'to be', '<li data-cnt="1"></li><li data-cnt="2"></li><li data-cnt="3"></li>');
  103. });
  104. });
  105. describe('optimizeTemplateHTML()', function () {
  106. it('removes spacing before and after tags', function () {
  107. let text = `<div id="test">
  108. <div>
  109. test 123
  110. </div>
  111. </div>`;
  112. expect(optimizeTemplateHTML(text), 'to be', '<div id="test"><div>test 123</div></div>');
  113. expect(optimizeTemplateHTML('foo'), 'to be', 'foo');
  114. });
  115. });
  116. });