add job test for index creation
also make other tests properly async
This commit is contained in:
@@ -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 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 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 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 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 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);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user