Skip to content

Commit 5bf0333

Browse files
committed
Upgrade array-changes to fix structural moves
1 parent 5986f9d commit 5bf0333

File tree

7 files changed

+86
-3
lines changed

7 files changed

+86
-3
lines changed

lib/arrayChanges.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const arrayChanges = require('array-changes');
2+
3+
module.exports = function (...args) {
4+
console.log(args);
5+
return arrayChanges(...args).map((change) => {
6+
if (change.type === 'moveSource' && change.equal === false) {
7+
return {
8+
type: 'remove',
9+
value: change.value,
10+
actualIndex: change.actualIndex,
11+
last: change.last,
12+
};
13+
}
14+
15+
if (change.type === 'moveTarget' && change.equal === false) {
16+
return {
17+
type: 'insert',
18+
value: change.expected,
19+
actualIndex: change.actualIndex,
20+
};
21+
}
22+
23+
return change;
24+
});
25+
};

lib/assertions.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const utils = require('./utils');
2-
const arrayChanges = require('array-changes');
2+
const arrayChanges = require('./arrayChanges');
33
const arrayChangesAsync = require('array-changes-async');
44
const throwIfNonUnexpectedError = require('./throwIfNonUnexpectedError');
55
const objectIs = utils.objectIs;

lib/types.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const utils = require('./utils');
22
const isRegExp = utils.isRegExp;
33
const leftPad = utils.leftPad;
4-
const arrayChanges = require('array-changes');
4+
const arrayChanges = require('./arrayChanges');
55
const ukkonen = require('ukkonen');
66
const detectIndent = require('detect-indent');
77
const defaultDepth = require('./defaultDepth');

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
},
2424
"main": "./build/lib/index.js",
2525
"dependencies": {
26-
"array-changes": "3.0.1",
26+
"array-changes": "^3.2.1",
2727
"array-changes-async": "3.0.1",
2828
"detect-indent": "3.0.1",
2929
"diff": "^5.0.0",

test/assertions/to-equal.spec.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -595,4 +595,24 @@ describe('to equal assertion', () => {
595595
expect(errors[0], 'to equal', errors[1]);
596596
});
597597
});
598+
599+
it('handles moving of objects with similar structures', () => {
600+
expect(
601+
function () {
602+
expect([42, { name: 'John', age: 34 }], 'to equal', [
603+
{ name: 'Jane', age: 24, children: 2 },
604+
42,
605+
]);
606+
},
607+
'to throw exception',
608+
"expected [ 42, { name: 'John', age: 34 } ]\n" +
609+
"to equal [ { name: 'Jane', age: 24, children: 2 }, 42 ]\n" +
610+
'\n' +
611+
'[\n' +
612+
" // missing { name: 'Jane', age: 24, children: 2 }\n" +
613+
' 42,\n' +
614+
" { name: 'John', age: 34 } // should be removed\n" +
615+
']'
616+
);
617+
});
598618
});

test/assertions/to-satisfy.spec.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2904,4 +2904,24 @@ describe('to satisfy assertion', () => {
29042904
);
29052905
});
29062906
});
2907+
2908+
it('handles moving of objects with similar structures', () => {
2909+
expect(
2910+
function () {
2911+
expect([42, { name: 'John', age: 34 }], 'to satisfy', [
2912+
{ name: 'Jane', age: 24, children: 2 },
2913+
42,
2914+
]);
2915+
},
2916+
'to throw exception',
2917+
"expected [ 42, { name: 'John', age: 34 } ]\n" +
2918+
"to satisfy [ { name: 'Jane', age: 24, children: 2 }, 42 ]\n" +
2919+
'\n' +
2920+
'[\n' +
2921+
" // missing { name: 'Jane', age: 24, children: 2 }\n" +
2922+
' 42,\n' +
2923+
" { name: 'John', age: 34 } // should be removed\n" +
2924+
']'
2925+
);
2926+
});
29072927
});

test/assertions/when-sorted.spec.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,22 @@ describe('when sorted assertion', () => {
2222
it("should also work without the 'when'", () => {
2323
expect(['c', 'd', 'a'], 'sorted', 'to equal', ['a', 'c', 'd']);
2424
});
25+
26+
describe('when failing', () => {
27+
it('shows an error describing what is wrong', () => {
28+
expect(
29+
() => expect(['c', 'a', 'b'], 'sorted', 'to equal', ['c', 'b', 'a']),
30+
'to throw',
31+
"expected [ 'c', 'a', 'b' ] sorted to equal [ 'c', 'b', 'a' ]\n" +
32+
'\n' +
33+
'[\n' +
34+
'┌───▷\n' +
35+
'│ ┌─▷\n' +
36+
"│ │ 'a',\n" +
37+
"│ └── 'b', // should be moved\n" +
38+
"└──── 'c' // should be moved\n" +
39+
']'
40+
);
41+
});
42+
});
2543
});

0 commit comments

Comments
 (0)