|
| 1 | +import { getChangedFields } from '../src'; |
| 2 | +import cloneDeep from 'lodash/cloneDeep'; |
| 3 | + |
| 4 | +const complexObject = { |
| 5 | + a: 1, |
| 6 | + b: '2', |
| 7 | + c: { c1: {}, c2: [] }, |
| 8 | + d: ['item1', 'item2', 'item2'], |
| 9 | + e: function () {}, |
| 10 | +}; |
| 11 | +const complexObjectKeys = ['a', 'b', 'c', 'd', 'e']; |
| 12 | + |
| 13 | +describe('getChangedFields()', () => { |
| 14 | + it('Empty parameter', () => { |
| 15 | + expect(getChangedFields(undefined, undefined)).toEqual([]); |
| 16 | + expect(getChangedFields(complexObject, undefined)).toEqual(complexObjectKeys); |
| 17 | + expect(getChangedFields(undefined, complexObject)).toEqual(complexObjectKeys); |
| 18 | + }); |
| 19 | + it('Both not plainObject parameter', () => { |
| 20 | + expect(getChangedFields(1, 2)).toEqual([]); |
| 21 | + expect(getChangedFields(2, '1')).toEqual([]); |
| 22 | + expect( |
| 23 | + getChangedFields( |
| 24 | + function a() {}, |
| 25 | + function b() {} |
| 26 | + ) |
| 27 | + ).toEqual([]); |
| 28 | + expect(getChangedFields(new Date(), new Date())).toEqual([]); |
| 29 | + }); |
| 30 | + it('One is not plainObject parameter', () => { |
| 31 | + expect(getChangedFields(1, complexObject)).toEqual(complexObjectKeys); |
| 32 | + expect(getChangedFields('1', complexObject)).toEqual(complexObjectKeys); |
| 33 | + expect(getChangedFields(function noop() {}, complexObject)).toEqual(complexObjectKeys); |
| 34 | + expect(getChangedFields(new Date(), complexObject)).toEqual(complexObjectKeys); |
| 35 | + |
| 36 | + expect(getChangedFields(complexObject, 1)).toEqual(complexObjectKeys); |
| 37 | + expect(getChangedFields(complexObject, '1')).toEqual(complexObjectKeys); |
| 38 | + expect(getChangedFields(complexObject, function noop() {})).toEqual(complexObjectKeys); |
| 39 | + expect(getChangedFields(complexObject, new Date())).toEqual(complexObjectKeys); |
| 40 | + }); |
| 41 | + it('Deep equal', () => { |
| 42 | + expect(getChangedFields(complexObject, complexObject)).toEqual([]); |
| 43 | + expect(getChangedFields(complexObject, cloneDeep(complexObject))).toEqual([]); |
| 44 | + }); |
| 45 | + it('Change one field', () => { |
| 46 | + expect(getChangedFields(complexObject, { ...cloneDeep(complexObject), a: 2 })).toEqual(['a']); |
| 47 | + expect(getChangedFields({ ...cloneDeep(complexObject), a: 2 }, complexObject)).toEqual(['a']); |
| 48 | + }); |
| 49 | + it('Change some fields', () => { |
| 50 | + expect( |
| 51 | + getChangedFields(complexObject, { |
| 52 | + a: 2, |
| 53 | + b: '3', |
| 54 | + c: { c1: {}, c2: [], c3: [] }, |
| 55 | + d: ['item1', 'item2'], |
| 56 | + e: function () {}, |
| 57 | + }) |
| 58 | + ).toEqual(['a', 'b', 'c', 'd']); |
| 59 | + expect( |
| 60 | + getChangedFields( |
| 61 | + { |
| 62 | + a: 2, |
| 63 | + b: '3', |
| 64 | + c: { c1: {}, c2: [], c3: [] }, |
| 65 | + d: ['item1', 'item2'], |
| 66 | + e: function () {}, |
| 67 | + }, |
| 68 | + complexObject |
| 69 | + ) |
| 70 | + ).toEqual(['a', 'b', 'c', 'd']); |
| 71 | + }); |
| 72 | + it('Delete one field', () => { |
| 73 | + expect( |
| 74 | + getChangedFields(complexObject, { |
| 75 | + a: 1, |
| 76 | + b: '2', |
| 77 | + c: { c1: {}, c2: [] }, |
| 78 | + d: ['item1', 'item2', 'item2'], |
| 79 | + }) |
| 80 | + ).toEqual(['e']); |
| 81 | + expect( |
| 82 | + getChangedFields( |
| 83 | + { |
| 84 | + a: 1, |
| 85 | + b: '2', |
| 86 | + c: { c1: {}, c2: [] }, |
| 87 | + d: ['item1', 'item2', 'item2'], |
| 88 | + }, |
| 89 | + complexObject |
| 90 | + ) |
| 91 | + ).toEqual(['e']); |
| 92 | + }); |
| 93 | + it('Delete some fields', () => { |
| 94 | + expect( |
| 95 | + getChangedFields(complexObject, { |
| 96 | + a: 1, |
| 97 | + b: '2', |
| 98 | + c: { c1: {}, c2: [] }, |
| 99 | + }) |
| 100 | + ).toEqual(['d', 'e']); |
| 101 | + expect( |
| 102 | + getChangedFields( |
| 103 | + { |
| 104 | + a: 1, |
| 105 | + b: '2', |
| 106 | + c: { c1: {}, c2: [] }, |
| 107 | + }, |
| 108 | + complexObject |
| 109 | + ) |
| 110 | + ).toEqual(['d', 'e']); |
| 111 | + }); |
| 112 | + it('Add one field', () => { |
| 113 | + expect( |
| 114 | + getChangedFields(complexObject, { |
| 115 | + ...complexObject, |
| 116 | + f: {}, |
| 117 | + }) |
| 118 | + ).toEqual(['f']); |
| 119 | + expect( |
| 120 | + getChangedFields( |
| 121 | + { |
| 122 | + ...complexObject, |
| 123 | + f: {}, |
| 124 | + }, |
| 125 | + complexObject |
| 126 | + ) |
| 127 | + ).toEqual(['f']); |
| 128 | + }); |
| 129 | + it('Add some fields', () => { |
| 130 | + expect( |
| 131 | + getChangedFields(complexObject, { |
| 132 | + ...complexObject, |
| 133 | + f: {}, |
| 134 | + g: [], |
| 135 | + }) |
| 136 | + ).toEqual(['f', 'g']); |
| 137 | + expect( |
| 138 | + getChangedFields( |
| 139 | + { |
| 140 | + ...complexObject, |
| 141 | + f: {}, |
| 142 | + g: [], |
| 143 | + }, |
| 144 | + complexObject |
| 145 | + ) |
| 146 | + ).toEqual(['f', 'g']); |
| 147 | + }); |
| 148 | +}); |
0 commit comments