cylon/spec/lib/utils.spec.js

218 lines
6.0 KiB
JavaScript
Raw Normal View History

2014-03-11 06:26:19 +08:00
"use strict";
var utils = lib("utils");
2014-02-28 06:52:25 +08:00
describe("Utils", function() {
describe("#makeUnique", function() {
it("returns the original name if it's not a conflict", function() {
var res = utils.makeUnique("hello", ["world"]);
expect(res).to.be.eql("hello");
});
it("generates a unique name if it does collide", function() {
var res = utils.makeUnique("hello", ["hello"]);
expect(res).to.be.eql("hello-1");
});
it("will ignore existing duplicates", function() {
var res = utils.makeUnique("hello", ["hello", "hello-1", "hello-2"]);
expect(res).to.be.eql("hello-3");
});
});
2014-03-11 06:26:19 +08:00
describe("#every", function() {
2014-07-05 00:49:29 +08:00
beforeEach(function() {
2014-03-11 06:26:19 +08:00
this.clock = sinon.useFakeTimers();
});
2014-07-05 00:49:29 +08:00
afterEach(function() {
2014-03-11 06:26:19 +08:00
this.clock.restore();
2014-02-28 06:52:25 +08:00
});
2014-12-16 01:37:52 +08:00
it("sets a function to be called when an interval passes", function() {
2014-03-11 06:26:19 +08:00
var func = spy();
utils.every(10, func);
this.clock.tick(25);
expect(func).to.be.calledTwice;
2014-02-28 06:52:25 +08:00
});
2014-03-11 06:26:19 +08:00
});
2014-02-28 06:52:25 +08:00
2014-03-11 06:26:19 +08:00
describe("#after", function() {
2014-07-05 00:49:29 +08:00
beforeEach(function() {
2014-03-11 06:26:19 +08:00
this.clock = sinon.useFakeTimers();
2014-02-28 06:52:25 +08:00
});
2014-07-05 00:49:29 +08:00
afterEach(function() {
2014-03-11 06:26:19 +08:00
this.clock.restore();
2013-10-25 05:25:42 +08:00
});
2014-02-28 06:52:25 +08:00
2014-12-16 01:37:52 +08:00
it("sets a function to be called after an interval passes", function() {
2014-03-11 06:26:19 +08:00
var func = spy();
utils.after(10, func);
this.clock.tick(15);
expect(func).to.be.called;
});
});
describe("constantly", function() {
2014-07-05 00:49:29 +08:00
beforeEach(function() {
2014-12-15 07:21:28 +08:00
stub(global, "every").returns(0);
2013-10-29 03:07:56 +08:00
});
2014-02-28 06:52:25 +08:00
2014-07-05 00:49:29 +08:00
afterEach(function() {
2014-03-11 06:26:19 +08:00
global.every.restore();
});
it("schedules a task to run continuously with #every", function() {
var func = function() {};
utils.constantly(func);
expect(global.every).to.be.calledWith(0, func);
});
});
2015-09-02 02:30:55 +08:00
describe("#finish", function() {
beforeEach(function() {
this.clock = sinon.useFakeTimers();
});
afterEach(function() {
this.clock.restore();
});
it("stops calling an interval function", function() {
var func = spy();
var interval = utils.every(10, func);
this.clock.tick(15);
utils.finish(interval);
this.clock.tick(15);
expect(func).to.be.calledOnce;
});
});
2014-03-11 06:26:19 +08:00
describe("#subclass", function() {
2014-12-15 07:21:28 +08:00
var BaseClass = function BaseClass(opts) {
this.greeting = opts.greeting;
};
2014-03-11 06:26:19 +08:00
2014-12-15 07:21:28 +08:00
BaseClass.prototype.sayHi = function() {
return "Hi!";
};
2014-03-11 06:26:19 +08:00
2014-12-15 07:21:28 +08:00
var SubClass = function SubClass() {
SubClass.__super__.constructor.apply(this, arguments);
};
2014-03-11 06:26:19 +08:00
2014-12-15 07:21:28 +08:00
utils.subclass(SubClass, BaseClass);
2014-03-11 06:26:19 +08:00
it("adds inheritance to Javascript classes", function() {
var sub = new SubClass({greeting: "Hello World"});
expect(sub.greeting).to.be.eql("Hello World");
expect(sub.sayHi()).to.be.eql("Hi!");
});
2013-10-25 05:25:42 +08:00
});
2014-02-28 06:52:25 +08:00
describe("#proxyFunctionsToObject", function() {
2014-12-15 07:21:28 +08:00
var methods = ["asString", "toString", "returnString"];
2014-02-28 09:39:02 +08:00
var ProxyClass = function ProxyClass() {};
2014-02-28 06:52:25 +08:00
ProxyClass.prototype.asString = function() {
return "[object ProxyClass]";
};
2014-02-28 06:52:25 +08:00
ProxyClass.prototype.toString = function() {
return "[object ProxyClass]";
};
2014-02-28 06:52:25 +08:00
ProxyClass.prototype.returnString = function(string) {
return string;
};
2014-02-28 06:52:25 +08:00
var TestClass = function TestClass() {
this.testInstance = new ProxyClass();
utils.proxyFunctionsToObject(methods, this.testInstance, this, true);
};
2014-02-28 06:52:25 +08:00
2014-03-11 06:26:19 +08:00
var testclass = new TestClass();
2014-12-15 07:21:28 +08:00
it("can alias methods", function() {
2014-03-11 06:26:19 +08:00
expect(testclass.asString()).to.be.eql("[object ProxyClass]");
2014-02-28 06:52:25 +08:00
});
2014-12-15 07:21:28 +08:00
it("can alias existing methods if forced to", function() {
2014-03-11 06:26:19 +08:00
expect(testclass.toString()).to.be.eql("[object ProxyClass]");
2014-02-28 06:52:25 +08:00
});
2014-12-15 07:21:28 +08:00
it("can alias methods with arguments", function() {
expect(testclass.returnString).to.be.a("function");
2014-02-28 06:52:25 +08:00
});
});
describe("#fetch", function() {
var fetch = utils.fetch,
2015-04-15 12:49:12 +08:00
obj = { property: "hello world", false: false, null: null };
context("if the property exists on the object", function() {
it("returns the value", function() {
2014-12-15 07:21:28 +08:00
expect(fetch(obj, "property")).to.be.eql("hello world");
expect(fetch(obj, "false")).to.be.eql(false);
expect(fetch(obj, "null")).to.be.eql(null);
});
});
context("if the property doesn't exist on the object", function() {
context("and no fallback value has been provided", function() {
it("throws an Error", function() {
var fn = function() { return fetch(obj, "notaproperty"); };
2014-12-15 07:21:28 +08:00
expect(fn).to.throw(Error, "key not found: \"notaproperty\"");
});
});
context("and a fallback value has been provided", function() {
2014-12-15 07:21:28 +08:00
it("returns the fallback value", function() {
expect(fetch(obj, "notakey", "fallback")).to.be.eql("fallback");
});
});
context("and a fallback function has been provided", function() {
context("if the function has no return value", function() {
it("throws an Error", function() {
2014-12-15 07:21:28 +08:00
var fn = function() { fetch(obj, "notakey", function() {}); },
str = "no return value from provided fallback function";
expect(fn).to.throw(Error, str);
});
});
context("if the function returns a value", function() {
it("returns the value returned by the fallback function", function() {
2014-12-15 07:21:28 +08:00
var fn = function(key) { return "Couldn't find " + key; },
value = "Couldn't find notakey";
2014-12-15 07:21:28 +08:00
expect(fetch(obj, "notakey", fn)).to.be.eql(value);
});
});
});
});
});
describe("#classCallCheck", function() {
it("checks that an object is an instance of a constructor", function() {
var fn = function(instance, Constructor) {
return utils.classCallCheck.bind(null, instance, Constructor);
};
function Class() {
utils.classCallCheck(this, Class);
}
expect(fn([], Array)).to.not.throw;
expect(fn([], Number)).to.throw(TypeError);
expect(Class).to.throw(TypeError);
expect(function() { return new Class(); }).not.to.throw(TypeError);
});
});
2014-02-28 06:52:25 +08:00
});