'use strict'; describe('angular', function() { var element; afterEach(function(){ dealoc(element); }); describe('case', function() { it('should change case', function() { expect(lowercase('ABC90')).toEqual('abc90'); expect(manualLowercase('ABC90')).toEqual('abc90'); expect(uppercase('abc90')).toEqual('ABC90'); expect(manualUppercase('abc90')).toEqual('ABC90'); }); }); describe("copy", function() { it("should return same object", function () { var obj = {}; var arr = []; expect(copy({}, obj)).toBe(obj); expect(copy([], arr)).toBe(arr); }); it("should copy Date", function() { var date = new Date(123); expect(copy(date) instanceof Date).toBeTruthy(); expect(copy(date).getTime()).toEqual(123); expect(copy(date) === date).toBeFalsy(); }); it("should deeply copy an array into an existing array", function() { var src = [1, {name:"value"}]; var dst = [{key:"v"}]; expect(copy(src, dst)).toBe(dst); expect(dst).toEqual([1, {name:"value"}]); expect(dst[1]).toEqual({name:"value"}); expect(dst[1]).not.toBe(src[1]); }); it("should deeply copy an array into a new array", function() { var src = [1, {name:"value"}]; var dst = copy(src); expect(src).toEqual([1, {name:"value"}]); expect(dst).toEqual(src); expect(dst).not.toBe(src); expect(dst[1]).not.toBe(src[1]); }); it('should copy empty array', function() { var src = []; var dst = [{key: "v"}]; expect(copy(src, dst)).toEqual([]); expect(dst).toEqual([]); }); it("should deeply copy an object into an existing object", function() { var src = {a:{name:"value"}}; var dst = {b:{key:"v"}}; expect(copy(src, dst)).toBe(dst); expect(dst).toEqual({a:{name:"value"}}); expect(dst.a).toEqual(src.a); expect(dst.a).not.toBe(src.a); }); it("should deeply copy an object into an existing object", function() { var src = {a:{name:"value"}}; var dst = copy(src, dst); expect(src).toEqual({a:{name:"value"}}); expect(dst).toEqual(src); expect(dst).not.toBe(src); expect(dst.a).toEqual(src.a); expect(dst.a).not.toBe(src.a); }); it("should copy primitives", function() { expect(copy(null)).toEqual(null); expect(copy('')).toBe(''); expect(copy('lala')).toBe('lala'); expect(copy(123)).toEqual(123); expect(copy([{key:null}])).toEqual([{key:null}]); }); it('should throw an exception if a Scope is being copied', inject(function($rootScope) { expect(function() { copy($rootScope.$new()); }).toThrow("Can't copy Window or Scope"); })); it('should throw an exception if a Window is being copied', function() { expect(function() { copy(window); }).toThrow("Can't copy Window or Scope"); }); it('should throw an exception when source and destination are equivalent', function() { var src, dst; src = dst = {key: 'value'}; expect(function() { copy(src, dst); }).toThrow("Can't copy equivalent objects or arrays"); src = dst = [2, 4]; expect(function() { copy(src, dst); }).toThrow("Can't copy equivalent objects or arrays"); }); }); describe('shallow copy', function() { it('should make a copy', function() { var original = {key:{}}; var copy = shallowCopy(original); expect(copy).toEqual(original); expect(copy.key).toBe(original.key); }); it('should not copy $$ properties nor prototype properties', function() { var original = {$$some: true, $$: true}; var clone = {}; expect(shallowCopy(original, clone)).toBe(clone); expect(clone.$$some).toBeUndefined(); expect(clone.$$).toBeUndefined(); }); }); describe('elementHTML', function() { it('should dump element', function() { expect(startingTag('
s1s2
").find("span"), log = []; forEach(jqObject, function(value, key) { log.push(key + ':' + value.innerHTML)}); expect(log).toEqual(['0:s1', '1:s2']); }); it('should handle NodeList objects like arrays', function() { var nodeList = jqLite("abc
")[0].childNodes, log = []; forEach(nodeList, function(value, key) { log.push(key + ':' + value.innerHTML)}); expect(log).toEqual(['0:a', '1:b', '2:c']); }); it('should handle HTMLCollection objects like arrays', function() { document.body.innerHTML = ""; var htmlCollection = document.getElementsByName('x'), log = []; forEach(htmlCollection, function(value, key) { log.push(key + ':' + value.innerHTML)}); expect(log).toEqual(['0:a', '1:c']); }); it('should handle arguments objects like arrays', function() { var args, log = []; (function(){ args = arguments}('a', 'b', 'c')); forEach(args, function(value, key) { log.push(key + ':' + value)}); expect(log).toEqual(['0:a', '1:b', '2:c']); }); it('should handle objects with length property as objects', function() { var obj = { 'foo' : 'bar', 'length': 2 }, log = []; forEach(obj, function(value, key) { log.push(key + ':' + value)}); expect(log).toEqual(['foo:bar', 'length:2']); }); it('should handle objects of custom types with length property as objects', function() { function CustomType() { this.length = 2; this.foo = 'bar' } var obj = new CustomType(), log = []; forEach(obj, function(value, key) { log.push(key + ':' + value)}); expect(log).toEqual(['length:2', 'foo:bar']); }); }); describe('sortedKeys', function() { it('should collect keys from object', function() { expect(sortedKeys({c:0, b:0, a:0})).toEqual(['a', 'b', 'c']); }); }); describe('encodeUriSegment', function() { it('should correctly encode uri segment and not encode chars defined as pchar set in rfc3986', function() { //don't encode alphanum expect(encodeUriSegment('asdf1234asdf')). toEqual('asdf1234asdf'); //don't encode unreserved' expect(encodeUriSegment("-_.!~*'() -_.!~*'()")). toEqual("-_.!~*'()%20-_.!~*'()"); //don't encode the rest of pchar' expect(encodeUriSegment(':@&=+$, :@&=+$,')). toEqual(':@&=+$,%20:@&=+$,'); //encode '/', ';' and ' '' expect(encodeUriSegment('/; /;')). toEqual('%2F%3B%20%2F%3B'); }); }); describe('encodeUriQuery', function() { it('should correctly encode uri query and not encode chars defined as pchar set in rfc3986', function() { //don't encode alphanum expect(encodeUriQuery('asdf1234asdf')). toEqual('asdf1234asdf'); //don't encode unreserved expect(encodeUriQuery("-_.!~*'() -_.!~*'()")). toEqual("-_.!~*'()+-_.!~*'()"); //don't encode the rest of pchar expect(encodeUriQuery(':@$, :@$,')). toEqual(':@$,+:@$,'); //encode '&', ';', '=', '+', and '#' expect(encodeUriQuery('&;=+# &;=+#')). toEqual('%26%3B%3D%2B%23+%26%3B%3D%2B%23'); //encode ' ' as '+' expect(encodeUriQuery(' ')). toEqual('++'); //encode ' ' as '%20' when a flag is used expect(encodeUriQuery(' ', true)). toEqual('%20%20'); }); }); describe('angularInit', function() { var bootstrapSpy; var element; beforeEach(function() { element = { getElementById: function (id) { return element.getElementById[id] || []; }, querySelectorAll: function(arg) { return element.querySelectorAll[arg] || []; }, getAttribute: function(name) { return element[name]; } }; bootstrapSpy = jasmine.createSpy('bootstrapSpy'); }); it('should do nothing when not found', function() { angularInit(element, bootstrapSpy); expect(bootstrapSpy).not.toHaveBeenCalled(); }); it('should look for ngApp directive as attr', function() { var appElement = jqLite('')[0]; element.querySelectorAll['[ng-app]'] = [appElement]; angularInit(element, bootstrapSpy); expect(bootstrapSpy).toHaveBeenCalledOnceWith(appElement, ['ABC']); }); it('should look for ngApp directive in id', function() { var appElement = jqLite('')[0]; jqLite(document.body).append(appElement); angularInit(element, bootstrapSpy); expect(bootstrapSpy).toHaveBeenCalledOnceWith(appElement, ['ABC']); }); it('should look for ngApp directive in className', function() { var appElement = jqLite('')[0]; element.querySelectorAll['.ng\\:app'] = [appElement]; angularInit(element, bootstrapSpy); expect(bootstrapSpy).toHaveBeenCalledOnceWith(appElement, ['ABC']); }); it('should look for ngApp directive using querySelectorAll', function() { var appElement = jqLite('')[0]; element.querySelectorAll['[ng\\:app]'] = [ appElement ]; angularInit(element, bootstrapSpy); expect(bootstrapSpy).toHaveBeenCalledOnceWith(appElement, ['ABC']); }); it('should bootstrap using class name', function() { var appElement = jqLite('')[0]; angularInit(jqLite('').append(appElement)[0], bootstrapSpy); expect(bootstrapSpy).toHaveBeenCalledOnceWith(appElement, ['ABC']); }); it('should bootstrap anonymously', function() { var appElement = jqLite('')[0]; element.querySelectorAll['[x-ng-app]'] = [ appElement ]; angularInit(element, bootstrapSpy); expect(bootstrapSpy).toHaveBeenCalledOnceWith(appElement, []); }); it('should bootstrap anonymously using class only', function() { var appElement = jqLite('')[0]; angularInit(jqLite('').append(appElement)[0], bootstrapSpy); expect(bootstrapSpy).toHaveBeenCalledOnceWith(appElement, []); }); it('should bootstrap if the annotation is on the root element', function() { var appElement = jqLite('')[0]; angularInit(appElement, bootstrapSpy); expect(bootstrapSpy).toHaveBeenCalledOnceWith(appElement, []); }); it('should complain if app module cannot be found', function() { var appElement = jqLite('')[0]; expect(function() { angularInit(appElement, bootstrap); }).toThrow('No module: doesntexist'); }); }); describe('angular service', function() { it('should override services', function() { module(function($provide){ $provide.value('fake', 'old'); $provide.value('fake', 'new'); }); inject(function(fake) { expect(fake).toEqual('new'); }); }); it('should inject dependencies specified by $inject and ignore function argument name', function() { expect(angular.injector([function($provide){ $provide.factory('svc1', function() { return 'svc1'; }); $provide.factory('svc2', ['svc1', function(s) { return 'svc2-' + s; }]); }]).get('svc2')).toEqual('svc2-svc1'); }); }); describe('isDate', function() { it('should return true for Date object', function() { expect(isDate(new Date())).toBe(true); }); it('should return false for non Date objects', function() { expect(isDate([])).toBe(false); expect(isDate('')).toBe(false); expect(isDate(23)).toBe(false); expect(isDate({})).toBe(false); }); }); describe('compile', function() { it('should link to existing node and create scope', inject(function($rootScope, $compile) { var template = angular.element('