11'use strict' ;
22
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+ }
412 var events = { } ;
513 var timeouts = this . timeouts = [ ] ;
14+ var locationHref = 'http://server/' ;
15+ var mockWindow = this ;
616
717 this . setTimeout = function ( fn ) {
818 return timeouts . push ( fn ) - 1 ;
@@ -36,8 +46,24 @@ function MockWindow() {
3646 } ;
3747
3848 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+ }
4167 } ;
4268
4369 this . history = {
@@ -451,6 +477,17 @@ describe('browser', function() {
451477 expect ( locationReplace ) . not . toHaveBeenCalled ( ) ;
452478 } ) ;
453479
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+
454491 it ( 'should use location.replace when history.replaceState not available' , function ( ) {
455492 sniffer . history = false ;
456493 browser . url ( 'http://new.org' , true ) ;
@@ -462,6 +499,7 @@ describe('browser', function() {
462499 expect ( fakeWindow . location . href ) . toEqual ( 'http://server/' ) ;
463500 } ) ;
464501
502+
465503 it ( 'should use location.replace and not use replaceState when the url only changed in the hash fragment to please IE10/11' , function ( ) {
466504 sniffer . history = true ;
467505 browser . url ( 'http://server/#123' , true ) ;
@@ -473,6 +511,7 @@ describe('browser', function() {
473511 expect ( fakeWindow . location . href ) . toEqual ( 'http://server/' ) ;
474512 } ) ;
475513
514+
476515 it ( 'should return $browser to allow chaining' , function ( ) {
477516 expect ( browser . url ( 'http://any.com' ) ) . toBe ( browser ) ;
478517 } ) ;
@@ -835,7 +874,6 @@ describe('browser', function() {
835874 expect ( browser . url ( ) ) . toBe ( newUrl ) ;
836875 $rootScope . $digest ( ) ;
837876 expect ( browser . url ( ) ) . toBe ( newUrl ) ;
838- expect ( fakeWindow . location . href ) . toBe ( current ) ;
839877 } ) ;
840878 } ) ;
841879
0 commit comments