|
| 1 | +import test from 'ava' |
| 2 | +import { applyPatch, encode, decode, TYPE } from '../' |
| 3 | +import { getNewPlain } from '../src/util/get' |
| 4 | +import { isPlainObject } from '../src/util/is' |
| 5 | + |
| 6 | +function testBasic(t, patch, expected, recursive = true) { |
| 7 | + const encoded = encode(patch) |
| 8 | + const decoded = decode(encoded) |
| 9 | + t.deepEqual(expected, encoded) |
| 10 | + t.deepEqual(patch, decoded) |
| 11 | + t.not(patch, encoded) |
| 12 | + t.not(encoded, decoded) |
| 13 | +} |
| 14 | + |
| 15 | +function testUnpatch(t, target, patch, expected, reverse = true) { |
| 16 | + const cloned = getNewPlain(target) |
| 17 | + const output = applyPatch(target, patch) |
| 18 | + const { unpatch, mutations, result } = output |
| 19 | + console.log({ result, cloned }) |
| 20 | + if (isPlainObject(result)) { |
| 21 | + t.is(target, result) |
| 22 | + } |
| 23 | + target = result |
| 24 | + t.deepEqual(target, expected) |
| 25 | + if (reverse) { |
| 26 | + const output2 = applyPatch(target, unpatch) |
| 27 | + t.deepEqual(output2.result, cloned) |
| 28 | + } |
| 29 | + return { unpatch, mutations } |
| 30 | +} |
| 31 | + |
| 32 | +test('patch', function(t) { |
| 33 | + const target = { array: ['a', 'b', 'c'] } |
| 34 | + const patch = { array: TYPE.Splice(0, 1, 'd') } |
| 35 | + const expected = { array: ['d', 'b', 'c'] } |
| 36 | + testUnpatch(t, target, patch, expected) |
| 37 | +}) |
0 commit comments