exclude-object-keys.spec.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import {expect} from 'chai';
  2. import {format} from '@e22m4u/js-format';
  3. import {excludeObjectKeys} from './exclude-object-keys.js';
  4. describe('excludeObjectKeys', function () {
  5. it('returns a given object without a specified key', function () {
  6. const input = {
  7. foo: 'string',
  8. bar: 10,
  9. baz: true,
  10. qux: [1, 2, 3],
  11. };
  12. const result = excludeObjectKeys(input, 'bar');
  13. expect(result).to.be.not.eq(input);
  14. expect(result).to.be.eql({
  15. foo: 'string',
  16. baz: true,
  17. qux: [1, 2, 3],
  18. });
  19. });
  20. it('returns a given object without a specified keys', function () {
  21. const input = {
  22. foo: 'string',
  23. bar: 10,
  24. baz: true,
  25. qux: [1, 2, 3],
  26. };
  27. const result = excludeObjectKeys(input, ['bar', 'qux']);
  28. expect(result).to.be.not.eq(input);
  29. expect(result).to.be.eql({
  30. foo: 'string',
  31. baz: true,
  32. });
  33. });
  34. it('throws an error for a non-object values', function () {
  35. const throwable = v => () => excludeObjectKeys(v, 'key');
  36. const error = v =>
  37. format('Cannot exclude keys from a non-Object value, %s given.', v);
  38. expect(throwable('str')).to.throw(error('"str"'));
  39. expect(throwable(10)).to.throw(error('10'));
  40. expect(throwable(true)).to.throw(error('true'));
  41. expect(throwable(false)).to.throw(error('false'));
  42. expect(throwable([])).to.throw(error('Array'));
  43. expect(throwable(null)).to.throw(error('null'));
  44. expect(throwable(undefined)).to.throw(error('undefined'));
  45. });
  46. });