34 lines
916 B
JavaScript
34 lines
916 B
JavaScript
|
import worker from './dispatcher-worker';
|
||
|
|
||
|
jest.mock('../db', () => ({
|
||
|
initDb: () => 'called initDb',
|
||
|
}));
|
||
|
|
||
|
describe('The dispatcher-worker ', () => {
|
||
|
let port;
|
||
|
beforeEach(() => {
|
||
|
port = {
|
||
|
postMessage: jest.fn(),
|
||
|
};
|
||
|
worker.onconnect({ ports: [port] });
|
||
|
});
|
||
|
test('creates a onmessage function', () => {
|
||
|
expect(port.onmessage).toBeDefined();
|
||
|
expect(port.postMessage).not.toBeCalled();
|
||
|
});
|
||
|
test('receives a ping and sends back an unknownAction', async () => {
|
||
|
await port.onmessage({ data: { id: 5, payload: { action: 'ping' } } });
|
||
|
expect(port.postMessage).toBeCalledWith({
|
||
|
id: 5,
|
||
|
payload: 'unknownAction',
|
||
|
});
|
||
|
});
|
||
|
test('calls initDb when required', async () => {
|
||
|
await port.onmessage({ data: { id: 5, payload: { action: 'initDb' } } });
|
||
|
expect(port.postMessage).toBeCalledWith({
|
||
|
id: 5,
|
||
|
payload: 'called initDb',
|
||
|
});
|
||
|
});
|
||
|
});
|