Reducer and mapping tests
This commit is contained in:
@@ -3,6 +3,6 @@
|
||||
import { createStore, applyMiddleware } from "redux";
|
||||
import thunk from "redux-thunk";
|
||||
|
||||
import { reducer } from "./worlds.reducer.js";
|
||||
import { reducer } from "./world.reducer";
|
||||
|
||||
export const store = createStore(reducer, applyMiddleware(thunk));
|
||||
|
||||
@@ -42,6 +42,7 @@
|
||||
"start": "react-app-rewired start",
|
||||
"build": "react-app-rewired build",
|
||||
"test": "react-app-rewired test --env=jsdom",
|
||||
"coverage": "react-app-rewired test --env=jsdom --coverage --no-cache",
|
||||
"eject": "react-app-rewired eject",
|
||||
"flow": "flow",
|
||||
"addTypes": "flow-typed install",
|
||||
|
||||
@@ -4,7 +4,7 @@ import { connect } from "react-redux";
|
||||
|
||||
import { RegionsTable } from "./regionsTable.component";
|
||||
|
||||
const getProps = state => ({
|
||||
export const getProps = state => ({
|
||||
list: state.regions,
|
||||
loading: state.loadingRegions
|
||||
});
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
/* @flow */
|
||||
|
||||
import Enzyme from "enzyme/build";
|
||||
import Adapter from "enzyme-adapter-react-16/build";
|
||||
|
||||
import { getProps } from "./regionsTable.connected.js";
|
||||
|
||||
Enzyme.configure({ adapter: new Adapter() });
|
||||
|
||||
describe("getProps for RegionsTable", () => {
|
||||
it("should extract regions and loading", () => {
|
||||
const initialState = {
|
||||
loadingCountries: false,
|
||||
currentCountry: "whatever",
|
||||
countries: [{ other: 1 }, { other: 2 }, { other: 3 }],
|
||||
loadingRegions: false,
|
||||
regions: [{ something: 1 }, { something: 2 }]
|
||||
};
|
||||
const initialJSON = JSON.stringify(initialState);
|
||||
|
||||
expect(getProps(initialState)).toEqual({
|
||||
list: [{ something: 1 }, { something: 2 }],
|
||||
loading: false
|
||||
});
|
||||
expect(JSON.stringify(initialState)).toBe(initialJSON);
|
||||
});
|
||||
});
|
||||
@@ -5,7 +5,7 @@ import thunk from "redux-thunk";
|
||||
import { createLogger } from "redux-logger";
|
||||
import { composeWithDevTools } from "redux-devtools-extension";
|
||||
|
||||
import { reducer } from "./worlds.reducer.js";
|
||||
import { reducer } from "./world.reducer.js";
|
||||
|
||||
const logger = createLogger({ duration: true });
|
||||
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
/* @flow */
|
||||
|
||||
import Enzyme from "enzyme/build";
|
||||
import Adapter from "enzyme-adapter-react-16/build";
|
||||
|
||||
import { reducer } from "./world.reducer.js";
|
||||
import { countriesRequest, regionsSuccess } from "./world.actions.js";
|
||||
|
||||
Enzyme.configure({ adapter: new Adapter() });
|
||||
|
||||
describe("The countries and regions reducer", () => {
|
||||
it("should process countryRequest actions", () => {
|
||||
const initialState = {
|
||||
loadingCountries: false,
|
||||
currentCountry: "whatever",
|
||||
countries: [{}, {}, {}],
|
||||
loadingRegions: false,
|
||||
regions: [{}, {}]
|
||||
};
|
||||
|
||||
const initialJSON = JSON.stringify(initialState);
|
||||
|
||||
expect(reducer(initialState, countriesRequest())).toEqual({
|
||||
loadingCountries: true,
|
||||
currentCountry: "whatever",
|
||||
countries: [],
|
||||
loadingRegions: false,
|
||||
regions: [{}, {}]
|
||||
});
|
||||
|
||||
expect(JSON.stringify(initialState)).toBe(initialJSON);
|
||||
});
|
||||
|
||||
it("should process regionsSuccess actions", () => {
|
||||
const initialState = {
|
||||
loadingCountries: false,
|
||||
currentCountry: "whatever",
|
||||
countries: [{}, {}, {}],
|
||||
loadingRegions: true,
|
||||
regions: []
|
||||
};
|
||||
|
||||
const initialJSON = JSON.stringify(initialState);
|
||||
|
||||
expect(
|
||||
reducer(
|
||||
initialState,
|
||||
regionsSuccess([
|
||||
{ something: 1 },
|
||||
{ something: 2 },
|
||||
{ something: 3 }
|
||||
])
|
||||
)
|
||||
).toEqual({
|
||||
loadingCountries: false,
|
||||
currentCountry: "whatever",
|
||||
countries: [{}, {}, {}],
|
||||
loadingRegions: false,
|
||||
regions: [{ something: 1 }, { something: 2 }, { something: 3 }]
|
||||
});
|
||||
|
||||
expect(JSON.stringify(initialState)).toBe(initialJSON);
|
||||
});
|
||||
|
||||
it("should return the initial state for unknown actions", () => {
|
||||
const initialState = {
|
||||
loadingCountries: false,
|
||||
currentCountry: "whatever",
|
||||
countries: [{}, {}, {}],
|
||||
loadingRegions: true,
|
||||
regions: []
|
||||
};
|
||||
const initialJSON = JSON.stringify(initialState);
|
||||
expect(
|
||||
JSON.stringify(reducer(initialState, { actionType: "other" }))
|
||||
).toBe(initialJSON);
|
||||
expect(JSON.stringify(initialState)).toBe(initialJSON);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user