mirror of
https://github.com/gundb/panic-server.git
synced 2026-04-15 03:00:16 -04:00
Add client Context constructor. All tests passing.
Added module for the "this" context that test runners will need. The constructor handles timeout settings, "done" callbacks, "env" variables (and merges), as well as basic input validation. It'll be used during tests as an interface to the exterior controls, like "done" events and iterables/progress reporters.
This commit is contained in:
24
client/framework/Context.js
Normal file
24
client/framework/Context.js
Normal file
@@ -0,0 +1,24 @@
|
||||
/*jslint node: true*/
|
||||
'use strict';
|
||||
|
||||
var assign = require('object-assign-deep');
|
||||
|
||||
function Context(test) {
|
||||
this.env = {};
|
||||
if (test instanceof Object) {
|
||||
assign(this.env, test.env);
|
||||
if (typeof test.timeout === 'number') {
|
||||
this.timeout = test.timeout || this.timeout;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Context.prototype = {
|
||||
constructor: Context,
|
||||
|
||||
// default timeout
|
||||
timeout: 15000,
|
||||
done: function () {}
|
||||
};
|
||||
|
||||
module.exports = Context;
|
||||
@@ -1,4 +1,9 @@
|
||||
/*jslint node: true*/
|
||||
'use strict';
|
||||
|
||||
module.exports = function () {};
|
||||
var Context = require('./Context');
|
||||
|
||||
module.exports = function (test) {
|
||||
var ctx = new Context(test);
|
||||
test.cb.call(ctx, ctx, ctx.done);
|
||||
};
|
||||
|
||||
57
spec/client/framework/Context.spec.js
Normal file
57
spec/client/framework/Context.spec.js
Normal file
@@ -0,0 +1,57 @@
|
||||
/*globals jasmine, describe, it, expect*/
|
||||
/*jslint node: true*/
|
||||
'use strict';
|
||||
|
||||
var Context = require('../../../client/framework/Context');
|
||||
|
||||
describe('The client context constructor', function () {
|
||||
it('should be a function', function () {
|
||||
expect(Context).toEqual(jasmine.any(Function));
|
||||
});
|
||||
|
||||
it('should always create an "env" property', function () {
|
||||
var context = new Context();
|
||||
expect(context.env).toEqual(jasmine.any(Object));
|
||||
});
|
||||
|
||||
it('should list "Context" as the constructor', function () {
|
||||
expect(Context.prototype.constructor).toBe(Context);
|
||||
});
|
||||
|
||||
it('should merge the "env" property with arg0', function () {
|
||||
var context = new Context({
|
||||
env: { success: true }
|
||||
});
|
||||
expect(context.env.success).toBe(true);
|
||||
});
|
||||
|
||||
it('should have a "done" method', function () {
|
||||
var context = new Context();
|
||||
expect(context.done).toEqual(jasmine.any(Function));
|
||||
});
|
||||
|
||||
it('should have a default timeout', function () {
|
||||
var timeout = Context.prototype.timeout;
|
||||
expect(timeout).toEqual(jasmine.any(Number));
|
||||
});
|
||||
|
||||
it('should watch for a timeout property', function () {
|
||||
var context = new Context({
|
||||
timeout: 42
|
||||
});
|
||||
expect(context.timeout).toBe(42);
|
||||
});
|
||||
|
||||
it('should validate timeouts', function () {
|
||||
var context = new Context({
|
||||
timeout: 'invalid'
|
||||
});
|
||||
expect(context.timeout).toEqual(jasmine.any(Number));
|
||||
});
|
||||
|
||||
it('should protect against null test inputs', function () {
|
||||
expect(function () {
|
||||
return new Context(null);
|
||||
}).not.toThrow();
|
||||
});
|
||||
});
|
||||
@@ -3,9 +3,51 @@
|
||||
'use strict';
|
||||
|
||||
var runner = require('../../../client/framework/runner');
|
||||
var Context = require('../../../client/framework/Context');
|
||||
|
||||
function run(prop, val) {
|
||||
if (!val) {
|
||||
val = prop;
|
||||
prop = 'cb';
|
||||
}
|
||||
var obj = {};
|
||||
obj[prop] = val;
|
||||
return runner(obj);
|
||||
}
|
||||
|
||||
describe('The client test runner', function () {
|
||||
it('should be a function', function () {
|
||||
expect(runner).toEqual(jasmine.any(Function));
|
||||
});
|
||||
|
||||
it('should take a test descriptor and invoke the cb', function (done) {
|
||||
run(done);
|
||||
});
|
||||
|
||||
it('should expose the "env" in the "this" context', function () {
|
||||
runner({
|
||||
env: { success: true },
|
||||
cb: function () {
|
||||
expect(this.env.success).toBe(true);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it('should use a context instance as the "this" value', function () {
|
||||
run(function () {
|
||||
expect(this).toEqual(jasmine.any(Context));
|
||||
});
|
||||
});
|
||||
|
||||
it('should send the context as the first cb param', function () {
|
||||
run(function (ctx) {
|
||||
expect(ctx).toEqual(jasmine.any(Context));
|
||||
});
|
||||
});
|
||||
|
||||
it('should pass the "done" function in as the second param', function () {
|
||||
run(function (ctx, done) {
|
||||
expect(done).toEqual(jasmine.any(Function));
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user