omit.spec.ts 3.5 KB

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