2017-07-14 02:38:17 +03:00
|
|
|
import 'jasmine';
|
|
|
|
import mutator from '../src/mutator';
|
|
|
|
import * as dispatcher from '../src/dispatcher';
|
2017-09-05 21:53:20 +03:00
|
|
|
import * as globalContext from '../src/globalContext';
|
2017-07-14 02:38:17 +03:00
|
|
|
import * as mobx from 'mobx';
|
|
|
|
|
|
|
|
describe('mutator', () => {
|
2017-09-05 21:53:20 +03:00
|
|
|
let mockGlobalContext: any;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
2020-03-18 21:05:57 +03:00
|
|
|
mockGlobalContext = { currentMutator: null };
|
2017-09-05 21:53:20 +03:00
|
|
|
spyOn(globalContext, 'getGlobalContext').and.returnValue(mockGlobalContext);
|
|
|
|
spyOn(dispatcher, 'subscribe');
|
|
|
|
});
|
|
|
|
|
2017-07-14 02:38:17 +03:00
|
|
|
it('throws if the action creator does not have an action ID', () => {
|
|
|
|
// Arrange
|
|
|
|
let actionCreator: any = {};
|
|
|
|
|
|
|
|
// Act / Assert
|
|
|
|
expect(() => {
|
|
|
|
mutator(actionCreator, () => {});
|
|
|
|
}).toThrow();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('subscribes the target function to the action', () => {
|
|
|
|
// Arrange
|
|
|
|
let actionId = 'testAction';
|
|
|
|
let actionCreator: any = { __SATCHELJS_ACTION_ID: actionId };
|
|
|
|
|
|
|
|
// Act
|
|
|
|
mutator(actionCreator, () => {});
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
expect(dispatcher.subscribe).toHaveBeenCalled();
|
|
|
|
expect((<jasmine.Spy>dispatcher.subscribe).calls.argsFor(0)[0]).toBe(actionId);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('wraps the subscribed callback in a MobX action', () => {
|
|
|
|
// Arrange
|
|
|
|
let callback = () => {};
|
|
|
|
let wrappedCallback = () => {};
|
|
|
|
let actionCreator: any = { __SATCHELJS_ACTION_ID: 'testAction' };
|
|
|
|
spyOn(mobx, 'action').and.returnValue(wrappedCallback);
|
|
|
|
|
|
|
|
// Act
|
|
|
|
mutator(actionCreator, callback);
|
|
|
|
|
|
|
|
// Assert
|
2018-01-31 20:16:54 +03:00
|
|
|
expect(mobx.action).toHaveBeenCalled();
|
2017-07-14 02:38:17 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('returns the target function', () => {
|
|
|
|
// Arrange
|
|
|
|
let actionCreator: any = { __SATCHELJS_ACTION_ID: 'testAction' };
|
|
|
|
let callback = () => {};
|
|
|
|
|
|
|
|
// Act
|
|
|
|
let returnValue = mutator(actionCreator, callback);
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
expect(returnValue).toBe(callback);
|
|
|
|
});
|
2017-09-05 21:05:48 +03:00
|
|
|
|
2020-03-18 21:05:57 +03:00
|
|
|
it('sets the currentMutator to actionMessage type for the duration of the mutator callback', () => {
|
2017-09-05 21:53:20 +03:00
|
|
|
// Arrange
|
2020-04-30 22:24:36 +03:00
|
|
|
let actionCreator: any = {
|
|
|
|
__SATCHELJS_ACTION_ID: 'testAction',
|
|
|
|
__SATCHELJS_ACTION_TYPE: 'testActionType',
|
|
|
|
};
|
2017-09-05 21:53:20 +03:00
|
|
|
let callback = () => {
|
2020-04-30 22:24:36 +03:00
|
|
|
expect(mockGlobalContext.currentMutator).toBe('testActionType');
|
2017-09-05 21:53:20 +03:00
|
|
|
};
|
|
|
|
mutator(actionCreator, callback);
|
|
|
|
|
|
|
|
// Act
|
|
|
|
let subscribedCallback = (dispatcher.subscribe as jasmine.Spy).calls.argsFor(0)[1];
|
|
|
|
subscribedCallback();
|
|
|
|
|
|
|
|
// Assert
|
2020-03-18 21:05:57 +03:00
|
|
|
expect(mockGlobalContext.currentMutator).toBe(null);
|
2017-09-05 21:53:20 +03:00
|
|
|
});
|
2022-02-28 23:33:00 +03:00
|
|
|
|
|
|
|
it('sets the currentMutator back to null if error is thrown', () => {
|
|
|
|
// Arrange
|
|
|
|
let actionCreator: any = {
|
|
|
|
__SATCHELJS_ACTION_ID: 'testAction',
|
|
|
|
__SATCHELJS_ACTION_TYPE: 'testActionType',
|
|
|
|
};
|
|
|
|
let callback: any = () => {
|
|
|
|
throw new Error('Error in Mutator');
|
|
|
|
};
|
|
|
|
mutator(actionCreator, callback);
|
|
|
|
|
|
|
|
// Act
|
|
|
|
let subscribedCallback = (dispatcher.subscribe as jasmine.Spy).calls.argsFor(0)[1];
|
|
|
|
try {
|
|
|
|
subscribedCallback();
|
|
|
|
} catch {
|
|
|
|
// no op
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
expect(mockGlobalContext.currentMutator).toBe(null);
|
|
|
|
});
|
2017-07-14 02:38:17 +03:00
|
|
|
});
|