I'm new to the world of BDD, but I'd like to get a grip of it as soon as possible. It's one of my first tests:
Fluent.js
'use strict';
var Fluent = module.exports = function Fluent(context, chain) {
if (this.constructor === Fluent) {
throw new Error("Can't initialize an abstract class!");
}
this.context = context;
this.chain = chain;
//Detect if we want to create new chainRoot or inherit it from parent
if (this.chain) {
this.chainRoot = this.chain.chainRoot;
}
};
Fluent.prototype.end = function () {
return this.chain;
};
fluent_test.js
'use strict';
var
_ = require('lodash'),
expect = require('expect.js')
;
var
Fluent = require('../../lib/jawan/Fluent')
;
var FooFluent = function FooFluent() {
Fluent.apply(this, arguments);
};
FooFluent.prototype = _.create(Fluent.prototype, { 'constructor': FooFluent });
var RootFluent = function FooFluent(context, chain) {
Fluent.apply(this, arguments);
this.chainRoot = this;
};
RootFluent.prototype = _.create(Fluent.prototype, { 'constructor': RootFluent });
var DummyObject = function DummyObject() {
this.person = 'Hank';
};
describe('abstract fluent interface', function () {
it('should refuse to be created', function (done) {
expect(function() { new Fluent(new DummyObject()); }).to.throwError();
done();
});
});
describe('concrete fluent interface', function () {
beforeEach(function () {
this.dummyInstance = new DummyObject();
this.foo = new RootFluent(this.dummyInstance);
this.bar = new FooFluent(this.dummyInstance, this.foo);
this.fizz = new FooFluent(this.dummyInstance, this.bar);
});
it('should set its context', function (done) {
expect(this.foo.context).to.be.a(DummyObject);
done();
});
it('should chain properly', function (done) {
expect(this.bar.chain).to.be(this.foo);
done();
});
it('should return its parent correctlz', function (done) {
expect(this.bar.end()).to.be(this.foo);
done();
});
it('should return its chain root properly', function (done) {
expect(this.fizz.chainRoot).to.be(this.foo);
done();
});
});
I'm testing for node only, browser-specific stuff is not my concern.