Skip to content

Deep match actions #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"lodash.clonedeep": "^4.5.0",
"lodash.findindex": "^4.4.0",
"lodash.flattendeep": "^4.2.0",
"lodash.isequal": "^4.5.0",
"redux": "^3.5.2",
"redux-mock-store": "^1.0.2"
}
Expand Down
2 changes: 1 addition & 1 deletion src/asserts/utils/assertDispatchedActions.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import findIndex from 'lodash.findindex';
import findIndex from './findDeepMatchIndex';
import { notDispatchedActionError } from '../errors/notDispatchedActionError';

function assertDispatchedActions(dispatched, expected) {
Expand Down
2 changes: 1 addition & 1 deletion src/asserts/utils/assertNotDispatchedActions.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import findIndex from 'lodash.findindex';
import findIndex from './findDeepMatchIndex';
import { dispatchedActionError } from '../errors/dispatchedActionError';

function assertNotDispatchedActions(dispatched, expected) {
Expand Down
8 changes: 8 additions & 0 deletions src/asserts/utils/findDeepMatchIndex.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import findIndex from 'lodash.findindex';
import isEqual from 'lodash.isequal';

function findDeepMatchIndex(list, value, fromIndex = 0) {
return findIndex(list, item => { return isEqual(item, value); }, fromIndex);
}

export default findDeepMatchIndex;
26 changes: 26 additions & 0 deletions test/asserts/utils/assertDispatchedActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,32 @@ describe('assertion utils', () => {
.toNotThrow();
});

it('should not find actions that shallow match', () => {
const dispatchedActions = [
{ type: '0', payload: [{ a: '0' }, { b: '1' }, { c: '2' }] },
{ type: '1', payload: [{ d: '3' }, { e: '4' }, { f: '5' }] }
];
const expectedActions = [
{ type: '0', payload: [] },
{ type: '1', payload: [{ d: '3' }, { e: '4' }, { f: '5' }] }
];
expect(() => { assertDispatchedActions(dispatchedActions, expectedActions); })
.toThrow();
});

it('should find actions that deep match', () => {
const dispatchedActions = [
{ type: '0', payload: [{ a: '0' }, { b: '1' }, { c: '2' }] },
{ type: '1', payload: [{ d: '3' }, { e: '4' }, { f: '5' }] }
];
const expectedActions = [
{ type: '0', payload: [{ a: '0' }, { b: '1' }, { c: '2' }] },
{ type: '1', payload: [{ d: '3' }, { e: '4' }, { f: '5' }] }
];
expect(() => { assertDispatchedActions(dispatchedActions, expectedActions); })
.toNotThrow();
});

describe('when expected duplicate actions were not dispatched', () => {
it('should throw an error', () => {
const dispatchedActions = [
Expand Down
51 changes: 51 additions & 0 deletions test/asserts/utils/findDeepMatchIndex.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import expect from 'expect';
import findDeepMatchIndex from '../../../src/asserts/utils/findDeepMatchIndex';

describe('assertion utils', () => {
describe('getDispatchedActions', () => {
it('should be function', () => { expect(findDeepMatchIndex).toBeA('function'); });

it('should find a object whose properties match', () => {
const list = [
{
type: 'a',
payload: [0, 1, 2]
},
{
type: 'b',
payload: [0, 1, 2]
},
{
type: 'c',
payload: [0, 1, 2]
}
];
const item = {
type: 'b',
payload: [0, 1, 2]
};
expect(findDeepMatchIndex(list, item)).toBe(1);
});
it('should not find a object whose properties match in type but not in value', () => {
const list = [
{
type: 'a',
payload: [0, 1, 2]
},
{
type: 'b',
payload: [0, 1, 2]
},
{
type: 'c',
payload: [0, 1, 2]
}
];
const item = {
type: 'b',
payload: []
};
expect(findDeepMatchIndex(list, item)).toBe(-1);
});
});
});