Skip to content

Commit

Permalink
Merge pull request #561 from patbenatar/feature/test-mode
Browse files Browse the repository at this point in the history
Adds a test mode for convenience in test suites
  • Loading branch information
behrad committed Apr 22, 2015
2 parents 47fd6dd + 25039c3 commit 42248a5
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 0 deletions.
30 changes: 30 additions & 0 deletions Readme.md
Expand Up @@ -805,6 +805,36 @@ app.use(kue.app);
app.listen(3000);
```
## Testing
Enable test mode to push all jobs into a `jobs` array. Make assertions against
the jobs in that array to ensure code under test is correctly enqueuing jobs.
```js
queue = require('kue').createQueue();

before(function() {
queue.testMode.enter();
});

afterEach(function() {
queue.testMode.clear();
});

after(function() {
queue.testMode.exit()
});

it('does something cool', function() {
queue.createJob('myJob', { foo: 'bar' }).save();
queue.createJob('anotherJob', { baz: 'bip' }).save();

expect(queue.testMode.jobs.length).to.equal(2);
expect(queue.testMode.jobs[0].type).to.equal('myJob');
expect(queue.testMode.jobs[0].data).to.eql({ foo: 'bar' });
});
```
## Screencasts
- [Introduction](http://www.screenr.com/oyNs) to Kue
Expand Down
7 changes: 7 additions & 0 deletions lib/kue.js
Expand Up @@ -606,3 +606,10 @@ Queue.prototype.delayedCount = function (type, fn) {
}
return this.cardByType(type, 'delayed', fn);
};

/**
* Test mode for convenience in test suites
* @api public
*/

Queue.prototype.testMode = require('./queue/test_mode');
52 changes: 52 additions & 0 deletions lib/queue/test_mode.js
@@ -0,0 +1,52 @@
var Job = require('./job'),
_ = require('lodash');

var originalJobSave = Job.prototype.save,
originalJobUpdate = Job.prototype.update,
jobs;

function testJobSave(fn) {
this.id = _.uniqueId;
jobs.push(this);
if(_.isFunction(fn)) fn();
};

function testJobUpdate(fn) {
if(_.isFunction(fn)) fn();
};

/**
* Array of jobs added to the queue
* @api public
*/

module.exports.jobs = jobs = [];

/**
* Enable test mode.
* @api public
*/

module.exports.enter = function() {
Job.prototype.save = testJobSave;
Job.prototype.update = testJobUpdate;
};

/**
* Disable test mode.
* @api public
*/

module.exports.exit = function() {
Job.prototype.save = originalJobSave;
Job.prototype.update = originalJobUpdate;
};

/**
* Clear the array of queued jobs
* @api public
*/

module.exports.clear = function() {
jobs.length = 0;
};
59 changes: 59 additions & 0 deletions test/test_mode.js
@@ -0,0 +1,59 @@
var kue = require('../'),
_ = require('lodash'),
queue = kue.createQueue();

describe('Test Mode', function() {
context('when enabled', function() {
before(function() {
queue.testMode.enter();
});

afterEach(function() {
queue.testMode.clear();
});

it('adds jobs to an array in memory', function() {
queue.createJob('myJob', { foo: 'bar' }).save();

var jobs = queue.testMode.jobs;
expect(jobs.length).to.equal(1);

var job = _.last(jobs);
expect(job.type).to.equal('myJob');
expect(job.data).to.eql({ foo: 'bar' });
});


describe('#clear', function() {
it('resets the list of jobs', function() {
queue.createJob('myJob', { foo: 'bar' }).save();
queue.testMode.clear();

var jobs = queue.testMode.jobs;
expect(jobs.length).to.equal(0);
});
});
});

context('when disabled', function() {
before(function() {
// Simulate entering and exiting test mode to ensure
// state is restored correctly.
queue.testMode.enter();
queue.testMode.exit();
});

it('processes jobs regularly', function(done) {
queue.createJob('myJob', { foo: 'bar' }).save();

var jobs = queue.testMode.jobs;
expect(jobs.length).to.equal(0);

queue.process('myJob', function (job, jdone) {
expect(job.data).to.eql({ foo: 'bar' });
jdone();
done();
});
});
});
});

0 comments on commit 42248a5

Please sign in to comment.