add job test for index creation

also make other tests properly async
This commit is contained in:
2016-04-22 17:49:33 -07:00
parent f120d84367
commit 7e0f45019b

View File

@@ -1,16 +1,23 @@
import events from 'events'; import events from 'events';
import expect from 'expect.js'; import expect from 'expect.js';
import sinon from 'sinon'; import sinon from 'sinon';
import _ from 'lodash'; import proxyquire from 'proxyquire';
import Job from '../../lib/job';
import elasticsearchMock from '../fixtures/elasticsearch'; import elasticsearchMock from '../fixtures/elasticsearch';
import { JOB_STATUS_PENDING } from '../../lib/helpers/constants'; 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 () { describe('Job Class', function () {
let client; let client;
let index; let index;
beforeEach(function () { beforeEach(function () {
createIndexMock.reset();
index = 'test'; index = 'test';
client = new elasticsearchMock.Client(); client = new elasticsearchMock.Client();
}); });
@@ -43,7 +50,7 @@ describe('Job Class', function () {
let timeout; let timeout;
function validateDoc(spy) { function validateDoc(spy) {
expect(spy.callCount).to.be(1); sinon.assert.callCount(spy, 1);
const spyCall = spy.getCall(0); const spyCall = spy.getCall(0);
return spyCall.args[0]; return spyCall.args[0];
} }
@@ -56,38 +63,55 @@ describe('Job Class', function () {
}); });
it('should index the payload', function () { it('should index the payload', function () {
new Job(client, index, type, payload); const job = new Job(client, index, type, payload);
return job.ready.then(() => {
const newDoc = validateDoc(client.index); const newDoc = validateDoc(client.index);
expect(newDoc).to.have.property('index', index); expect(newDoc).to.have.property('index', index);
expect(newDoc).to.have.property('type', type); expect(newDoc).to.have.property('type', type);
expect(newDoc).to.have.property('body'); expect(newDoc).to.have.property('body');
expect(newDoc.body).to.have.property('payload', payload); expect(newDoc.body).to.have.property('payload', payload);
}); });
});
it('should index timeout value from options', function () { it('should index timeout value from options', function () {
new Job(client, index, type, payload, timeout); const job = new Job(client, index, type, payload, timeout);
return job.ready.then(() => {
const newDoc = validateDoc(client.index); const newDoc = validateDoc(client.index);
expect(newDoc.body).to.have.property('timeout', timeout); expect(newDoc.body).to.have.property('timeout', timeout);
}); });
});
it('should set event times', function () { it('should set event times', function () {
new Job(client, index, type, payload, timeout); const job = new Job(client, index, type, payload, timeout);
return job.ready.then(() => {
const newDoc = validateDoc(client.index); const newDoc = validateDoc(client.index);
expect(newDoc.body).to.have.property('created'); expect(newDoc.body).to.have.property('created');
expect(newDoc.body).to.have.property('started'); expect(newDoc.body).to.have.property('started');
expect(newDoc.body).to.have.property('completed'); expect(newDoc.body).to.have.property('completed');
}); });
});
it('should set attempt count', function () { it('should set attempt count', function () {
new Job(client, index, type, payload, timeout); const job = new Job(client, index, type, payload, timeout);
return job.ready.then(() => {
const newDoc = validateDoc(client.index); const newDoc = validateDoc(client.index);
expect(newDoc.body).to.have.property('attempts'); expect(newDoc.body).to.have.property('attempts');
}); });
});
it('should set status as pending', function () { it('should set status as pending', function () {
new Job(client, index, type, payload, timeout); const job = new Job(client, index, type, payload, timeout);
return job.ready.then(() => {
const newDoc = validateDoc(client.index); const newDoc = validateDoc(client.index);
expect(newDoc.body).to.have.property('status', JOB_STATUS_PENDING); 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);
});
});
});
}); });