Tests for actions, reducers, and thunks
This commit is contained in:
Generated
+15
@@ -9261,6 +9261,12 @@
|
||||
"integrity": "sha1-+wMJF/hqMTTlvJvsDWngAT3f7bI=",
|
||||
"dev": true
|
||||
},
|
||||
"lodash.isplainobject": {
|
||||
"version": "4.0.6",
|
||||
"resolved": "https://registry.npmjs.org/lodash.isplainobject/-/lodash.isplainobject-4.0.6.tgz",
|
||||
"integrity": "sha1-fFJqUtibRcRcxpC4gWO+BJf1UMs=",
|
||||
"dev": true
|
||||
},
|
||||
"lodash.memoize": {
|
||||
"version": "4.1.2",
|
||||
"resolved": "https://registry.npmjs.org/lodash.memoize/-/lodash.memoize-4.1.2.tgz",
|
||||
@@ -12736,6 +12742,15 @@
|
||||
"deep-diff": "^0.3.5"
|
||||
}
|
||||
},
|
||||
"redux-mock-store": {
|
||||
"version": "1.5.3",
|
||||
"resolved": "https://registry.npmjs.org/redux-mock-store/-/redux-mock-store-1.5.3.tgz",
|
||||
"integrity": "sha512-ryhkkb/4D4CUGpAV2ln1GOY/uh51aczjcRz9k2L2bPx/Xja3c5pSGJJPyR25GNVRXtKIExScdAgFdiXp68GmJA==",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
"lodash.isplainobject": "^4.0.6"
|
||||
}
|
||||
},
|
||||
"redux-thunk": {
|
||||
"version": "2.3.0",
|
||||
"resolved": "https://registry.npmjs.org/redux-thunk/-/redux-thunk-2.3.0.tgz",
|
||||
|
||||
@@ -23,7 +23,8 @@
|
||||
"react-devtools": "^3.2.3",
|
||||
"react-scripts": "1.1.4",
|
||||
"react-test-renderer": "^16.4.2",
|
||||
"redux-devtools-extension": "^2.13.5"
|
||||
"redux-devtools-extension": "^2.13.5",
|
||||
"redux-mock-store": "^1.5.3"
|
||||
},
|
||||
"dependencies": {
|
||||
"axios": "^0.18.0",
|
||||
|
||||
@@ -72,6 +72,20 @@ export const getCountries = () => async dispatch => {
|
||||
}
|
||||
};
|
||||
|
||||
export const getCountries2 = () => async (dispatch, getState) => {
|
||||
if (getState().countries.length) {
|
||||
// no need to do anything!
|
||||
} else {
|
||||
try {
|
||||
dispatch(countriesRequest());
|
||||
const result = await getCountriesAPI();
|
||||
dispatch(countriesSuccess(result.data));
|
||||
} catch (e) {
|
||||
dispatch(countriesFailure());
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
export const getRegions = (country: string) => async dispatch => {
|
||||
if (country) {
|
||||
try {
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
/* @flow */
|
||||
|
||||
import configureMockStore from "redux-mock-store";
|
||||
import thunk from "redux-thunk";
|
||||
|
||||
import {
|
||||
getCountries,
|
||||
getCountries2,
|
||||
countriesSuccess,
|
||||
COUNTRIES_REQUEST,
|
||||
COUNTRIES_SUCCESS,
|
||||
COUNTRIES_FAILURE
|
||||
} from "./world.actions.js";
|
||||
|
||||
import { getCountriesAPI } from "./serviceApi";
|
||||
|
||||
let mockPromise;
|
||||
jest.mock("./serviceApi", () => {
|
||||
return {
|
||||
getCountriesAPI: jest.fn().mockImplementation(() => mockPromise)
|
||||
};
|
||||
});
|
||||
|
||||
describe("getCountries", () => {
|
||||
it("on API success", async () => {
|
||||
const fakeCountries = {
|
||||
data: [{ code: "UY" }, { code: "AR" }, { code: "BR" }]
|
||||
};
|
||||
mockPromise = Promise.resolve(fakeCountries);
|
||||
|
||||
const store = configureMockStore([thunk])({});
|
||||
|
||||
await store.dispatch(getCountries());
|
||||
|
||||
const dispatchedActions = store.getActions();
|
||||
|
||||
expect(getCountriesAPI).toHaveBeenCalledWith();
|
||||
expect(dispatchedActions.length).toBe(2);
|
||||
expect(dispatchedActions[0].type).toBe(COUNTRIES_REQUEST);
|
||||
expect(dispatchedActions[1].type).toBe(COUNTRIES_SUCCESS);
|
||||
expect(dispatchedActions[1].listOfCountries).toEqual(
|
||||
fakeCountries.data
|
||||
);
|
||||
|
||||
/*
|
||||
We could have written... but then we'd need tests for countriesSuccess()
|
||||
*/
|
||||
expect(dispatchedActions[1]).toEqual(
|
||||
countriesSuccess(fakeCountries.data)
|
||||
);
|
||||
});
|
||||
|
||||
it("on API failure", async () => {
|
||||
mockPromise = Promise.reject(new Error("failure!"));
|
||||
|
||||
const store = configureMockStore([thunk])({});
|
||||
|
||||
await store.dispatch(getCountries());
|
||||
|
||||
const dispatchedActions = store.getActions();
|
||||
|
||||
expect(getCountriesAPI).toHaveBeenCalledWith();
|
||||
expect(dispatchedActions.length).toBe(2);
|
||||
expect(dispatchedActions[0].type).toBe(COUNTRIES_REQUEST);
|
||||
expect(dispatchedActions[1].type).toBe(COUNTRIES_FAILURE);
|
||||
});
|
||||
});
|
||||
|
||||
describe("getCountries2", () => {
|
||||
it("doesn't do unneeded calls", async () => {
|
||||
const store = configureMockStore([thunk])({
|
||||
countries: [{ land: 1 }, { land: 2 }]
|
||||
});
|
||||
await store.dispatch(getCountries2());
|
||||
expect(store.getActions().length).toBe(0);
|
||||
});
|
||||
});
|
||||
@@ -1,13 +1,8 @@
|
||||
/* @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 = {
|
||||
|
||||
Reference in New Issue
Block a user