Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #561 from patbenatar/feature/test-mode
Adds a test mode for convenience in test suites
- Loading branch information
Showing
4 changed files
with
148 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
}); | ||
}); | ||
}); | ||
}); |