Leaflet/spec/suites/core/UtilSpec.js

213 lines
4.7 KiB
JavaScript
Raw Normal View History

2010-09-07 00:14:04 +08:00
describe('Util', function() {
2010-09-02 21:14:25 +08:00
describe('#extend', function() {
var a;
2013-02-05 00:31:14 +08:00
2010-09-02 21:14:25 +08:00
beforeEach(function() {
a = {
foo: 5,
bar: 'asd'
};
});
2013-02-05 00:31:14 +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
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
});
});
});
describe('#bind', function() {
it('returns the given function with the given context', function() {
2010-09-02 21:14:25 +08:00
var fn = function() {
return this;
};
2013-02-05 00:31:14 +08:00
2013-03-02 05:49:20 +08:00
var fn2 = L.Util.bind(fn, { foo: 'bar' });
2013-02-05 00:31:14 +08:00
2013-03-02 05:49:20 +08:00
expect(fn2()).to.eql({ foo: 'bar' });
2010-09-02 21:14:25 +08:00
});
2013-02-05 00:31:14 +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 = {},
b = {};
var fn2 = L.Util.bind(fn, foo, a, b);
fn2();
2013-03-02 05:49:20 +08:00
expect(fn.calledWith(a, b)).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
2010-09-06 21:17:06 +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
describe('#invokeEach', function () {
it('calls the given method/context with each key/value and additional arguments', function () {
var spy = sinon.spy(),
ctx = {};
var result = L.Util.invokeEach({
foo: 'bar',
yo: 'hey'
}, spy, ctx, 1, 2, 3);
expect(spy.firstCall.calledWith('foo', 'bar', 1, 2, 3)).to.be.ok();
expect(spy.secondCall.calledWith('yo', 'hey', 1, 2, 3)).to.be.ok();
expect(spy.firstCall.calledOn(ctx)).to.be.ok();
expect(spy.secondCall.calledOn(ctx)).to.be.ok();
expect(result).to.be(true);
});
it('returns false if the given agument is not object', function () {
var spy = sinon.spy();
expect(L.Util.invokeEach('foo', spy)).to.be(false);
expect(spy.called).to.be(false);
});
});
2013-02-05 00:31:14 +08:00
describe('#falseFn', function () {
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 () {
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
});
});
describe('#getParamString', function() {
it('creates a valid query string for appending depending on url input', function() {
var a = {
2012-12-27 00:45:38 +08:00
url: "http://example.com/get",
obj: {bar: 7, baz: 3},
result: "?bar=7&baz=3"
2012-12-23 12:52:25 +08:00
};
2013-03-02 05:49:20 +08:00
expect(L.Util.getParamString(a.obj,a.url)).to.eql(a.result);
2013-02-05 00:31:14 +08:00
var b = {
2012-12-27 00:45:38 +08:00
url: "http://example.com/get?justone=qs",
obj: {bar: 7, baz: 3},
result: "&bar=7&baz=3"
2012-12-23 12:52:25 +08:00
};
2013-02-05 00:31:14 +08:00
2013-03-02 05:49:20 +08:00
expect(L.Util.getParamString(b.obj,b.url)).to.eql(b.result);
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},
result: "?bar=7&baz=3"
};
2013-02-05 00:31:14 +08:00
2013-03-02 05:49:20 +08:00
expect(L.Util.getParamString(c.obj,c.url)).to.eql(c.result);
});
});
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(),
spy2 = 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
});
});
describe('#limitExecByInterval', 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
var fn = L.Util.limitExecByInterval(spy, 20);
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 () {
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']);
});
});
2012-07-05 17:54:40 +08:00
// TODO setOptions
2013-02-05 00:31:14 +08:00
describe('#template', function () {
it('evaluates templates with a given data object', function () {
2013-02-05 00:31:14 +08:00
var tpl = 'Hello {foo} and {bar}!';
var str = L.Util.template(tpl, {
foo: 'Vlad',
bar: 'Dave'
});
2013-03-02 05:49:20 +08:00
expect(str).to.eql('Hello Vlad and Dave!');
});
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-05 00:31:14 +08:00
it('throws when a template token is not given', function () {
2013-02-05 00:31:14 +08:00
expect(function () {
L.Util.template(tpl, {foo: 'bar'});
2013-03-02 05:49:20 +08:00
}).to.throwError();
2013-02-05 00:31:14 +08:00
});
});
2012-12-27 00:45:38 +08:00
});