Skip to content

Commit

Permalink
test(server): add SockJSServer test (#1956)
Browse files Browse the repository at this point in the history
* test(server): added SockJSServer test

* test(server): change test done callback
  • Loading branch information
knagaitsev authored and hiroppy committed Jun 3, 2019
1 parent eecc1e4 commit d346a53
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions test/SockJSServer.test.js
@@ -0,0 +1,64 @@
'use strict';

const http = require('http');
const express = require('express');
const SockJS = require('sockjs-client/dist/sockjs');
const SockJSServer = require('../lib/servers/SockJSServer');

describe('SockJSServer', () => {
let socketServer;
let listeningApp;

beforeAll((done) => {
// eslint-disable-next-line new-cap
const app = new express();
listeningApp = http.createServer(app);
listeningApp.listen(8080, 'localhost', () => {
const server = {
log: {
error: () => {},
debug: () => {},
},
sockPath: '/sockjs-node',
listeningApp,
};
socketServer = new SockJSServer(server);
done();
});
});

describe('server', () => {
it('should recieve connection, send message, and close client', (done) => {
const data = [];
socketServer.onConnection((connection) => {
data.push('open');
socketServer.send(connection, 'hello world');
setTimeout(() => {
socketServer.close(connection);
}, 1000);
});

const client = new SockJS('http://localhost:8080/sockjs-node');

client.onmessage = function(e) {
data.push(e.data);
};

client.onclose = function() {
data.push('close');
};

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);
});
});

0 comments on commit d346a53

Please sign in to comment.