testing a Promise.all returned from dispatched action with jest and enzyme
I've got a quite problem with understanding how to test an async action creator.
Action is fetching data to github api which is taking user information and also fetching his repositories i put them into Promise.all() to resolve them as 1 promise and I didn't see any clues to test it right way to mock fetchData
here is action:
import fetch from 'isomorphic-fetch';
import apikey from '../../apikey';
import {
fetchUserBegin,
fetchUserInfoSucces,
fetchUserError,
fetchUserReposSuccess,
fetchUserLoadingEnd,
} from './index';
const apkey = process.env.NODE_ENV === 'production' ? '' : apikey;
export function fetchData(url) {
return (
fetch(url)
.then(result => result.json())
);
}
export default function takeUserNameAndFetchData(name) {
const userInfoUrl = `https://api.github.com/users/${name}${apkey}`;
const userRepoUrl = `https://api.github.com/users/${name}/repos${apkey}`;
return (dispatch) => {
dispatch(fetchUserBegin());
return Promise.all([
fetchData(userInfoUrl),
fetchData(userRepoUrl),
])
.then(([info, repos]) => {
console.log(info, repos);
dispatch(fetchUserInfoSucces(info));
dispatch(fetchUserReposSuccess(repos));
dispatch(fetchUserLoadingEnd());
})
.catch((err) => {
dispatch(fetchUserError(err));
});
};
}
and here is my test:
import configureMockStore from 'redux-mock-store';
import thunk from 'redux-thunk';
import fetchMock from 'fetch-mock';
import * as types from '../../src/actions/types';
import takeUserNameAndFetchData from '../../src/actions/fetchData';
const middlewares = [thunk];
const mockStore = configureMockStore(middlewares);
describe('testing fetchData actions', () => {
afterEach(() => {
fetchMock.restore();
});
test('should pass user name and fetch data', () => {
fetchMock.getOnce('*', {
body: [
{ userInfo: {} },
{ userRepos: },
],
});
const expectedActions = [
{ type: types.FETCH_USER_BEGIN },
{ type: types.FETCH_USER_INFO_SUCCESS, payload: { body: { userInfo: {} } } },
{ type: types.FETCH_USER_REPOS_SUCCESS, payload: { body: { userRepos: } } },
{ type: types.FETCH_USER_LOADING_END },
];
const store = mockStore({ userInfo: {}, userRepos: });
return store.dispatch(takeUserNameAndFetchData())
.then(() => {
const actualActions = store.getActions().map(action => action.type);
expect(actualActions).toEqual(expectedActions);
});
});
});
I am really confused about to test it right way with jest/enzyme.
redux jestjs enzyme redux-thunk
add a comment |
I've got a quite problem with understanding how to test an async action creator.
Action is fetching data to github api which is taking user information and also fetching his repositories i put them into Promise.all() to resolve them as 1 promise and I didn't see any clues to test it right way to mock fetchData
here is action:
import fetch from 'isomorphic-fetch';
import apikey from '../../apikey';
import {
fetchUserBegin,
fetchUserInfoSucces,
fetchUserError,
fetchUserReposSuccess,
fetchUserLoadingEnd,
} from './index';
const apkey = process.env.NODE_ENV === 'production' ? '' : apikey;
export function fetchData(url) {
return (
fetch(url)
.then(result => result.json())
);
}
export default function takeUserNameAndFetchData(name) {
const userInfoUrl = `https://api.github.com/users/${name}${apkey}`;
const userRepoUrl = `https://api.github.com/users/${name}/repos${apkey}`;
return (dispatch) => {
dispatch(fetchUserBegin());
return Promise.all([
fetchData(userInfoUrl),
fetchData(userRepoUrl),
])
.then(([info, repos]) => {
console.log(info, repos);
dispatch(fetchUserInfoSucces(info));
dispatch(fetchUserReposSuccess(repos));
dispatch(fetchUserLoadingEnd());
})
.catch((err) => {
dispatch(fetchUserError(err));
});
};
}
and here is my test:
import configureMockStore from 'redux-mock-store';
import thunk from 'redux-thunk';
import fetchMock from 'fetch-mock';
import * as types from '../../src/actions/types';
import takeUserNameAndFetchData from '../../src/actions/fetchData';
const middlewares = [thunk];
const mockStore = configureMockStore(middlewares);
describe('testing fetchData actions', () => {
afterEach(() => {
fetchMock.restore();
});
test('should pass user name and fetch data', () => {
fetchMock.getOnce('*', {
body: [
{ userInfo: {} },
{ userRepos: },
],
});
const expectedActions = [
{ type: types.FETCH_USER_BEGIN },
{ type: types.FETCH_USER_INFO_SUCCESS, payload: { body: { userInfo: {} } } },
{ type: types.FETCH_USER_REPOS_SUCCESS, payload: { body: { userRepos: } } },
{ type: types.FETCH_USER_LOADING_END },
];
const store = mockStore({ userInfo: {}, userRepos: });
return store.dispatch(takeUserNameAndFetchData())
.then(() => {
const actualActions = store.getActions().map(action => action.type);
expect(actualActions).toEqual(expectedActions);
});
});
});
I am really confused about to test it right way with jest/enzyme.
redux jestjs enzyme redux-thunk
well I replaced fetch with axios and I made an global mock of axios it is much easier than mocking globally fetch
– Michał Lutecki
Nov 21 '18 at 21:29
add a comment |
I've got a quite problem with understanding how to test an async action creator.
Action is fetching data to github api which is taking user information and also fetching his repositories i put them into Promise.all() to resolve them as 1 promise and I didn't see any clues to test it right way to mock fetchData
here is action:
import fetch from 'isomorphic-fetch';
import apikey from '../../apikey';
import {
fetchUserBegin,
fetchUserInfoSucces,
fetchUserError,
fetchUserReposSuccess,
fetchUserLoadingEnd,
} from './index';
const apkey = process.env.NODE_ENV === 'production' ? '' : apikey;
export function fetchData(url) {
return (
fetch(url)
.then(result => result.json())
);
}
export default function takeUserNameAndFetchData(name) {
const userInfoUrl = `https://api.github.com/users/${name}${apkey}`;
const userRepoUrl = `https://api.github.com/users/${name}/repos${apkey}`;
return (dispatch) => {
dispatch(fetchUserBegin());
return Promise.all([
fetchData(userInfoUrl),
fetchData(userRepoUrl),
])
.then(([info, repos]) => {
console.log(info, repos);
dispatch(fetchUserInfoSucces(info));
dispatch(fetchUserReposSuccess(repos));
dispatch(fetchUserLoadingEnd());
})
.catch((err) => {
dispatch(fetchUserError(err));
});
};
}
and here is my test:
import configureMockStore from 'redux-mock-store';
import thunk from 'redux-thunk';
import fetchMock from 'fetch-mock';
import * as types from '../../src/actions/types';
import takeUserNameAndFetchData from '../../src/actions/fetchData';
const middlewares = [thunk];
const mockStore = configureMockStore(middlewares);
describe('testing fetchData actions', () => {
afterEach(() => {
fetchMock.restore();
});
test('should pass user name and fetch data', () => {
fetchMock.getOnce('*', {
body: [
{ userInfo: {} },
{ userRepos: },
],
});
const expectedActions = [
{ type: types.FETCH_USER_BEGIN },
{ type: types.FETCH_USER_INFO_SUCCESS, payload: { body: { userInfo: {} } } },
{ type: types.FETCH_USER_REPOS_SUCCESS, payload: { body: { userRepos: } } },
{ type: types.FETCH_USER_LOADING_END },
];
const store = mockStore({ userInfo: {}, userRepos: });
return store.dispatch(takeUserNameAndFetchData())
.then(() => {
const actualActions = store.getActions().map(action => action.type);
expect(actualActions).toEqual(expectedActions);
});
});
});
I am really confused about to test it right way with jest/enzyme.
redux jestjs enzyme redux-thunk
I've got a quite problem with understanding how to test an async action creator.
Action is fetching data to github api which is taking user information and also fetching his repositories i put them into Promise.all() to resolve them as 1 promise and I didn't see any clues to test it right way to mock fetchData
here is action:
import fetch from 'isomorphic-fetch';
import apikey from '../../apikey';
import {
fetchUserBegin,
fetchUserInfoSucces,
fetchUserError,
fetchUserReposSuccess,
fetchUserLoadingEnd,
} from './index';
const apkey = process.env.NODE_ENV === 'production' ? '' : apikey;
export function fetchData(url) {
return (
fetch(url)
.then(result => result.json())
);
}
export default function takeUserNameAndFetchData(name) {
const userInfoUrl = `https://api.github.com/users/${name}${apkey}`;
const userRepoUrl = `https://api.github.com/users/${name}/repos${apkey}`;
return (dispatch) => {
dispatch(fetchUserBegin());
return Promise.all([
fetchData(userInfoUrl),
fetchData(userRepoUrl),
])
.then(([info, repos]) => {
console.log(info, repos);
dispatch(fetchUserInfoSucces(info));
dispatch(fetchUserReposSuccess(repos));
dispatch(fetchUserLoadingEnd());
})
.catch((err) => {
dispatch(fetchUserError(err));
});
};
}
and here is my test:
import configureMockStore from 'redux-mock-store';
import thunk from 'redux-thunk';
import fetchMock from 'fetch-mock';
import * as types from '../../src/actions/types';
import takeUserNameAndFetchData from '../../src/actions/fetchData';
const middlewares = [thunk];
const mockStore = configureMockStore(middlewares);
describe('testing fetchData actions', () => {
afterEach(() => {
fetchMock.restore();
});
test('should pass user name and fetch data', () => {
fetchMock.getOnce('*', {
body: [
{ userInfo: {} },
{ userRepos: },
],
});
const expectedActions = [
{ type: types.FETCH_USER_BEGIN },
{ type: types.FETCH_USER_INFO_SUCCESS, payload: { body: { userInfo: {} } } },
{ type: types.FETCH_USER_REPOS_SUCCESS, payload: { body: { userRepos: } } },
{ type: types.FETCH_USER_LOADING_END },
];
const store = mockStore({ userInfo: {}, userRepos: });
return store.dispatch(takeUserNameAndFetchData())
.then(() => {
const actualActions = store.getActions().map(action => action.type);
expect(actualActions).toEqual(expectedActions);
});
});
});
I am really confused about to test it right way with jest/enzyme.
redux jestjs enzyme redux-thunk
redux jestjs enzyme redux-thunk
edited Nov 21 '18 at 16:10
skyboyer
3,66111129
3,66111129
asked Nov 21 '18 at 10:11
Michał LuteckiMichał Lutecki
62
62
well I replaced fetch with axios and I made an global mock of axios it is much easier than mocking globally fetch
– Michał Lutecki
Nov 21 '18 at 21:29
add a comment |
well I replaced fetch with axios and I made an global mock of axios it is much easier than mocking globally fetch
– Michał Lutecki
Nov 21 '18 at 21:29
well I replaced fetch with axios and I made an global mock of axios it is much easier than mocking globally fetch
– Michał Lutecki
Nov 21 '18 at 21:29
well I replaced fetch with axios and I made an global mock of axios it is much easier than mocking globally fetch
– Michał Lutecki
Nov 21 '18 at 21:29
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53409708%2ftesting-a-promise-all-returned-from-dispatched-action-with-jest-and-enzyme%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53409708%2ftesting-a-promise-all-returned-from-dispatched-action-with-jest-and-enzyme%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
well I replaced fetch with axios and I made an global mock of axios it is much easier than mocking globally fetch
– Michał Lutecki
Nov 21 '18 at 21:29