Code coverage report for ./suites/core/UtilSpec.js

Statements: 100% (76 / 76)      Branches: 100% (0 / 0)      Functions: 100% (30 / 30)      Lines: 100% (76 / 76)     

All files » ./suites/core/ » UtilSpec.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 1861   1 1   1 2           1 1         1             1 1   1               1 1 1 1     1   1     1 1         1   1   1       1 1 1     1 1   1     1       1 1 1       1 1 1 1         1 1 1           1   1           1   1           1       1 1 1       1   1 1 1     1       1 1 1   1   1 1 1   1   1 1 1         1 1 1           1 1 1   1         1     1 1     1 1 1          
describe('Util', function() {
 
	describe('#extend', function() {
		var a;
 
		beforeEach(function() {
			a = {
				foo: 5,
				bar: 'asd'
			};
		});
 
		it('extends the first argument with the properties of the second', function() {
			L.Util.extend(a, {
				bar: 7,
				baz: 3
			});
 
			expect(a).to.eql({
				foo: 5,
				bar: 7,
				baz: 3
			});
		});
 
		it('accepts more than 2 arguments', function() {
			L.Util.extend(a, {bar: 7}, {baz: 3});
 
			expect(a).to.eql({
				foo: 5,
				bar: 7,
				baz: 3
			});
		});
	});
 
	describe('#bind', function() {
		it('returns the given function with the given context', function() {
			var fn = function() {
				return this;
			};
 
			var fn2 = L.Util.bind(fn, { foo: 'bar' });
 
			expect(fn2()).to.eql({ foo: 'bar' });
		});
 
		it('passes additional arguments to the bound function', function () {
			var fn = sinon.spy(),
				foo = {},
				a = {},
				b = {};
 
			var fn2 = L.Util.bind(fn, foo, a, b);
 
			fn2();
 
			expect(fn.calledWith(a, b)).to.be.ok();
		});
	});
 
	describe('#stamp', function() {
		it('sets a unique id on the given object and returns it', function() {
			var a = {},
				id = L.Util.stamp(a);
 
			expect(typeof id).to.eql('number');
			expect(L.Util.stamp(a)).to.eql(id);
 
			var b = {},
				id2 = L.Util.stamp(b);
 
			expect(id2).not.to.eql(id);
		});
	});
 
	describe('#falseFn', function () {
		it('returns false', function () {
			expect(L.Util.falseFn()).to.be(false);
		});
	});
 
	describe('#formatNum', function () {
		it('formats numbers with a given precision', function () {
			expect(L.Util.formatNum(13.12325555, 3)).to.eql(13.123);
			expect(L.Util.formatNum(13.12325555)).to.eql(13.12326);
		});
	});
 
 
	describe('#getParamString', function() {
		it('creates a valid query string for appending depending on url input', function() {
			var a = {
				url: "http://example.com/get",
				obj: {bar: 7, baz: 3},
				result: "?bar=7&baz=3"
			};
 
			expect(L.Util.getParamString(a.obj,a.url)).to.eql(a.result);
 
			var b = {
				url: "http://example.com/get?justone=qs",
				obj: {bar: 7, baz: 3},
				result: "&bar=7&baz=3"
			};
 
			expect(L.Util.getParamString(b.obj,b.url)).to.eql(b.result);
 
			var c = {
				url: undefined,
				obj: {bar: 7, baz: 3},
				result: "?bar=7&baz=3"
			};
 
			expect(L.Util.getParamString(c.obj,c.url)).to.eql(c.result);
		});
	});
 
	describe('#requestAnimFrame', function () {
		it('calles a function on next frame, unless canceled', function (done) {
			var spy = sinon.spy(),
				spy2 = sinon.spy(),
				foo = {};
 
			L.Util.requestAnimFrame(spy);
 
			L.Util.requestAnimFrame(function () {
				expect(this).to.eql(foo);
				done();
			}, foo);
 
			L.Util.cancelAnimFrame(spy);
		});
	});
 
	describe('#limitExecByInterval', function() {
		it('limits execution to not more often than specified time interval', function (done) {
			var spy = sinon.spy();
 
			var fn = L.Util.limitExecByInterval(spy, 20);
 
			fn();
			fn();
			fn();
 
			expect(spy.callCount).to.eql(1);
 
			setTimeout(function () {
				expect(spy.callCount).to.eql(2);
				done();
			}, 30);
		});
	});
 
	describe('#splitWords', function () {
		it('splits words into an array', function () {
			expect(L.Util.splitWords('foo bar baz')).to.eql(['foo', 'bar', 'baz']);
		});
	});
 
	// TODO setOptions
 
	describe('#template', function () {
		it('evaluates templates with a given data object', function () {
			var tpl = 'Hello {foo} and {bar}!';
 
			var str = L.Util.template(tpl, {
				foo: 'Vlad',
				bar: 'Dave'
			});
 
			expect(str).to.eql('Hello Vlad and Dave!');
		});
 
		it('does not modify text without a token variable', function () {
			expect(L.Util.template('foo', {})).to.eql('foo');
		});
 
		it('throws when a template token is not given', function () {
			expect(function () {
				L.Util.template(tpl, {foo: 'bar'});
			}).to.throwError();
		});
	});
});