Skip to content

Commit 5c1ccd4

Browse files
committed
testPatchUnpatch now encode and decode before apply patch
1 parent 14e5285 commit 5c1ccd4

File tree

7 files changed

+79
-47
lines changed

7 files changed

+79
-47
lines changed

src/types/Delete.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ Delete.patch = function ({ patch, target, prop, old_value }) {
1717

1818
Delete.encode = function ({ value }) {
1919
if (value instanceof Delete || value === Delete) {
20-
return { [DELETE_KEY]: 0 } // we don't go deeper
20+
return { [DELETE_KEY]: 0 }
2121
} else if (isValidToDecodeDelete({ value })) {
22-
return { [ESCAPE_KEY]: value } // we don't go deeper
22+
return { [ESCAPE_KEY]: value }
2323
}
2424
return value
2525
}

src/types/Function.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Function.encode = function ({
1818
: registerLocalFunctionFromEncode(value)
1919
return { [FUNCTION_KEY]: function_id }
2020
} else if (isValidToDecode({ value, key: FUNCTION_KEY })) {
21-
return { [ESCAPE_KEY]: value } // we don't go deeper
21+
return { [ESCAPE_KEY]: value }
2222
}
2323
return value
2424
}

src/types/Replace.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ Replace.patch = function ({ patch, target, prop, old_value }) {
1818

1919
Replace.encode = function ({ value }) {
2020
if (value instanceof Replace) {
21-
return { [REPLACE_KEY]: value.value } // we don't go deeper
21+
return { [REPLACE_KEY]: value.value }
2222
} else if (isValidToDecode({ value, key: REPLACE_KEY })) {
23-
return { [ESCAPE_KEY]: value } // we don't go deeper
23+
return { [ESCAPE_KEY]: value }
2424
}
2525
return value
2626
}

src/util/converter.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ import merge, { mergeMutator } from '../util/merge'
33
import { isArray } from '../util/is'
44

55
export default function converter(patch, params, converters) {
6-
const target = isArray(patch) ? [] : {}
7-
forEachObject(patch, target, ({ patch, prop, target, path }) => {
6+
const patch_root = { '': patch } // a trick to allow top level
7+
const target_root = { '': isArray(patch) ? [] : {} } // a trick to allow top level
8+
forEachObject(patch_root, target_root, ({ patch, prop, target, path }) => {
89
const value = converters.reduce(
910
(value, converter) =>
1011
converter(
@@ -28,5 +29,5 @@ export default function converter(patch, params, converters) {
2829
return mergeMutator({ patch, target, prop })
2930
}
3031
})
31-
return target
32+
return target_root['']
3233
}

test/applyPatch.js

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ test('11 / https://tools.ietf.org/html/rfc7386 ', function (t) {
8787
const patch = null
8888
const expected = null
8989

90-
testPatchUnpatch(t, target, patch, expected)
90+
testPatchUnpatch(t, target, patch, expected, true, true, false)
9191
})
9292

9393
test('12 / https://tools.ietf.org/html/rfc7386 ', function (t) {
@@ -155,7 +155,7 @@ test('basic mutations', function (t) {
155155
const patch = { number: 2, bool: true, string: 'world', func: func2 }
156156
const expected = { number: 2, bool: true, string: 'world', func: func2 }
157157

158-
testPatchUnpatch(t, target, patch, expected)
158+
testPatchUnpatch(t, target, patch, expected, true, false)
159159
})
160160

161161
test('value didnt exists', function (t) {
@@ -251,7 +251,7 @@ test('function to target', function (t) {
251251
const patch = { prop: {} }
252252
const expected = { prop: {} }
253253

254-
testPatchUnpatch(t, target, patch, expected)
254+
testPatchUnpatch(t, target, patch, expected, true, false)
255255
})
256256

257257
test('target to function', function (t) {
@@ -260,7 +260,7 @@ test('target to function', function (t) {
260260
const patch = { prop: f }
261261
const expected = { prop: f }
262262

263-
testPatchUnpatch(t, target, patch, expected)
263+
testPatchUnpatch(t, target, patch, expected, true, false)
264264
})
265265

266266
test('plain to noplain', function (t) {
@@ -272,7 +272,14 @@ test('plain to noplain', function (t) {
272272
const patch = { prop: { deep: true } }
273273
const expected = { prop: { deep: true } }
274274

275-
const { mutations } = testPatchUnpatch(t, target, patch, expected)
275+
const { mutations } = testPatchUnpatch(
276+
t,
277+
target,
278+
patch,
279+
expected,
280+
true,
281+
false
282+
)
276283
t.is(mutations.length, 1)
277284
})
278285

@@ -285,7 +292,7 @@ test('noplain to plain', function (t) {
285292
const patch = { prop: instance }
286293
const expected = { prop: instance }
287294

288-
testPatchUnpatch(t, target, patch, expected)
295+
testPatchUnpatch(t, target, patch, expected, true, false)
289296
})
290297

291298
test('syntax mutations', function (t) {
@@ -417,7 +424,7 @@ test('should assign `undefined` values', function (t) {
417424
const patch = { value: undefined, value2: undefined }
418425
const expected = { value: undefined, value2: undefined }
419426

420-
testPatchUnpatch(t, target, patch, expected)
427+
testPatchUnpatch(t, target, patch, expected, true, false)
421428
})
422429

423430
test('should assign non array/buffer/typed-array/plain-target source values directly', function (t) {
@@ -436,7 +443,7 @@ test('should assign non array/buffer/typed-array/plain-target source values dire
436443
const patch = { values: values }
437444
const expected = { values: values }
438445

439-
testPatchUnpatch(t, target, patch, expected)
446+
testPatchUnpatch(t, target, patch, expected, true, false)
440447
})
441448

442449
test('same patch as target generate no mutations', function (t) {
@@ -454,7 +461,14 @@ test('same array as patch generate no mutations even if we mutate target object'
454461
original.push(4)
455462
const patch = { array: target.array }
456463
const expected = { array: [1, 2, 3, 4] }
457-
const { mutations, unpatch } = testPatchUnpatch(t, target, patch, expected)
464+
const { mutations, unpatch } = testPatchUnpatch(
465+
t,
466+
target,
467+
patch,
468+
expected,
469+
false,
470+
false
471+
)
458472
t.is(mutations.length, 0)
459473
t.deepEqual(unpatch, {})
460474
t.is(target.array, original)
@@ -566,7 +580,14 @@ test('checking different types and mutating multiple deep values', function (t)
566580
}
567581
const expected = { ...patch, string: 'string' }
568582

569-
const { mutations } = testPatchUnpatch(t, target, patch, expected)
583+
const { mutations } = testPatchUnpatch(
584+
t,
585+
target,
586+
patch,
587+
expected,
588+
true,
589+
false
590+
)
570591
t.is(mutations.length, 30)
571592
})
572593

test/type_replace.js

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,111 +2,111 @@ import test from 'ava'
22
import { decode, TYPE } from '../'
33
import { testEncodeDecode, testPatchUnpatch } from './utils'
44

5-
test('Valid type', function(t) {
5+
test('Valid type', function (t) {
66
const patch = { convert: TYPE.Replace({ hello: 'world' }) }
77
const expected = { convert: { $r: { hello: 'world' } } }
88
testEncodeDecode(t, patch, expected)
99
})
1010

11-
test('Escape', function(t) {
11+
test('Escape', function (t) {
1212
const patch = { escape: { $r: 1 } }
1313
const expected = { escape: { $escape: { $r: 1 } } }
1414
testEncodeDecode(t, patch, expected)
1515
})
1616

17-
test('Ignore', function(t) {
17+
test('Ignore', function (t) {
1818
const patch = { ignore: { $r: 1, $other: 1 } }
1919
const expected = { ignore: { $r: 1, $other: 1 } }
2020
testEncodeDecode(t, patch, expected)
2121
})
2222

23-
test('$escaping', function(t) {
23+
test('$escaping', function (t) {
2424
const patch = {
2525
convert: TYPE.Replace([1, 2, 3]),
26-
escape: { $r: 1 }
26+
escape: { $r: 1 },
2727
}
2828
const expected = {
2929
convert: { $r: [1, 2, 3] },
30-
escape: { $escape: { $r: 1 } }
30+
escape: { $escape: { $r: 1 } },
3131
}
3232
testEncodeDecode(t, patch, expected)
3333
})
3434

35-
test('This should not be escaped because $r has another valid prop', function(t) {
35+
test('This should not be escaped because $r has another valid prop', function (t) {
3636
const patch = {
3737
convert: TYPE.Replace([1, 2, 3]),
38-
ignored: { $r: [1, 2, 3], $escape: [1, 2, 3] }
38+
ignored: { $r: [1, 2, 3], $escape: [1, 2, 3] },
3939
}
4040
const expected = {
4141
convert: { $r: [1, 2, 3] },
42-
ignored: { $r: [1, 2, 3], $escape: [1, 2, 3] }
42+
ignored: { $r: [1, 2, 3], $escape: [1, 2, 3] },
4343
}
4444
testEncodeDecode(t, patch, expected)
4545
})
4646

47-
test('This should not be escaped because $r has another valid prop 2', function(t) {
47+
test('This should not be escaped because $r has another valid prop 2', function (t) {
4848
const patch = {
4949
convert: TYPE.Replace([1, 2, 3]),
50-
escape: { $escape: [1, 2, 3], $r: TYPE.Replace([1, 2, 3]) }
50+
escape: { $escape: [1, 2, 3], $r: TYPE.Replace([1, 2, 3]) },
5151
}
5252
const expected = {
5353
convert: { $r: [1, 2, 3] },
54-
escape: { $escape: [1, 2, 3], $r: { $r: [1, 2, 3] } }
54+
escape: { $escape: [1, 2, 3], $r: { $r: [1, 2, 3] } },
5555
}
5656
testEncodeDecode(t, patch, expected)
5757
})
5858

59-
test('Decode alone', function(t) {
59+
test('Decode alone', function (t) {
6060
const patch = {
6161
convert: { $r: [1, 2, 3] },
6262
string: { $r: 'string' },
6363
escape: { $escape: { $r: [1, 2, 3] } },
6464
ignore: {
6565
$escape: { $r: [1, 2, 3] },
66-
two: { $r: [1, 2, 3] }
67-
}
66+
two: { $r: [1, 2, 3] },
67+
},
6868
}
6969
const expected = {
7070
convert: TYPE.Replace([1, 2, 3]),
7171
string: TYPE.Replace('string'),
7272
escape: { $r: [1, 2, 3] },
7373
ignore: {
7474
$escape: TYPE.Replace([1, 2, 3]),
75-
two: TYPE.Replace([1, 2, 3])
76-
}
75+
two: TYPE.Replace([1, 2, 3]),
76+
},
7777
}
7878
t.deepEqual(decode(patch), expected)
7979
})
8080

81-
test('patch array', function(t) {
81+
test('patch array', function (t) {
8282
const target = { value: { a: 1, b: 2 } }
8383
const patch = { value: TYPE.Replace([1, 2, 3]) }
8484
const expected = { value: [1, 2, 3] }
8585

8686
testPatchUnpatch(t, target, patch, expected)
8787
})
8888

89-
test('patch object', function(t) {
89+
test('patch object', function (t) {
9090
const target = { value: { a: 1, b: 2 } }
9191
const patch = { value: TYPE.Replace({ c: 3 }) }
9292
const expected = { value: { c: 3 } }
9393

9494
testPatchUnpatch(t, target, patch, expected)
9595
})
9696

97-
test('patch should replace the complete object', function(t) {
97+
test('patch should replace the complete object', function (t) {
9898
const obj_to_replace = { c: 3 }
9999
const target = { value: { a: 1, b: 2 } }
100100
const patch = { value: TYPE.Replace(obj_to_replace) }
101101
const expected = { value: { c: 3 } }
102102
const copyvalue = target.value
103103

104-
testPatchUnpatch(t, target, patch, expected, false)
104+
testPatchUnpatch(t, target, patch, expected, false, false)
105105
t.is(obj_to_replace, target.value)
106106
t.not(copyvalue, target.value)
107107
})
108108

109-
test('testing that last test works correctly without replace', function(t) {
109+
test('testing that last test works correctly without replace', function (t) {
110110
const target = { value: { a: 1, b: 2 } }
111111
const patch = { value: { c: 3 } }
112112
const expected = { value: { a: 1, b: 2, c: 3 } }
@@ -118,15 +118,15 @@ test('testing that last test works correctly without replace', function(t) {
118118
t.deepEqual(copyvalue, target.value)
119119
})
120120

121-
test('replace array', function(t) {
121+
test('replace array', function (t) {
122122
const target = { value: { a: 1, b: 2 } }
123123
const patch = TYPE.Replace([1, 2])
124124
const expected = [1, 2]
125125

126126
testPatchUnpatch(t, target, patch, expected)
127127
})
128128

129-
test('same behavior as replace array', function(t) {
129+
test('same behavior as replace array', function (t) {
130130
const target = { value: { a: 1, b: 2 } }
131131
const patch = [1, 2]
132132
const expected = [1, 2]

test/utils.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ function isInteger(number) {
1717
)
1818
}
1919

20+
function encodeDecode(patch, encodedecode = true, serialize = true) {
21+
return encodedecode
22+
? decode(serializeDeserialize(encode(patch), serialize))
23+
: patch
24+
}
25+
2026
function serializeDeserialize(patch, serialize = true) {
2127
return serialize ? JSON.parse(JSON.stringify(patch)) : patch
2228
}
@@ -45,21 +51,25 @@ function testPatchUnpatch(
4551
patch,
4652
expected,
4753
reverse = true,
54+
encodedecode = true,
4855
serialize = true
4956
) {
5057
const cloned = getNewPlain(target)
51-
52-
// patch = decode(serializeDeserialize(encode(patch), serialize))
53-
54-
const output = applyPatch(target, patch)
58+
const output = applyPatch(
59+
target,
60+
encodeDecode(patch, encodedecode, serialize)
61+
)
5562
const { unpatch, mutations, result } = output
5663
if (isPlainObject(result) && isPlainObject(target)) {
5764
t.is(target, result)
5865
}
5966
target = result
6067
t.deepEqual(target, expected)
6168
if (reverse) {
62-
const output2 = applyPatch(target, unpatch)
69+
const output2 = applyPatch(
70+
target,
71+
encodeDecode(unpatch, encodedecode, serialize)
72+
)
6373
t.deepEqual(output2.result, cloned, '(Unpatching)')
6474
}
6575
return { unpatch, mutations, result }

0 commit comments

Comments
 (0)