Skip to content

Instantly share code, notes, and snippets.

@vrinek
Created October 9, 2016 20:54
Show Gist options
  • Save vrinek/ea9fcc8b4ead67b41fb1b8ce88440a4b to your computer and use it in GitHub Desktop.
Save vrinek/ea9fcc8b4ead67b41fb1b8ce88440a4b to your computer and use it in GitHub Desktop.
const configureMockStore = require('redux-mock-store').default;
const thunk = require('redux-thunk').default;
const nock = require('../');
const expect = require('expect'); // You can use any testing library
const fetch = require('isomorphic-fetch');
function fetchTodosRequest() {
return {
type: 'FETCH_TODOS_REQUEST'
}
}
function fetchTodosSuccess(body) {
return {
type: 'FETCH_TODOS_SUCCESS',
body
}
}
function fetchTodosFailure(ex) {
return {
type: 'FETCH_TODOS_FAILURE',
ex
}
}
function fetchTodos() {
return dispatch => {
dispatch(fetchTodosRequest())
return fetch('http://example.com/todos')
.then(res => res.json())
.then(json => dispatch(fetchTodosSuccess(json.body)))
.catch(ex => dispatch(fetchTodosFailure(ex)))
}
}
const middlewares = [ thunk ]
const mockStore = configureMockStore(middlewares)
nock('http://example.com/')
.get('/todos')
.reply(200, { body: { todos: ['do something'] }})
const expectedActions = [
{ type: 'FETCH_TODOS_REQUEST' },
{ type: 'FETCH_TODOS_SUCCESS', body: { todos: ['do something'] } }
]
const store = mockStore({ todos: [] })
store.dispatch(fetchTodos())
.then(() => { // return of async actions
expect(store.getActions()).toEqual(expectedActions);
expect(nock.isDone()).toEqual(true);
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment