UNPKG

2.18 kBJavaScriptView Raw
1import { Patch } from '.';
2import { expect } from '../test';
3describe('Patch', () => {
4 describe('toPatchSet', () => {
5 it('empty', () => {
6 const test = (forward, backward) => {
7 const res = Patch.toPatchSet(forward, backward);
8 expect(res.prev).to.eql([]);
9 expect(res.next).to.eql([]);
10 };
11 test();
12 test([], []);
13 test(undefined, []);
14 test(undefined, []);
15 test(undefined, [undefined]);
16 });
17 it('converts paths to strings', () => {
18 const p1 = { op: 'add', path: ['foo', 'bar'], value: 123 };
19 const p2 = { op: 'remove', path: ['foo', 'bar'], value: 123 };
20 const test = (res) => {
21 expect(res.next[0].op).to.eql('add');
22 expect(res.prev[0].op).to.eql('remove');
23 expect(res.next[0].path).to.eql('foo/bar');
24 expect(res.prev[0].path).to.eql('foo/bar');
25 };
26 test(Patch.toPatchSet([p1], [p2]));
27 test(Patch.toPatchSet(p1, p2));
28 });
29 it('throw: when property name contains "/"', () => {
30 const patch = { op: 'add', path: ['foo', 'bar/baz'], value: 123 };
31 const err = /Property names cannot contain the "\/" character/;
32 expect(() => Patch.toPatchSet(patch)).to.throw(err);
33 expect(() => Patch.toPatchSet([], patch)).to.throw(err);
34 });
35 });
36 describe('isEmpty', () => {
37 const test = (input, expected) => {
38 const res = Patch.isEmpty(input);
39 expect(res).to.eql(expected);
40 };
41 it('is empty', () => {
42 test(undefined, true);
43 test(null, true);
44 test({}, true);
45 test(' ', true);
46 test({ next: [], prev: [] }, true);
47 });
48 it('is not empty', () => {
49 const p1 = { op: 'add', path: ['foo', 'bar'], value: 123 };
50 const p2 = { op: 'remove', path: ['foo', 'bar'], value: 123 };
51 const patches = Patch.toPatchSet([p1, p2], [p2, p1]);
52 test(patches, false);
53 });
54 });
55});