Skip to content

Commit

Permalink
test: new events
Browse files Browse the repository at this point in the history
  • Loading branch information
trs committed Feb 10, 2018
1 parent 6ea6bac commit 0c7cc4f
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
24 changes: 24 additions & 0 deletions test/commands/registration/retr.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ const Promise = require('bluebird');
const bunyan = require('bunyan');
const {expect} = require('chai');
const sinon = require('sinon');
const EventEmitter = require('events');

const CMD = 'RETR';
describe(CMD, function () {
let sandbox;
let log = bunyan.createLogger({name: CMD});
let emitter;
const mockClient = {
commandSocket: {
pause: () => {},
Expand All @@ -29,6 +31,11 @@ describe(CMD, function () {
read: () => {}
};

emitter = new EventEmitter();
mockClient.emit = emitter.emit.bind(emitter);
mockClient.on = emitter.on.bind(emitter);
mockClient.once = emitter.once.bind(emitter);

sandbox.spy(mockClient, 'reply');
});
afterEach(() => sandbox.restore());
Expand Down Expand Up @@ -56,6 +63,7 @@ describe(CMD, function () {
return Promise.reject(new Promise.TimeoutError());
});


return cmdFn({log, command: {arg: 'test.txt'}})
.then(() => {
expect(mockClient.reply.args[0][0]).to.equal(425);
Expand All @@ -72,4 +80,20 @@ describe(CMD, function () {
expect(mockClient.reply.args[0][0]).to.equal(551);
});
});

it('// unsuccessful | emits error event', () => {
sandbox.stub(mockClient.connector, 'waitForConnection').callsFake(function () {
return Promise.reject(new Error('test'));
});

let errorEmitted = false;
emitter.once('RETR', err => {
errorEmitted = !!err;
});

return cmdFn({log, command: {arg: 'test.txt'}})
.then(() => {
expect(errorEmitted).to.equal(true);
});
});
});
23 changes: 23 additions & 0 deletions test/commands/registration/stor.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ const Promise = require('bluebird');
const bunyan = require('bunyan');
const {expect} = require('chai');
const sinon = require('sinon');
const EventEmitter = require('events');

const CMD = 'STOR';
describe(CMD, function () {
let sandbox;
let log = bunyan.createLogger({name: CMD});
let emitter;
const mockClient = {
commandSocket: {
pause: () => {},
Expand All @@ -29,6 +31,11 @@ describe(CMD, function () {
write: () => {}
};

emitter = new EventEmitter();
mockClient.emit = emitter.emit.bind(emitter);
mockClient.on = emitter.on.bind(emitter);
mockClient.once = emitter.once.bind(emitter);

sandbox.spy(mockClient, 'reply');
});
afterEach(() => sandbox.restore());
Expand Down Expand Up @@ -72,4 +79,20 @@ describe(CMD, function () {
expect(mockClient.reply.args[0][0]).to.equal(550);
});
});

it('// unsuccessful | emits error event', () => {
sandbox.stub(mockClient.connector, 'waitForConnection').callsFake(function () {
return Promise.reject(new Error('test'));
});

let errorEmitted = false;
emitter.once('STOR', err => {
errorEmitted = !!err;
});

return cmdFn({log, command: {arg: 'test.txt'}})
.then(() => {
expect(errorEmitted).to.equal(true);
});
});
});
9 changes: 9 additions & 0 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,11 @@ describe('Integration', function () {
it('STOR tést.txt', done => {
const buffer = Buffer.from('test text file');
const fsPath = `${clientDirectory}/${name}/tést.txt`;

connection.once('STOR', err => {
expect(err).to.not.exist;
});

client.put(buffer, 'tést.txt', err => {
expect(err).to.not.exist;
setTimeout(() => {
Expand Down Expand Up @@ -219,6 +224,10 @@ describe('Integration', function () {
});

it('RETR tést.txt', done => {
connection.once('RETR', err => {
expect(err).to.not.exist;
});

client.get('tést.txt', (err, stream) => {
expect(err).to.not.exist;
let text = '';
Expand Down

0 comments on commit 0c7cc4f

Please sign in to comment.