Commit | Line | Data |
---|---|---|
a074ffee | 1 | import { expect } from 'expect' |
bb3d0b23 | 2 | import { restore, stub } from 'sinon' |
a074ffee | 3 | import { ThreadWorker } from '../../lib/index.js' |
fd027a65 | 4 | |
e1ffb94f | 5 | describe('Thread worker test suite', () => { |
e1ffb94f | 6 | class SpyWorker extends ThreadWorker { |
85aeb3f3 JB |
7 | constructor (fn) { |
8 | super(fn) | |
bb3d0b23 | 9 | this.port = { postMessage: stub().returns() } |
e1ffb94f | 10 | } |
74a2a194 | 11 | } |
74a2a194 | 12 | |
bb3d0b23 JB |
13 | afterEach(() => { |
14 | restore() | |
15 | }) | |
16 | ||
2431bdb4 JB |
17 | it('Verify that handleError() method is working properly', () => { |
18 | const error = new Error('Error as an error') | |
19 | const worker = new ThreadWorker(() => {}) | |
18ecc6c5 | 20 | expect(worker.handleError(error)).toBeInstanceOf(Error) |
2431bdb4 JB |
21 | expect(worker.handleError(error)).toStrictEqual(error) |
22 | const errorMessage = 'Error as a string' | |
23 | expect(worker.handleError(errorMessage)).toStrictEqual(errorMessage) | |
24 | }) | |
25 | ||
85aeb3f3 | 26 | it('Verify worker invokes the postMessage() method on port property', () => { |
74a2a194 APA |
27 | const worker = new SpyWorker(() => {}) |
28 | worker.sendToMainWorker({ ok: 1 }) | |
bb3d0b23 | 29 | expect(worker.port.postMessage.calledOnce).toBe(true) |
74a2a194 | 30 | }) |
fd027a65 | 31 | }) |