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
|
import { WebSocket as MockWebSocket, Server as MockServer } from "mock-socket"
import * as ActionCable from "../../../../app/javascript/action_cable/index"
import {defer, testURL} from "./index"
export default function(name, options, callback) {
if (options == null) { options = {} }
if (callback == null) {
callback = options
options = {}
}
if (options.url == null) { options.url = testURL }
return QUnit.test(name, function(assert) {
const doneAsync = assert.async()
ActionCable.adapters.WebSocket = MockWebSocket
const server = new MockServer(options.url)
const consumer = ActionCable.createConsumer(options.url)
server.on("connection", function() {
const clients = server.clients()
assert.equal(clients.length, 1)
assert.equal(clients[0].readyState, WebSocket.OPEN)
})
server.broadcastTo = function(subscription, data, callback) {
if (data == null) { data = {} }
data.identifier = subscription.identifier
if (data.message_type) {
data.type = ActionCable.INTERNAL.message_types[data.message_type]
delete data.message_type
}
server.send(JSON.stringify(data))
defer(callback)
}
const done = function() {
consumer.disconnect()
server.close()
doneAsync()
}
const testData = {assert, consumer, server, done}
if (options.connect === false) {
callback(testData)
} else {
server.on("connection", function() {
testData.client = server.clients()[0]
callback(testData)
})
consumer.connect()
}
})
}
|