utils.spec.tsx 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import {omit} from './utils';
  2. describe('omit', () => {
  3. // Omit and omit fn signatures are the same, so we can test them together
  4. // and they should be compatible for shallow objects.
  5. it('returns empty object for invalid input', () => {
  6. for (const v of [1, 'a', true, false, NaN, undefined, null, [], () => {}]) {
  7. const o = omit(v as object, 'a');
  8. expect(Object.prototype.toString.call(o)).toBe('[object Object]');
  9. expect(Object.keys(o)).toHaveLength(0);
  10. }
  11. });
  12. it('does nothing if key does not exist', () => {
  13. const obj = {a: 1, ab: 2};
  14. expect(omit(obj, 'b')).toEqual(obj);
  15. expect(omit(obj, 'b')).not.toBe(obj);
  16. });
  17. it('omits a key', () => {
  18. expect(omit({a: 1, b: 2}, 'a')).toEqual({b: 2});
  19. });
  20. it('omits keys', () => {
  21. expect(omit({a: 1, b: 2, c: 3}, ['a', 'c'])).toEqual({b: 2});
  22. });
  23. it('omits nested keys', () => {
  24. expect(omit({a: {b: {c: 3}}}, 'a.b.c')).toEqual({a: {b: {}}});
  25. });
  26. it('omits shallow key if it is a property', () => {
  27. expect(omit({a: 1, 'a.b.c': 2}, 'a.b.c')).toEqual({a: 1});
  28. });
  29. it('omits both shallow and deep key if they are valid properties', () => {
  30. // c is a shallow key, a.b.c is a deep key, both are valid properties and should be removed
  31. expect(omit({a: {b: {c: 2}}, 'a.b.c': 2, d: 1}, 'a.b.c')).toEqual({a: {b: {}}, d: 1});
  32. });
  33. it('does not omit on partial path hit', () => {
  34. expect(omit({a: {b: {c: 3}}}, 'a.b.d')).toEqual({a: {b: {c: 3}}});
  35. });
  36. it('fallbacks to cloneDeep if source is not cloneable', () => {
  37. const v = {a: 1, b: () => {}};
  38. expect(omit(v, 'a')).toEqual({b: v.b});
  39. });
  40. it('does not mutate argument value', () => {
  41. const v = {a: {b: 1, c: 2}};
  42. expect(omit(v, 'a.b')).toEqual({a: {c: 2}});
  43. expect(v).toEqual({a: {b: 1, c: 2}});
  44. });
  45. });