From c55cd8cd974a8c16376f8e3ee39afb084e37aeb5 Mon Sep 17 00:00:00 2001 From: James Tarling Date: Thu, 12 Oct 2017 12:43:20 +0100 Subject: [PATCH 1/3] Make breaking test --- test/asserts/utils/assertDispatchedActions.js | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/test/asserts/utils/assertDispatchedActions.js b/test/asserts/utils/assertDispatchedActions.js index 108cfb4..370a84e 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: [0, 1, 2] }, + { type: '1', payload: [] } + ]; + const expectedActions = [ + { type: '0', payload: [0, 1, 2] }, + { type: '1', payload: [2, 3, 4] } + ]; + expect(() => { assertDispatchedActions(dispatchedActions, expectedActions); }) + .toThrow(); + }); + + it('should find actions that deep match', () => { + const dispatchedActions = [ + { type: '0', payload: [0, 1, 2] }, + { type: '1', payload: [2, 3, 4] } + ]; + const expectedActions = [ + { type: '0', payload: [0, 1, 2] }, + { type: '1', payload: [2, 3, 4] } + ]; + expect(() => { assertDispatchedActions(dispatchedActions, expectedActions); }) + .toNotThrow(); + }); + describe('when expected duplicate actions were not dispatched', () => { it('should throw an error', () => { const dispatchedActions = [ From a39b8dffc13e1b8a4a28f898d7421495ee862554 Mon Sep 17 00:00:00 2001 From: James Tarling Date: Thu, 12 Oct 2017 12:44:02 +0100 Subject: [PATCH 2/3] Use findIndex with isEqual to deep match actions --- package.json | 1 + src/asserts/utils/assertDispatchedActions.js | 2 +- .../utils/assertNotDispatchedActions.js | 2 +- src/asserts/utils/findDeepMatchIndex.js | 8 +++ test/asserts/utils/findDeepMatchIndex.js | 51 +++++++++++++++++++ 5 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 src/asserts/utils/findDeepMatchIndex.js create mode 100644 test/asserts/utils/findDeepMatchIndex.js 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/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); + }); + }); +}); From 1ee942ed0268ca384d135e4822d2dae797ef111a Mon Sep 17 00:00:00 2001 From: James Tarling Date: Thu, 12 Oct 2017 13:03:21 +0100 Subject: [PATCH 3/3] =?UTF-8?q?Test=20wasn=E2=80=99t=20working?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/asserts/utils/assertDispatchedActions.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/test/asserts/utils/assertDispatchedActions.js b/test/asserts/utils/assertDispatchedActions.js index 370a84e..92be92f 100644 --- a/test/asserts/utils/assertDispatchedActions.js +++ b/test/asserts/utils/assertDispatchedActions.js @@ -43,12 +43,12 @@ describe('assertion utils', () => { it('should not find actions that shallow match', () => { const dispatchedActions = [ - { type: '0', payload: [0, 1, 2] }, - { type: '1', payload: [] } + { type: '0', payload: [{ a: '0' }, { b: '1' }, { c: '2' }] }, + { type: '1', payload: [{ d: '3' }, { e: '4' }, { f: '5' }] } ]; const expectedActions = [ - { type: '0', payload: [0, 1, 2] }, - { type: '1', payload: [2, 3, 4] } + { type: '0', payload: [] }, + { type: '1', payload: [{ d: '3' }, { e: '4' }, { f: '5' }] } ]; expect(() => { assertDispatchedActions(dispatchedActions, expectedActions); }) .toThrow(); @@ -56,12 +56,12 @@ describe('assertion utils', () => { it('should find actions that deep match', () => { const dispatchedActions = [ - { type: '0', payload: [0, 1, 2] }, - { type: '1', payload: [2, 3, 4] } + { type: '0', payload: [{ a: '0' }, { b: '1' }, { c: '2' }] }, + { type: '1', payload: [{ d: '3' }, { e: '4' }, { f: '5' }] } ]; const expectedActions = [ - { type: '0', payload: [0, 1, 2] }, - { type: '1', payload: [2, 3, 4] } + { type: '0', payload: [{ a: '0' }, { b: '1' }, { c: '2' }] }, + { type: '1', payload: [{ d: '3' }, { e: '4' }, { f: '5' }] } ]; expect(() => { assertDispatchedActions(dispatchedActions, expectedActions); }) .toNotThrow();