UNPKG

3.45 kBJavaScriptView Raw
1/*jshint indent:2, laxcomma:true, laxbreak:true*/
2var util = require('util')
3, expect = require('expect.js')
4, eql = require('deep-equal')
5, deep = require('..')
6;
7
8var lhs = {
9 "id": "Release",
10 "phases": [{
11 "id": "Phase1",
12 "tasks": [
13 {"id": "Task1"},
14 {"id": "Task2"}
15 ]
16 }, {
17 "id": "Phase2",
18 "tasks": [
19 {"id": "Task3"}
20 ]
21 }]
22};
23var rhs = {
24 "id": "Release",
25 "phases": [{
26 // E: Phase1 -> Phase2
27 "id": "Phase2",
28 "tasks": [
29 {"id": "Task3"}
30 ]
31 }, {
32 "id": "Phase1",
33 "tasks": [
34 {"id": "Task1"},
35 {"id": "Task2"}
36 ]
37 }]
38 };
39
40 var diff = deep.diff(lhs, rhs);
41
42 // there should be differences
43 expect(diff).to.be.ok();
44 expect(diff.length).to.be(6);
45
46
47 // It.phases[0].id changed from 'Phase1' to 'Phase2'
48 //
49 expect(diff[0].kind).to.be('E');
50 expect(diff[0].path).to.be.an('array');
51 expect(diff[0].path).to.have.length(3);
52 expect(diff[0].path[0]).to.be('phases');
53 expect(diff[0].path[1]).to.be(0);
54 expect(diff[0].path[2]).to.be('id');
55 expect(diff[0].lhs).to.be('Phase1');
56 expect(diff[0].rhs).to.be('Phase2');
57
58 // It.phases[0].tasks[0].id changed from 'Task1' to 'Task3'
59 //
60 expect(diff[1].kind).to.be('E');
61 expect(diff[1].path).to.be.an('array');
62 expect(diff[1].path).to.have.length(5);
63 expect(diff[1].path[0]).to.be('phases');
64 expect(diff[1].path[1]).to.be(0);
65 expect(diff[1].path[2]).to.be('tasks');
66 expect(diff[1].path[3]).to.be(0);
67 expect(diff[1].path[4]).to.be('id');
68 expect(diff[1].lhs).to.be('Task1');
69 expect(diff[1].rhs).to.be('Task3');
70
71 // It.phases[0].tasks[1] was deleted
72 //
73 expect(diff[2].kind).to.be('A');
74 expect(diff[2].path).to.be.an('array');
75 expect(diff[2].path).to.have.length(3);
76 expect(diff[2].path[0]).to.be('phases');
77 expect(diff[2].path[1]).to.be(0);
78 expect(diff[2].path[2]).to.be('tasks');
79 expect(diff[2].index).to.be(1);
80 expect(diff[2].item.kind).to.be('D');
81
82 // It.phases[1].id changed from 'Phase2' to 'Phase1'
83 //
84 expect(diff[3].kind).to.be('E');
85 expect(diff[3].path).to.be.an('array');
86 expect(diff[3].path).to.have.length(3);
87 expect(diff[3].path[0]).to.be('phases');
88 expect(diff[3].path[1]).to.be(1);
89 expect(diff[3].path[2]).to.be('id');
90 expect(diff[3].lhs).to.be('Phase2');
91 expect(diff[3].rhs).to.be('Phase1');
92
93 // It.phases[1].tasks[0].id changed from 'Task3' to 'Task1'
94 //
95 expect(diff[4].kind).to.be('E');
96 expect(diff[4].path).to.be.an('array');
97 expect(diff[4].path).to.have.length(5);
98 expect(diff[4].path[0]).to.be('phases');
99 expect(diff[4].path[1]).to.be(1);
100 expect(diff[4].path[2]).to.be('tasks');
101 expect(diff[4].path[3]).to.be(0);
102 expect(diff[4].path[4]).to.be('id');
103 expect(diff[4].lhs).to.be('Task3');
104 expect(diff[4].rhs).to.be('Task1');
105
106 // It.phases[1].tasks[1] is new
107 //
108 expect(diff[5].kind).to.be('A');
109 expect(diff[5].path).to.be.an('array');
110 expect(diff[5].path).to.have.length(3);
111 expect(diff[5].path[0]).to.be('phases');
112 expect(diff[5].path[1]).to.be(1);
113 expect(diff[5].path[2]).to.be('tasks');
114 expect(diff[5].index).to.be(1);
115 expect(diff[5].item.kind).to.be('N');
116
117 var applied = deep.applyDiff(lhs, rhs);