-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
test.js
124 lines (104 loc) · 2.78 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
import { expect } from '@esm-bundle/chai';
import mqtt from '../../'; // this will resolve to mqtt/dist/mqtt.esm.js
// needed to test no-esm version /dist/mqtt.js
/** @type { import('../../src') }*/
const mqtt2 = window.mqtt
// get browser name
const userAgent = navigator.userAgent.toLowerCase().replace(/ /g, '_').replace(/\//g, '_')
let browser = 'unknown'
if (userAgent.includes('chrome')) {
browser = 'chrome'
} else if (userAgent.includes('firefox')) {
browser = 'firefox'
} else if (userAgent.includes('safari')) {
browser = 'safari'
}
const browserTopic = `test/${browser}`
console.log('User Agent:', userAgent)
console.log('Browser:', browser)
function testProto(proto, port, cb = () => { }) {
const testTopic = `${browserTopic}/${proto}`
describe('MQTT.js browser test with ' + proto.toUpperCase(), () => {
after(() => {
if (client) {
client.end(() => {
cb()
client = null;
});
} else {
cb()
}
})
/** @type { import('../../src').MqttClient }*/
let client = null;
it('should connect-publish-subscribe', (done) => {
expect(typeof mqtt.Client.VERSION).to.equal('string')
client = mqtt.connect(`${proto}://localhost:${port}`, {
// log: console.log.bind(console),
clientId: `testClient-${browser}-${proto}`,
})
client.on('offline', () => {
console.log('client offline')
done(new Error('client offline'))
})
client.on('connect', () => {
console.log('client connect')
})
client.on('reconnect', () => {
console.log('client reconnect')
})
const payload = 'Hello World!'
client.on('connect', () => {
client.on('message', (topic, msg) => {
expect(topic).to.equal(testTopic);
expect(msg.toString()).to.equal(payload);
client.end(() => {
client = null;
done();
});
});
client.subscribe(testTopic, (err) => {
expect(err).to.not.exist;
if (!err) {
client.publish(testTopic, payload, (err2) => {
expect(err2).to.not.exist;
});
}
});
});
client.on('error', (err) => {
done(err);
});
})
})
}
describe('MQTT.js browser tests', () => {
it('should work with ESM version', (done) => {
expect(mqtt2).to.exist
expect(mqtt2.connect).to.be.a('function')
expect(mqtt2.Client).to.be.a('function')
done()
})
it('should work in a Web Worker', (done) => {
const worker = new Worker('test/browser/worker.js')
let ready = false
worker.onmessage = (e) => {
if (e.data === 'worker ready') {
ready = true
} else if(e.data === 'keepalive'){
worker.onerror = null
// worker.terminate()
expect(ready).to.be.true
done()
}else {
done(Error(e.data))
}
}
worker.onerror = (e) => {
done(Error(e.message))
}
})
testProto('ws', window.wsPort, () => {
testProto('wss', window.wssPort)
})
})