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' }); }); });