Skip to content
This repository has been archived by the owner on Mar 24, 2021. It is now read-only.

Commit

Permalink
Add namespace helper.
Browse files Browse the repository at this point in the history
  • Loading branch information
Gareth Rogers committed Mar 11, 2013
1 parent 5d75036 commit 5ce6404
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
17 changes: 17 additions & 0 deletions script/namespace.js
@@ -0,0 +1,17 @@
var GOVUK = GOVUK || {};
GOVUK.performance = {};

GOVUK.performance.addToNamespace = function (name, obj) {
if (GOVUK.performance[name] === undefined) {
GOVUK.performance[name] = obj;
}
else {
throw new Error("There is already a key: '" + name + "' in the namespace.");
}
};


GOVUK.performance.close = function () {
GOVUK.performance.addToNamespace = undefined;
GOVUK.performance.close = undefined;
};
22 changes: 22 additions & 0 deletions tests/unit/namespaceSpec.js
@@ -0,0 +1,22 @@
describe("namespacing", function () {

it("should add to the namespace", function () {
GOVUK.performance.addToNamespace("foo", function () { return "bar"; });
expect(GOVUK.performance.foo()).toBe("bar");
});


it("should throw an exception if the name already exists and not add to the namespace", function () {
expect(function () { GOVUK.performance.addToNamespace("foo", "blah blah blah"); })
.toThrow(new Error("There is already a key: 'foo' in the namespace."));
expect(GOVUK.performance.foo()).toBe("bar");
});


it("should remove namespace helpers when closed", function () {
GOVUK.performance.close();
expect(function () { GOVUK.performance.addToNamespace("test", {}); }).toThrow("Property 'addToNamespace' of object #<Object> is not a function");
expect(function () { GOVUK.performance.close(); }).toThrow("Property 'close' of object #<Object> is not a function");
});

});
2 changes: 2 additions & 0 deletions tests/unit/unit_tests.html
Expand Up @@ -12,10 +12,12 @@
<script type="text/javascript" src="../lib/jasmine-1.3.1/jasmine-html.js"></script>

<!-- include source files here... -->
<script type="text/javascript" src="../../script/namespace.js"></script>
<script type="text/javascript" src="../../script/lightGate.js"></script>
<script type="text/javascript" src="../../script/cookieUtils.js"></script>

<!-- include spec files here... -->
<script type="text/javascript" src="namespaceSpec.js"></script>
<script type="text/javascript" src="../stubAnalyticsService.js"></script>
<script type="text/javascript" src="lightGateSpec.js"></script>
<script type="text/javascript" src="cookieUtilsSpec.js"></script>
Expand Down

0 comments on commit 5ce6404

Please sign in to comment.