From 7e0f45019b2bc3cbbce06a365b6681b2b72436aa Mon Sep 17 00:00:00 2001 From: Joe Fleming Date: Fri, 22 Apr 2016 17:49:33 -0700 Subject: [PATCH] add job test for index creation also make other tests properly async --- test/src/job.js | 70 +++++++++++++++++++++++++++++++++---------------- 1 file changed, 47 insertions(+), 23 deletions(-) diff --git a/test/src/job.js b/test/src/job.js index 2b73237..1c0dddd 100644 --- a/test/src/job.js +++ b/test/src/job.js @@ -1,16 +1,23 @@ import events from 'events'; import expect from 'expect.js'; import sinon from 'sinon'; -import _ from 'lodash'; -import Job from '../../lib/job'; +import proxyquire from 'proxyquire'; import elasticsearchMock from '../fixtures/elasticsearch'; import { JOB_STATUS_PENDING } from '../../lib/helpers/constants'; +const createIndexMock = sinon.stub().returns(Promise.resolve('mock')); +const module = proxyquire.noPreserveCache()('../../lib/job', { + './helpers/create_index': createIndexMock +}); + +const Job = module; + describe('Job Class', function () { let client; let index; beforeEach(function () { + createIndexMock.reset(); index = 'test'; client = new elasticsearchMock.Client(); }); @@ -43,7 +50,7 @@ describe('Job Class', function () { let timeout; function validateDoc(spy) { - expect(spy.callCount).to.be(1); + sinon.assert.callCount(spy, 1); const spyCall = spy.getCall(0); return spyCall.args[0]; } @@ -56,38 +63,55 @@ describe('Job Class', function () { }); it('should index the payload', function () { - new Job(client, index, type, payload); - const newDoc = validateDoc(client.index); - expect(newDoc).to.have.property('index', index); - expect(newDoc).to.have.property('type', type); - expect(newDoc).to.have.property('body'); - expect(newDoc.body).to.have.property('payload', payload); + const job = new Job(client, index, type, payload); + return job.ready.then(() => { + const newDoc = validateDoc(client.index); + expect(newDoc).to.have.property('index', index); + expect(newDoc).to.have.property('type', type); + expect(newDoc).to.have.property('body'); + expect(newDoc.body).to.have.property('payload', payload); + }); }); it('should index timeout value from options', function () { - new Job(client, index, type, payload, timeout); - const newDoc = validateDoc(client.index); - expect(newDoc.body).to.have.property('timeout', timeout); + const job = new Job(client, index, type, payload, timeout); + return job.ready.then(() => { + const newDoc = validateDoc(client.index); + expect(newDoc.body).to.have.property('timeout', timeout); + }); }); it('should set event times', function () { - new Job(client, index, type, payload, timeout); - const newDoc = validateDoc(client.index); - expect(newDoc.body).to.have.property('created'); - expect(newDoc.body).to.have.property('started'); - expect(newDoc.body).to.have.property('completed'); + const job = new Job(client, index, type, payload, timeout); + return job.ready.then(() => { + const newDoc = validateDoc(client.index); + expect(newDoc.body).to.have.property('created'); + expect(newDoc.body).to.have.property('started'); + expect(newDoc.body).to.have.property('completed'); + }); }); it('should set attempt count', function () { - new Job(client, index, type, payload, timeout); - const newDoc = validateDoc(client.index); - expect(newDoc.body).to.have.property('attempts'); + const job = new Job(client, index, type, payload, timeout); + return job.ready.then(() => { + const newDoc = validateDoc(client.index); + expect(newDoc.body).to.have.property('attempts'); + }); }); it('should set status as pending', function () { - new Job(client, index, type, payload, timeout); - const newDoc = validateDoc(client.index); - expect(newDoc.body).to.have.property('status', JOB_STATUS_PENDING); + const job = new Job(client, index, type, payload, timeout); + return job.ready.then(() => { + const newDoc = validateDoc(client.index); + expect(newDoc.body).to.have.property('status', JOB_STATUS_PENDING); + }); + }); + + it('should create the target index', function () { + const job = new Job(client, index, type, payload, timeout); + return job.ready.then(() => { + sinon.assert.calledOnce(createIndexMock); + }); }); }); });