diff --git a/test/fixtures/job.js b/test/fixtures/job.js new file mode 100644 index 0000000..20c2bb3 --- /dev/null +++ b/test/fixtures/job.js @@ -0,0 +1,17 @@ +import events from 'events'; + +export default class Job extends events.EventEmitter { + constructor(queue, index, type, payload, options = {}) { + super(); + + this.queue = queue; + this.index = index; + this.jobType = type; + this.payload = payload; + this.options = options; + } + + getProp(name) { + return this[name]; + } +} \ No newline at end of file diff --git a/test/fixtures/worker.js b/test/fixtures/worker.js new file mode 100644 index 0000000..a7599bc --- /dev/null +++ b/test/fixtures/worker.js @@ -0,0 +1,16 @@ +import events from 'events'; + +export default class Worker extends events.EventEmitter { + constructor(queue, type, workerFn, opts = {}) { + super(); + + this.queue = queue; + this.type = type; + this.workerFn = workerFn; + this.options = opts; + } + + getProp(name) { + return this[name]; + } +} \ No newline at end of file diff --git a/test/src/index.js b/test/src/index.js index ed99984..1c872e0 100644 --- a/test/src/index.js +++ b/test/src/index.js @@ -1,9 +1,17 @@ import events from 'events'; import expect from 'expect.js'; import sinon from 'sinon'; +import proxyquire from 'proxyquire'; import { noop, times } from 'lodash'; +import constants from '../../lib/constants'; import elasticsearchMock from '../fixtures/elasticsearch'; -import Esqueue from '../../lib/index'; +import jobMock from '../fixtures/job'; +import workerMock from '../fixtures/worker'; + +const Esqueue = proxyquire.noPreserveCache()('../../lib/index', { + './job.js': jobMock, + './worker.js': workerMock, +}); describe('Esqueue class', function () { let client; @@ -48,6 +56,47 @@ describe('Esqueue class', function () { }); }); + describe('Adding jobs', function () { + let indexName; + let jobType; + let payload; + let queue; + + beforeEach(function () { + indexName = 'esqueue-index'; + jobType = 'test-test'; + payload = { payload: true }; + queue = new Esqueue(indexName, { client }); + }); + + it('should pass queue instance, index name, type and payload', function () { + const job = queue.addJob(jobType, payload); + expect(job.getProp('queue')).to.equal(queue); + expect(job.getProp('index')).to.match(new RegExp(indexName)); + expect(job.getProp('jobType')).to.equal(jobType); + expect(job.getProp('payload')).to.equal(payload); + }); + + it('should pass default settings', function () { + const job = queue.addJob(jobType, payload); + const options = job.getProp('options'); + expect(options).to.have.property('timeout', constants.DEFAULT_SETTING_TIMEOUT); + expect(options).to.have.property('doctype', constants.DEFAULT_SETTING_DOCTYPE); + }); + + it('should pass queue index settings', function () { + const indexSettings = { + index: { + number_of_shards: 1 + } + }; + + queue = new Esqueue(indexName, { client, indexSettings }); + const job = queue.addJob(jobType, payload); + expect(job.getProp('options')).to.have.property('indexSettings', indexSettings); + }); + }); + describe('Registering workers', function () { it('should keep track of workers', function () { const queue = new Esqueue('esqueue', { client });