Skip to content

Commit 5c44df0

Browse files
authored
Merge pull request #8 from unexpectedjs/ssimonsen/set-value-on-move-target
Set the correct value on move targets
2 parents 23b5664 + bfba504 commit 5c44df0

File tree

2 files changed

+67
-5
lines changed

2 files changed

+67
-5
lines changed

lib/arrayChanges.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,11 @@ module.exports = function arrayChanges(actual, expected, equal, similar, options
8383
var added = removed.map(function (v) {
8484
return extend({}, v, { last: false, type: 'moveTarget' });
8585
});
86-
removed.forEach(function (v) {
86+
removed.forEach(function (v, index) {
8787
v.type = 'moveSource';
88+
v.expectedIndex = offsetIndex(diffItem.to + index);
89+
v.expected = expected[v.expectedIndex];
90+
v.equal = equal(v.value, v.expected);
8891
});
8992
var insertIndex = offsetIndex(diffItem.to);
9093
Array.prototype.splice.apply(mutatedArray, [insertIndex, 0].concat(added));
@@ -97,7 +100,7 @@ module.exports = function arrayChanges(actual, expected, equal, similar, options
97100

98101
inserts.forEach(function (diffItem) {
99102
var added = new Array(diffItem.values.length);
100-
for (var i = 0 ; i < diffItem.values.length ; i += 1) {
103+
for (var i = 0; i < diffItem.values.length; i += 1) {
101104
added[i] = {
102105
type: 'insert',
103106
value: diffItem.values[i],

test/arrayChanges.spec.js

Lines changed: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ describe('array-changes', function () {
113113
{ type: 'equal', value: 1, actualIndex: 0, expected: 1, expectedIndex: 1 },
114114
{ type: 'equal', value: 2, actualIndex: 1, expected: 2, expectedIndex: 2 },
115115
{ type: 'equal', value: 3, actualIndex: 2, expected: 3, expectedIndex: 3 },
116-
{ type: 'moveSource', value: 0, actualIndex: 3, last: true }
116+
{ type: 'moveSource', value: 0, actualIndex: 3, expected: 0, expectedIndex: 0, equal: true, last: true }
117117
]);
118118
});
119119

@@ -124,7 +124,7 @@ describe('array-changes', function () {
124124
{ type: 'equal', value: 0, actualIndex: 0, expected: 0, expectedIndex: 0 },
125125
{ type: 'moveTarget', value: 2, actualIndex: 2, last: false },
126126
{ type: 'equal', value: 1, actualIndex: 1, expected: 1, expectedIndex: 2 },
127-
{ type: 'moveSource', value: 2, actualIndex: 2 },
127+
{ type: 'moveSource', value: 2, actualIndex: 2, expected: 2, expectedIndex: 1, equal: true },
128128
{ type: 'equal', value: 3, actualIndex: 3, expected: 3, expectedIndex: 3, last: true }
129129
]);
130130
});
@@ -362,7 +362,7 @@ describe('array-changes', function () {
362362
return a === b;
363363
}, function (a, b) {
364364
return a === b;
365-
}, { includeNonNumericalProperties: [ 'foo' ] }), 'to equal', [
365+
}, { includeNonNumericalProperties: ['foo'] }), 'to equal', [
366366
{ type: 'equal', value: 1, expected: 1, actualIndex: 0, expectedIndex: 0 },
367367
{ type: 'remove', actualIndex: 'foo', value: 456, last: true }
368368
]);
@@ -405,4 +405,63 @@ describe('array-changes', function () {
405405
);
406406
}, 'to be valid for all', arrays, arrays);
407407
});
408+
409+
it("handles moves with similar items", function () {
410+
var a = [
411+
{
412+
kind: 0,
413+
type: 'tag',
414+
name: 'p',
415+
children: [{ data: 'A', type: 'text' }],
416+
attribs: {}
417+
},
418+
{
419+
kind: 1,
420+
type: 'tag',
421+
name: 'p',
422+
children: [{ data: 'Hello world 2023', type: 'text' }],
423+
attribs: {}
424+
}
425+
];
426+
var b = [
427+
{
428+
kind: 1,
429+
type: 'tag',
430+
name: 'p',
431+
children: [{ data: 'Hello world 2025', type: 'text' }],
432+
attribs: {}
433+
},
434+
{
435+
kind: 0,
436+
type: 'tag',
437+
name: 'p',
438+
children: [{ data: 'A', type: 'text' }],
439+
attribs: {}
440+
}
441+
];
442+
443+
expect(arrayChanges(a, b, function (a, b) {
444+
return expect.equal(a, b);
445+
}, function (a, b) {
446+
return a.kind === b.kind;
447+
}), 'to equal', [
448+
{ type: 'moveTarget', value: { kind: 1, type: 'tag', name: 'p', children: [{ data: 'Hello world 2023', type: 'text' }], attribs: {} }, actualIndex: 1, last: false },
449+
{
450+
type: 'equal',
451+
value: { kind: 0, type: 'tag', name: 'p', children: [{ data: 'A', type: 'text' }], attribs: {} },
452+
actualIndex: 0,
453+
expected: { kind: 0, type: 'tag', name: 'p', children: [{ data: 'A', type: 'text' }], attribs: {} },
454+
expectedIndex: 1
455+
},
456+
{
457+
type: 'moveSource',
458+
value: { kind: 1, type: 'tag', name: 'p', children: [{ data: 'Hello world 2023', type: 'text' }], attribs: {} },
459+
expected: { kind: 1, type: 'tag', name: 'p', children: [{ data: 'Hello world 2025', type: 'text' }], attribs: {} },
460+
actualIndex: 1,
461+
expectedIndex: 0,
462+
equal: false,
463+
last: true
464+
}
465+
]);
466+
});
408467
});

0 commit comments

Comments
 (0)