added tests for headerReducer and HeaderActions

This commit is contained in:
Art Lowel
2017-01-11 09:40:23 +01:00
parent 9994947ece
commit 4b62d964a9
3 changed files with 100 additions and 0 deletions

View File

@@ -103,6 +103,7 @@
"@types/body-parser": "0.0.33",
"@types/compression": "0.0.33",
"@types/cookie-parser": "1.3.30",
"@types/deep-freeze": "0.0.29",
"@types/express": "4.0.34",
"@types/express-serve-static-core": "4.0.39",
"@types/hammerjs": "2.0.33",
@@ -123,6 +124,7 @@
"cookie-parser": "1.4.3",
"copy-webpack-plugin": "4.0.1",
"css-loader": "^0.26.0",
"deep-freeze": "0.0.1",
"html-webpack-plugin": "^2.21.0",
"imports-loader": "0.7.0",
"istanbul-instrumenter-loader": "^0.2.0",

View File

@@ -0,0 +1,22 @@
import { HeaderActions } from "./header.actions";
describe("HeaderActions", () => {
describe("collapse", () => {
it("should return a COLLAPSE action", () => {
expect(HeaderActions.collapse().type).toEqual(HeaderActions.COLLAPSE);
});
});
describe("expand", () => {
it("should return an EXPAND action", () => {
expect(HeaderActions.expand().type).toEqual(HeaderActions.EXPAND);
});
});
describe("toggle", () => {
it("should return a TOGGLE action", () => {
expect(HeaderActions.toggle().type).toEqual(HeaderActions.TOGGLE);
});
})
});

View File

@@ -0,0 +1,76 @@
import * as deepFreeze from "deep-freeze";
import { headerReducer } from "./header.reducer";
import { HeaderActions } from "./header.actions";
describe("headerReducer", () => {
it("should return the current state when no valid actions have been made", () => {
const state = { navCollapsed: false };
const newState = headerReducer(state, {type: 'undefined-action'});
expect(newState).toEqual(state);
});
it("should start with navCollapsed = true", () => {
const initialState = headerReducer(undefined, {type: 'undefined-action'});
// The navigation starts collapsed
expect(initialState.navCollapsed).toEqual(true);
});
it("should set navCollapsed to true in response to the COLLAPSE action", () => {
const state = { navCollapsed: false };
const action = HeaderActions.collapse();
const newState = headerReducer(state, action);
expect(newState.navCollapsed).toEqual(true);
});
it("should perform the COLLAPSE action without mutating the previous state", () => {
const state = { navCollapsed: false };
deepFreeze(state);
const action = HeaderActions.collapse();
headerReducer(state, action);
//no expect required, deepFreeze will ensure an exception is thrown if the state
//is mutated, and any uncaught exception will cause the test to fail
});
it("should set navCollapsed to false in response to the EXPAND action", () => {
const state = { navCollapsed: true };
const action = HeaderActions.expand();
const newState = headerReducer(state, action);
expect(newState.navCollapsed).toEqual(false);
});
it("should perform the EXPAND action without mutating the previous state", () => {
const state = { navCollapsed: true };
deepFreeze(state);
const action = HeaderActions.expand();
headerReducer(state, action);
});
it("should flip the value of navCollapsed in response to the TOGGLE action", () => {
const state1 = { navCollapsed: true };
const action = HeaderActions.toggle();
const state2 = headerReducer(state1, action);
const state3 = headerReducer(state2, action);
expect(state2.navCollapsed).toEqual(false);
expect(state3.navCollapsed).toEqual(true);
});
it("should perform the TOGGLE action without mutating the previous state", () => {
const state = { navCollapsed: true };
deepFreeze(state);
const action = HeaderActions.toggle();
headerReducer(state, action);
});
});