diff --git a/package.json b/package.json index d8da556..6258f1b 100644 --- a/package.json +++ b/package.json @@ -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" } diff --git a/src/asserts/utils/assertDispatchedActions.js b/src/asserts/utils/assertDispatchedActions.js index f28f953..91bb8bc 100644 --- a/src/asserts/utils/assertDispatchedActions.js +++ b/src/asserts/utils/assertDispatchedActions.js @@ -1,4 +1,4 @@ -import findIndex from 'lodash.findindex'; +import findIndex from './findDeepMatchIndex'; import { notDispatchedActionError } from '../errors/notDispatchedActionError'; function assertDispatchedActions(dispatched, expected) { diff --git a/src/asserts/utils/assertNotDispatchedActions.js b/src/asserts/utils/assertNotDispatchedActions.js index eb463a0..4b8c206 100644 --- a/src/asserts/utils/assertNotDispatchedActions.js +++ b/src/asserts/utils/assertNotDispatchedActions.js @@ -1,4 +1,4 @@ -import findIndex from 'lodash.findindex'; +import findIndex from './findDeepMatchIndex'; import { dispatchedActionError } from '../errors/dispatchedActionError'; function assertNotDispatchedActions(dispatched, expected) { diff --git a/src/asserts/utils/findDeepMatchIndex.js b/src/asserts/utils/findDeepMatchIndex.js new file mode 100644 index 0000000..e34061a --- /dev/null +++ b/src/asserts/utils/findDeepMatchIndex.js @@ -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; diff --git a/test/asserts/utils/assertDispatchedActions.js b/test/asserts/utils/assertDispatchedActions.js index 108cfb4..92be92f 100644 --- a/test/asserts/utils/assertDispatchedActions.js +++ b/test/asserts/utils/assertDispatchedActions.js @@ -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 = [ diff --git a/test/asserts/utils/findDeepMatchIndex.js b/test/asserts/utils/findDeepMatchIndex.js new file mode 100644 index 0000000..81cabe4 --- /dev/null +++ b/test/asserts/utils/findDeepMatchIndex.js @@ -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); + }); + }); +});