/
Socket.test.js
176 lines (144 loc) · 5.6 KB
/
Socket.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
174
175
176
import { afterEach, beforeEach, jest, describe, it, expect } from '@jest/globals'
import UppySocket from './Socket.js'
describe('Socket', () => {
let webSocketConstructorSpy
let webSocketCloseSpy
let webSocketSendSpy
beforeEach(() => {
webSocketConstructorSpy = jest.fn()
webSocketCloseSpy = jest.fn()
webSocketSendSpy = jest.fn()
globalThis.WebSocket = class WebSocket {
constructor (target) {
webSocketConstructorSpy(target)
}
// eslint-disable-next-line class-methods-use-this
close (args) {
webSocketCloseSpy(args)
}
// eslint-disable-next-line class-methods-use-this
send (json) {
webSocketSendSpy(json)
}
triggerOpen () {
this.onopen()
}
triggerClose () {
this.onclose()
}
}
})
afterEach(() => {
globalThis.WebSocket = undefined
})
it('should expose a class', () => {
expect(UppySocket.name).toEqual('UppySocket')
expect(
new UppySocket({
target: 'foo',
}) instanceof UppySocket,
)
})
it('should setup a new WebSocket', () => {
new UppySocket({ target: 'foo' }) // eslint-disable-line no-new
expect(webSocketConstructorSpy.mock.calls[0][0]).toEqual('foo')
})
it('should send a message via the websocket if the connection is open', () => {
const uppySocket = new UppySocket({ target: 'foo' })
const webSocketInstance = uppySocket[Symbol.for('uppy test: getSocket')]()
webSocketInstance.triggerOpen()
uppySocket.send('bar', 'boo')
expect(webSocketSendSpy.mock.calls.length).toEqual(1)
expect(webSocketSendSpy.mock.calls[0]).toEqual([
JSON.stringify({ action: 'bar', payload: 'boo' }),
])
})
it('should queue the message for the websocket if the connection is not open', () => {
const uppySocket = new UppySocket({ target: 'foo' })
uppySocket.send('bar', 'boo')
expect(uppySocket[Symbol.for('uppy test: getQueued')]()).toEqual([{ action: 'bar', payload: 'boo' }])
expect(webSocketSendSpy.mock.calls.length).toEqual(0)
})
it('should queue any messages for the websocket if the connection is not open, then send them when the connection is open', () => {
const uppySocket = new UppySocket({ target: 'foo' })
const webSocketInstance = uppySocket[Symbol.for('uppy test: getSocket')]()
uppySocket.send('bar', 'boo')
uppySocket.send('moo', 'baa')
expect(uppySocket[Symbol.for('uppy test: getQueued')]()).toEqual([
{ action: 'bar', payload: 'boo' },
{ action: 'moo', payload: 'baa' },
])
expect(webSocketSendSpy.mock.calls.length).toEqual(0)
webSocketInstance.triggerOpen()
expect(uppySocket[Symbol.for('uppy test: getQueued')]()).toEqual([])
expect(webSocketSendSpy.mock.calls.length).toEqual(2)
expect(webSocketSendSpy.mock.calls[0]).toEqual([
JSON.stringify({ action: 'bar', payload: 'boo' }),
])
expect(webSocketSendSpy.mock.calls[1]).toEqual([
JSON.stringify({ action: 'moo', payload: 'baa' }),
])
})
it('should start queuing any messages when the websocket connection is closed', () => {
const uppySocket = new UppySocket({ target: 'foo' })
const webSocketInstance = uppySocket[Symbol.for('uppy test: getSocket')]()
webSocketInstance.triggerOpen()
uppySocket.send('bar', 'boo')
expect(uppySocket[Symbol.for('uppy test: getQueued')]()).toEqual([])
webSocketInstance.triggerClose()
uppySocket.send('bar', 'boo')
expect(uppySocket[Symbol.for('uppy test: getQueued')]()).toEqual([{ action: 'bar', payload: 'boo' }])
})
it('should close the websocket when it is force closed', () => {
const uppySocket = new UppySocket({ target: 'foo' })
const webSocketInstance = uppySocket[Symbol.for('uppy test: getSocket')]()
webSocketInstance.triggerOpen()
uppySocket.close()
expect(webSocketCloseSpy.mock.calls.length).toEqual(1)
})
it('should be able to subscribe to messages received on the websocket', () => {
const uppySocket = new UppySocket({ target: 'foo' })
const webSocketInstance = uppySocket[Symbol.for('uppy test: getSocket')]()
const emitterListenerMock = jest.fn()
uppySocket.on('hi', emitterListenerMock)
webSocketInstance.triggerOpen()
webSocketInstance.onmessage({
data: JSON.stringify({ action: 'hi', payload: 'ho' }),
})
expect(emitterListenerMock.mock.calls).toEqual([
['ho', undefined, undefined, undefined, undefined, undefined],
])
})
it('should be able to emit messages and subscribe to them', () => {
const uppySocket = new UppySocket({ target: 'foo' })
const emitterListenerMock = jest.fn()
uppySocket.on('hi', emitterListenerMock)
uppySocket.emit('hi', 'ho')
uppySocket.emit('hi', 'ho')
uppySocket.emit('hi', 'off to work we go')
expect(emitterListenerMock.mock.calls).toEqual([
['ho', undefined, undefined, undefined, undefined, undefined],
['ho', undefined, undefined, undefined, undefined, undefined],
[
'off to work we go',
undefined,
undefined,
undefined,
undefined,
undefined,
],
])
})
it('should be able to subscribe to the first event for a particular action', () => {
const uppySocket = new UppySocket({ target: 'foo' })
const emitterListenerMock = jest.fn()
uppySocket.once('hi', emitterListenerMock)
uppySocket.emit('hi', 'ho')
uppySocket.emit('hi', 'ho')
uppySocket.emit('hi', 'off to work we go')
expect(emitterListenerMock.mock.calls.length).toEqual(1)
expect(emitterListenerMock.mock.calls).toEqual([
['ho', undefined, undefined, undefined, undefined, undefined],
])
})
})