select-object-keys.spec.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import {expect} from 'chai';
  2. import {selectObjectKeys} from './select-object-keys.js';
  3. describe('selectObjectKeys', function () {
  4. it('returns a new object with selected fields', function () {
  5. const input = {foo: 'foo', bar: 'bar', baz: 'baz'};
  6. const result = selectObjectKeys(input, ['bar', 'baz']);
  7. expect(result).to.be.eql({bar: 'bar', baz: 'baz'});
  8. });
  9. it('does not throw an error if a selected property is not found', function () {
  10. const input = {foo: 'foo', bar: 'bar', baz: 'baz'};
  11. const result = selectObjectKeys(input, ['bar', 'qux']);
  12. expect(result).to.be.eql({bar: 'bar'});
  13. });
  14. it('throws an error if a given value is not an Object', function () {
  15. const throwable = () => selectObjectKeys(10, ['key']);
  16. expect(throwable).to.throw(
  17. 'The first argument of selectObjectKeys ' +
  18. 'should be an Object, but 10 given.',
  19. );
  20. });
  21. it('throws an error if a given keys is not an Array', function () {
  22. const throwable = () => selectObjectKeys({});
  23. expect(throwable).to.throw(
  24. 'The second argument of selectObjectKeys ' +
  25. 'should be an Array of String, but undefined given.',
  26. );
  27. });
  28. it('throws an error if a given keys is not an String', function () {
  29. const throwable = () => selectObjectKeys({}, [10]);
  30. expect(throwable).to.throw(
  31. 'The second argument of selectObjectKeys ' +
  32. 'should be an Array of String, but 10 given.',
  33. );
  34. });
  35. });