Skip to content

Instantly share code, notes, and snippets.

@tiagogomes772
Created October 22, 2018 08:21
Show Gist options
  • Save tiagogomes772/437eb1b8276ff573d23dee2978a5f04a to your computer and use it in GitHub Desktop.
Save tiagogomes772/437eb1b8276ff573d23dee2978a5f04a to your computer and use it in GitHub Desktop.
describe('AsyncActions', () => {
beforeAll(() => {
mockStore = ReduxMockStore.default(middlewares);
initialState = {};
});
beforeEach(() => {
fetch.resetMocks();
});
it('Does a getRandomAdvice action successful', () => {
fetch.mockResponse(JSON.stringify({slip: {
advice: 'Advice'
}}));
const expectedActions = [
{ type: 'REQUEST_ADVICE' },
{ payload: 'Advice', type: 'RESPONSE_ADVICE_SUCCESS' },
];
const store = mockStore(initialState);
return (
store.dispatch(actions.getRandomAdvice()).then(() => {
expect(store.getActions()).toEqual(expectedActions);
})
);
});
it('Does a getRandomAdvice action when the server is down', () => {
fetch.mockResponse(JSON.stringify({slip: {
advice: 'Advice'
}}), {status: 500});
const expectedActions = [
{ type: 'REQUEST_ADVICE' },
{ type: 'RESPONSE_ADVICE_FAILURE' },
];
const store = mockStore(initialState);
return (
store.dispatch(actions.getRandomAdvice()).then(() => {
expect(store.getActions()).toEqual(expectedActions);
})
);
});
it('Does a getRandomAdvice successful', () => {
fetch.mockReject(new Error());
const expectedActions = [
{ type: 'REQUEST_ADVICE' },
{ type: 'RESPONSE_ADVICE_FAILURE' },
];
const store = mockStore(initialState);
return (
store.dispatch(actions.getRandomAdvice()).then(() => {
expect(store.getActions()).toEqual(expectedActions);
})
);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment