Test helper classes for the controllers are provided in objective-redux/dist/test.
This file exports two sets of imports: actual and mocked. The actual imports are everything from the real objective-redux module. The mocked imports are a select few modules that have been altered to facilitate easier testing.
Using your mocking framework, you can map the test exports to the original exports.
For example, using Jest:
jest.mock('objective-redux', () => {
const { actual, mocked } = require('objective-redux/dist/test');
return {
...actual,
...mocked,
};
});
The StateController test mock will return a decorated mutator in place of an action.
it('returns the mutation function when withAddressableName is not called', () => {
const controller: any = new MyStateController();
expect(controller.myAction).toEqual(testMutationOne);
expect(controller.myAction.actionName).toBeUndefined();
});
it('returns the mutation function when withAddressableName is called', () => {
const controller: any = new MyStateController();
expect(controller.myActionWithName).toEqual(testMutationTwo);
expect(controller.myActionWithName.actionName).toEqual(testName);
});
The StatelessController test mock will return a decorator saga in place of the action.
it('has the correct configuration', () => {
// The actions now point directly to the saga that was passed into the register function
// In addition, the function has been decorated with additional properties provided when built
const controller: any = new MyStatelessController(objectiveStoreMock);
expect(controller.myAction.actionName).toEqual('MyActionName'); // Ensure the value of withAddressableName was used
expect(controller.myAction.effectType).toEqual('TAKE_EVERY'); // Ensure that the configureTakeEvery was used
});
it('calls saga correctly', () => {
const payload = { foo: 'bar' };
const controller = new MyStatelessController(objectiveStoreMock);
// The action points directly to the generator function passed into the register method
const saga: any = controller.myAction({ payload });
// We can now verify the generator function directly
saga.next();
expect(myInternalFunction).toBeCalledWith(myParams);
});