Skip to content

Commit

Permalink
#58 further improve test coverage in frontend action reducing
Browse files Browse the repository at this point in the history
  • Loading branch information
[email protected] authored and [email protected] committed Jun 18, 2020
1 parent 92a1615 commit e8a4f58
Show file tree
Hide file tree
Showing 4 changed files with 213 additions and 3 deletions.
5 changes: 4 additions & 1 deletion client/app/actions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,10 @@ export const toggleUserMenu = () => ({type: TOGGLE_USER_MENU});
export const toggleLog = () => ({type: TOGGLE_LOG});
export const editStory = (storyId) => ({type: EDIT_STORY, storyId});
export const cancelEditStory = (storyId) => ({type: CANCEL_EDIT_STORY, storyId});
export const setLanguage = (language) => ({type: SET_LANGUAGE, language});
export const setLanguage = (language) => {
clientSettingsStore.setPresetLanguage(language);
return {type: SET_LANGUAGE, language};
};
export const hideNewUserHints = () => {
clientSettingsStore.setHideNewUserHints(true);
return {type: HIDE_NEW_USER_HINTS};
Expand Down
2 changes: 0 additions & 2 deletions client/app/services/clientActionReducer.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import clientSettingsStore from '../store/clientSettingsStore';
import {
TOGGLE_BACKLOG,
TOGGLE_USER_MENU,
Expand Down Expand Up @@ -85,7 +84,6 @@ export default function clientActionReducer(state, action) {
}
case SET_LANGUAGE: {
const language = action.language;
clientSettingsStore.setPresetLanguage(language);
state.setLanguage(language);
return {...state, language};
}
Expand Down
207 changes: 207 additions & 0 deletions client/test/unit/clientActionReducerTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
import {v4 as uuid} from 'uuid';

import {
CANCEL_EDIT_STORY,
COMMAND_SENT,
EDIT_STORY,
EVENT_RECEIVED,
HIDE_NEW_USER_HINTS,
LOCATION_CHANGED,
SET_LANGUAGE,
STATUS_FETCHED,
TOGGLE_BACKLOG,
TOGGLE_LOG,
TOGGLE_USER_MENU
} from '../../app/actions/types';
import initialState from '../../app/store/initialState';
import clientActionReducer from '../../app/services/clientActionReducer';
import {
cancelEditStory,
editStory,
hideNewUserHints,
setLanguage,
toggleBacklog,
toggleLog,
toggleUserMenu
} from '../../app/actions';

test(COMMAND_SENT, () => {
const startingState = initialState();

const cmdId1 = uuid();
const cmdId2 = uuid();

let modifiedState = clientActionReducer(startingState, {
type: COMMAND_SENT,
command: {
id: cmdId1,
name: 'superCommand',
payload: {
some: 'more data'
}
}
});

modifiedState = clientActionReducer(modifiedState, {
type: COMMAND_SENT,
command: {
id: cmdId2,
name: 'secondSuperCommand'
}
});

expect(modifiedState.pendingCommands).toEqual({
[cmdId1]: {
id: cmdId1,
name: 'superCommand',
payload: {
some: 'more data'
}
},
[cmdId2]: {
id: cmdId2,
name: 'secondSuperCommand'
}
});
});

test(EVENT_RECEIVED, () => {
const cmdId1 = uuid();
const cmdId2 = uuid();

const startingState = {
...initialState(),
pendingCommands: {
[cmdId1]: {
id: cmdId1,
name: 'superCommand',
payload: {
some: 'more data'
}
},
[cmdId2]: {
id: cmdId2,
name: 'secondSuperCommand'
}
}
};

let modifiedState = clientActionReducer(startingState, {
type: EVENT_RECEIVED,
correlationId: 'does-not-match',
eventName: 'usernameSet'
});
expect(Object.values(modifiedState.pendingCommands).length).toBe(2); // still both commands

modifiedState = clientActionReducer(modifiedState, {
type: EVENT_RECEIVED,
correlationId: cmdId1,
eventName: 'usernameSet'
});
expect(modifiedState.pendingCommands[cmdId1]).toBeUndefined();
expect(modifiedState.pendingCommands[cmdId2]).toBeDefined();

modifiedState = clientActionReducer(modifiedState, {
type: EVENT_RECEIVED,
correlationId: cmdId2,
eventName: 'usernameSet'
});

expect(modifiedState.pendingCommands[cmdId2]).toBeUndefined();
});

test(TOGGLE_BACKLOG, () => {
const startingState = initialState();

let modifiedState = clientActionReducer(startingState, toggleBacklog());
expect(modifiedState.backlogShown).toBe(true);

modifiedState = clientActionReducer(modifiedState, toggleBacklog());
expect(modifiedState.backlogShown).toBe(false);
});

test(TOGGLE_USER_MENU + ' and ' + TOGGLE_LOG, () => {
const startingState = initialState();

let modifiedState = clientActionReducer(startingState, toggleUserMenu());
expect(modifiedState.userMenuShown).toBe(true);
expect(modifiedState.logShown).toBe(false);

modifiedState = clientActionReducer(modifiedState, toggleLog());
expect(modifiedState.userMenuShown).toBe(false);
expect(modifiedState.logShown).toBe(true);

modifiedState = clientActionReducer(modifiedState, toggleUserMenu());
expect(modifiedState.userMenuShown).toBe(true);
expect(modifiedState.logShown).toBe(false);

modifiedState = clientActionReducer(modifiedState, toggleUserMenu());
expect(modifiedState.userMenuShown).toBe(false);
expect(modifiedState.logShown).toBe(false);

modifiedState = clientActionReducer(modifiedState, toggleLog());
modifiedState = clientActionReducer(modifiedState, toggleLog());
expect(modifiedState.userMenuShown).toBe(false);
expect(modifiedState.logShown).toBe(false);
});

test(EDIT_STORY + ' and ' + CANCEL_EDIT_STORY, () => {
const storyId1 = uuid();
const storyId2 = uuid();

const startingState = {
...initialState(),
stories: {
[storyId1]: {},
[storyId2]: {}
}
};

let modifiedState = clientActionReducer(startingState, editStory(storyId1));
expect(modifiedState.stories[storyId1].editMode).toBe(true);

// multiple stories can be in edit mode simultaneously
modifiedState = clientActionReducer(modifiedState, editStory(storyId2));
expect(modifiedState.stories[storyId1].editMode).toBe(true);
expect(modifiedState.stories[storyId2].editMode).toBe(true);

// cancel edit mode for story 1
modifiedState = clientActionReducer(modifiedState, cancelEditStory(storyId1));
expect(modifiedState.stories[storyId1].editMode).toBe(false);
expect(modifiedState.stories[storyId2].editMode).toBe(true);
});

test(SET_LANGUAGE, () => {
const startingState = initialState();

let modifiedState = clientActionReducer(startingState, setLanguage('someLangCode'));
expect(modifiedState.language).toEqual('someLangCode');
});

test(HIDE_NEW_USER_HINTS, () => {
const startingState = initialState();

let modifiedState = clientActionReducer(startingState, hideNewUserHints());
expect(modifiedState.hideNewUserHints).toBe(true);
});

test(STATUS_FETCHED, () => {
const startingState = initialState();

let modifiedState = clientActionReducer(startingState, {
type: STATUS_FETCHED,
status: {some: 'data'}
});
expect(modifiedState.appStatus).toEqual({
some: 'data'
});
});
test(LOCATION_CHANGED, () => {
const startingState = initialState();

let modifiedState = clientActionReducer(startingState, {
type: LOCATION_CHANGED,
pathname: 'somePathName'
});
expect(modifiedState.pathname).toEqual('somePathName');
});
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,15 @@ test('Editing stories', () => {
selectedStory: firstStoryId,
stories: {
[secondStoryId]: {
editMode: true,
createdAt: 1592115972307,
description: 'dscription second... from other john',
estimations: {},
id: secondStoryId,
title: 'Second story'
},
[firstStoryId]: {
editMode: true,
createdAt: 1592115935676,
description: 'description one',
estimations: {},
Expand Down

0 comments on commit e8a4f58

Please sign in to comment.