Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions lib/arrayChanges.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const arrayChanges = require('array-changes');

module.exports = function (...args) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we now need to wrap array-changes I guess we could consider making these adjustments directly in the library, or maybe even just copy the whole thing in here. I don't see this as a blocker, just an observation.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should do that, because I think we should consider showing the similar diff with a move. I just don't want to make that change right now.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mhh maybe you are right, maybe we don't want to see moves for similar items. Could we merge this, then I'll add an issue to upstream the change into array-changes itself.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, absolutely!

return arrayChanges(...args).map((change) => {
if (change.type === 'moveSource' && change.equal === false) {
return {
type: 'remove',
value: change.value,
actualIndex: change.actualIndex,
last: change.last,
};
}

if (change.type === 'moveTarget' && change.equal === false) {
return {
type: 'insert',
value: change.expected,
actualIndex: change.actualIndex,
};
}

return change;
});
};
2 changes: 1 addition & 1 deletion lib/assertions.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const utils = require('./utils');
const arrayChanges = require('array-changes');
const arrayChanges = require('./arrayChanges');
const arrayChangesAsync = require('array-changes-async');
const throwIfNonUnexpectedError = require('./throwIfNonUnexpectedError');
const objectIs = utils.objectIs;
Expand Down
2 changes: 1 addition & 1 deletion lib/types.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const utils = require('./utils');
const isRegExp = utils.isRegExp;
const leftPad = utils.leftPad;
const arrayChanges = require('array-changes');
const arrayChanges = require('./arrayChanges');
const ukkonen = require('ukkonen');
const detectIndent = require('detect-indent');
const defaultDepth = require('./defaultDepth');
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
},
"main": "./build/lib/index.js",
"dependencies": {
"array-changes": "3.0.1",
"array-changes": "^3.2.1",
"array-changes-async": "3.0.1",
"detect-indent": "3.0.1",
"diff": "^5.0.0",
Expand Down
20 changes: 20 additions & 0 deletions test/assertions/to-equal.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -595,4 +595,24 @@ describe('to equal assertion', () => {
expect(errors[0], 'to equal', errors[1]);
});
});

it('handles moving of objects with similar structures', () => {
expect(
function () {
expect([42, { name: 'John', age: 34 }], 'to equal', [
{ name: 'Jane', age: 24, children: 2 },
42,
]);
},
'to throw exception',
"expected [ 42, { name: 'John', age: 34 } ]\n" +
"to equal [ { name: 'Jane', age: 24, children: 2 }, 42 ]\n" +
'\n' +
'[\n' +
" // missing { name: 'Jane', age: 24, children: 2 }\n" +
' 42,\n' +
" { name: 'John', age: 34 } // should be removed\n" +
']'
);
});
});
20 changes: 20 additions & 0 deletions test/assertions/to-satisfy.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2904,4 +2904,24 @@ describe('to satisfy assertion', () => {
);
});
});

it('handles moving of objects with similar structures', () => {
expect(
function () {
expect([42, { name: 'John', age: 34 }], 'to satisfy', [
{ name: 'Jane', age: 24, children: 2 },
42,
]);
},
'to throw exception',
"expected [ 42, { name: 'John', age: 34 } ]\n" +
"to satisfy [ { name: 'Jane', age: 24, children: 2 }, 42 ]\n" +
'\n' +
'[\n' +
" // missing { name: 'Jane', age: 24, children: 2 }\n" +
' 42,\n' +
" { name: 'John', age: 34 } // should be removed\n" +
']'
);
});
});
18 changes: 18 additions & 0 deletions test/assertions/when-sorted.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,22 @@ describe('when sorted assertion', () => {
it("should also work without the 'when'", () => {
expect(['c', 'd', 'a'], 'sorted', 'to equal', ['a', 'c', 'd']);
});

describe('when failing', () => {
it('shows an error describing what is wrong', () => {
expect(
() => expect(['c', 'a', 'b'], 'sorted', 'to equal', ['c', 'b', 'a']),
'to throw',
"expected [ 'c', 'a', 'b' ] sorted to equal [ 'c', 'b', 'a' ]\n" +
'\n' +
'[\n' +
'┌───▷\n' +
'│ ┌─▷\n' +
"│ │ 'a',\n" +
"│ └── 'b', // should be moved\n" +
"└──── 'c' // should be moved\n" +
']'
);
});
});
});
Loading