Last active
December 9, 2017 11:49
-
-
Save junibrosas/9575cea593eadf75e94d3ce768f3562b to your computer and use it in GitHub Desktop.
Compilation of snippets for testing Redux actions.
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
import * as React from 'react'; | |
import { expect, assert } from 'chai'; | |
import { mount, render, shallow } from 'enzyme'; | |
import * as sinon from 'Sinon'; | |
import { AttendanceActionCreators } from './path/to/actions'; | |
import { AttendanceReducer } from './path/to/reducer'; | |
import { IAttendState } from './path/to/types'; | |
import { Service } from './path/to/service'; | |
describe('Action Test', () => | |
{ | |
var defaultState:IAttendState = { | |
State: AttendanceReducer(undefined, undefined) | |
} | |
var server; | |
var sandbox: sinon.SinonSandbox; | |
var dispatch; | |
var spyDispatch: sinon.SinonSpy; | |
beforeEach(() => | |
{ | |
server = sinon.fakeServer.create(); | |
sandbox = sinon.sandbox.create(); | |
dispatch = { Dispatch: payload => { } }; | |
spyDispatch = sandbox.spy(dispatch, "Dispatch"); | |
}); | |
afterEach(function () | |
{ | |
sandbox.restore(); | |
spyDispatch.restore(); | |
}); | |
it('Dispatch actions properly', () => | |
{ | |
// Pre | |
var testState = Object.assign({}, defaultState); | |
var expectedCustomerId:number = 1; | |
var expectedInit = sandbox.spy(AttendanceActionCreators, 'InitStore'); // mock the action thunk method. | |
var expectedService = sandbox.spy(Service, "LoadClasses"); // mock the service api call. | |
// Execute | |
AttendanceActionCreators.InitStore(expectedCustomerId)(dispatch.Dispatch, () => testState); // execute the action thunk. | |
TestHelper.SimulateOkayApiResult(server); | |
// Test | |
// Test dispatched action types and values. | |
expect(spyDispatch.firstCall.args[0].type).to.equal("COA_SetCustomerId", 'Action type is not COA_SetCustomerId.'); | |
expect(spyDispatch.firstCall.args[0].value).to.equal(expectedCustomerId, 'Wrong expected customer id.'); | |
expect(expectedInit.calledOnce).to.equal(true, 'Action not called once.'); | |
expect(spyDispatch.secondCall.args[0].type).to.equal('COA_SetServiceFilterList', 'Type is not correct.'); | |
expect(expectedService.calledOnce).equals(true, 'Service not called once.'); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment