Skip to content

Commit

Permalink
Implementing unregister method and corresponding tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasperezv committed May 18, 2016
1 parent cfd9d17 commit 9b03347
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 1 deletion.
10 changes: 10 additions & 0 deletions dist/modulejs.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,12 +259,22 @@ return /******/ (function(modules) { // webpackBootstrap
return out;
};

// Unregisters a module for `id: String`
var unregister = function unregister(id) {
// check arguments
assert(isString(id), 'id must be string: ' + id);
assert(has(definitions, id), 'id already defined: ' + id);

delete definitions[id];
};

return {
create: create,
define: define,
log: log,
require: require,
state: state,
unregister: unregister,
_private: {
assert: assert,
contains: contains,
Expand Down
2 changes: 1 addition & 1 deletion dist/modulejs.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions lib/modulejs.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,12 +192,22 @@ const create = () => {
return out;
};

// Unregisters a module for `id: String`
const unregister = id => {
// check arguments
assert(isString(id), 'id must be string: ' + id);
assert(has(definitions, id), 'id already defined: ' + id);

delete definitions[id];
};

return {
create,
define,
log,
require,
state,
unregister,
_private: {
assert,
contains,
Expand Down
1 change: 1 addition & 0 deletions test/sub/modulejs.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const PROPS = [
'log',
'require',
'state',
'unregister',
'_private'
];

Expand Down
44 changes: 44 additions & 0 deletions test/sub/unregister.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const {test, assert, insp} = require('scar');
const modulejs = require('../loader').modulejs;

test('modulejs.unregister is function', () => {
assert.equal(typeof modulejs.unregister, 'function');
});

test('modulejs.unregister() throws', () => {
const modjs = modulejs.create();
assert.throws(() => modjs.unregister(), /id must be string/);
});

test('modulejs.unregister() throws if non-string id', () => {
const modjs = modulejs.create();
assert.throws(() => modjs.unregister(true), /id must be string/);
});

test('modulejs.unregister() throws if undefined module', () => {
const modjs = modulejs.create();
assert.throws(() => modjs.unregister('a'), /id already defined/);
});

[
undefined,
null,
true,
false,
0,
1,
[],
{},
''
].forEach(x => {
test(`modulejs.unregister(id, arr, ${insp(x)}) -> .define(id, arr, function () { return ${insp(x)}; })`, () => {
const modjs = modulejs.create();
const defs = modjs._private.definitions;
const id = 'a';
assert.equal(modjs.define(id, x), undefined);

modjs.unregister(id);
assert.equal(typeof defs[id], 'undefined');
assert.throws(() => modjs.require('a'), /id not defined/);
});
});
1 change: 1 addition & 0 deletions test/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ require('./sub/require');
require('./sub/resolve');
require('./sub/state');
require('./sub/uniq');
require('./sub/unregister');
test.cli();

0 comments on commit 9b03347

Please sign in to comment.