defuse
Version:
A poor man's module namespacing solution for the browser
128 lines (106 loc) • 3.6 kB
JavaScript
describe("defuse", function() {
var assert = require('assert'),
defuse = require('./');
function objIsSubset(subset, superset) {
return Object
.keys(subset)
.every(function(k) { return subset[k] === superset[k]; });
}
describe(".def", function() {
it("should register the module", function() {
defuse.def('foo.bar', function() { return 'baz'; });
assert.equal(defuse.registry['foo.bar'], null);
assert.equal(defuse.pending['foo.bar'](), 'baz');
});
});
describe(".use", function() {
beforeEach(function() {
defuse.def('foo.bar', function(exp) { exp.baz = 'qux'; });
});
afterEach(function() {
defuse.undef('foo.bar');
});
describe("if the module is not registered", function() {
it("should throw an error", function() {
assert.throws(
function() { defuse.use('larp.larp'); },
/module 'larp.larp' does not exist/);
});
});
describe("if the module has not yet been loaded", function() {
it("should load the module", function() {
assert.strictEqual(defuse.registry['foo.bar'], null);
defuse.use('foo.bar');
assert.deepEqual(defuse.registry['foo.bar'], {baz: 'qux'});
});
it("should return the now loaded module", function() {
assert.deepEqual(defuse.use('foo.bar'), {baz: 'qux'});
});
});
describe("if the module has already been loaded", function() {
it("should retrieve the already loaded module", function() {
defuse.use('foo.bar');
assert.deepEqual(defuse.use('foo.bar'), {baz: 'qux'});
});
});
});
describe(".assignTo", function() {
it("should add defuse's api methods to the given object", function() {
var obj = {};
assert(!objIsSubset(defuse.api, obj));
defuse.assignTo(obj);
assert(objIsSubset(defuse.api, obj));
});
});
describe(".pollute", function() {
afterEach(function() {
defuse.unpollute();
});
it("should add defuse's api methods to the global namespace", function() {
assert(!objIsSubset(defuse.api, global));
defuse.pollute();
assert(objIsSubset(defuse.api, global));
});
});
describe(".unpollute", function() {
beforeEach(function() {
global.def = 'foo';
global.use = 'bar';
});
afterEach(function() {
delete global.def;
delete global.use;
});
it("should restore the overriden global properties", function() {
assert.equal(global.def, 'foo');
assert.equal(global.use, 'bar');
defuse.pollute();
assert.equal(global.def, defuse.def);
assert.equal(global.use, defuse.use);
defuse.unpollute();
assert.equal(global.def, 'foo');
assert.equal(global.use, 'bar');
});
it("should not keep properties that the global object did not have", function() {
assert(!('undef' in global));
defuse.pollute();
assert.equal(global.undef, defuse.undef);
defuse.unpollute();
assert(!('undef' in global));
});
describe("if the global object has not yet been polluted", function() {
it("should throw an error", function() {
assert.throws(
function() { defuse.unpollute(); },
/defuse has not been assigned to the global object/);
});
});
});
describe(".exports", function() {
it("should return a shallow copy of defuse's api", function() {
var exp = defuse.exports();
assert.notEqual(exp, defuse.api);
assert.deepEqual(exp, defuse.api);
});
});
});