dyomedea/src/workers/dispatcher-main.test.js

29 lines
815 B
JavaScript
Raw Normal View History

2022-11-06 16:53:01 +00:00
import dispatch, { init, worker } from './dispatcher-main';
describe('The dispatcher-main', () => {
var postMessage = jest.fn();
beforeEach(() => {
global.SharedWorker = jest.fn(function (source) {
this.source = source;
this.port = {
postMessage,
};
return this;
});
init();
});
test('should create a new shared web worker', () => {
expect(SharedWorker).toBeCalled();
});
test('should create a onmessage function', () => {
expect(worker.port.onmessage).toBeDefined();
});
test('should return a promise if no callback is provided', () => {
expect(dispatch('ping')).toBeInstanceOf(Promise);
});
test('should send back the message', () => {
dispatch('ping');
expect(postMessage).toBeCalledWith({ id: 0, payload: 'ping' });
});
});