change the job scontructor, update tests

This commit is contained in:
2016-04-22 17:03:01 -07:00
parent 3318c775c0
commit 730ec68bfa
3 changed files with 31 additions and 29 deletions

View File

@@ -7,33 +7,32 @@ import * as elasticsearchMock from '../fixtures/elasticsearch';
import { JOB_STATUS_PENDING } from '../../lib/helpers/constants';
describe('Job Class', function () {
let mockQueue;
let client;
let index;
beforeEach(function () {
mockQueue = {
index: 'test',
client: new elasticsearchMock.Client(),
};
index = 'test';
client = new elasticsearchMock.Client();
});
it('should be an event emitter', function () {
const job = new Job(mockQueue, 'test', {});
const job = new Job(client, index, 'test', {});
expect(job).to.be.an(events.EventEmitter);
});
describe('invalid construction', function () {
it('should throw with a missing type', function () {
const init = () => new Job(mockQueue);
const init = () => new Job(client, index);
expect(init).to.throwException(/type.+string/i);
});
it('should throw with an invalid type', function () {
const init = () => new Job(mockQueue, { 'not a string': true });
const init = () => new Job(client, index, { 'not a string': true });
expect(init).to.throwException(/type.+string/i);
});
it('should throw with an invalid payload', function () {
const init = () => new Job(mockQueue, 'type1', [1, 2, 3]);
const init = () => new Job(client, index, 'type1', [1, 2, 3]);
expect(init).to.throwException(/plain.+object/i);
});
});
@@ -53,41 +52,41 @@ describe('Job Class', function () {
type = 'type1';
payload = { id: '123' };
timeout = 4567;
sinon.spy(mockQueue.client, 'index');
sinon.spy(client, 'index');
});
it('should index the payload', function () {
new Job(mockQueue, type, payload);
const newDoc = validateDoc(mockQueue.client.index);
expect(newDoc).to.have.property('index', mockQueue.index);
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);
});
it('should index timeout value from options', function () {
new Job(mockQueue, type, payload, timeout);
const newDoc = validateDoc(mockQueue.client.index);
new Job(client, index, type, payload, timeout);
const newDoc = validateDoc(client.index);
expect(newDoc.body).to.have.property('timeout', timeout);
});
it('should set event times', function () {
new Job(mockQueue, type, payload, timeout);
const newDoc = validateDoc(mockQueue.client.index);
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');
});
it('should set attempt count', function () {
new Job(mockQueue, type, payload, timeout);
const newDoc = validateDoc(mockQueue.client.index);
new Job(client, index, type, payload, timeout);
const newDoc = validateDoc(client.index);
expect(newDoc.body).to.have.property('attempts');
});
it('should set status as pending', function () {
new Job(mockQueue, type, payload, timeout);
const newDoc = validateDoc(mockQueue.client.index);
new Job(client, index, type, payload, timeout);
const newDoc = validateDoc(client.index);
expect(newDoc.body).to.have.property('status', JOB_STATUS_PENDING);
});
});