2020-10-09 19:35:46 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
2021-05-21 15:17:21 +02:00
|
|
|
const { checkThrow } = require('./testing/test-utils.js');
|
|
|
|
const eventManager = require('./eventManager').default;
|
2020-10-09 19:35:46 +02:00
|
|
|
|
2023-02-20 17:02:29 +02:00
|
|
|
describe('eventManager', () => {
|
2020-10-09 19:35:46 +02:00
|
|
|
|
2022-11-15 12:23:50 +02:00
|
|
|
beforeEach(async () => {
|
2020-10-09 19:35:46 +02:00
|
|
|
eventManager.reset();
|
|
|
|
});
|
|
|
|
|
2022-11-15 12:23:50 +02:00
|
|
|
afterEach(async () => {
|
2020-10-09 19:35:46 +02:00
|
|
|
eventManager.reset();
|
|
|
|
});
|
|
|
|
|
2020-12-01 20:05:24 +02:00
|
|
|
it('should watch state props', (async () => {
|
2020-10-09 19:35:46 +02:00
|
|
|
let localStateName = '';
|
|
|
|
let callCount = 0;
|
|
|
|
|
|
|
|
function nameWatch(event) {
|
|
|
|
callCount++;
|
|
|
|
localStateName = event.value;
|
|
|
|
}
|
|
|
|
|
|
|
|
const globalState = {
|
|
|
|
name: 'john',
|
|
|
|
};
|
|
|
|
|
|
|
|
eventManager.appStateOn('name', nameWatch);
|
|
|
|
eventManager.appStateEmit(globalState);
|
|
|
|
|
|
|
|
expect(localStateName).toBe('john');
|
|
|
|
|
|
|
|
globalState.name = 'paul';
|
|
|
|
|
|
|
|
eventManager.appStateEmit(globalState);
|
|
|
|
|
|
|
|
expect(localStateName).toBe('paul');
|
|
|
|
|
|
|
|
expect(callCount).toBe(2);
|
|
|
|
|
|
|
|
eventManager.appStateEmit(globalState);
|
|
|
|
|
|
|
|
expect(callCount).toBe(2);
|
|
|
|
}));
|
|
|
|
|
2020-12-01 20:05:24 +02:00
|
|
|
it('should unwatch state props', (async () => {
|
2020-10-09 19:35:46 +02:00
|
|
|
let localStateName = '';
|
|
|
|
|
|
|
|
function nameWatch(event) {
|
|
|
|
localStateName = event.value;
|
|
|
|
}
|
|
|
|
|
|
|
|
const globalState = {
|
|
|
|
name: 'john',
|
|
|
|
};
|
|
|
|
|
|
|
|
eventManager.appStateOn('name', nameWatch);
|
|
|
|
eventManager.appStateOff('name', nameWatch);
|
|
|
|
eventManager.appStateEmit(globalState);
|
|
|
|
|
|
|
|
expect(localStateName).toBe('');
|
|
|
|
}));
|
|
|
|
|
2020-12-01 20:05:24 +02:00
|
|
|
it('should watch nested props', (async () => {
|
2020-10-09 19:35:46 +02:00
|
|
|
let localStateName = '';
|
|
|
|
|
|
|
|
function nameWatch(event) {
|
|
|
|
localStateName = event.value;
|
|
|
|
}
|
|
|
|
|
|
|
|
const globalState = {
|
|
|
|
user: {
|
|
|
|
name: 'john',
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
eventManager.appStateOn('user.name', nameWatch);
|
|
|
|
eventManager.appStateEmit(globalState);
|
|
|
|
|
|
|
|
expect(localStateName).toBe('john');
|
|
|
|
|
|
|
|
globalState.user.name = 'paul';
|
|
|
|
|
|
|
|
eventManager.appStateEmit(globalState);
|
|
|
|
|
|
|
|
expect(localStateName).toBe('paul');
|
|
|
|
}));
|
|
|
|
|
2020-12-01 20:05:24 +02:00
|
|
|
it('should not be possible to modify state props', (async () => {
|
2020-10-09 19:35:46 +02:00
|
|
|
let localUser = {};
|
|
|
|
|
|
|
|
function userWatch(event) {
|
|
|
|
// Normally, the user should not keep a reference to the whole object
|
|
|
|
// but make a copy. However, if they do keep a reference and try to
|
|
|
|
// modify it, it should throw an exception as that would be an attempt
|
|
|
|
// to directly modify the Redux state.
|
|
|
|
localUser = event.value;
|
|
|
|
}
|
|
|
|
|
|
|
|
const globalState = {
|
|
|
|
user: {
|
|
|
|
name: 'john',
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
eventManager.appStateOn('user', userWatch);
|
|
|
|
eventManager.appStateEmit(globalState);
|
|
|
|
|
|
|
|
expect(checkThrow(() => localUser.name = 'paul')).toBe(true);
|
|
|
|
}));
|
|
|
|
|
|
|
|
});
|