|
| 1 | +import setOrDeleteWithMessages from '../set-or-delete-with-messages' |
| 2 | + |
| 3 | +test('change the value of a property', () => { |
| 4 | + const dst = { a: 1 } |
| 5 | + const src = { a: 2 } |
| 6 | + let messages = [] |
| 7 | + setOrDeleteWithMessages(dst, src, messages) |
| 8 | + expect(messages).toMatchInlineSnapshot(` |
| 9 | + Array [ |
| 10 | + "a: changing 1 to 2", |
| 11 | + ] |
| 12 | + `) |
| 13 | +}) |
| 14 | + |
| 15 | +test('change the value of a property and leave other property alone', () => { |
| 16 | + const dst = { a: 1, b: 2 } |
| 17 | + const src = { a: 2 } |
| 18 | + let messages = [] |
| 19 | + setOrDeleteWithMessages(dst, src, messages) |
| 20 | + expect(dst).toMatchObject(src) |
| 21 | + expect(messages).toMatchInlineSnapshot(` |
| 22 | + Array [ |
| 23 | + "a: changing 1 to 2", |
| 24 | + ] |
| 25 | + `) |
| 26 | +}) |
| 27 | + |
| 28 | +test('adding an obj', () => { |
| 29 | + const dst = { a: 1, b: 2 } |
| 30 | + const src = { c: { d: 4, e: 5 } } |
| 31 | + let messages = [] |
| 32 | + setOrDeleteWithMessages(dst, src, messages) |
| 33 | + expect(dst).toMatchObject(src) |
| 34 | + expect(messages).toMatchInlineSnapshot(` |
| 35 | + Array [ |
| 36 | + "c: changing not-present to { |
| 37 | + \\"d\\": 4, |
| 38 | + \\"e\\": 5 |
| 39 | + }", |
| 40 | + ] |
| 41 | + `) |
| 42 | +}) |
| 43 | + |
| 44 | +test('deleting a property', () => { |
| 45 | + const dst = { a: 1, b: 2 } |
| 46 | + const src = { c: 3, a: undefined } |
| 47 | + let messages = [] |
| 48 | + setOrDeleteWithMessages(dst, src, messages) |
| 49 | + expect(dst).toMatchObject({ b: 2, c: 3 }) |
| 50 | + expect(messages).toMatchInlineSnapshot(` |
| 51 | + Array [ |
| 52 | + "c: changing not-present to 3", |
| 53 | + "deleting a: 1", |
| 54 | + ] |
| 55 | + `) |
| 56 | +}) |
| 57 | + |
| 58 | +test('make an array smaller', () => { |
| 59 | + const dst = [1, 2, 3] |
| 60 | + const src = [1, 2] |
| 61 | + let messages = [] |
| 62 | + setOrDeleteWithMessages(dst, src, messages) |
| 63 | + expect(dst).toMatchObject(src) |
| 64 | + expect(messages).toMatchInlineSnapshot(` |
| 65 | + Array [ |
| 66 | + "deleting [2]: 3", |
| 67 | + ] |
| 68 | + `) |
| 69 | +}) |
| 70 | + |
| 71 | +test('make an array within an object smaller', () => { |
| 72 | + const dst = { a: [1, 2, 3], b: 1, c: { a: 1 } } |
| 73 | + const src = { a: [1, 2] } |
| 74 | + let messages = [] |
| 75 | + setOrDeleteWithMessages(dst, src, messages) |
| 76 | + expect(dst).toMatchObject({ a: [1, 2], b: 1, c: { a: 1 } }) |
| 77 | + expect(messages).toMatchInlineSnapshot(` |
| 78 | + Array [ |
| 79 | + "deleting a[2]: 3", |
| 80 | + ] |
| 81 | + `) |
| 82 | +}) |
| 83 | + |
| 84 | +test('can not set an object to a string', () => { |
| 85 | + const dst = { a: 1 } |
| 86 | + const src = "can't set" |
| 87 | + let messages = [] |
| 88 | + setOrDeleteWithMessages(dst, src, messages) |
| 89 | + expect(dst).toMatchObject(dst) |
| 90 | + expect(messages).toMatchInlineSnapshot(` |
| 91 | + Array [ |
| 92 | + "Can not change type object to string", |
| 93 | + ] |
| 94 | + `) |
| 95 | +}) |
| 96 | + |
| 97 | +test('can set a deeper object to a string', () => { |
| 98 | + const dst = { a: 1, b: { c: 2 } } |
| 99 | + const src = { b: 'string' } |
| 100 | + let messages = [] |
| 101 | + setOrDeleteWithMessages(dst, src, messages) |
| 102 | + expect(dst).toMatchObject({ a: 1, b: 'string' }) |
| 103 | + expect(messages).toMatchInlineSnapshot(` |
| 104 | + Array [ |
| 105 | + "b: changing { |
| 106 | + \\"c\\": 2 |
| 107 | + } to \\"string\\"", |
| 108 | + ] |
| 109 | + `) |
| 110 | +}) |
| 111 | + |
| 112 | +function be() {} |
| 113 | +function func() {} |
| 114 | +function b1() { |
| 115 | + return 1 |
| 116 | +} |
| 117 | +function newFunc() { |
| 118 | + return 1 |
| 119 | +} |
| 120 | + |
| 121 | +test('can work with functions', () => { |
| 122 | + const dst = { a: 1, b: be, c: { func: func } } |
| 123 | + const src = { a: 'string', b: b1, c: { newFunc: newFunc } } |
| 124 | + let messages = [] |
| 125 | + setOrDeleteWithMessages(dst, src, messages) |
| 126 | + expect(dst).toMatchObject({ a: 'string', b: b1, c: { func: func, newFunc: newFunc } }) |
| 127 | + expect(messages).toMatchInlineSnapshot(` |
| 128 | + Array [ |
| 129 | + "a: changing 1 to \\"string\\"", |
| 130 | + "b: changing be() to b1()", |
| 131 | + "c.newFunc: changing not-present to newFunc()", |
| 132 | + ] |
| 133 | + `) |
| 134 | +}) |
| 135 | + |
| 136 | +test('can work without messages', () => { |
| 137 | + const dst = { a: 1, b: be, c: { func: func } } |
| 138 | + const src = { a: 'string', b: b1, c: { newFunc: newFunc } } |
| 139 | + setOrDeleteWithMessages(dst, src) |
| 140 | + expect(dst).toMatchObject({ a: 'string', b: b1, c: { func: func, newFunc: newFunc } }) |
| 141 | +}) |
| 142 | + |
| 143 | +test('can sort of change an obj to an array', () => { |
| 144 | + const dst = { 0: 'a', 1: 'b', 2: 'c' } |
| 145 | + const src = ['d', 'e', 'f'] |
| 146 | + let messages = [] |
| 147 | + setOrDeleteWithMessages(dst, src, messages) |
| 148 | + expect(dst).toMatchObject({ 0: 'd', 1: 'e', 2: 'f' }) |
| 149 | + expect(messages).toMatchInlineSnapshot(` |
| 150 | + Array [ |
| 151 | + "0: changing \\"a\\" to \\"d\\"", |
| 152 | + "1: changing \\"b\\" to \\"e\\"", |
| 153 | + "2: changing \\"c\\" to \\"f\\"", |
| 154 | + ] |
| 155 | + `) |
| 156 | +}) |
| 157 | + |
| 158 | +test('can change false to true and numbers to strings and vice versa', () => { |
| 159 | + const dst = { a: true, b: false, c: 3, d: 'four' } |
| 160 | + const src = { a: false, b: true, c: 'three', d: 4 } |
| 161 | + let messages = [] |
| 162 | + setOrDeleteWithMessages(dst, src, messages) |
| 163 | + expect(dst).toMatchObject({ a: false, b: true, c: 'three', d: 4 }) |
| 164 | + expect(messages).toMatchInlineSnapshot(` |
| 165 | + Array [ |
| 166 | + "a: changing true to false", |
| 167 | + "b: changing false to true", |
| 168 | + "c: changing 3 to \\"three\\"", |
| 169 | + "d: changing \\"four\\" to 4", |
| 170 | + ] |
| 171 | + `) |
| 172 | +}) |
| 173 | + |
| 174 | +test('make changes to sparse arrays', () => { |
| 175 | + const dst = { a: ['a', 'b', 'c'], b: 6 } |
| 176 | + dst.a[7] = 'z' |
| 177 | + const src = { a: ['a', 'b', 'c'] } |
| 178 | + src.a[7] = 'zz' |
| 179 | + src.a[6] = 'six' |
| 180 | + const result = { a: ['a', 'b', 'c'], b: 6 } |
| 181 | + result.a[6] = 'six' |
| 182 | + result.a[7] = 'zz' |
| 183 | + let messages = [] |
| 184 | + setOrDeleteWithMessages(dst, src, messages) |
| 185 | + expect(dst).toMatchObject(result) |
| 186 | + expect(messages).toMatchInlineSnapshot(` |
| 187 | + Array [ |
| 188 | + "a[6]: changing not-present to \\"six\\"", |
| 189 | + "a[7]: changing \\"z\\" to \\"zz\\"", |
| 190 | + ] |
| 191 | + `) |
| 192 | +}) |
| 193 | + |
| 194 | +test('add a deep deep prop', () => { |
| 195 | + const dst = { a: 1 } |
| 196 | + const src = { b: { c: { d: { d: 2 } } } } |
| 197 | + let messages = [] |
| 198 | + setOrDeleteWithMessages(dst, src, messages) |
| 199 | + expect(dst).toMatchObject({ a: 1, b: { c: { d: { d: 2 } } } }) |
| 200 | + expect(messages).toMatchInlineSnapshot(` |
| 201 | + Array [ |
| 202 | + "b: changing not-present to { |
| 203 | + \\"c\\": { |
| 204 | + \\"d\\": { |
| 205 | + \\"d\\": 2 |
| 206 | + } |
| 207 | + } |
| 208 | + }", |
| 209 | + ] |
| 210 | + `) |
| 211 | +}) |
0 commit comments