state-synchronizers
Version:
Deterministically update state based on other state
55 lines (54 loc) • 2.28 kB
JavaScript
;
exports.__esModule = true;
var create_synchronized_state_updater_1 = require("./create-synchronized-state-updater");
describe('createSynchronizedStateUpdater', function () {
var synchronizer = jest.fn();
beforeEach(function () {
jest.clearAllMocks();
});
it('should return a function', function () {
expect.hasAssertions();
var updater = create_synchronized_state_updater_1.createSynchronizedStateUpdater(synchronizer, {});
expect(updater).toBeInstanceOf(Function);
});
describe('returned state updater', function () {
var initialState = {
value: 0
};
var updater;
beforeEach(function () {
updater = create_synchronized_state_updater_1.createSynchronizedStateUpdater(synchronizer, initialState);
});
it('should call the state synchronizer when the state changed', function () {
expect.hasAssertions();
var newState = { value: 1 };
updater(newState);
expect(synchronizer).toHaveBeenCalledTimes(1);
expect(synchronizer).toHaveBeenCalledWith(newState, initialState);
});
it('should return the new state when the state changed', function () {
expect.hasAssertions();
var newState = { value: 1 };
var result = updater(newState);
expect(result).toStrictEqual(newState);
});
it('should not call the synchronizer when the state did not change', function () {
expect.hasAssertions();
updater(initialState);
expect(synchronizer).not.toHaveBeenCalled();
});
it('should return the previous state when the state did not change', function () {
expect.hasAssertions();
var result = updater(initialState);
expect(result).toStrictEqual(initialState);
});
it('should cache previous state', function () {
expect.hasAssertions();
var newState = { value: 1 };
updater(newState);
jest.clearAllMocks();
updater(newState);
expect(synchronizer).not.toHaveBeenCalled();
});
});
});