Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/twbs/rewire.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohannes <mail@johannesewald.de>2012-06-12 01:29:10 +0400
committerJohannes <mail@johannesewald.de>2012-06-12 01:29:10 +0400
commit0af1dcb6b2ccbc7ec0ae757549688e666cf569a7 (patch)
tree4b3854b937ff1f05bf8677c8fc1547fa733e6feb /test/__set__.test.js
parent75f6a7765a09344dab54a4a2ca99ac12f547a75d (diff)
- changed APIv0.2.0
- introduced __set__ and __get__ to rewired modules
Diffstat (limited to 'test/__set__.test.js')
-rw-r--r--test/__set__.test.js93
1 files changed, 93 insertions, 0 deletions
diff --git a/test/__set__.test.js b/test/__set__.test.js
new file mode 100644
index 0000000..e7950cb
--- /dev/null
+++ b/test/__set__.test.js
@@ -0,0 +1,93 @@
+"use strict"; // run code in ES5 strict mode
+
+var expect = require("expect.js"),
+ __set__ = require("../lib/__set__.js"),
+ vm = require("vm"),
+
+ expectReferenceError = expectError(ReferenceError),
+ expectTypeError = expectError(TypeError);
+
+function expectError(ErrConstructor) {
+ return function expectReferenceError(err) {
+ expect(err.constructor.name).to.be(ErrConstructor.name);
+ };
+}
+
+describe("__set__", function () {
+ var moduleFake;
+
+ beforeEach(function () {
+ moduleFake = {
+ myNumber: 0, // copy by value
+ myObj: {} // copy by reference
+ };
+
+ vm.runInNewContext(
+ "__set__ = " + __set__.toString() + "; " +
+ "getNumber = function () { return myNumber; }; " +
+ "getObj = function () { return myObj; }; ",
+ moduleFake
+ );
+ });
+ it("should set the new number when calling with varName, varValue", function () {
+ expect(moduleFake.getNumber()).to.be(0);
+ moduleFake.__set__("myNumber", 2);
+ expect(moduleFake.getNumber()).to.be(2);
+ });
+ it("should set the new object when calling with varName, varValue", function () {
+ var newObj = { hello: "hello" };
+
+ expect(moduleFake.getObj()).to.eql({});
+ moduleFake.__set__("myObj", newObj);
+ expect(moduleFake.getObj()).to.be(newObj);
+ });
+ it("should set the new number and the new obj when calling with an env-obj", function () {
+ var newObj = { hello: "hello" };
+
+ expect(moduleFake.getNumber()).to.be(0);
+ expect(moduleFake.getObj()).to.eql({});
+ moduleFake.__set__({
+ myNumber: 2,
+ myObj: newObj
+ });
+ expect(moduleFake.getNumber()).to.be(2);
+ expect(moduleFake.getObj()).to.be(newObj);
+ });
+ it("should return undefined", function () {
+ expect(moduleFake.__set__("myNumber", 4)).to.be(undefined);
+ });
+ it("should throw a ReferenceError when trying to set non-existing vars", function () {
+ expect(function () {
+ moduleFake.__set__("notExisting", 3);
+ }).to.throwException();
+ expect(function () {
+ moduleFake.__set__({
+ notExisting: "bla",
+ notExistingAsWell: "blabla"
+ });
+ }).to.throwException(expectReferenceError);
+ });
+ it("should throw a TypeError when passing misfitting params", function () {
+ expect(function () {
+ moduleFake.__set__();
+ }).to.throwException(expectTypeError);
+ expect(function () {
+ moduleFake.__set__(undefined);
+ }).to.throwException(expectTypeError);
+ expect(function () {
+ moduleFake.__set__(null);
+ }).to.throwException(expectTypeError);
+ expect(function () {
+ moduleFake.__set__(true);
+ }).to.throwException(expectTypeError);
+ expect(function () {
+ moduleFake.__set__(2);
+ }).to.throwException(expectTypeError);
+ expect(function () {
+ moduleFake.__set__("");
+ }).to.throwException(expectTypeError);
+ expect(function () {
+ moduleFake.__set__(function () {});
+ }).to.throwException(expectTypeError);
+ });
+}); \ No newline at end of file