forked from lukeschaefer/WorkerBee
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sideworker.test.js
124 lines (101 loc) · 3.1 KB
/
sideworker.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
const { expect } = chai
let SideWorker
describe('SideWorker', () => {
before(async () => {
const module = await import('../sideworker.js')
SideWorker = module.default
})
it('should be a function', () => {
expect(SideWorker).to.be.a('function')
})
it('should be a constructor', () => {
const _test = () => {
const w = new SideWorker()
w.worker.terminate()
}
expect(() => _test()).not.to.throw()
})
describe('when creating an instance', () => {
let instance
before(() => {
instance = new SideWorker()
})
it('should expose `define` method', () => {
expect(instance.define).to.be.a('function')
})
it('should expose `run` property of type `Object`', () => {
expect(instance.run).to.be.an('object')
})
it('should expose `worker` containing a Web Worker instance', () => {
expect(instance.worker).to.be.an.instanceof(Worker)
})
describe('when defining a method', () => {
before(() => {
instance.define('test', () => 42)
})
it('should expose it in the `run` object', () => {
expect(instance.run.test).to.be.a('function')
})
it('should return a Promise when the method is called', () => {
expect(instance.run.test()).to.be.a('promise')
})
it('should eventually return the methods result', async () => {
expect(await instance.run.test()).to.equal(42)
})
})
describe('when defining an error-prone method', () => {
before(() => {
instance.define(
'errorProne',
() => { throw new Error('Error in test!') }
)
})
it('should reject the Promise', async () => {
try {
const result = await instance.run.errorProne()
} catch(err) {
expect(err).to.be.an.instanceof(Error)
expect(err.message).to.equal('Error in test!')
}
})
})
describe('when defining a Promise-returning method', () => {
before(() => {
instance.define('promiseMe', () => {
return new Promise((resolve) => {
resolve(42)
})
})
})
it('should return the result of the Promised-function', () => {
it('should eventually return the methods result', async () => {
expect(await instance.run.promiseMe()).to.equal(42)
})
})
})
after(() => {
instance.worker.terminate()
})
})
describe('when creating an instance with `init` option', () => {
let instance
before(() => {
instance = new SideWorker({
init: () => {
self.testResult = 'passed'
self.getFirst = () => 'test'
}
})
})
it('should expose the `init` method in the `run` object', () => {
expect(instance.run.init).to.be.a('function')
})
it('should allow it to define globally available variables and functions', async () => {
instance.define('conclude', () => [getFirst(), testResult].join(' '))
expect(await instance.run.conclude()).to.equal('test passed')
})
after(() => {
instance.worker.terminate()
})
})
})