1
1
'use strict' ;
2
2
3
- function MockWindow ( ) {
3
+ /* global getHash:true, stripHash:true */
4
+
5
+ var historyEntriesLength ;
6
+ var sniffer = { } ;
7
+
8
+ function MockWindow ( options ) {
9
+ if ( typeof options !== 'object' ) {
10
+ options = { } ;
11
+ }
4
12
var events = { } ;
5
13
var timeouts = this . timeouts = [ ] ;
14
+ var locationHref = 'http://server/' ;
15
+ var mockWindow = this ;
6
16
7
17
this . setTimeout = function ( fn ) {
8
18
return timeouts . push ( fn ) - 1 ;
@@ -36,8 +46,24 @@ function MockWindow() {
36
46
} ;
37
47
38
48
this . location = {
39
- href : 'http://server/' ,
40
- replace : noop
49
+ get href ( ) {
50
+ return locationHref ;
51
+ } ,
52
+ set href ( value ) {
53
+ locationHref = value ;
54
+ mockWindow . history . state = null ;
55
+ historyEntriesLength ++ ;
56
+ } ,
57
+ get hash ( ) {
58
+ return getHash ( locationHref ) ;
59
+ } ,
60
+ set hash ( value ) {
61
+ locationHref = stripHash ( locationHref ) + '#' + value ;
62
+ } ,
63
+ replace : function ( url ) {
64
+ locationHref = url ;
65
+ mockWindow . history . state = null ;
66
+ }
41
67
} ;
42
68
43
69
this . history = {
@@ -451,6 +477,17 @@ describe('browser', function() {
451
477
expect ( locationReplace ) . not . toHaveBeenCalled ( ) ;
452
478
} ) ;
453
479
480
+ it ( "should retain the # character when the only change is clearing the hash fragment, to prevent page reload" , function ( ) {
481
+ sniffer . history = true ;
482
+
483
+ browser . url ( 'http://server/#123' ) ;
484
+ expect ( fakeWindow . location . href ) . toEqual ( 'http://server/#123' ) ;
485
+
486
+ browser . url ( 'http://server/' ) ;
487
+ expect ( fakeWindow . location . href ) . toEqual ( 'http://server/#' ) ;
488
+
489
+ } ) ;
490
+
454
491
it ( 'should use location.replace when history.replaceState not available' , function ( ) {
455
492
sniffer . history = false ;
456
493
browser . url ( 'http://new.org' , true ) ;
@@ -462,6 +499,7 @@ describe('browser', function() {
462
499
expect ( fakeWindow . location . href ) . toEqual ( 'http://server/' ) ;
463
500
} ) ;
464
501
502
+
465
503
it ( 'should use location.replace and not use replaceState when the url only changed in the hash fragment to please IE10/11' , function ( ) {
466
504
sniffer . history = true ;
467
505
browser . url ( 'http://server/#123' , true ) ;
@@ -473,6 +511,7 @@ describe('browser', function() {
473
511
expect ( fakeWindow . location . href ) . toEqual ( 'http://server/' ) ;
474
512
} ) ;
475
513
514
+
476
515
it ( 'should return $browser to allow chaining' , function ( ) {
477
516
expect ( browser . url ( 'http://any.com' ) ) . toBe ( browser ) ;
478
517
} ) ;
@@ -835,7 +874,6 @@ describe('browser', function() {
835
874
expect ( browser . url ( ) ) . toBe ( newUrl ) ;
836
875
$rootScope . $digest ( ) ;
837
876
expect ( browser . url ( ) ) . toBe ( newUrl ) ;
838
- expect ( fakeWindow . location . href ) . toBe ( current ) ;
839
877
} ) ;
840
878
} ) ;
841
879
0 commit comments