slice-clause-tool.spec.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import {expect} from 'chai';
  2. import {format} from '@e22m4u/util-format';
  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 = v => () => SliceClauseTool.validateSkipClause(v);
  69. const error = v =>
  70. format(
  71. 'The provided option "skip" should be a Number, but %s given.',
  72. v,
  73. );
  74. expect(validate('str')).to.throw(error('"str"'));
  75. expect(validate(true)).to.throw(error('true'));
  76. expect(validate([])).to.throw(error('Array'));
  77. validate('');
  78. validate(false);
  79. validate(undefined);
  80. validate(null);
  81. validate(10);
  82. validate(0);
  83. });
  84. });
  85. describe('validateLimitClause', function () {
  86. it('requires a number value or a falsy value', function () {
  87. const validate = v => () => SliceClauseTool.validateLimitClause(v);
  88. const error = v =>
  89. format(
  90. 'The provided option "limit" should be a Number, but %s given.',
  91. v,
  92. );
  93. expect(validate('str')).to.throw(error('"str"'));
  94. expect(validate(true)).to.throw(error('true'));
  95. expect(validate([])).to.throw(error('Array'));
  96. validate('');
  97. validate(false);
  98. validate(undefined);
  99. validate(null);
  100. validate(10);
  101. validate(0);
  102. });
  103. });
  104. });