Skip to content

Commit

Permalink
Add tests for companionWindow action 'toggleNode'
Browse files Browse the repository at this point in the history
  • Loading branch information
lutzhelm committed Mar 5, 2020
1 parent 1b5c396 commit 8950dce
Showing 1 changed file with 69 additions and 0 deletions.
69 changes: 69 additions & 0 deletions __tests__/src/actions/companionWindow.test.js
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down Expand Up @@ -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 } });
});
});
});

0 comments on commit 8950dce

Please sign in to comment.