|
| 1 | +import test from 'ava' |
| 2 | +import { applyPatch } from '../' |
| 3 | +import { TYPE } from '../' |
| 4 | +import { testEncodeDecode, testPatchUnpatch } from './utils' |
| 5 | + |
| 6 | +test('Valid type', function (t) { |
| 7 | + const patch = { convert: TYPE.Swap(0, 1) } |
| 8 | + const expected = { convert: { $w: [0, 1] } } |
| 9 | + testEncodeDecode(t, patch, expected) |
| 10 | +}) |
| 11 | + |
| 12 | +test('Escape', function (t) { |
| 13 | + const patch = { escape: { $w: [0, 1] } } |
| 14 | + const expected = { escape: { $escape: { $w: [0, 1] } } } |
| 15 | + testEncodeDecode(t, patch, expected) |
| 16 | +}) |
| 17 | + |
| 18 | +test('Ignore', function (t) { |
| 19 | + const patch = { ignore: { $w: { a: 2 } } } |
| 20 | + const expected = { ignore: { $w: { a: 2 } } } |
| 21 | + testEncodeDecode(t, patch, expected) |
| 22 | +}) |
| 23 | + |
| 24 | +test('API', function (t) { |
| 25 | + const target = { array: ['a', 'b'] } |
| 26 | + const patch = { array: TYPE.Swap(0, 1) } |
| 27 | + const expected = { array: ['b', 'a'] } |
| 28 | + const { unpatch, result, mutations } = testPatchUnpatch( |
| 29 | + t, |
| 30 | + target, |
| 31 | + patch, |
| 32 | + expected |
| 33 | + ) |
| 34 | + t.is(mutations.length, 1) |
| 35 | + t.is(result.array, target.array) |
| 36 | + t.true(unpatch.array instanceof TYPE.Swap) |
| 37 | + t.deepEqual(unpatch.array.args, [1, 0]) |
| 38 | +}) |
| 39 | + |
| 40 | +test('Patching a non array must do nothing', function (t) { |
| 41 | + const target = { array: 1234 } |
| 42 | + const patch = { array: TYPE.Swap(0, 1) } |
| 43 | + const expected = { array: 1234 } |
| 44 | + const { mutations } = testPatchUnpatch(t, target, patch, expected) |
| 45 | + t.is(mutations.length, 0) |
| 46 | +}) |
| 47 | + |
| 48 | +test('Multiple mutations', function (t) { |
| 49 | + const target = { array: ['a', 'b', 'c', 'd'] } |
| 50 | + const patch = { array: TYPE.Swap(0, 2, 1, 3) } |
| 51 | + const expected = { array: ['c', 'd', 'a', 'b'] } |
| 52 | + testPatchUnpatch(t, target, patch, expected) |
| 53 | +}) |
| 54 | + |
| 55 | +test('Combining swaps', function (t) { |
| 56 | + const target = { array: ['a', 'b', 'c'] } |
| 57 | + const patch = { array: TYPE.Swap(0, 1, 2, 1) } |
| 58 | + const expected = { array: ['b', 'c', 'a'] } |
| 59 | + testPatchUnpatch(t, target, patch, expected) |
| 60 | +}) |
| 61 | + |
| 62 | +test('Repeating swaps', function (t) { |
| 63 | + const target = { array: ['a', 'b'] } |
| 64 | + const patch = { array: TYPE.Swap(0, 1, 1, 0) } |
| 65 | + const expected = { array: ['a', 'b'] } |
| 66 | + testPatchUnpatch(t, target, patch, expected) |
| 67 | +}) |
| 68 | + |
| 69 | +test('Swaping do not mutate inner objects, must be the same instances', function (t) { |
| 70 | + const target = { array: [{ a: 1 }, { b: 2 }] } |
| 71 | + const a = target.array[0] |
| 72 | + const b = target.array[1] |
| 73 | + const patch = { array: TYPE.Swap(0, 1) } |
| 74 | + const expected = { array: [{ b: 2 }, { a: 1 }] } |
| 75 | + testPatchUnpatch(t, target, patch, expected, false) |
| 76 | + t.is(a, target.array[1]) |
| 77 | + t.is(b, target.array[0]) |
| 78 | +}) |
0 commit comments