2013-10-29 18:38:21 +08:00
|
|
|
describe('Util', function () {
|
2010-09-02 21:14:25 +08:00
|
|
|
|
2013-10-29 18:38:21 +08:00
|
|
|
describe('#extend', function () {
|
2010-09-02 21:14:25 +08:00
|
|
|
var a;
|
2013-02-05 00:31:14 +08:00
|
|
|
|
2013-10-29 18:38:21 +08:00
|
|
|
beforeEach(function () {
|
2010-09-02 21:14:25 +08:00
|
|
|
a = {
|
|
|
|
foo: 5,
|
|
|
|
bar: 'asd'
|
|
|
|
};
|
|
|
|
});
|
2013-02-05 00:31:14 +08:00
|
|
|
|
2013-10-29 18:38:21 +08:00
|
|
|
it('extends the first argument with the properties of the second', function () {
|
2010-09-02 21:14:25 +08:00
|
|
|
L.Util.extend(a, {
|
|
|
|
bar: 7,
|
|
|
|
baz: 3
|
|
|
|
});
|
2013-02-05 00:31:14 +08:00
|
|
|
|
2013-03-02 05:49:20 +08:00
|
|
|
expect(a).to.eql({
|
2010-09-02 21:14:25 +08:00
|
|
|
foo: 5,
|
|
|
|
bar: 7,
|
|
|
|
baz: 3
|
|
|
|
});
|
|
|
|
});
|
2013-02-05 00:31:14 +08:00
|
|
|
|
2013-10-29 18:38:21 +08:00
|
|
|
it('accepts more than 2 arguments', function () {
|
2010-09-02 21:14:25 +08:00
|
|
|
L.Util.extend(a, {bar: 7}, {baz: 3});
|
2013-02-05 00:31:14 +08:00
|
|
|
|
2013-03-02 05:49:20 +08:00
|
|
|
expect(a).to.eql({
|
2010-09-02 21:14:25 +08:00
|
|
|
foo: 5,
|
|
|
|
bar: 7,
|
|
|
|
baz: 3
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2013-10-29 18:38:21 +08:00
|
|
|
describe('#bind', function () {
|
|
|
|
it('returns the given function with the given context', function () {
|
|
|
|
var fn = function () {
|
2010-09-02 21:14:25 +08:00
|
|
|
return this;
|
|
|
|
};
|
2013-02-05 00:31:14 +08:00
|
|
|
|
2015-01-29 01:32:27 +08:00
|
|
|
var fn2 = L.Util.bind(fn, {foo: 'bar'});
|
2013-02-05 00:31:14 +08:00
|
|
|
|
2015-01-29 01:32:27 +08:00
|
|
|
expect(fn2()).to.eql({foo: 'bar'});
|
2010-09-02 21:14:25 +08:00
|
|
|
});
|
2013-02-05 00:31:14 +08:00
|
|
|
|
2013-02-20 04:41:48 +08:00
|
|
|
it('passes additional arguments to the bound function', function () {
|
2013-03-02 05:49:20 +08:00
|
|
|
var fn = sinon.spy(),
|
2013-02-05 00:31:14 +08:00
|
|
|
foo = {},
|
|
|
|
a = {},
|
2013-11-28 00:27:29 +08:00
|
|
|
b = {},
|
|
|
|
c = {};
|
2013-02-05 00:31:14 +08:00
|
|
|
|
|
|
|
var fn2 = L.Util.bind(fn, foo, a, b);
|
|
|
|
|
2013-11-28 00:27:29 +08:00
|
|
|
fn2(c);
|
2013-02-05 00:31:14 +08:00
|
|
|
|
2013-11-28 00:27:29 +08:00
|
|
|
expect(fn.calledWith(a, b, c)).to.be.ok();
|
2013-02-05 00:31:14 +08:00
|
|
|
});
|
2010-09-02 21:14:25 +08:00
|
|
|
});
|
2013-02-05 00:31:14 +08:00
|
|
|
|
2013-10-29 18:38:21 +08:00
|
|
|
describe('#stamp', function () {
|
|
|
|
it('sets a unique id on the given object and returns it', function () {
|
2010-09-06 21:17:06 +08:00
|
|
|
var a = {},
|
|
|
|
id = L.Util.stamp(a);
|
2013-02-05 00:31:14 +08:00
|
|
|
|
2013-03-02 05:49:20 +08:00
|
|
|
expect(typeof id).to.eql('number');
|
|
|
|
expect(L.Util.stamp(a)).to.eql(id);
|
2013-02-05 00:31:14 +08:00
|
|
|
|
2010-09-06 21:17:06 +08:00
|
|
|
var b = {},
|
|
|
|
id2 = L.Util.stamp(b);
|
2013-02-05 00:31:14 +08:00
|
|
|
|
2013-03-02 05:49:20 +08:00
|
|
|
expect(id2).not.to.eql(id);
|
2010-09-06 21:17:06 +08:00
|
|
|
});
|
|
|
|
});
|
2012-07-05 17:54:40 +08:00
|
|
|
|
2013-02-05 00:31:14 +08:00
|
|
|
describe('#falseFn', function () {
|
2013-02-20 04:41:48 +08:00
|
|
|
it('returns false', function () {
|
2013-03-02 05:49:20 +08:00
|
|
|
expect(L.Util.falseFn()).to.be(false);
|
2013-02-05 00:31:14 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('#formatNum', function () {
|
2013-02-20 04:41:48 +08:00
|
|
|
it('formats numbers with a given precision', function () {
|
2013-03-02 05:49:20 +08:00
|
|
|
expect(L.Util.formatNum(13.12325555, 3)).to.eql(13.123);
|
|
|
|
expect(L.Util.formatNum(13.12325555)).to.eql(13.12326);
|
2013-02-05 00:31:14 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2012-12-23 06:47:38 +08:00
|
|
|
|
2013-10-29 18:38:21 +08:00
|
|
|
describe('#getParamString', function () {
|
|
|
|
it('creates a valid query string for appending depending on url input', function () {
|
2012-12-23 06:47:38 +08:00
|
|
|
var a = {
|
2013-10-29 18:38:21 +08:00
|
|
|
url: 'http://example.com/get',
|
2012-12-27 00:45:38 +08:00
|
|
|
obj: {bar: 7, baz: 3},
|
2013-10-29 18:38:21 +08:00
|
|
|
result: '?bar=7&baz=3'
|
2012-12-23 12:52:25 +08:00
|
|
|
};
|
2012-12-23 06:47:38 +08:00
|
|
|
|
2013-10-29 18:38:21 +08:00
|
|
|
expect(L.Util.getParamString(a.obj, a.url)).to.eql(a.result);
|
2013-02-05 00:31:14 +08:00
|
|
|
|
|
|
|
var b = {
|
2013-10-29 18:38:21 +08:00
|
|
|
url: 'http://example.com/get?justone=qs',
|
2012-12-27 00:45:38 +08:00
|
|
|
obj: {bar: 7, baz: 3},
|
2013-10-29 18:38:21 +08:00
|
|
|
result: '&bar=7&baz=3'
|
2012-12-23 12:52:25 +08:00
|
|
|
};
|
2013-02-05 00:31:14 +08:00
|
|
|
|
2013-10-29 18:38:21 +08:00
|
|
|
expect(L.Util.getParamString(b.obj, b.url)).to.eql(b.result);
|
2012-12-23 06:47:38 +08:00
|
|
|
|
2013-02-05 00:31:14 +08:00
|
|
|
var c = {
|
2012-12-28 03:39:19 +08:00
|
|
|
url: undefined,
|
|
|
|
obj: {bar: 7, baz: 3},
|
2013-10-29 18:38:21 +08:00
|
|
|
result: '?bar=7&baz=3'
|
2012-12-28 03:39:19 +08:00
|
|
|
};
|
2013-02-05 00:31:14 +08:00
|
|
|
|
2013-10-29 18:38:21 +08:00
|
|
|
expect(L.Util.getParamString(c.obj, c.url)).to.eql(c.result);
|
2012-12-23 06:47:38 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2013-02-05 00:31:14 +08:00
|
|
|
describe('#requestAnimFrame', function () {
|
2013-03-02 05:49:20 +08:00
|
|
|
it('calles a function on next frame, unless canceled', function (done) {
|
|
|
|
var spy = sinon.spy(),
|
2013-02-05 00:31:14 +08:00
|
|
|
foo = {};
|
2012-07-05 17:54:40 +08:00
|
|
|
|
2013-03-02 05:49:20 +08:00
|
|
|
L.Util.requestAnimFrame(spy);
|
2012-07-05 17:54:40 +08:00
|
|
|
|
2013-03-02 05:49:20 +08:00
|
|
|
L.Util.requestAnimFrame(function () {
|
|
|
|
expect(this).to.eql(foo);
|
|
|
|
done();
|
|
|
|
}, foo);
|
2012-07-05 17:54:40 +08:00
|
|
|
|
2013-03-02 05:49:20 +08:00
|
|
|
L.Util.cancelAnimFrame(spy);
|
2013-02-05 00:31:14 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2013-12-21 14:49:25 +08:00
|
|
|
describe('#throttle', function () {
|
2013-03-02 05:49:20 +08:00
|
|
|
it('limits execution to not more often than specified time interval', function (done) {
|
|
|
|
var spy = sinon.spy();
|
2013-02-05 00:31:14 +08:00
|
|
|
|
2013-12-21 14:49:25 +08:00
|
|
|
var fn = L.Util.throttle(spy, 20);
|
2013-02-05 00:31:14 +08:00
|
|
|
|
2013-03-02 05:49:20 +08:00
|
|
|
fn();
|
|
|
|
fn();
|
|
|
|
fn();
|
2013-02-05 00:31:14 +08:00
|
|
|
|
2013-03-02 05:49:20 +08:00
|
|
|
expect(spy.callCount).to.eql(1);
|
2013-02-05 00:31:14 +08:00
|
|
|
|
2013-03-02 05:49:20 +08:00
|
|
|
setTimeout(function () {
|
|
|
|
expect(spy.callCount).to.eql(2);
|
|
|
|
done();
|
|
|
|
}, 30);
|
2013-02-05 00:31:14 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('#splitWords', function () {
|
2013-02-20 04:41:48 +08:00
|
|
|
it('splits words into an array', function () {
|
2013-03-02 05:49:20 +08:00
|
|
|
expect(L.Util.splitWords('foo bar baz')).to.eql(['foo', 'bar', 'baz']);
|
2013-02-16 06:38:11 +08:00
|
|
|
});
|
|
|
|
});
|
2012-07-05 17:54:40 +08:00
|
|
|
|
2013-12-16 05:30:22 +08:00
|
|
|
describe('#setOptions', function () {
|
|
|
|
it('sets specified options on object', function () {
|
|
|
|
var o = {};
|
|
|
|
L.Util.setOptions(o, {foo: 'bar'});
|
|
|
|
expect(o.options.foo).to.eql('bar');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns options', function () {
|
|
|
|
var o = {};
|
|
|
|
var r = L.Util.setOptions(o, {foo: 'bar'});
|
|
|
|
expect(r).to.equal(o.options);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('accepts undefined', function () {
|
|
|
|
var o = {};
|
|
|
|
L.Util.setOptions(o, undefined);
|
|
|
|
expect(o.options).to.eql({});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('creates a distinct options object', function () {
|
|
|
|
var opts = {},
|
|
|
|
o = L.Util.create({options: opts});
|
|
|
|
L.Util.setOptions(o, {});
|
|
|
|
expect(o.options).not.to.equal(opts);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("doesn't create a distinct options object if object already has own options", function () {
|
|
|
|
var opts = {},
|
|
|
|
o = {options: opts};
|
|
|
|
L.Util.setOptions(o, {});
|
|
|
|
expect(o.options).to.equal(opts);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('inherits options prototypally', function () {
|
|
|
|
var opts = {},
|
|
|
|
o = L.Util.create({options: opts});
|
|
|
|
L.Util.setOptions(o, {});
|
|
|
|
opts.foo = 'bar';
|
|
|
|
expect(o.options.foo).to.eql('bar');
|
|
|
|
});
|
|
|
|
});
|
2012-07-05 17:54:40 +08:00
|
|
|
|
2013-02-05 00:31:14 +08:00
|
|
|
describe('#template', function () {
|
2013-02-20 04:41:48 +08:00
|
|
|
it('evaluates templates with a given data object', function () {
|
2013-11-21 04:42:06 +08:00
|
|
|
var tpl = 'Hello {foo} and {bar}!';
|
2013-02-05 00:31:14 +08:00
|
|
|
|
|
|
|
var str = L.Util.template(tpl, {
|
2013-08-13 20:05:41 +08:00
|
|
|
foo: 'Vlad',
|
|
|
|
bar: 'Dave'
|
|
|
|
});
|
|
|
|
|
2013-11-21 04:42:06 +08:00
|
|
|
expect(str).to.eql('Hello Vlad and Dave!');
|
2013-08-13 20:05:41 +08:00
|
|
|
});
|
2013-02-16 06:38:11 +08:00
|
|
|
it('does not modify text without a token variable', function () {
|
2013-03-02 05:49:20 +08:00
|
|
|
expect(L.Util.template('foo', {})).to.eql('foo');
|
2013-02-16 06:38:11 +08:00
|
|
|
});
|
2013-02-05 00:31:14 +08:00
|
|
|
|
2013-10-23 21:14:59 +08:00
|
|
|
it('supports templates with double quotes', function () {
|
|
|
|
expect(L.Util.template('He said: "{foo}"!', {
|
|
|
|
foo: 'Hello'
|
|
|
|
})).to.eql('He said: "Hello"!');
|
|
|
|
});
|
|
|
|
|
2013-02-16 06:38:11 +08:00
|
|
|
it('throws when a template token is not given', function () {
|
2013-02-05 00:31:14 +08:00
|
|
|
expect(function () {
|
2013-11-08 05:54:33 +08:00
|
|
|
L.Util.template(undefined, {foo: 'bar'});
|
2013-03-02 05:49:20 +08:00
|
|
|
}).to.throwError();
|
2013-02-05 00:31:14 +08:00
|
|
|
});
|
|
|
|
});
|
2013-10-08 21:59:38 +08:00
|
|
|
|
|
|
|
describe('#isArray', function () {
|
2013-11-08 05:54:33 +08:00
|
|
|
expect(L.Util.isArray([1, 2, 3])).to.be(true);
|
2015-01-29 01:32:27 +08:00
|
|
|
/*eslint no-array-constructor:0*/
|
2013-11-08 05:54:33 +08:00
|
|
|
expect(L.Util.isArray(new Array(1, 2, 3))).to.be(true);
|
|
|
|
expect(L.Util.isArray('blabla')).to.be(false);
|
|
|
|
expect(L.Util.isArray({0: 1, 1: 2})).to.be(false);
|
2013-10-08 21:59:38 +08:00
|
|
|
});
|
2012-12-27 00:45:38 +08:00
|
|
|
});
|