Created
December 28, 2015 00:00
-
-
Save cherta/b559dba30ebee267b3f3 to your computer and use it in GitHub Desktop.
Testing async actions on redux without following the manual - gist 3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// src/actions.js | |
export function getPost(postId) { | |
return function (dispatch, getState) { | |
$ | |
.getJSON('/posts/' + posotId) | |
.done(function (postData) { | |
dispatch( { type: 'SHOW_POST', post: postData } ) | |
}) | |
.fail(function (postData) { | |
dispatch( { type: 'SHOW_ERROR' } ) | |
}) | |
} | |
} | |
// test/actions.js | |
import test from 'tape' | |
import test from '../src/configureStore' | |
import nock from 'nock' //for mocking http requests | |
import * as actions from '../src/actions' | |
test('getPost action creator', function () { | |
t.plan(1) | |
nock('http://localhost:300/posts/1') //nock let you mock http requests | |
.get('/todos') | |
.reply(200, { id: 1, title: 'Post Title' } ) | |
const store = configureStore() | |
const dispatch = sinon.spy(store, 'dispatch') | |
const fn = actions.getPost(1) | |
fn(dispatch, store.getState) | |
t.true(dispatch.calledWith({ type: 'SHOW_POST', post: { id: 1, title: 'Post Title' } })) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment