Files
panic-server/test/Client.js
Jesse Gibson 4d4335ba34 Add Client class
Addressing another panic pain point: you have to jump through insane,
unintuitive hoops just to access a single, non-dynamic client.
Useful for picking out clients and running code on JUST one, without
needing dynamic exclusion lists and `.pluck`.
2016-10-05 08:19:41 -06:00

128 lines
3.0 KiB
JavaScript

/* eslint-env mocha*/
'use strict';
var Client = require('../src/Client');
var Emitter = require('events');
var Promise = require('bluebird');
var expect = require('expect');
describe('A client', function () {
var client, socket, platform;
beforeEach(function () {
socket = new Emitter();
platform = {
name: 'Node.js',
version: '6.6.0',
};
client = new Client({
socket: socket,
platform: platform,
});
});
it('should expose the socket', function () {
expect(client.socket).toBe(socket);
});
it('should expose the platform', function () {
expect(client.platform).toBe(platform);
});
describe('"run" call', function () {
var spy;
beforeEach(function () {
spy = expect.createSpy();
});
it('should send jobs to the client', function () {
client.socket.on('run', spy);
client.run(function () {});
expect(spy).toHaveBeenCalled();
});
it('should make sure the job is a function', function () {
function fail () {
client.run(9000);
}
expect(fail).toThrow(TypeError);
});
it('should send the stringified job', function () {
client.socket.on('run', spy);
client.run(function () {
// I haz a comment.
});
var str = spy.calls[0].arguments[0];
expect(str).toContain('I haz a comment');
});
it('should pass a job ID', function () {
client.socket.on('run', spy);
client.run(function () {});
client.run(function () {});
var id1 = spy.calls[0].arguments[1];
var id2 = spy.calls[1].arguments[1];
expect(id1).toBeA('string');
expect(id2).toBeA('string');
expect(id1).toNotBe(id2);
});
it('should send the props to the client', function () {
client.socket.on('run', spy);
var props = {};
client.run(function () {}, props);
var args = spy.calls[0].arguments;
expect(args[2]).toBe(props);
});
it('should return a bluebird promise', function () {
var job = client.run(function () {});
expect(job).toBeA(Promise);
});
it('should resolve when the job does', function (done) {
client.socket.on('run', spy);
var job = client.run(function () {});
var jobID = spy.calls[0].arguments[1];
job.then(done);
client.socket.emit(jobID);
});
it('should reject if the job fails', function (done) {
client.socket.on('run', spy);
var job = client.run(function () {});
var jobID = spy.calls[0].arguments[1];
var error = new Error('".run" rejection test.');
job.catch(function (err) {
expect(err).toBe(error);
done();
});
client.socket.emit(jobID, error);
});
it('should unsubscribe once finished', function () {
var socket = client.socket;
socket.on('run', spy);
client.run(function () {});
var jobID = spy.calls[0].arguments[1];
expect(socket.listenerCount(jobID)).toBe(1);
socket.emit(jobID);
expect(socket.listenerCount(jobID)).toBe(0);
});
it('should resolve if disconnected', function (done) {
var job = client.run(function () {});
job.then(done);
client.socket.emit('disconnect');
});
});
});