|
| 1 | +import { WorkerClient, WorkerClientMock } from './worker-client'; |
| 2 | +import { initProvider } from './worker-provider'; |
| 3 | + |
| 4 | +class MockMessageEvent { |
| 5 | + constructor(public data) { |
| 6 | + |
| 7 | + } |
| 8 | +} |
| 9 | + |
| 10 | +class MockErrorEvent { |
| 11 | + constructor(public message) { |
| 12 | + |
| 13 | + } |
| 14 | +} |
| 15 | + |
| 16 | +class MockWorker { |
| 17 | + onerror: (ev: MockErrorEvent) => any; |
| 18 | + onmessage: (ev: MockMessageEvent) => any; |
| 19 | + |
| 20 | + postMessage(message: any, transfer?: any[]): void { |
| 21 | + } |
| 22 | + |
| 23 | + terminate(): void { |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +class MockScope { |
| 28 | + onerror: (ev: MockErrorEvent) => any; |
| 29 | + onmessage: (ev: MockMessageEvent) => any; |
| 30 | + |
| 31 | + constructor(private worker: MockWorker) { |
| 32 | + worker.postMessage = (message: any, transfer?: any[]) => { |
| 33 | + this.onmessage(new MockMessageEvent(message)); |
| 34 | + }; |
| 35 | + } |
| 36 | + |
| 37 | + postMessage(message: any, transfer?: any[]): void { |
| 38 | + this.worker.onmessage(new MockMessageEvent(message)); |
| 39 | + } |
| 40 | + |
| 41 | +} |
| 42 | + |
| 43 | +describe('Worker Client', function() { |
| 44 | + it('should init client with worker', async function() { |
| 45 | + const mockWorker = new MockWorker(); |
| 46 | + const mockScope = new MockScope(mockWorker); |
| 47 | + initProvider({}, mockScope); |
| 48 | + |
| 49 | + const client = new WorkerClient(mockWorker as any as Worker); |
| 50 | + expect(client).toBeTruthy(); |
| 51 | + await expect(client.initWorker()).resolves.toBeTruthy(); |
| 52 | + |
| 53 | + spyOn(mockWorker, 'terminate'); |
| 54 | + |
| 55 | + client.destroy(); |
| 56 | + |
| 57 | + expect(mockWorker.terminate).toHaveBeenCalled(); |
| 58 | + }); |
| 59 | + |
| 60 | + it('should fail to init', async function() { |
| 61 | + const mockWorker = new MockWorker(); |
| 62 | + mockWorker.postMessage = (msg) => { |
| 63 | + mockWorker.onerror(new MockErrorEvent('failed')); |
| 64 | + }; |
| 65 | + const client = new WorkerClient(mockWorker as any as Worker); |
| 66 | + expect(client).toBeTruthy(); |
| 67 | + await expect(client.initWorker()).rejects.toEqual({ message: 'failed' }); |
| 68 | + }); |
| 69 | + |
| 70 | + it('should request hello', async function() { |
| 71 | + const mockWorker = new MockWorker(); |
| 72 | + const mockScope = new MockScope(mockWorker); |
| 73 | + initProvider({ |
| 74 | + hello(name: string) { |
| 75 | + return 'Hello ' + name; |
| 76 | + } |
| 77 | + }, mockScope); |
| 78 | + |
| 79 | + const client = new WorkerClient(mockWorker as any as Worker); |
| 80 | + expect(client).toBeTruthy(); |
| 81 | + await expect(client.initWorker()).resolves.toBeTruthy(); |
| 82 | + await expect(client.call('hello', 'World')).resolves.toBe('Hello World'); |
| 83 | + |
| 84 | + }); |
| 85 | + |
| 86 | + it('should fail', async function() { |
| 87 | + const mockWorker = new MockWorker(); |
| 88 | + const mockScope = new MockScope(mockWorker); |
| 89 | + initProvider({ |
| 90 | + fail() { |
| 91 | + throw new Error('failed'); |
| 92 | + } |
| 93 | + }, mockScope); |
| 94 | + |
| 95 | + const client = new WorkerClient(mockWorker as any as Worker); |
| 96 | + expect(client).toBeTruthy(); |
| 97 | + await expect(client.initWorker()).resolves.toBeTruthy(); |
| 98 | + await expect(client.call('fail')).rejects.toBe(new Error('failed').toString()); |
| 99 | + |
| 100 | + }); |
| 101 | + |
| 102 | +}); |
| 103 | + |
| 104 | +describe('Worker Client Mock', function() { |
| 105 | + it('should call hello', async function() { |
| 106 | + const client = new WorkerClientMock({ |
| 107 | + hello(name) { |
| 108 | + return 'Hello ' + name; |
| 109 | + } |
| 110 | + }); |
| 111 | + |
| 112 | + expect(client).toBeTruthy(); |
| 113 | + |
| 114 | + await expect(client.call('hello', 'World')).resolves.toBe('Hello World'); |
| 115 | + |
| 116 | + client.destroy(); |
| 117 | + |
| 118 | + }); |
| 119 | +}); |
0 commit comments