omit.spec.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import { omit } from './omit';
  2. declare const File: any;
  3. describe('omit()', () => {
  4. let patchedFileClass = false;
  5. beforeAll(() => {
  6. // In Node.js there is no File constructor, so we need to patch
  7. // a mock version.
  8. if (typeof File === 'undefined') {
  9. (global as any).File = class MockFile {};
  10. patchedFileClass = true;
  11. }
  12. });
  13. afterAll(() => {
  14. if (patchedFileClass) {
  15. delete (global as any).File;
  16. }
  17. });
  18. it('returns a new object', () => {
  19. const obj = { foo: 1, bar: 2 };
  20. expect(omit(obj, ['bar'])).not.toBe(obj);
  21. });
  22. it('works with 1-level-deep objects', () => {
  23. expect(omit({ foo: 1, bar: 2 }, ['bar'])).toEqual({ foo: 1 });
  24. expect(omit({ foo: 1, bar: 2 }, ['bar', 'foo'])).toEqual({});
  25. });
  26. it('works with deeply-nested objects', () => {
  27. expect(
  28. omit(
  29. {
  30. name: {
  31. first: 'joe',
  32. last: 'smith',
  33. },
  34. address: {
  35. number: 12,
  36. },
  37. },
  38. ['address'],
  39. ),
  40. ).toEqual({
  41. name: {
  42. first: 'joe',
  43. last: 'smith',
  44. },
  45. });
  46. });
  47. describe('recursive', () => {
  48. it('returns a new object', () => {
  49. const obj = { foo: 1, bar: 2 };
  50. expect(omit(obj, ['bar'], true)).not.toBe(obj);
  51. });
  52. it('works with 1-level-deep objects', () => {
  53. const input = {
  54. foo: 1,
  55. bar: 2,
  56. baz: 3,
  57. };
  58. const expected = { foo: 1, baz: 3 };
  59. expect(omit(input, ['bar'], true)).toEqual(expected);
  60. });
  61. it('works with 2-level-deep objects', () => {
  62. const input = {
  63. foo: 1,
  64. bar: {
  65. bad: true,
  66. good: true,
  67. },
  68. baz: {
  69. bad: true,
  70. },
  71. };
  72. const expected = {
  73. foo: 1,
  74. bar: {
  75. good: true,
  76. },
  77. baz: {},
  78. };
  79. expect(omit(input, ['bad'], true)).toEqual(expected);
  80. });
  81. it('works with array of objects', () => {
  82. const input = {
  83. foo: 1,
  84. bar: [
  85. {
  86. bad: true,
  87. good: true,
  88. },
  89. { bad: true },
  90. ],
  91. };
  92. const expected = {
  93. foo: 1,
  94. bar: [{ good: true }, {}],
  95. };
  96. expect(omit(input, ['bad'], true)).toEqual(expected);
  97. });
  98. it('works top-level array', () => {
  99. const input = [{ foo: 1 }, { bad: true }, { bar: 2 }];
  100. const expected = [{ foo: 1 }, {}, { bar: 2 }];
  101. expect(omit(input, ['bad'], true)).toEqual(expected);
  102. });
  103. it('preserves File objects', () => {
  104. const file = new File([], 'foo');
  105. const input = [{ foo: 1 }, { bad: true }, { bar: file }];
  106. const expected = [{ foo: 1 }, {}, { bar: file }];
  107. expect(omit(input, ['bad'], true)).toEqual(expected);
  108. });
  109. });
  110. });