|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const http = require('http'); |
| 4 | +const express = require('express'); |
| 5 | +const sockjs = require('sockjs'); |
| 6 | +const SockJSClient = require('../lib/clients/SockJSClient'); |
| 7 | + |
| 8 | +describe('SockJSClient', () => { |
| 9 | + let socketServer; |
| 10 | + let listeningApp; |
| 11 | + |
| 12 | + beforeAll((done) => { |
| 13 | + // eslint-disable-next-line new-cap |
| 14 | + const app = new express(); |
| 15 | + listeningApp = http.createServer(app); |
| 16 | + listeningApp.listen(8080, 'localhost', () => { |
| 17 | + socketServer = sockjs.createServer(); |
| 18 | + socketServer.installHandlers(listeningApp, { |
| 19 | + prefix: '/sockjs-node', |
| 20 | + }); |
| 21 | + done(); |
| 22 | + }); |
| 23 | + }); |
| 24 | + |
| 25 | + describe('client', () => { |
| 26 | + it('should open, recieve message, and close', (done) => { |
| 27 | + socketServer.on('connection', (connection) => { |
| 28 | + connection.write('hello world'); |
| 29 | + setTimeout(() => { |
| 30 | + connection.close(); |
| 31 | + }, 1000); |
| 32 | + }); |
| 33 | + const client = new SockJSClient('http://localhost:8080/sockjs-node'); |
| 34 | + const data = []; |
| 35 | + client.onOpen(() => { |
| 36 | + data.push('open'); |
| 37 | + }); |
| 38 | + client.onClose(() => { |
| 39 | + data.push('close'); |
| 40 | + }); |
| 41 | + client.onMessage((msg) => { |
| 42 | + data.push(msg); |
| 43 | + }); |
| 44 | + setTimeout(() => { |
| 45 | + expect(data.length).toEqual(3); |
| 46 | + expect(data[0]).toEqual('open'); |
| 47 | + expect(data[1]).toEqual('hello world'); |
| 48 | + expect(data[2]).toEqual('close'); |
| 49 | + done(); |
| 50 | + }, 3000); |
| 51 | + }); |
| 52 | + }); |
| 53 | + |
| 54 | + afterAll((done) => { |
| 55 | + listeningApp.close(() => { |
| 56 | + done(); |
| 57 | + }); |
| 58 | + }); |
| 59 | +}); |
0 commit comments