Skip to content

Commit

Permalink
Updated example with README, and added getLastActionCalled function
Browse files Browse the repository at this point in the history
  • Loading branch information
3LOK committed Dec 21, 2016
1 parent 6a24b85 commit b5ec8ac
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 27 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
dist
dist
*.swp
53 changes: 27 additions & 26 deletions examples/example.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import chai, { expect } from 'chai';
import chaiAsPromised from 'chai-as-promised';
import { call, take, put } from 'redux-saga/effects';
import SagaTester, { resetAction } from '../';
import SagaTester from '../src/SagaTester.js';

chai.use(chaiAsPromised);

Expand All @@ -22,13 +22,16 @@ const middleware = store => next => action => next({

const fetchApi = () => someResult;

const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms))

function* listenAndFetch() {
yield take(fetchRequestActionType);
const result = yield call(fetchApi);
yield call(delay, 500); // For async example.
yield put({ type : fetchSuccessActionType, payload : result });
}

it('Showcases the tester API', done => {
it('Showcases the tester API', async () => {
// Start up the saga tester
const sagaTester = new SagaTester({
initialState,
Expand All @@ -40,33 +43,31 @@ it('Showcases the tester API', done => {
// Check that state was populated with initialState
expect(sagaTester.getState()).to.deep.equal(initialState);

// Hook into the success action
sagaTester.waitFor(fetchSuccessActionType).then(() => {
// Check that all actions have the meta property from the middleware
sagaTester.getActionsCalled().forEach(action => {
expect(action.meta).to.equal(middlewareMeta)
});

// Check that the new state was affected by the reducer
expect(sagaTester.getState()).to.deep.equal({
someKey : someOtherValue
});
// Dispatch the event to start the saga
sagaTester.dispatch({type : fetchRequestActionType});

// Check that the saga listens only once
sagaTester.dispatch({ type : fetchRequestActionType });
expect(sagaTester.numCalled(fetchRequestActionType)).to.equal(2);
expect(sagaTester.numCalled(fetchSuccessActionType)).to.equal(1);
// Hook into the success action
await sagaTester.waitFor(fetchSuccessActionType);

// Reset the state and action list, dispatch again
// and check that it was called
sagaTester.reset(true);
expect(sagaTester.wasCalled(fetchRequestActionType)).to.equal(false);
sagaTester.dispatch({ type : fetchRequestActionType });
expect(sagaTester.wasCalled(fetchRequestActionType)).to.equal(true);
// Check that all actions have the meta property from the middleware
sagaTester.getActionsCalled().forEach(action => {
expect(action.meta).to.equal(middlewareMeta)
});

done();
// Check that the new state was affected by the reducer
expect(sagaTester.getState()).to.deep.equal({
someKey : someOtherValue
});

// Dispatch the event to start the saga
sagaTester.dispatch({type : fetchRequestActionType});
// Check that the saga listens only once
sagaTester.dispatch({ type : fetchRequestActionType });
expect(sagaTester.numCalled(fetchRequestActionType)).to.equal(2);
expect(sagaTester.numCalled(fetchSuccessActionType)).to.equal(1);

// Reset the state and action list, dispatch again
// and check that it was called
sagaTester.reset(true);
expect(sagaTester.wasCalled(fetchRequestActionType)).to.equal(false);
sagaTester.dispatch({ type : fetchRequestActionType });
expect(sagaTester.wasCalled(fetchRequestActionType)).to.equal(true);
})
4 changes: 4 additions & 0 deletions src/SagaTester.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ export default class SagaIntegrationTester {
return this.actionsCalled;
}

getLastActionCalled(num = 1) {
return this.actionsCalled.slice(-1 * num);
}

wasCalled(actionType) {
return !!this.actionLookups[actionType];
}
Expand Down

0 comments on commit b5ec8ac

Please sign in to comment.