|
| 1 | +describe('Compare Arrays', function () { |
| 2 | + it('Should recognize when two arrays have the same contents', function () { |
| 3 | + var subject = ['A', {}, function () {}] |
| 4 | + var compareResult = ko.utils.compareArrays(subject, subject.slice(0)) |
1 | 5 |
|
2 | | -describe('Compare Arrays', function() { |
3 | | - it('Should recognize when two arrays have the same contents', function () { |
4 | | - var subject = ["A", {}, function () { } ]; |
5 | | - var compareResult = ko.utils.compareArrays(subject, subject.slice(0)); |
| 6 | + expect(compareResult.length).to.deep.equal(subject.length) |
| 7 | + for (var i = 0; i < subject.length; i++) { |
| 8 | + expect(compareResult[i].status).to.deep.equal('retained') |
| 9 | + expect(compareResult[i].value).to.deep.equal(subject[i]) |
| 10 | + } |
| 11 | + }) |
6 | 12 |
|
7 | | - expect(compareResult.length).to.deep.equal(subject.length); |
8 | | - for (var i = 0; i < subject.length; i++) { |
9 | | - expect(compareResult[i].status).to.deep.equal("retained"); |
10 | | - expect(compareResult[i].value).to.deep.equal(subject[i]); |
11 | | - } |
12 | | - }); |
| 13 | + it('Should recognize added items', function () { |
| 14 | + var oldArray = ['A', 'B'] |
| 15 | + var newArray = ['A', 'A2', 'A3', 'B', 'B2'] |
| 16 | + var compareResult = ko.utils.compareArrays(oldArray, newArray) |
| 17 | + expect(compareResult).to.deep.equal([ |
| 18 | + { status: 'retained', value: 'A' }, |
| 19 | + { status: 'added', value: 'A2', index: 1 }, |
| 20 | + { status: 'added', value: 'A3', index: 2 }, |
| 21 | + { status: 'retained', value: 'B' }, |
| 22 | + { status: 'added', value: 'B2', index: 4 } |
| 23 | + ]) |
| 24 | + }) |
13 | 25 |
|
14 | | - it('Should recognize added items', function () { |
15 | | - var oldArray = ["A", "B"]; |
16 | | - var newArray = ["A", "A2", "A3", "B", "B2"]; |
17 | | - var compareResult = ko.utils.compareArrays(oldArray, newArray); |
18 | | - expect(compareResult).to.deep.equal([ |
19 | | - { status: "retained", value: "A" }, |
20 | | - { status: "added", value: "A2", index: 1 }, |
21 | | - { status: "added", value: "A3", index: 2 }, |
22 | | - { status: "retained", value: "B" }, |
23 | | - { status: "added", value: "B2", index: 4 } |
24 | | - ]); |
25 | | - }); |
| 26 | + it('Should recognize deleted items', function () { |
| 27 | + var oldArray = ['A', 'B', 'C', 'D', 'E'] |
| 28 | + var newArray = ['B', 'C', 'E'] |
| 29 | + var compareResult = ko.utils.compareArrays(oldArray, newArray) |
| 30 | + expect(compareResult).to.deep.equal([ |
| 31 | + { status: 'deleted', value: 'A', index: 0 }, |
| 32 | + { status: 'retained', value: 'B' }, |
| 33 | + { status: 'retained', value: 'C' }, |
| 34 | + { status: 'deleted', value: 'D', index: 3 }, |
| 35 | + { status: 'retained', value: 'E' } |
| 36 | + ]) |
| 37 | + }) |
26 | 38 |
|
27 | | - it('Should recognize deleted items', function () { |
28 | | - var oldArray = ["A", "B", "C", "D", "E"]; |
29 | | - var newArray = ["B", "C", "E"]; |
30 | | - var compareResult = ko.utils.compareArrays(oldArray, newArray); |
31 | | - expect(compareResult).to.deep.equal([ |
32 | | - { status: "deleted", value: "A", index: 0 }, |
33 | | - { status: "retained", value: "B" }, |
34 | | - { status: "retained", value: "C" }, |
35 | | - { status: "deleted", value: "D", index: 3 }, |
36 | | - { status: "retained", value: "E" } |
37 | | - ]); |
38 | | - }); |
| 39 | + it('Should recognize mixed edits', function () { |
| 40 | + var oldArray = ['A', 'B', 'C', 'D', 'E'] |
| 41 | + var newArray = [123, 'A', 'E', 'C', 'D'] |
| 42 | + var compareResult = ko.utils.compareArrays(oldArray, newArray) |
| 43 | + expect(compareResult).to.deep.equal([ |
| 44 | + { status: 'added', value: 123, index: 0 }, |
| 45 | + { status: 'retained', value: 'A' }, |
| 46 | + { status: 'deleted', value: 'B', index: 1 }, |
| 47 | + { status: 'added', value: 'E', index: 2, moved: 4 }, |
| 48 | + { status: 'retained', value: 'C' }, |
| 49 | + { status: 'retained', value: 'D' }, |
| 50 | + { status: 'deleted', value: 'E', index: 4, moved: 2 } |
| 51 | + ]) |
| 52 | + }) |
39 | 53 |
|
40 | | - it('Should recognize mixed edits', function () { |
41 | | - var oldArray = ["A", "B", "C", "D", "E"]; |
42 | | - var newArray = [123, "A", "E", "C", "D"]; |
43 | | - var compareResult = ko.utils.compareArrays(oldArray, newArray); |
44 | | - expect(compareResult).to.deep.equal([ |
45 | | - { status: "added", value: 123, index: 0 }, |
46 | | - { status: "retained", value: "A" }, |
47 | | - { status: "deleted", value: "B", index: 1 }, |
48 | | - { status: "added", value: "E", index: 2, moved: 4 }, |
49 | | - { status: "retained", value: "C" }, |
50 | | - { status: "retained", value: "D" }, |
51 | | - { status: "deleted", value: "E", index: 4, moved: 2 } |
52 | | - ]); |
53 | | - }); |
| 54 | + it('Should recognize replaced array', function () { |
| 55 | + var oldArray = ['A', 'B', 'C', 'D', 'E'] |
| 56 | + var newArray = ['F', 'G', 'H', 'I', 'J'] |
| 57 | + var compareResult = ko.utils.compareArrays(oldArray, newArray) |
| 58 | + // The ordering of added/deleted items for replaced entries isn't defined, so |
| 59 | + // we'll sort the results first to ensure the results are in a known order for verification. |
| 60 | + compareResult.sort(function (a, b) { |
| 61 | + return a.index - b.index || a.status.localeCompare(b.status) |
| 62 | + }) |
| 63 | + expect(compareResult).to.deep.equal([ |
| 64 | + { status: 'added', value: 'F', index: 0 }, |
| 65 | + { status: 'deleted', value: 'A', index: 0 }, |
| 66 | + { status: 'added', value: 'G', index: 1 }, |
| 67 | + { status: 'deleted', value: 'B', index: 1 }, |
| 68 | + { status: 'added', value: 'H', index: 2 }, |
| 69 | + { status: 'deleted', value: 'C', index: 2 }, |
| 70 | + { status: 'added', value: 'I', index: 3 }, |
| 71 | + { status: 'deleted', value: 'D', index: 3 }, |
| 72 | + { status: 'added', value: 'J', index: 4 }, |
| 73 | + { status: 'deleted', value: 'E', index: 4 } |
| 74 | + ]) |
| 75 | + }) |
54 | 76 |
|
55 | | - it('Should recognize replaced array', function () { |
56 | | - var oldArray = ["A", "B", "C", "D", "E"]; |
57 | | - var newArray = ["F", "G", "H", "I", "J"]; |
58 | | - var compareResult = ko.utils.compareArrays(oldArray, newArray); |
59 | | - // The ordering of added/deleted items for replaced entries isn't defined, so |
60 | | - // we'll sort the results first to ensure the results are in a known order for verification. |
61 | | - compareResult.sort(function(a, b) { return (a.index - b.index) || a.status.localeCompare(b.status); }); |
62 | | - expect(compareResult).to.deep.equal([ |
63 | | - { status : 'added', value : 'F', index : 0 }, |
64 | | - { status : 'deleted', value : 'A', index : 0 }, |
65 | | - { status : 'added', value : 'G', index : 1 }, |
66 | | - { status : 'deleted', value : 'B', index : 1 }, |
67 | | - { status : 'added', value : 'H', index : 2 }, |
68 | | - { status : 'deleted', value : 'C', index : 2 }, |
69 | | - { status : 'added', value : 'I', index : 3 }, |
70 | | - { status : 'deleted', value : 'D', index : 3 }, |
71 | | - { status : 'added', value : 'J', index : 4 }, |
72 | | - { status : 'deleted', value : 'E', index : 4 } |
73 | | - ]); |
74 | | - }); |
| 77 | + it('Should support sparse diffs', function () { |
| 78 | + // A sparse diff is exactly like a regular diff, except it doesn't contain any |
| 79 | + // 'retained' items. This still preserves enough information for most things |
| 80 | + // you'd want to do with the changeset. |
75 | 81 |
|
76 | | - it('Should support sparse diffs', function() { |
77 | | - // A sparse diff is exactly like a regular diff, except it doesn't contain any |
78 | | - // 'retained' items. This still preserves enough information for most things |
79 | | - // you'd want to do with the changeset. |
| 82 | + var oldArray = ['A', 'B', 'C', 'D', 'E'] |
| 83 | + var newArray = [123, 'A', 'E', 'C', 'D'] |
| 84 | + var compareResult = ko.utils.compareArrays(oldArray, newArray, { sparse: true }) |
| 85 | + expect(compareResult).to.deep.equal([ |
| 86 | + { status: 'added', value: 123, index: 0 }, |
| 87 | + { status: 'deleted', value: 'B', index: 1 }, |
| 88 | + { status: 'added', value: 'E', index: 2, moved: 4 }, |
| 89 | + { status: 'deleted', value: 'E', index: 4, moved: 2 } |
| 90 | + ]) |
| 91 | + }) |
80 | 92 |
|
81 | | - var oldArray = ["A", "B", "C", "D", "E"]; |
82 | | - var newArray = [123, "A", "E", "C", "D"]; |
83 | | - var compareResult = ko.utils.compareArrays(oldArray, newArray, { sparse: true }); |
84 | | - expect(compareResult).to.deep.equal([ |
85 | | - { status: "added", value: 123, index: 0 }, |
86 | | - { status: "deleted", value: "B", index: 1 }, |
87 | | - { status: "added", value: "E", index: 2, moved: 4 }, |
88 | | - { status: "deleted", value: "E", index: 4, moved: 2 } |
89 | | - ]); |
90 | | - }); |
| 93 | + it('Should honor "dontLimitMoves" option', function () { |
| 94 | + // In order to test this, we must have a scenario in which a move is not recognized as such without the option. |
| 95 | + // This scenario doesn't represent the definition of the spec itself and may need to be modified if the move |
| 96 | + // detection algorithm in Knockout is changed. |
| 97 | + var oldArray = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T'] |
| 98 | + var newArray = [1, 2, 3, 4, 'T', 6, 7, 8, 9, 10] |
91 | 99 |
|
92 | | - it('Should honor "dontLimitMoves" option', function() { |
93 | | - // In order to test this, we must have a scenario in which a move is not recognized as such without the option. |
94 | | - // This scenario doesn't represent the definition of the spec itself and may need to be modified if the move |
95 | | - // detection algorithm in Knockout is changed. |
96 | | - var oldArray = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T"]; |
97 | | - var newArray = [1, 2, 3, 4, "T", 6, 7, 8, 9, 10]; |
| 100 | + var compareResult = ko.utils.compareArrays(oldArray, newArray) |
| 101 | + expect(compareResult[compareResult.length - 1]).to.deep.equal({ status: 'deleted', value: 'T', index: 19 }) |
98 | 102 |
|
99 | | - var compareResult = ko.utils.compareArrays(oldArray, newArray); |
100 | | - expect(compareResult[compareResult.length-1]).to.deep.equal({ status: 'deleted', value: 'T', index: 19 }); |
101 | | - |
102 | | - compareResult = ko.utils.compareArrays(oldArray, newArray, { dontLimitMoves: true }); |
103 | | - expect(compareResult[compareResult.length-1]).to.deep.equal({ status: 'deleted', value: 'T', index: 19, moved: 4 }); |
104 | | - }); |
105 | | -}); |
| 103 | + compareResult = ko.utils.compareArrays(oldArray, newArray, { dontLimitMoves: true }) |
| 104 | + expect(compareResult[compareResult.length - 1]).to.deep.equal({ |
| 105 | + status: 'deleted', |
| 106 | + value: 'T', |
| 107 | + index: 19, |
| 108 | + moved: 4 |
| 109 | + }) |
| 110 | + }) |
| 111 | +}) |
0 commit comments