2011-07-17 08:05:43 +00:00
'use strict' ;
2011-10-07 18:27:49 +00:00
describe ( 'angular' , function ( ) {
2011-11-23 05:28:39 +00:00
var element ;
afterEach ( function ( ) {
dealoc ( element ) ;
} ) ;
2011-10-07 18:27:49 +00:00
describe ( 'case' , function ( ) {
it ( 'should change case' , function ( ) {
2011-02-12 18:13:28 +00:00
expect ( lowercase ( 'ABC90' ) ) . toEqual ( 'abc90' ) ;
expect ( manualLowercase ( 'ABC90' ) ) . toEqual ( 'abc90' ) ;
expect ( uppercase ( 'abc90' ) ) . toEqual ( 'ABC90' ) ;
expect ( manualUppercase ( 'abc90' ) ) . toEqual ( 'ABC90' ) ;
} ) ;
} ) ;
2011-10-07 18:27:49 +00:00
describe ( "copy" , function ( ) {
it ( "should return same object" , function ( ) {
2011-02-12 18:13:28 +00:00
var obj = { } ;
var arr = [ ] ;
expect ( copy ( { } , obj ) ) . toBe ( obj ) ;
expect ( copy ( [ ] , arr ) ) . toBe ( arr ) ;
} ) ;
2011-10-07 18:27:49 +00:00
it ( "should copy Date" , function ( ) {
2011-02-12 18:13:28 +00:00
var date = new Date ( 123 ) ;
expect ( copy ( date ) instanceof Date ) . toBeTruthy ( ) ;
expect ( copy ( date ) . getTime ( ) ) . toEqual ( 123 ) ;
expect ( copy ( date ) === date ) . toBeFalsy ( ) ;
} ) ;
2013-02-12 06:10:58 +00:00
it ( "should deeply copy an array into an existing array" , function ( ) {
2011-02-12 18:13:28 +00:00
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 ] ) ;
} ) ;
2013-02-12 06:10:58 +00:00
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 ] ) ;
} ) ;
2011-02-12 18:13:28 +00:00
it ( 'should copy empty array' , function ( ) {
var src = [ ] ;
var dst = [ { key : "v" } ] ;
expect ( copy ( src , dst ) ) . toEqual ( [ ] ) ;
expect ( dst ) . toEqual ( [ ] ) ;
} ) ;
2013-02-12 06:10:58 +00:00
it ( "should deeply copy an object into an existing object" , function ( ) {
2011-02-12 18:13:28 +00:00
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 ) ;
} ) ;
2013-02-12 06:10:58 +00:00
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 ) ;
} ) ;
2011-10-07 18:27:49 +00:00
it ( "should copy primitives" , function ( ) {
2011-02-12 18:13:28 +00:00
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 } ] ) ;
} ) ;
2011-11-30 19:06:04 +00:00
it ( 'should throw an exception if a Scope is being copied' , inject ( function ( $rootScope ) {
2013-05-24 18:00:14 +00:00
expect ( function ( ) { copy ( $rootScope . $new ( ) ) ; } ) .
2013-06-08 01:24:30 +00:00
toThrow ( "[ng:cpws] Can't copy! Making copies of Window or Scope instances is not supported." ) ;
2011-11-30 19:06:04 +00:00
} ) ) ;
it ( 'should throw an exception if a Window is being copied' , function ( ) {
2013-05-24 18:00:14 +00:00
expect ( function ( ) { copy ( window ) ; } ) .
2013-06-08 01:24:30 +00:00
toThrow ( "[ng:cpws] Can't copy! Making copies of Window or Scope instances is not supported." ) ;
2011-11-30 19:06:04 +00:00
} ) ;
2011-12-19 02:33:25 +00:00
it ( 'should throw an exception when source and destination are equivalent' , function ( ) {
var src , dst ;
src = dst = { key : 'value' } ;
2013-06-08 01:24:30 +00:00
expect ( function ( ) { copy ( src , dst ) ; } ) . toThrow ( "[ng:cpi] Can't copy! Source and destination are identical." ) ;
2011-12-19 02:33:25 +00:00
src = dst = [ 2 , 4 ] ;
2013-06-08 01:24:30 +00:00
expect ( function ( ) { copy ( src , dst ) ; } ) . toThrow ( "[ng:cpi] Can't copy! Source and destination are identical." ) ;
2011-12-19 02:33:25 +00:00
} ) ;
2013-04-17 00:09:43 +00:00
it ( 'should not copy the private $$hashKey' , function ( ) {
var src , dst ;
src = { } ;
hashKey ( src ) ;
dst = copy ( src ) ;
expect ( hashKey ( dst ) ) . not . toEqual ( hashKey ( src ) ) ;
} ) ;
it ( 'should retain the previous $$hashKey' , function ( ) {
var src , dst , h ;
src = { } ;
dst = { } ;
// force creation of a hashkey
h = hashKey ( dst ) ;
hashKey ( src ) ;
dst = copy ( src , dst ) ;
// make sure we don't copy the key
expect ( hashKey ( dst ) ) . not . toEqual ( hashKey ( src ) ) ;
// make sure we retain the old key
expect ( hashKey ( dst ) ) . toEqual ( h ) ;
} ) ;
} ) ;
describe ( "extend" , function ( ) {
it ( 'should not copy the private $$hashKey' , function ( ) {
var src , dst ;
src = { } ;
dst = { } ;
hashKey ( src ) ;
dst = extend ( dst , src ) ;
expect ( hashKey ( dst ) ) . not . toEqual ( hashKey ( src ) ) ;
} ) ;
it ( 'should retain the previous $$hashKey' , function ( ) {
var src , dst , h ;
src = { } ;
dst = { } ;
h = hashKey ( dst ) ;
hashKey ( src ) ;
dst = extend ( dst , src ) ;
// make sure we don't copy the key
expect ( hashKey ( dst ) ) . not . toEqual ( hashKey ( src ) ) ;
// make sure we retain the old key
expect ( hashKey ( dst ) ) . toEqual ( h ) ;
} ) ;
it ( 'should work when extending with itself' , function ( ) {
var src , dst , h ;
dst = src = { } ;
h = hashKey ( dst ) ;
dst = extend ( dst , src ) ;
// make sure we retain the old key
expect ( hashKey ( dst ) ) . toEqual ( h ) ;
} ) ;
2011-02-12 18:13:28 +00:00
} ) ;
2011-11-29 20:11:32 +00:00
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 ) ;
} ) ;
2012-03-28 23:03:59 +00:00
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 ( ) ;
} ) ;
2011-11-29 20:11:32 +00:00
} ) ;
describe ( 'elementHTML' , function ( ) {
it ( 'should dump element' , function ( ) {
2012-06-08 18:53:17 +00:00
expect ( startingTag ( '<div attr="123">something<span></span></div>' ) ) .
2011-11-29 20:11:32 +00:00
toEqual ( '<div attr="123">' ) ;
} ) ;
} ) ;
2011-10-07 18:27:49 +00:00
describe ( 'equals' , function ( ) {
it ( 'should return true if same object' , function ( ) {
2011-02-12 18:13:28 +00:00
var o = { } ;
expect ( equals ( o , o ) ) . toEqual ( true ) ;
2011-03-23 16:33:29 +00:00
expect ( equals ( o , { } ) ) . toEqual ( true ) ;
expect ( equals ( 1 , '1' ) ) . toEqual ( false ) ;
2011-02-12 18:13:28 +00:00
expect ( equals ( 1 , '2' ) ) . toEqual ( false ) ;
} ) ;
2011-10-07 18:27:49 +00:00
it ( 'should recurse into object' , function ( ) {
2011-02-12 18:13:28 +00:00
expect ( equals ( { } , { } ) ) . toEqual ( true ) ;
expect ( equals ( { name : 'misko' } , { name : 'misko' } ) ) . toEqual ( true ) ;
expect ( equals ( { name : 'misko' , age : 1 } , { name : 'misko' } ) ) . toEqual ( false ) ;
expect ( equals ( { name : 'misko' } , { name : 'misko' , age : 1 } ) ) . toEqual ( false ) ;
expect ( equals ( { name : 'misko' } , { name : 'adam' } ) ) . toEqual ( false ) ;
expect ( equals ( [ 'misko' ] , [ 'misko' ] ) ) . toEqual ( true ) ;
expect ( equals ( [ 'misko' ] , [ 'adam' ] ) ) . toEqual ( false ) ;
expect ( equals ( [ 'misko' ] , [ 'misko' , 'adam' ] ) ) . toEqual ( false ) ;
} ) ;
2013-01-22 06:00:15 +00:00
it ( 'should ignore undefined member variables during comparison' , function ( ) {
2013-01-18 00:53:30 +00:00
var obj1 = { name : 'misko' } ,
obj2 = { name : 'misko' , undefinedvar : undefined } ;
2013-01-22 06:00:15 +00:00
expect ( equals ( obj1 , obj2 ) ) . toBe ( true ) ;
expect ( equals ( obj2 , obj1 ) ) . toBe ( true ) ;
2013-01-18 00:53:30 +00:00
} ) ;
2011-10-07 18:27:49 +00:00
it ( 'should ignore $ member variables' , function ( ) {
2011-02-12 18:13:28 +00:00
expect ( equals ( { name : 'misko' , $id : 1 } , { name : 'misko' , $id : 2 } ) ) . toEqual ( true ) ;
expect ( equals ( { name : 'misko' } , { name : 'misko' , $id : 2 } ) ) . toEqual ( true ) ;
expect ( equals ( { name : 'misko' , $id : 1 } , { name : 'misko' } ) ) . toEqual ( true ) ;
} ) ;
2011-10-07 18:27:49 +00:00
it ( 'should ignore functions' , function ( ) {
2011-02-12 18:13:28 +00:00
expect ( equals ( { func : function ( ) { } } , { bar : function ( ) { } } ) ) . toEqual ( true ) ;
} ) ;
it ( 'should work well with nulls' , function ( ) {
expect ( equals ( null , '123' ) ) . toBe ( false ) ;
expect ( equals ( '123' , null ) ) . toBe ( false ) ;
var obj = { foo : 'bar' } ;
expect ( equals ( null , obj ) ) . toBe ( false ) ;
expect ( equals ( obj , null ) ) . toBe ( false ) ;
expect ( equals ( null , null ) ) . toBe ( true ) ;
} ) ;
it ( 'should work well with undefined' , function ( ) {
expect ( equals ( undefined , '123' ) ) . toBe ( false ) ;
expect ( equals ( '123' , undefined ) ) . toBe ( false ) ;
var obj = { foo : 'bar' } ;
expect ( equals ( undefined , obj ) ) . toBe ( false ) ;
expect ( equals ( obj , undefined ) ) . toBe ( false ) ;
expect ( equals ( undefined , undefined ) ) . toBe ( true ) ;
} ) ;
2011-11-16 00:45:36 +00:00
it ( 'should treat two NaNs as equal' , function ( ) {
expect ( equals ( NaN , NaN ) ) . toBe ( true ) ;
} ) ;
2011-11-30 19:06:04 +00:00
it ( 'should compare Scope instances only by identity' , inject ( function ( $rootScope ) {
var scope1 = $rootScope . $new ( ) ,
scope2 = $rootScope . $new ( ) ;
expect ( equals ( scope1 , scope1 ) ) . toBe ( true ) ;
expect ( equals ( scope1 , scope2 ) ) . toBe ( false ) ;
expect ( equals ( $rootScope , scope1 ) ) . toBe ( false ) ;
expect ( equals ( undefined , scope1 ) ) . toBe ( false ) ;
} ) ) ;
it ( 'should compare Window instances only by identity' , function ( ) {
expect ( equals ( window , window ) ) . toBe ( true ) ;
expect ( equals ( window , window . parent ) ) . toBe ( false ) ;
expect ( equals ( window , undefined ) ) . toBe ( false ) ;
} ) ;
2012-02-23 21:57:28 +00:00
it ( 'should compare dates' , function ( ) {
expect ( equals ( new Date ( 0 ) , new Date ( 0 ) ) ) . toBe ( true ) ;
expect ( equals ( new Date ( 0 ) , new Date ( 1 ) ) ) . toBe ( false ) ;
expect ( equals ( new Date ( 0 ) , 0 ) ) . toBe ( false ) ;
expect ( equals ( 0 , new Date ( 0 ) ) ) . toBe ( false ) ;
} ) ;
2011-02-12 18:13:28 +00:00
} ) ;
2011-03-27 22:58:24 +00:00
describe ( 'size' , function ( ) {
it ( 'should return the number of items in an array' , function ( ) {
expect ( size ( [ ] ) ) . toBe ( 0 ) ;
expect ( size ( [ 'a' , 'b' , 'c' ] ) ) . toBe ( 3 ) ;
} ) ;
it ( 'should return the number of properties of an object' , function ( ) {
expect ( size ( { } ) ) . toBe ( 0 ) ;
expect ( size ( { a : 1 , b : 'a' , c : noop } ) ) . toBe ( 3 ) ;
} ) ;
it ( 'should return the number of own properties of an object' , function ( ) {
var obj = inherit ( { protoProp : 'c' , protoFn : noop } , { a : 1 , b : 'a' , c : noop } ) ;
expect ( size ( obj ) ) . toBe ( 5 ) ;
expect ( size ( obj , true ) ) . toBe ( 3 ) ;
} ) ;
it ( 'should return the string length' , function ( ) {
expect ( size ( '' ) ) . toBe ( 0 ) ;
expect ( size ( 'abc' ) ) . toBe ( 3 ) ;
} ) ;
2011-03-27 23:19:03 +00:00
it ( 'should not rely on length property of an object to determine its size' , function ( ) {
expect ( size ( { length : 99 } ) ) . toBe ( 1 ) ;
} ) ;
2011-03-27 22:58:24 +00:00
} ) ;
2011-02-12 18:13:28 +00:00
describe ( 'parseKeyValue' , function ( ) {
it ( 'should parse a string into key-value pairs' , function ( ) {
expect ( parseKeyValue ( '' ) ) . toEqual ( { } ) ;
expect ( parseKeyValue ( 'simple=pair' ) ) . toEqual ( { simple : 'pair' } ) ;
expect ( parseKeyValue ( 'first=1&second=2' ) ) . toEqual ( { first : '1' , second : '2' } ) ;
expect ( parseKeyValue ( 'escaped%20key=escaped%20value' ) ) .
2010-09-27 23:00:05 +00:00
toEqual ( { 'escaped key' : 'escaped value' } ) ;
2011-02-12 18:13:28 +00:00
expect ( parseKeyValue ( 'emptyKey=' ) ) . toEqual ( { emptyKey : '' } ) ;
expect ( parseKeyValue ( 'flag1&key=value&flag2' ) ) .
2010-09-27 23:00:05 +00:00
toEqual ( { flag1 : true , key : 'value' , flag2 : true } ) ;
2011-02-12 18:13:28 +00:00
} ) ;
2013-06-20 12:59:46 +00:00
it ( 'should ignore key values that are not valid URI components' , function ( ) {
expect ( function ( ) { parseKeyValue ( '%' ) ; } ) . not . toThrow ( ) ;
expect ( parseKeyValue ( '%' ) ) . toEqual ( { } ) ;
expect ( parseKeyValue ( 'invalid=%' ) ) . toEqual ( { invalid : undefined } ) ;
expect ( parseKeyValue ( 'invalid=%&valid=good' ) ) . toEqual ( { invalid : undefined , valid : 'good' } ) ;
} ) ;
2013-04-26 21:31:56 +00:00
it ( 'should parse a string into key-value pairs with duplicates grouped in an array' , function ( ) {
expect ( parseKeyValue ( '' ) ) . toEqual ( { } ) ;
expect ( parseKeyValue ( 'duplicate=pair' ) ) . toEqual ( { duplicate : 'pair' } ) ;
expect ( parseKeyValue ( 'first=1&first=2' ) ) . toEqual ( { first : [ '1' , '2' ] } ) ;
expect ( parseKeyValue ( 'escaped%20key=escaped%20value&&escaped%20key=escaped%20value2' ) ) .
toEqual ( { 'escaped key' : [ 'escaped value' , 'escaped value2' ] } ) ;
expect ( parseKeyValue ( 'flag1&key=value&flag1' ) ) .
toEqual ( { flag1 : [ true , true ] , key : 'value' } ) ;
expect ( parseKeyValue ( 'flag1&flag1=value&flag1=value2&flag1' ) ) .
toEqual ( { flag1 : [ true , 'value' , 'value2' , true ] } ) ;
} ) ;
2010-09-27 23:00:05 +00:00
} ) ;
2010-10-16 15:13:53 +00:00
2011-02-12 18:13:28 +00:00
describe ( 'toKeyValue' , function ( ) {
2013-04-26 21:31:56 +00:00
it ( 'should serialize key-value pairs into string' , function ( ) {
2011-02-12 18:13:28 +00:00
expect ( toKeyValue ( { } ) ) . toEqual ( '' ) ;
expect ( toKeyValue ( { simple : 'pair' } ) ) . toEqual ( 'simple=pair' ) ;
expect ( toKeyValue ( { first : '1' , second : '2' } ) ) . toEqual ( 'first=1&second=2' ) ;
expect ( toKeyValue ( { 'escaped key' : 'escaped value' } ) ) .
2010-10-16 15:13:53 +00:00
toEqual ( 'escaped%20key=escaped%20value' ) ;
2011-02-12 18:13:28 +00:00
expect ( toKeyValue ( { emptyKey : '' } ) ) . toEqual ( 'emptyKey=' ) ;
} ) ;
2013-04-26 21:31:56 +00:00
it ( 'should serialize true values into flags' , function ( ) {
2011-02-12 18:13:28 +00:00
expect ( toKeyValue ( { flag1 : true , key : 'value' , flag2 : true } ) ) . toEqual ( 'flag1&key=value&flag2' ) ;
} ) ;
2013-04-26 21:31:56 +00:00
it ( 'should serialize duplicates into duplicate param strings' , function ( ) {
expect ( toKeyValue ( { key : [ 323 , 'value' , true ] } ) ) . toEqual ( 'key=323&key=value&key' ) ;
expect ( toKeyValue ( { key : [ 323 , 'value' , true , 1234 ] } ) ) .
toEqual ( 'key=323&key=value&key&key=1234' ) ;
} ) ;
2011-02-12 18:13:28 +00:00
} ) ;
2012-03-21 22:42:35 +00:00
describe ( 'forEach' , function ( ) {
it ( 'should iterate over *own* object properties' , function ( ) {
function MyObj ( ) {
this . bar = 'barVal' ;
this . baz = 'bazVal' ;
2013-02-11 20:25:34 +00:00
}
2012-03-21 22:42:35 +00:00
MyObj . prototype . foo = 'fooVal' ;
var obj = new MyObj ( ) ,
log = [ ] ;
forEach ( obj , function ( value , key ) { log . push ( key + ':' + value ) } ) ;
expect ( log ) . toEqual ( [ 'bar:barVal' , 'baz:bazVal' ] ) ;
} ) ;
2013-02-11 20:25:34 +00:00
it ( 'should handle JQLite and jQuery objects like arrays' , function ( ) {
var jqObject = jqLite ( "<p><span>s1</span><span>s2</span></p>" ) . 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 ( "<p><span>a</span><span>b</span><span>c</span></p>" ) [ 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 = "<p>" +
"<a name='x'>a</a>" +
"<a name='y'>b</a>" +
"<a name='x'>c</a>" +
"</p>" ;
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' ] ) ;
} ) ;
2012-03-21 22:42:35 +00:00
} ) ;
2011-10-07 18:27:49 +00:00
describe ( 'sortedKeys' , function ( ) {
it ( 'should collect keys from object' , function ( ) {
2011-09-08 20:56:29 +00:00
expect ( sortedKeys ( { c : 0 , b : 0 , a : 0 } ) ) . toEqual ( [ 'a' , 'b' , 'c' ] ) ;
} ) ;
} ) ;
2011-02-12 18:13:28 +00:00
2011-02-17 00:48:21 +00:00
describe ( 'encodeUriSegment' , function ( ) {
2011-04-01 04:45:28 +00:00
it ( 'should correctly encode uri segment and not encode chars defined as pchar set in rfc3986' ,
2011-02-17 00:48:21 +00:00
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' ) ;
} ) ;
} ) ;
2011-04-01 04:45:28 +00:00
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' ) ;
2013-02-26 05:25:18 +00:00
//do not encode `null` as '+' when flag is used
expect ( encodeUriQuery ( 'null' , true ) ) .
toEqual ( 'null' ) ;
//do not encode `null` with no flag
expect ( encodeUriQuery ( 'null' ) ) .
toEqual ( 'null' ) ;
2011-04-01 04:45:28 +00:00
} ) ;
} ) ;
2012-01-07 02:10:47 +00:00
describe ( 'angularInit' , function ( ) {
2012-07-27 18:29:48 +00:00
var bootstrapSpy ;
2012-01-07 02:10:47 +00:00
var element ;
2011-02-12 18:13:28 +00:00
2012-01-07 02:10:47 +00:00
beforeEach ( function ( ) {
element = {
getElementById : function ( id ) {
return element . getElementById [ id ] || [ ] ;
} ,
2011-02-12 18:13:28 +00:00
2012-07-27 18:01:06 +00:00
querySelectorAll : function ( arg ) {
return element . querySelectorAll [ arg ] || [ ] ;
} ,
2012-01-07 02:10:47 +00:00
getAttribute : function ( name ) {
return element [ name ] ;
2011-10-25 15:47:02 +00:00
}
2011-02-12 18:13:28 +00:00
} ;
2012-07-27 18:29:48 +00:00
bootstrapSpy = jasmine . createSpy ( 'bootstrapSpy' ) ;
2011-03-07 07:42:25 +00:00
} ) ;
2012-01-07 02:10:47 +00:00
it ( 'should do nothing when not found' , function ( ) {
2012-07-27 18:29:48 +00:00
angularInit ( element , bootstrapSpy ) ;
expect ( bootstrapSpy ) . not . toHaveBeenCalled ( ) ;
2011-03-07 07:42:25 +00:00
} ) ;
2012-07-27 18:01:06 +00:00
it ( 'should look for ngApp directive as attr' , function ( ) {
var appElement = jqLite ( '<div ng-app="ABC"></div>' ) [ 0 ] ;
element . querySelectorAll [ '[ng-app]' ] = [ appElement ] ;
2012-07-27 18:29:48 +00:00
angularInit ( element , bootstrapSpy ) ;
expect ( bootstrapSpy ) . toHaveBeenCalledOnceWith ( appElement , [ 'ABC' ] ) ;
2012-07-27 18:01:06 +00:00
} ) ;
2012-04-09 18:20:55 +00:00
it ( 'should look for ngApp directive in id' , function ( ) {
2012-03-09 08:00:05 +00:00
var appElement = jqLite ( '<div id="ng-app" data-ng-app="ABC"></div>' ) [ 0 ] ;
2012-01-07 02:10:47 +00:00
jqLite ( document . body ) . append ( appElement ) ;
2012-07-27 18:29:48 +00:00
angularInit ( element , bootstrapSpy ) ;
expect ( bootstrapSpy ) . toHaveBeenCalledOnceWith ( appElement , [ 'ABC' ] ) ;
2011-02-12 18:13:28 +00:00
} ) ;
2012-04-09 18:20:55 +00:00
it ( 'should look for ngApp directive in className' , function ( ) {
2012-01-07 02:10:47 +00:00
var appElement = jqLite ( '<div data-ng-app="ABC"></div>' ) [ 0 ] ;
element . querySelectorAll [ '.ng\\:app' ] = [ appElement ] ;
2012-07-27 18:29:48 +00:00
angularInit ( element , bootstrapSpy ) ;
expect ( bootstrapSpy ) . toHaveBeenCalledOnceWith ( appElement , [ 'ABC' ] ) ;
2011-02-12 18:13:28 +00:00
} ) ;
2011-03-10 22:26:22 +00:00
2012-04-09 18:20:55 +00:00
it ( 'should look for ngApp directive using querySelectorAll' , function ( ) {
2012-01-07 02:10:47 +00:00
var appElement = jqLite ( '<div x-ng-app="ABC"></div>' ) [ 0 ] ;
element . querySelectorAll [ '[ng\\:app]' ] = [ appElement ] ;
2012-07-27 18:29:48 +00:00
angularInit ( element , bootstrapSpy ) ;
expect ( bootstrapSpy ) . toHaveBeenCalledOnceWith ( appElement , [ 'ABC' ] ) ;
2011-03-10 22:26:22 +00:00
} ) ;
2012-01-07 02:10:47 +00:00
it ( 'should bootstrap using class name' , function ( ) {
var appElement = jqLite ( '<div class="ng-app: ABC;"></div>' ) [ 0 ] ;
2012-07-27 18:29:48 +00:00
angularInit ( jqLite ( '<div></div>' ) . append ( appElement ) [ 0 ] , bootstrapSpy ) ;
expect ( bootstrapSpy ) . toHaveBeenCalledOnceWith ( appElement , [ 'ABC' ] ) ;
2011-03-10 22:26:22 +00:00
} ) ;
2012-01-07 02:10:47 +00:00
it ( 'should bootstrap anonymously' , function ( ) {
var appElement = jqLite ( '<div x-ng-app></div>' ) [ 0 ] ;
element . querySelectorAll [ '[x-ng-app]' ] = [ appElement ] ;
2012-07-27 18:29:48 +00:00
angularInit ( element , bootstrapSpy ) ;
expect ( bootstrapSpy ) . toHaveBeenCalledOnceWith ( appElement , [ ] ) ;
2011-03-10 22:26:22 +00:00
} ) ;
2012-01-07 02:10:47 +00:00
it ( 'should bootstrap anonymously using class only' , function ( ) {
var appElement = jqLite ( '<div class="ng-app"></div>' ) [ 0 ] ;
2012-07-27 18:29:48 +00:00
angularInit ( jqLite ( '<div></div>' ) . append ( appElement ) [ 0 ] , bootstrapSpy ) ;
expect ( bootstrapSpy ) . toHaveBeenCalledOnceWith ( appElement , [ ] ) ;
2011-03-10 22:26:22 +00:00
} ) ;
2012-01-07 02:10:47 +00:00
it ( 'should bootstrap if the annotation is on the root element' , function ( ) {
var appElement = jqLite ( '<div class="ng-app"></div>' ) [ 0 ] ;
2012-07-27 18:29:48 +00:00
angularInit ( appElement , bootstrapSpy ) ;
expect ( bootstrapSpy ) . toHaveBeenCalledOnceWith ( appElement , [ ] ) ;
} ) ;
it ( 'should complain if app module cannot be found' , function ( ) {
var appElement = jqLite ( '<div ng-app="doesntexist"></div>' ) [ 0 ] ;
expect ( function ( ) {
angularInit ( appElement , bootstrap ) ;
2013-07-02 00:23:24 +00:00
} ) . toThrowMatching (
/\[\$injector:modulerr] Failed to instantiate module doesntexist due to:\n.*\[\$injector:nomod] Module 'doesntexist' is not available! You either misspelled the module name or forgot to load it\./
) ;
2011-03-10 22:26:22 +00:00
} ) ;
} ) ;
2011-02-12 18:13:28 +00:00
describe ( 'angular service' , function ( ) {
2012-01-12 19:06:10 +00:00
it ( 'should override services' , function ( ) {
module ( function ( $provide ) {
$provide . value ( 'fake' , 'old' ) ;
$provide . value ( 'fake' , 'new' ) ;
} ) ;
inject ( function ( fake ) {
expect ( fake ) . toEqual ( 'new' ) ;
} ) ;
} ) ;
2011-02-12 18:13:28 +00:00
2011-06-22 17:14:56 +00:00
it ( 'should inject dependencies specified by $inject and ignore function argument name' , function ( ) {
2012-01-12 19:06:10 +00:00
expect ( angular . injector ( [ function ( $provide ) {
2011-11-02 04:09:54 +00:00
$provide . factory ( 'svc1' , function ( ) { return 'svc1' ; } ) ;
$provide . factory ( 'svc2' , [ 'svc1' , function ( s ) { return 'svc2-' + s ; } ] ) ;
2012-01-12 19:06:10 +00:00
} ] ) . get ( 'svc2' ) ) . toEqual ( 'svc2-svc1' ) ;
2011-06-22 17:14:56 +00:00
} ) ;
2011-02-12 18:13:28 +00:00
} ) ;
2011-11-09 01:19:54 +00:00
2011-02-12 18:13:28 +00:00
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 ) ;
} ) ;
} ) ;
2011-10-07 18:27:49 +00:00
describe ( 'compile' , function ( ) {
2011-10-26 05:21:21 +00:00
it ( 'should link to existing node and create scope' , inject ( function ( $rootScope , $compile ) {
2011-10-17 23:56:56 +00:00
var template = angular . element ( '<div>{{greeting = "hello world"}}</div>' ) ;
2011-11-23 05:28:39 +00:00
element = $compile ( template ) ( $rootScope ) ;
2011-10-17 23:56:56 +00:00
$rootScope . $digest ( ) ;
2011-02-25 22:03:02 +00:00
expect ( template . text ( ) ) . toEqual ( 'hello world' ) ;
2011-10-17 23:56:56 +00:00
expect ( $rootScope . greeting ) . toEqual ( 'hello world' ) ;
} ) ) ;
2011-02-12 18:13:28 +00:00
2011-10-26 05:21:21 +00:00
it ( 'should link to existing node and given scope' , inject ( function ( $rootScope , $compile ) {
2011-10-17 23:56:56 +00:00
var template = angular . element ( '<div>{{greeting = "hello world"}}</div>' ) ;
2011-11-23 05:28:39 +00:00
element = $compile ( template ) ( $rootScope ) ;
2011-10-17 23:56:56 +00:00
$rootScope . $digest ( ) ;
2011-02-25 22:03:02 +00:00
expect ( template . text ( ) ) . toEqual ( 'hello world' ) ;
2011-10-17 23:56:56 +00:00
} ) ) ;
2011-02-12 18:13:28 +00:00
2011-10-26 05:21:21 +00:00
it ( 'should link to new node and given scope' , inject ( function ( $rootScope , $compile ) {
2011-10-17 23:56:56 +00:00
var template = jqLite ( '<div>{{greeting = "hello world"}}</div>' ) ;
2011-02-25 22:03:02 +00:00
2011-11-23 05:28:39 +00:00
var compile = $compile ( template ) ;
2011-02-14 00:13:21 +00:00
var templateClone = template . clone ( ) ;
2011-02-25 22:03:02 +00:00
2011-11-23 05:28:39 +00:00
element = compile ( $rootScope , function ( clone ) {
2011-02-14 00:13:21 +00:00
templateClone = clone ;
} ) ;
2011-10-17 23:56:56 +00:00
$rootScope . $digest ( ) ;
2011-02-25 22:03:02 +00:00
2011-11-23 05:28:39 +00:00
expect ( template . text ( ) ) . toEqual ( '{{greeting = "hello world"}}' ) ;
2011-10-17 23:56:56 +00:00
expect ( element . text ( ) ) . toEqual ( 'hello world' ) ;
expect ( element ) . toEqual ( templateClone ) ;
expect ( $rootScope . greeting ) . toEqual ( 'hello world' ) ;
} ) ) ;
2011-10-26 05:21:21 +00:00
it ( 'should link to cloned node and create scope' , inject ( function ( $rootScope , $compile ) {
2011-10-17 23:56:56 +00:00
var template = jqLite ( '<div>{{greeting = "hello world"}}</div>' ) ;
2011-11-23 05:28:39 +00:00
element = $compile ( template ) ( $rootScope , noop ) ;
2011-10-17 23:56:56 +00:00
$rootScope . $digest ( ) ;
2011-11-23 05:28:39 +00:00
expect ( template . text ( ) ) . toEqual ( '{{greeting = "hello world"}}' ) ;
2011-10-17 23:56:56 +00:00
expect ( element . text ( ) ) . toEqual ( 'hello world' ) ;
expect ( $rootScope . greeting ) . toEqual ( 'hello world' ) ;
} ) ) ;
2010-11-07 06:50:04 +00:00
} ) ;
2011-04-07 21:36:41 +00:00
describe ( 'nodeName_' , function ( ) {
it ( 'should correctly detect node name with "namespace" when xmlns is defined' , function ( ) {
var div = jqLite ( '<div xmlns:ngtest="http://angularjs.org/">' +
2011-09-15 03:36:00 +00:00
'<ngtest:foo ngtest:attr="bar"></ngtest:foo>' +
2011-04-07 21:36:41 +00:00
'</div>' ) [ 0 ] ;
expect ( nodeName _ ( div . childNodes [ 0 ] ) ) . toBe ( 'NGTEST:FOO' ) ;
expect ( div . childNodes [ 0 ] . getAttribute ( 'ngtest:attr' ) ) . toBe ( 'bar' ) ;
} ) ;
if ( ! msie || msie >= 9 ) {
it ( 'should correctly detect node name with "namespace" when xmlns is NOT defined' , function ( ) {
var div = jqLite ( '<div xmlns:ngtest="http://angularjs.org/">' +
2012-03-09 08:00:05 +00:00
'<ngtest:foo ngtest:attr="bar"></ng-test>' +
2011-04-07 21:36:41 +00:00
'</div>' ) [ 0 ] ;
expect ( nodeName _ ( div . childNodes [ 0 ] ) ) . toBe ( 'NGTEST:FOO' ) ;
expect ( div . childNodes [ 0 ] . getAttribute ( 'ngtest:attr' ) ) . toBe ( 'bar' ) ;
} ) ;
}
} ) ;
2011-04-12 20:40:23 +00:00
2011-10-07 18:27:49 +00:00
describe ( 'nextUid()' , function ( ) {
it ( 'should return new id per call' , function ( ) {
2011-04-12 20:40:23 +00:00
var seen = { } ;
var count = 100 ;
while ( count -- ) {
var current = nextUid ( ) ;
expect ( current . match ( /[\d\w]+/ ) ) . toBeTruthy ( ) ;
expect ( seen [ current ] ) . toBeFalsy ( ) ;
seen [ current ] = true ;
}
} ) ;
} ) ;
2011-07-12 00:31:29 +00:00
describe ( 'version' , function ( ) {
it ( 'version should have full/major/minor/dot/codeName properties' , function ( ) {
expect ( version ) . toBeDefined ( ) ;
expect ( version . full ) . toBe ( '"NG_VERSION_FULL"' ) ;
expect ( version . major ) . toBe ( "NG_VERSION_MAJOR" ) ;
expect ( version . minor ) . toBe ( "NG_VERSION_MINOR" ) ;
expect ( version . dot ) . toBe ( "NG_VERSION_DOT" ) ;
expect ( version . codeName ) . toBe ( '"NG_VERSION_CODENAME"' ) ;
} ) ;
2011-07-27 20:24:07 +00:00
} ) ;
2011-11-11 04:04:15 +00:00
describe ( 'bootstrap' , function ( ) {
it ( 'should bootstrap app' , function ( ) {
var element = jqLite ( '<div>{{1+2}}</div>' ) ;
2012-01-12 19:06:10 +00:00
var injector = angular . bootstrap ( element ) ;
2011-11-11 04:04:15 +00:00
expect ( injector ) . toBeDefined ( ) ;
2013-02-21 19:55:16 +00:00
expect ( element . injector ( ) ) . toBe ( injector ) ;
2011-11-11 04:04:15 +00:00
dealoc ( element ) ;
} ) ;
2012-07-27 18:29:48 +00:00
it ( "should complain if app module can't be found" , function ( ) {
var element = jqLite ( '<div>{{1+2}}</div>' ) ;
expect ( function ( ) {
angular . bootstrap ( element , [ 'doesntexist' ] ) ;
2013-07-02 00:23:24 +00:00
} ) . toThrowMatching (
/\[\$injector:modulerr\] Failed to instantiate module doesntexist due to:\n.*\[\$injector:nomod\] Module 'doesntexist' is not available! You either misspelled the module name or forgot to load it\./ ) ;
2012-07-27 18:29:48 +00:00
expect ( element . html ( ) ) . toBe ( '{{1+2}}' ) ;
dealoc ( element ) ;
} ) ;
2013-02-21 19:55:16 +00:00
describe ( 'deferred bootstrap' , function ( ) {
var originalName = window . name ,
element ;
beforeEach ( function ( ) {
window . name = '' ;
element = jqLite ( '<div>{{1+2}}</div>' ) ;
} ) ;
afterEach ( function ( ) {
dealoc ( element ) ;
window . name = originalName ;
} ) ;
it ( 'should wait for extra modules' , function ( ) {
window . name = 'NG_DEFER_BOOTSTRAP!' ;
angular . bootstrap ( element ) ;
expect ( element . html ( ) ) . toBe ( '{{1+2}}' ) ;
angular . resumeBootstrap ( ) ;
expect ( element . html ( ) ) . toBe ( '3' ) ;
expect ( window . name ) . toEqual ( '' ) ;
} ) ;
it ( 'should load extra modules' , function ( ) {
element = jqLite ( '<div>{{1+2}}</div>' ) ;
window . name = 'NG_DEFER_BOOTSTRAP!' ;
var bootstrapping = jasmine . createSpy ( 'bootstrapping' ) ;
angular . bootstrap ( element , [ bootstrapping ] ) ;
expect ( bootstrapping ) . not . toHaveBeenCalled ( ) ;
expect ( element . injector ( ) ) . toBeUndefined ( ) ;
angular . module ( 'addedModule' , [ ] ) . value ( 'foo' , 'bar' ) ;
angular . resumeBootstrap ( [ 'addedModule' ] ) ;
expect ( bootstrapping ) . toHaveBeenCalledOnce ( ) ;
expect ( element . injector ( ) . get ( 'foo' ) ) . toEqual ( 'bar' ) ;
} ) ;
it ( 'should not defer bootstrap without window.name cue' , function ( ) {
angular . bootstrap ( element , [ ] ) ;
angular . module ( 'addedModule' , [ ] ) . value ( 'foo' , 'bar' ) ;
expect ( function ( ) {
element . injector ( ) . get ( 'foo' ) ;
2013-06-08 01:24:30 +00:00
} ) . toThrow ( '[$injector:unpr] Unknown provider: fooProvider <- foo' ) ;
2013-02-21 19:55:16 +00:00
expect ( element . injector ( ) . get ( '$http' ) ) . toBeDefined ( ) ;
} ) ;
it ( 'should restore the original window.name after bootstrap' , function ( ) {
window . name = 'NG_DEFER_BOOTSTRAP!my custom name' ;
angular . bootstrap ( element ) ;
expect ( element . html ( ) ) . toBe ( '{{1+2}}' ) ;
angular . resumeBootstrap ( ) ;
expect ( element . html ( ) ) . toBe ( '3' ) ;
expect ( window . name ) . toEqual ( 'my custom name' ) ;
} ) ;
} ) ;
2011-11-11 04:04:15 +00:00
} ) ;
2011-11-29 20:11:32 +00:00
describe ( 'startingElementHtml' , function ( ) {
it ( 'should show starting element tag only' , function ( ) {
2012-03-09 22:49:40 +00:00
expect ( startingTag ( '<ng-abc x="2A"><div>text</div></ng-abc>' ) ) .
2012-06-08 18:53:17 +00:00
toBe ( '<ng-abc x="2A">' ) ;
2011-11-29 20:11:32 +00:00
} ) ;
} ) ;
2013-02-26 05:25:18 +00:00
2013-01-19 04:51:17 +00:00
describe ( 'startingTag' , function ( ) {
it ( 'should allow passing in Nodes instead of Elements' , function ( ) {
var txtNode = document . createTextNode ( 'some text' ) ;
expect ( startingTag ( txtNode ) ) . toBe ( 'some text' ) ;
} ) ;
} ) ;
2011-11-23 05:28:39 +00:00
describe ( 'snake_case' , function ( ) {
it ( 'should convert to snake_case' , function ( ) {
expect ( snake _case ( 'ABC' ) ) . toEqual ( 'a_b_c' ) ;
expect ( snake _case ( 'alanBobCharles' ) ) . toEqual ( 'alan_bob_charles' ) ;
} ) ;
} ) ;
2012-03-28 19:21:07 +00:00
describe ( 'fromJson' , function ( ) {
it ( 'should delegate to JSON.parse' , function ( ) {
var spy = spyOn ( JSON , 'parse' ) . andCallThrough ( ) ;
expect ( fromJson ( '{}' ) ) . toEqual ( { } ) ;
expect ( spy ) . toHaveBeenCalled ( ) ;
} ) ;
} ) ;
describe ( 'toJson' , function ( ) {
it ( 'should delegate to JSON.stringify' , function ( ) {
var spy = spyOn ( JSON , 'stringify' ) . andCallThrough ( ) ;
expect ( toJson ( { } ) ) . toEqual ( '{}' ) ;
expect ( spy ) . toHaveBeenCalled ( ) ;
} ) ;
it ( 'should format objects pretty' , function ( ) {
expect ( toJson ( { a : 1 , b : 2 } , true ) ) .
toBeOneOf ( '{\n "a": 1,\n "b": 2\n}' , '{\n "a":1,\n "b":2\n}' ) ;
expect ( toJson ( { a : { b : 2 } } , true ) ) .
toBeOneOf ( '{\n "a": {\n "b": 2\n }\n}' , '{\n "a":{\n "b":2\n }\n}' ) ;
} ) ;
it ( 'should not serialize properties starting with $' , function ( ) {
expect ( toJson ( { $few : 'v' , $$some : 'value' } , false ) ) . toEqual ( '{}' ) ;
} ) ;
it ( 'should not serialize $window object' , function ( ) {
expect ( toJson ( window ) ) . toEqual ( '"$WINDOW"' ) ;
} ) ;
it ( 'should not serialize $document object' , function ( ) {
expect ( toJson ( document ) ) . toEqual ( '"$DOCUMENT"' ) ;
} ) ;
it ( 'should not serialize scope instances' , inject ( function ( $rootScope ) {
expect ( toJson ( { key : $rootScope } ) ) . toEqual ( '{"key":"$SCOPE"}' ) ;
} ) ) ;
} ) ;
2012-11-06 22:35:47 +00:00
2010-11-07 06:50:04 +00:00
} ) ;