diff --git a/src/job.js b/src/job.js index e3117c6..54d1096 100644 --- a/src/job.js +++ b/src/job.js @@ -1,6 +1,6 @@ import events from 'events'; import { isPlainObject } from 'lodash'; -import { omit, values } from 'lodash'; +import { omit } from 'lodash'; import logger from './helpers/logger'; import { jobStatuses } from './helpers/constants'; import createIndex from './helpers/create_index'; @@ -80,7 +80,7 @@ export default class Job extends events.EventEmitter { type: this.type, payload: this.payload, timeout: this.timeout, - maxAttempts: this.maxAttempts, + max_attempts: this.maxAttempts, priority: this.priority, }, omit(this.document, ['version'])); } diff --git a/test/src/job.js b/test/src/job.js index dd98c25..21e4732 100644 --- a/test/src/job.js +++ b/test/src/job.js @@ -148,11 +148,6 @@ describe('Job Class', function () { beforeEach(function () { type = 'type2'; payload = { id: '123' }; - options = { - timeout: 4567, - max_attempts: 9, - }; - sinon.spy(client, 'index'); }); 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'); + }); + }); + }); + });