omit.spec.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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(omit({
  14. name: {
  15. first: 'joe',
  16. last: 'smith',
  17. },
  18. address: {
  19. number: 12,
  20. },
  21. }, ['address'])).toEqual({
  22. name: {
  23. first: 'joe',
  24. last: 'smith',
  25. },
  26. });
  27. });
  28. describe('recursive', () => {
  29. it('returns a new object', () => {
  30. const obj = { foo: 1, bar: 2 };
  31. expect(omit(obj, ['bar'], true)).not.toBe(obj);
  32. });
  33. it('works with 1-level-deep objects', () => {
  34. const input = {
  35. foo: 1,
  36. bar: 2,
  37. baz: 3,
  38. };
  39. const expected = { foo: 1, baz: 3 };
  40. expect(omit(input, ['bar'], true)).toEqual(expected);
  41. });
  42. it('works with 2-level-deep objects', () => {
  43. const input = {
  44. foo: 1,
  45. bar: {
  46. bad: true,
  47. good: true,
  48. },
  49. baz: {
  50. bad: true,
  51. },
  52. };
  53. const expected = {
  54. foo: 1,
  55. bar: {
  56. good: true,
  57. },
  58. baz: {},
  59. };
  60. expect(omit(input, ['bad'], true)).toEqual(expected);
  61. });
  62. it('works with array of objects', () => {
  63. const input = {
  64. foo: 1,
  65. bar: [
  66. {
  67. bad: true,
  68. good: true,
  69. },
  70. { bad: true },
  71. ],
  72. };
  73. const expected = {
  74. foo: 1,
  75. bar: [
  76. { good: true },
  77. {},
  78. ],
  79. };
  80. expect(omit(input, ['bad'], true)).toEqual(expected);
  81. });
  82. it('works top-level array', () => {
  83. const input = [
  84. { foo: 1 },
  85. { bad: true },
  86. { bar: 2 },
  87. ];
  88. const expected = [
  89. { foo: 1 },
  90. {},
  91. { bar: 2 },
  92. ];
  93. expect(omit(input, ['bad'], true)).toEqual(expected);
  94. });
  95. it('preserves File objects', () => {
  96. const file = new File([], 'foo');
  97. const input = [
  98. { foo: 1 },
  99. { bad: true },
  100. { bar: file },
  101. ];
  102. const expected = [
  103. { foo: 1 },
  104. {},
  105. { bar: file },
  106. ];
  107. expect(omit(input, ['bad'], true)).toEqual(expected);
  108. });
  109. });
  110. });