/
SockJSClient.test.js
59 lines (54 loc) · 1.48 KB
/
SockJSClient.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
'use strict';
const http = require('http');
const express = require('express');
const sockjs = require('sockjs');
const SockJSClient = require('../client-src/clients/SockJSClient');
describe('SockJSClient', () => {
let socketServer;
let listeningApp;
beforeAll((done) => {
// eslint-disable-next-line new-cap
const app = new express();
listeningApp = http.createServer(app);
listeningApp.listen(8080, 'localhost', () => {
socketServer = sockjs.createServer();
socketServer.installHandlers(listeningApp, {
prefix: '/sockjs-node',
});
done();
});
});
describe('client', () => {
it('should open, recieve message, and close', (done) => {
socketServer.on('connection', (connection) => {
connection.write('hello world');
setTimeout(() => {
connection.close();
}, 1000);
});
const client = new SockJSClient('http://localhost:8080/sockjs-node');
const data = [];
client.onOpen(() => {
data.push('open');
});
client.onClose(() => {
data.push('close');
});
client.onMessage((msg) => {
data.push(msg);
});
setTimeout(() => {
expect(data.length).toEqual(3);
expect(data[0]).toEqual('open');
expect(data[1]).toEqual('hello world');
expect(data[2]).toEqual('close');
done();
}, 3000);
});
});
afterAll((done) => {
listeningApp.close(() => {
done();
});
});
});