-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.js
173 lines (147 loc) · 3.96 KB
/
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
'use strict'
var test = require('tape')
var sinon = require('sinon')
var msgr = require('./index')
// ---
// Mocks
// ---
var client_knownMessageTypeStub = sinon.stub()
var client_unknownMessageTypeStub = sinon.stub()
var worker_knownMessageTypeStub = sinon.stub()
var worker_unknownMessageTypeStub = sinon.stub()
var serviceWorker = {
postMessage: function (message) {
worker._handleMessage.call(worker, {
data: message,
ports: [{
postMessage: function (message) {
client._handleMessage.call(client, {data: message})
}
}]
})
}
}
var MessageChannel = function () {
this.port1 = {onmessage: null}
}
function resetStubs () {
worker_knownMessageTypeStub.reset()
worker_unknownMessageTypeStub.reset()
client_knownMessageTypeStub.reset()
client_unknownMessageTypeStub.reset()
}
// ---
// Client
// ---
var clientHandlers = {CLIENT_KNOWN_MESSAGE_TYPE: client_knownMessageTypeStub}
var client = msgr.client(serviceWorker, clientHandlers, MessageChannel)
client.receive(client_unknownMessageTypeStub)
// ---
// Worker
// ---
var workerHandlers = {WORKER_KNOWN_MESSAGE_TYPE: worker_knownMessageTypeStub}
var worker_connectSpy = workerHandlers[msgr.types.CONNECT] = sinon.spy()
var worker = msgr.worker(workerHandlers)
worker.receive(worker_unknownMessageTypeStub)
// ---
// Tests
// ---
test('Runs ready handler once channel is open', function (t) {
client.ready(function () {
t.end()
})
})
test('Message handler throws on malformed message', function (t) {
try {
client._handleMessage({data: 'blergh'})
} catch (err) {
t.equal(err.message, 'msgr: malformed message')
t.end()
}
})
test('Client sends CONNECT, worker receives', function (t) {
t.equal(worker_connectSpy.callCount, 1)
client.ready(function () {
t.end()
})
})
test('Client sends known message with data, worker receives', function (t) {
resetStubs()
client.send('WORKER_KNOWN_MESSAGE_TYPE', 'data')
client.ready(function () {
t.equal(worker_knownMessageTypeStub.callCount, 1)
t.end()
})
})
test('Client sends known message without data, worker receives', function (t) {
resetStubs()
client.send('WORKER_KNOWN_MESSAGE_TYPE')
client.ready(function () {
t.equal(worker_knownMessageTypeStub.callCount, 1)
t.end()
})
})
test('Worker sends known message, client receives', function (t) {
resetStubs()
worker.send('CLIENT_KNOWN_MESSAGE_TYPE', 'data')
worker.ready(function () {
t.equal(client_knownMessageTypeStub.callCount, 1)
t.end()
})
})
test('Client sends known message, worker responds', function (t) {
resetStubs()
var message = client.send('WORKER_KNOWN_MESSAGE_TYPE', 'data')
client.ready(function () {
worker_knownMessageTypeStub.callArg(1)
message.then(function () {
t.end()
})
})
})
test('Worker sends known message, client responds', function (t) {
resetStubs()
var message = worker.send('CLIENT_KNOWN_MESSAGE_TYPE', 'data')
worker.ready(function () {
client_knownMessageTypeStub.callArg(1)
message.then(function () {
t.end()
})
})
})
test('Client sends unknown message, worker receives', function (t) {
resetStubs()
client.send('data')
client.ready(function () {
t.equal(worker_unknownMessageTypeStub.callCount, 1)
t.end()
})
})
test('Worker sends unknown message, client receives', function (t) {
resetStubs()
worker.send('data')
worker.ready(function () {
t.equal(client_unknownMessageTypeStub.callCount, 1)
t.end()
})
})
test('Client sends unknown message, worker responds', function (t) {
resetStubs()
var message = client.send('data')
client.ready(function () {
worker_unknownMessageTypeStub.callArg(1)
message.then(function () {
t.end()
})
})
})
test('Worker sends unknown message, client responds', function (t) {
resetStubs()
var message = worker.send('data')
worker.ready(function () {
client_unknownMessageTypeStub.callArg(1)
message.then(function () {
t.end()
})
})
})