update toJSON, add tests, remove unused var

This commit is contained in:
2016-04-25 15:16:00 -07:00
parent 4d5b08c3c7
commit 58a0cf328f
2 changed files with 35 additions and 7 deletions

View File

@@ -1,6 +1,6 @@
import events from 'events'; import events from 'events';
import { isPlainObject } from 'lodash'; import { isPlainObject } from 'lodash';
import { omit, values } from 'lodash'; import { omit } from 'lodash';
import logger from './helpers/logger'; import logger from './helpers/logger';
import { jobStatuses } from './helpers/constants'; import { jobStatuses } from './helpers/constants';
import createIndex from './helpers/create_index'; import createIndex from './helpers/create_index';
@@ -80,7 +80,7 @@ export default class Job extends events.EventEmitter {
type: this.type, type: this.type,
payload: this.payload, payload: this.payload,
timeout: this.timeout, timeout: this.timeout,
maxAttempts: this.maxAttempts, max_attempts: this.maxAttempts,
priority: this.priority, priority: this.priority,
}, omit(this.document, ['version'])); }, omit(this.document, ['version']));
} }

View File

@@ -148,11 +148,6 @@ describe('Job Class', function () {
beforeEach(function () { beforeEach(function () {
type = 'type2'; type = 'type2';
payload = { id: '123' }; payload = { id: '123' };
options = {
timeout: 4567,
max_attempts: 9,
};
sinon.spy(client, 'index');
}); });
it('should return the job document', function () { it('should return the job document', function () {
@@ -171,4 +166,37 @@ describe('Job Class', function () {
}); });
}); });
describe('toJSON method', function () {
beforeEach(function () {
type = 'type2';
payload = { id: '123' };
options = {
timeout: 4567,
max_attempts: 9,
priority: 8,
};
});
it('should return false if no elasticsearch document', function () {
const job = new Job(client, index, 'test', {});
// same tick, document promise has not resolved
expect(job.toJSON()).to.equal(false);
});
it('should return the static information about the job', function () {
const job = new Job(client, index, type, payload, options);
// toJSON is sync, but only works once the document has been written
return job.ready.then(() => {
const doc = job.toJSON();
expect(doc).to.have.property('index', index);
expect(doc).to.have.property('type', type);
expect(doc).to.have.property('timeout', options.timeout);
expect(doc).to.have.property('max_attempts', options.max_attempts);
expect(doc).to.have.property('priority', options.priority);
expect(doc).to.have.property('id');
expect(doc).to.not.have.property('version');
});
});
});
}); });