omit.spec.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import { omit } from './omit';
  2. declare const File: any;
  3. describe('omit()', () => {
  4. it('returns a new object', () => {
  5. const obj = { foo: 1, bar: 2 };
  6. expect(omit(obj, ['bar'])).not.toBe(obj);
  7. });
  8. it('works with 1-level-deep objects', () => {
  9. expect(omit({ foo: 1, bar: 2 }, ['bar'])).toEqual({ foo: 1 });
  10. expect(omit({ foo: 1, bar: 2 }, ['bar', 'foo'])).toEqual({});
  11. });
  12. it('works with deeply-nested objects', () => {
  13. expect(
  14. omit(
  15. {
  16. name: {
  17. first: 'joe',
  18. last: 'smith',
  19. },
  20. address: {
  21. number: 12,
  22. },
  23. },
  24. ['address'],
  25. ),
  26. ).toEqual({
  27. name: {
  28. first: 'joe',
  29. last: 'smith',
  30. },
  31. });
  32. });
  33. describe('recursive', () => {
  34. it('returns a new object', () => {
  35. const obj = { foo: 1, bar: 2 };
  36. expect(omit(obj, ['bar'], true)).not.toBe(obj);
  37. });
  38. it('works with 1-level-deep objects', () => {
  39. const input = {
  40. foo: 1,
  41. bar: 2,
  42. baz: 3,
  43. };
  44. const expected = { foo: 1, baz: 3 };
  45. expect(omit(input, ['bar'], true)).toEqual(expected);
  46. });
  47. it('works with 2-level-deep objects', () => {
  48. const input = {
  49. foo: 1,
  50. bar: {
  51. bad: true,
  52. good: true,
  53. },
  54. baz: {
  55. bad: true,
  56. },
  57. };
  58. const expected = {
  59. foo: 1,
  60. bar: {
  61. good: true,
  62. },
  63. baz: {},
  64. };
  65. expect(omit(input, ['bad'], true)).toEqual(expected);
  66. });
  67. it('works with array of objects', () => {
  68. const input = {
  69. foo: 1,
  70. bar: [
  71. {
  72. bad: true,
  73. good: true,
  74. },
  75. { bad: true },
  76. ],
  77. };
  78. const expected = {
  79. foo: 1,
  80. bar: [{ good: true }, {}],
  81. };
  82. expect(omit(input, ['bad'], true)).toEqual(expected);
  83. });
  84. it('works top-level array', () => {
  85. const input = [{ foo: 1 }, { bad: true }, { bar: 2 }];
  86. const expected = [{ foo: 1 }, {}, { bar: 2 }];
  87. expect(omit(input, ['bad'], true)).toEqual(expected);
  88. });
  89. it('preserves File objects', () => {
  90. const file = new File([], 'foo');
  91. const input = [{ foo: 1 }, { bad: true }, { bar: file }];
  92. const expected = [{ foo: 1 }, {}, { bar: file }];
  93. expect(omit(input, ['bad'], true)).toEqual(expected);
  94. });
  95. });
  96. });