From 8950dce2cd33b0ed41bf0ee305a94fce502c4c63 Mon Sep 17 00:00:00 2001 From: Lutz Helm Date: Thu, 5 Mar 2020 11:51:22 +0100 Subject: [PATCH] Add tests for companionWindow action 'toggleNode' --- __tests__/src/actions/companionWindow.test.js | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/__tests__/src/actions/companionWindow.test.js b/__tests__/src/actions/companionWindow.test.js index 23633844fa..916fa42030 100644 --- a/__tests__/src/actions/companionWindow.test.js +++ b/__tests__/src/actions/companionWindow.test.js @@ -1,6 +1,11 @@ import * as actions from '../../../src/state/actions'; import ActionTypes from '../../../src/state/actions/action-types'; +jest.mock('../../../src/state/selectors', () => ({ + getManuallyExpandedNodeIds: (state, args, expanded) => (expanded ? ['openVisible', 'open'] : ['closedVisible', 'closed']), + getVisibleNodeIds: (state, args) => ['openVisible', 'closedVisible', 'visible'], +})); + describe('companionWindow actions', () => { describe('addCompanionWindow', () => { it('should return correct action object', () => { @@ -96,4 +101,68 @@ describe('companionWindow actions', () => { expect(action.windowId).toBe('window'); }); }); + + describe('toggleNode', () => { + let mockDispatch; + let mockGetState; + + beforeEach(() => { + mockDispatch = jest.fn(() => ({})); + mockGetState = jest.fn(() => ({})); + }); + + it('returns a collapsing action for visible nodes that are not present in the state', () => { + const thunk = actions.toggleNode('window1', 'cw1', 'visible'); + thunk(mockDispatch, mockGetState); + + const action = mockDispatch.mock.calls[0][0]; + expect(action.id).toBe('cw1'); + expect(action.windowId).toBe('window1'); + expect(action.type).toBe(ActionTypes.TOGGLE_TOC_NODE); + expect(action.payload).toMatchObject({ visible: { expanded: false } }); + }); + + it('returns an expanding action for non visible nodes that are not present in the state', () => { + const thunk = actions.toggleNode('window1', 'cw1', 'foo'); + thunk(mockDispatch, mockGetState); + + const action = mockDispatch.mock.calls[0][0]; + expect(action.id).toBe('cw1'); + expect(action.windowId).toBe('window1'); + expect(action.type).toBe(ActionTypes.TOGGLE_TOC_NODE); + expect(action.payload).toMatchObject({ foo: { expanded: true } }); + }); + + it('returns a correct action any node that is present in the state', () => { + const openVisibleThunk = actions.toggleNode('window1', 'cw1', 'openVisible'); + openVisibleThunk(mockDispatch, mockGetState); + + const openThunk = actions.toggleNode('window1', 'cw1', 'open'); + openThunk(mockDispatch, mockGetState); + + const closedVisibleThunk = actions.toggleNode('window1', 'cw1', 'closedVisible'); + closedVisibleThunk(mockDispatch, mockGetState); + + const closedThunk = actions.toggleNode('window1', 'cw1', 'closed'); + closedThunk(mockDispatch, mockGetState); + + const actionForOpenVisible = mockDispatch.mock.calls[0][0]; + expect(actionForOpenVisible.id).toBe('cw1'); + expect(actionForOpenVisible.windowId).toBe('window1'); + expect(actionForOpenVisible.type).toBe(ActionTypes.TOGGLE_TOC_NODE); + expect(actionForOpenVisible.payload).toMatchObject({ openVisible: { expanded: false } }); + + const actionForOpen = mockDispatch.mock.calls[1][0]; + expect(actionForOpen.type).toBe(ActionTypes.TOGGLE_TOC_NODE); + expect(actionForOpen.payload).toMatchObject({ open: { expanded: false } }); + + const actionForClosedVisible = mockDispatch.mock.calls[2][0]; + expect(actionForClosedVisible.type).toBe(ActionTypes.TOGGLE_TOC_NODE); + expect(actionForClosedVisible.payload).toMatchObject({ closedVisible: { expanded: true } }); + + const actionForClosed = mockDispatch.mock.calls[3][0]; + expect(actionForClosed.type).toBe(ActionTypes.TOGGLE_TOC_NODE); + expect(actionForClosed.payload).toMatchObject({ closed: { expanded: true } }); + }); + }); });