slice-clause-tool.spec.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import {expect} from 'chai';
  2. import {format} from 'util';
  3. import {SliceClauseTool} from './slice-clause-tool.js';
  4. const S = new SliceClauseTool();
  5. describe('SliceClauseTool', function () {
  6. describe('filter', function () {
  7. it('does nothing if no clauses provided', function () {
  8. const objects = [{id: 1}, {id: 2}, {id: 3}];
  9. const result = S.slice(objects);
  10. expect(result).to.be.eql(objects);
  11. });
  12. it('does nothing if a given skip is zero', function () {
  13. const objects = [{id: 1}, {id: 2}, {id: 3}];
  14. const result = S.slice(objects, 0);
  15. expect(result).to.be.eql(objects);
  16. });
  17. it('uses a given skip to exclude array elements from start', function () {
  18. const objects = [{id: 1}, {id: 2}, {id: 3}];
  19. const result = S.slice(objects, 2);
  20. expect(result).to.have.length(1);
  21. expect(result[0]).to.be.eql(objects[2]);
  22. });
  23. it('returns an empty array if skipping too much', function () {
  24. const objects = [{id: 1}, {id: 2}, {id: 3}];
  25. const result = S.slice(objects, 10);
  26. expect(result).to.have.length(0);
  27. });
  28. it('does nothing if a given limit is zero', function () {
  29. const objects = [{id: 1}, {id: 2}, {id: 3}];
  30. const result = S.slice(objects, undefined, 0);
  31. expect(result).to.be.eql(objects);
  32. });
  33. it('uses a given limit to trim a given array', function () {
  34. const objects = [{id: 1}, {id: 2}, {id: 3}];
  35. const result = S.slice(objects, undefined, 2);
  36. expect(result).to.have.length(2);
  37. expect(result[0]).to.be.eql(objects[0]);
  38. expect(result[1]).to.be.eql(objects[1]);
  39. });
  40. it('able to combine a skip and a limit option together', function () {
  41. const objects = [{id: 1}, {id: 2}, {id: 3}];
  42. const result = S.slice(objects, 1, 1);
  43. expect(result).to.have.length(1);
  44. expect(result[0]).to.be.eql(objects[1]);
  45. });
  46. it('throws an error if a first argument is not an array', function () {
  47. const throwable = () => S.slice(10);
  48. expect(throwable).to.throw(
  49. 'A first argument of SliceClauseTool.slice ' +
  50. 'should be an Array, but 10 given.',
  51. );
  52. });
  53. it('throws an error if the given "skip" option is not a number', function () {
  54. const throwable = () => S.slice([], 'invalid');
  55. expect(throwable).to.throw(
  56. 'The provided option "skip" should be a Number, but "invalid" given.',
  57. );
  58. });
  59. it('throws an error if the given "limit" option is not a number', function () {
  60. const throwable = () => S.slice([], undefined, 'invalid');
  61. expect(throwable).to.throw(
  62. 'The provided option "limit" should be a Number, but "invalid" given.',
  63. );
  64. });
  65. });
  66. describe('validateSkipClause', function () {
  67. it('requires a number value or a falsy value', function () {
  68. const validate = clause => () =>
  69. SliceClauseTool.validateSkipClause(clause);
  70. const error = value =>
  71. format(
  72. 'The provided option "skip" should be a Number, but %s given.',
  73. value,
  74. );
  75. expect(validate('str')).to.throw(error('"str"'));
  76. expect(validate(true)).to.throw(error('true'));
  77. expect(validate([])).to.throw(error('Array'));
  78. validate('');
  79. validate(false);
  80. validate(undefined);
  81. validate(null);
  82. validate(10);
  83. validate(0);
  84. });
  85. });
  86. describe('validateLimitClause', function () {
  87. it('requires a number value or a falsy value', function () {
  88. const validate = clause => () =>
  89. SliceClauseTool.validateLimitClause(clause);
  90. const error = value =>
  91. format(
  92. 'The provided option "limit" should be a Number, but %s given.',
  93. value,
  94. );
  95. expect(validate('str')).to.throw(error('"str"'));
  96. expect(validate(true)).to.throw(error('true'));
  97. expect(validate([])).to.throw(error('Array'));
  98. validate('');
  99. validate(false);
  100. validate(undefined);
  101. validate(null);
  102. validate(10);
  103. validate(0);
  104. });
  105. });
  106. });