Skip to content

Redux Tests #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Jun 25, 2017
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions config/localStorageMock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class LocalStorageMock {
constructor() {
this.store = {};
}

clear() {
this.store = {};
}

getItem(key) {
return this.store[key];
}

setItem(key, value) {
this.store[key] = value.toString();
}

removeItem(key) {
delete this.store[key];
}
};

global.localStorage = new LocalStorageMock;
13 changes: 9 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
"react-dom": "^15.5.4",
"react-redux": "^5.0.4",
"redux": "^3.6.0",
"redux-thunk": "^2.2.0",
"redux-api-middleware": "^1.0.3",
"rimraf": "~2.6.1",
"semver": "~5.3.0",
"serve-static": "~1.12.2",
Expand Down Expand Up @@ -132,6 +132,7 @@
"promise": "7.1.1",
"react-dev-utils": "^0.5.2",
"sass-loader": "^6.0.5",
"sinon": "^2.3.4",
"style-loader": "0.13.1",
"supertest": "~3.0.0",
"url-loader": "0.5.7",
Expand All @@ -145,10 +146,11 @@
"src/**/*.{js,jsx}"
],
"setupFiles": [
"<rootDir>/config/polyfills.js"
"<rootDir>/config/polyfills.js",
"<rootDir>/config/localStorageMock.js"
],
"testPathIgnorePatterns": [
"<rootDir>[/\\\\](build|docs|node_modules|scripts)[/\\\\]"
"<rootDir>[/\\\\](build|docs|node_modules|scripts|clicktests)[/\\\\]"
],
"testEnvironment": "node",
"testURL": "http://localhost",
Expand All @@ -162,7 +164,10 @@
],
"moduleNameMapper": {
"^react-native$": "react-native-web"
}
},
"modulePaths": [
"src-react"
]
},
"babel": {
"presets": [
Expand Down
231 changes: 231 additions & 0 deletions src-react/__tests__/reducers/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,231 @@
import sinon from 'sinon';
import * as types from 'constants/action-types';

import app from 'reducers/app';

describe('app.js reducers', () => {
let initialState;

beforeEach(() => {
initialState = {
gitVersionErrorVisible: false,
showNewVersionAvailable: false,
showBugtrackingNagscreen: false,
showNPSSurvey: false
};
});

describe('when action doesn\'t match any case' , () => {

it('should return original state except the showNPSSurvey state', () => {
const state = app(initialState, { type: types.NO_OP });
expect(state.gitVersionErrorVisible).toEqual(initialState.gitVersionErrorVisible);
expect(state.showNewVersionAvailable).toEqual(initialState.showNewVersionAvailable);
expect(state.showBugtrackingNagscreen).toEqual(initialState.showBugtrackingNagscreen);
});

describe('but last NPS dismissed >= 6 month and Math.random < 0.01', () => {
beforeEach(() => {
localStorage.setItem('NPSSurveyLastDismissed', 1466227885);
sinon.stub(Math, 'random').returns(0.006);
});

it('showNPSSurvey state will be true', () => {
const state = app(initialState, { type: types.NO_OP });
expect(state.showNPSSurvey).toEqual(true);
});

afterEach(() => {
Math.random.restore();
});
});

describe('but last NPS dismissed < 6 month', () => {
beforeEach(() => {
localStorage.setItem('NPSSurveyLastDismissed', Date.now());
sinon.stub(Math, 'random').returns(0.1);
});

it('showNPSSurvey state will be false', () => {
const state = app(initialState, { type: types.NO_OP });
expect(state.showNPSSurvey).toEqual(false);
});

afterEach(() => {
Math.random.restore();
});
});

describe('but Math.random >= 0.01', () => {
beforeEach(() => {
localStorage.setItem('NPSSurveyLastDismissed', 1466227885);
sinon.stub(Math, 'random').returns(0.1);
});

it('showNPSSurvey state will be false', () => {
const state = app(initialState, { type: types.NO_OP });
expect(state.showNPSSurvey).toEqual(false);
});

afterEach(() => {
Math.random.restore();
});
});
});

describe('when FETCH_UNGIT_CONFIG_SUCCESS action dispatch', () => {
describe('if ungitConfig.config.bugtracking is false and bugtrackingNagscreenDismissed is false', () => {
it('showBugtrackingNagscreen state will be true', () => {
const ungitConfig = { config: { bugtracking: false } };
const state = app(initialState, { type: types.FETCH_UNGIT_CONFIG_SUCCESS, payload: ungitConfig }, { ungitConfig });
expect(state.showBugtrackingNagscreen).toEqual(true);
});
});

describe('if ungitConfig.config.bugtracking is true', () => {
it('showBugtrackingNagscreen state will be false', () => {
const ungitConfig = { config: { bugtracking: true } };
const state = app(initialState, { type: types.FETCH_UNGIT_CONFIG_SUCCESS, payload: ungitConfig }, { ungitConfig });
expect(state.showBugtrackingNagscreen).toEqual(false);
});
});

describe('if bugtrackingNagscreenDismissed is true', () => {
it('showBugtrackingNagscreen state will be false', () => {
localStorage.setItem('bugtrackingNagscreenDismissed', true);
const ungitConfig = { config: { bugtracking: false } };
const state = app(initialState, { type: types.FETCH_UNGIT_CONFIG_SUCCESS, payload: ungitConfig }, { ungitConfig });
expect(state.showBugtrackingNagscreen).toEqual(false);
});
});
});

describe('when FETCH_GIT_VERSION_SUCCESS action dispatch', () => {
describe('if ungitConfig.config.gitVersionCheckOverride is false and gitVersionError is defined && gitVersionErrorDismissed is false', () => {
it('gitVersionErrorVisible state will be true', () => {
localStorage.setItem('gitVersionErrorDismissed', false);
const ungitConfig = { config: { gitVersionCheckOverride: false } };
const gitVersion = {
satisfied: false,
error: 'Failed to parse git version number.'
};
const config = { ungitConfig, versions: { gitVersion } };

const state = app(initialState, { type: types.FETCH_GIT_VERSION_SUCCESS, payload: gitVersion }, config);
expect(state.gitVersionErrorVisible).toEqual(true);
});
});

describe('if ungitConfig.config.gitVersionCheckOverride is true', () => {
it('gitVersionErrorVisible state will be false', () => {
localStorage.setItem('gitVersionErrorDismissed', false);
const ungitConfig = { config: { gitVersionCheckOverride: true } };
const gitVersion = {
satisfied: false,
error: 'Failed to parse git version number.'
};
const config = { ungitConfig, versions: { gitVersion } };

const state = app(initialState, { type: types.FETCH_GIT_VERSION_SUCCESS, payload: gitVersion }, config);
expect(state.gitVersionErrorVisible).toEqual(false);
});
});

describe('if gitVersionError is not defined', () => {
it('gitVersionErrorVisible state will be false', () => {
localStorage.setItem('gitVersionErrorDismissed', false);
const ungitConfig = { config: { gitVersionCheckOverride: false } };
const gitVersion = { satisfied: true };
const config = { ungitConfig, versions: { gitVersion } };

const state = app(initialState, { type: types.FETCH_GIT_VERSION_SUCCESS, payload: gitVersion }, config);
expect(state.gitVersionErrorVisible).toEqual(false);
});
});

describe('if gitVersionErrorDismissed is true', () => {
it('gitVersionErrorVisible state will be false', () => {
localStorage.setItem('gitVersionErrorDismissed', true);
const ungitConfig = { config: { gitVersionCheckOverride: false } };
const gitVersion = {
satisfied: false,
error: 'Failed to parse git version number.'
};
const config = { ungitConfig, versions: { gitVersion } };

const state = app(initialState, { type: types.FETCH_GIT_VERSION_SUCCESS, payload: gitVersion }, config);
expect(state.gitVersionErrorVisible).toEqual(false);
});
});
});

describe('when FETCH_LATEST_VERSION_SUCCESS action dispatch', () => {
describe('gitVersionCheckOverride is false and latestVersion.outdated is true', () => {
it('showNewVersionAvailable state will be true', () => {
const ungitConfig = { config: { gitVersionCheckOverride: false } };
const latestVersion = { outdated: true };
const config = { ungitConfig, versions: { latestVersion } };

const state = app(initialState, { type: types.FETCH_LATEST_VERSION_SUCCESS, payload: latestVersion }, config);
expect(state.showNewVersionAvailable).toEqual(true);
});
});

describe('gitVersionCheckOverride is true', () => {
it('showNewVersionAvailable state will be false', () => {
const ungitConfig = { config: { gitVersionCheckOverride: true } };
const latestVersion = { outdated: true };
const config = { ungitConfig, versions: { latestVersion } };

const state = app(initialState, { type: types.FETCH_LATEST_VERSION_SUCCESS, payload: latestVersion }, config);
expect(state.showNewVersionAvailable).toEqual(false);
});
});

describe('latestVersion.outdated is true', () => {
it('showNewVersionAvailable state will be false', () => {
const ungitConfig = { config: { gitVersionCheckOverride: false } };
const latestVersion = { outdated: false };
const config = { ungitConfig, versions: { latestVersion } };

const state = app(initialState, { type: types.FETCH_LATEST_VERSION_SUCCESS, payload: latestVersion }, config);
expect(state.showNewVersionAvailable).toEqual(false);
});
});
});

describe('when FETCH_USER_CONFIG_SUCCESS action dispatch', () => {
describe('userConfig.bugtracking = false and bugtrackingNagscreenDismissed = false', () => {
it('showBugtrackingNagscreen state will be true', () => {
const userConfig = { bugtracking: false };
const ungitConfig = { config: { bugtracking: true } };
localStorage.setItem('bugtrackingNagscreenDismissed', false);

const state = app(initialState, { type: types.FETCH_USER_CONFIG_SUCCESS, payload: userConfig }, { userConfig, ungitConfig });
expect(state.showBugtrackingNagscreen).toEqual(true);
});
});

describe('userConfig.bugtracking = true', () => {
it('showBugtrackingNagscreen state will be false', () => {
const userConfig = { bugtracking: true };
const ungitConfig = { config: { bugtracking: true } };
localStorage.setItem('bugtrackingNagscreenDismissed', false);

const state = app(initialState, { type: types.FETCH_USER_CONFIG_SUCCESS, payload: userConfig }, { userConfig, ungitConfig });
expect(state.showBugtrackingNagscreen).toEqual(false);
});
});

describe('bugtrackingNagscreenDismissed = true', () => {
it('showBugtrackingNagscreen state will be false', () => {
const userConfig = { bugtracking: false };
const ungitConfig = { config: { bugtracking: true } };
localStorage.setItem('bugtrackingNagscreenDismissed', true);

const state = app(initialState, { type: types.FETCH_USER_CONFIG_SUCCESS, payload: userConfig }, { userConfig, ungitConfig });
expect(state.showBugtrackingNagscreen).toEqual(false);
});
});
});
});
93 changes: 93 additions & 0 deletions src-react/__tests__/reducers/path.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import * as types from 'constants/action-types';

import path from 'reducers/path';

describe('path.js reducers', () => {
let initialState;

const REQUEST_ACTIONS = [
types.FETCH_USER_CONFIG_REQUEST,
types.FETCH_LATEST_VERSION_REQUEST,
types.FETCH_GIT_VERSION_REQUEST,
types.FETCH_UNGIT_CONFIG_REQUEST
];

const SUCCESS_ACTIONS = [
types.FETCH_USER_CONFIG_SUCCESS,
types.FETCH_LATEST_VERSION_SUCCESS,
types.FETCH_GIT_VERSION_SUCCESS,
types.FETCH_UNGIT_CONFIG_SUCCESS
];

const FAILURE_ACTIONS = [
types.FETCH_USER_CONFIG_FAILURE,
types.FETCH_LATEST_VERSION_FAILURE,
types.FETCH_GIT_VERSION_FAILURE,
types.FETCH_UNGIT_CONFIG_FAILURE
];

beforeEach(() => {
initialState = {
pending: null,
errMessage: []
};
});

it('should return original state if action doesn\'t match any case' , function() {
const state = path(initialState, { type: types.NO_OP });
expect(state.pending).toEqual(initialState.pending);
expect(state.errMessage.length).toEqual(initialState.errMessage.length);
});

REQUEST_ACTIONS.forEach(actionName => {
describe(`when ${actionName} action dispatch`, () => {
beforeEach(() => {
initialState = {
pending: 1,
errMessage: []
};
});
it('pending state should be plus one', () => {
const action = { type: actionName };

const state = path(initialState, action);
expect(state.pending).toEqual(initialState.pending + 1);
});
});
});

SUCCESS_ACTIONS.forEach(actionName => {
describe(`when ${actionName} action dispatch`, () => {
beforeEach(() => {
initialState = {
pending: 1,
errMessage: []
};
});
it('pending state should be minused one', () => {
const action = { type: actionName };

const state = path(initialState, action);
expect(state.pending).toEqual(initialState.pending - 1);
});
});
});

FAILURE_ACTIONS.forEach(actionName => {
describe(`when ${actionName} action dispatch`, () => {
beforeEach(() => {
initialState = {
pending: 1,
errMessage: []
};
});
it('pending state should be minused one', () => {
const action = { type: actionName, payload: { message: 'error' } };

const state = path(initialState, action);
expect(state.pending).toEqual(initialState.pending - 1);
expect(state.errMessage[0]).toEqual(action.payload.message);
});
});
});
});
Loading