@@ -495,6 +495,19 @@ describe('browser', function() {
495
495
browser . url ( current ) ;
496
496
expect ( fakeWindow . location . href ) . toBe ( 'dontchange' ) ;
497
497
} ) ;
498
+
499
+ it ( 'should not read out location.href if a reload was triggered but still allow to change the url' , function ( ) {
500
+ sniffer . history = false ;
501
+ browser . url ( 'http://server/someOtherUrlThatCausesReload' ) ;
502
+ expect ( fakeWindow . location . href ) . toBe ( 'http://server/someOtherUrlThatCausesReload' ) ;
503
+
504
+ fakeWindow . location . href = 'http://someNewUrl' ;
505
+ expect ( browser . url ( ) ) . toBe ( 'http://server/someOtherUrlThatCausesReload' ) ;
506
+
507
+ browser . url ( 'http://server/someOtherUrl' ) ;
508
+ expect ( browser . url ( ) ) . toBe ( 'http://server/someOtherUrl' ) ;
509
+ expect ( fakeWindow . location . href ) . toBe ( 'http://server/someOtherUrl' ) ;
510
+ } ) ;
498
511
} ) ;
499
512
500
513
describe ( 'urlChange' , function ( ) {
@@ -573,15 +586,15 @@ describe('browser', function() {
573
586
beforeEach ( function ( ) {
574
587
sniffer . history = false ;
575
588
sniffer . hashchange = false ;
576
- browser . url ( "http://server. current" ) ;
589
+ browser . url ( "http://server/# current" ) ;
577
590
} ) ;
578
591
579
592
it ( 'should fire callback with the correct URL on location change outside of angular' , function ( ) {
580
593
browser . onUrlChange ( callback ) ;
581
594
582
- fakeWindow . location . href = 'http://server. new' ;
595
+ fakeWindow . location . href = 'http://server/# new' ;
583
596
fakeWindow . setTimeout . flush ( ) ;
584
- expect ( callback ) . toHaveBeenCalledWith ( 'http://server. new' ) ;
597
+ expect ( callback ) . toHaveBeenCalledWith ( 'http://server/# new' ) ;
585
598
586
599
fakeWindow . fire ( 'popstate' ) ;
587
600
fakeWindow . fire ( 'hashchange' ) ;
@@ -645,33 +658,134 @@ describe('browser', function() {
645
658
646
659
describe ( 'integration tests with $location' , function ( ) {
647
660
648
- beforeEach ( module ( function ( $provide , $locationProvider ) {
649
- spyOn ( fakeWindow . history , 'pushState' ) . andCallFake ( function ( stateObj , title , newUrl ) {
650
- fakeWindow . location . href = newUrl ;
661
+ function setup ( options ) {
662
+ module ( function ( $provide , $locationProvider ) {
663
+ spyOn ( fakeWindow . history , 'pushState' ) . andCallFake ( function ( stateObj , title , newUrl ) {
664
+ fakeWindow . location . href = newUrl ;
665
+ } ) ;
666
+ spyOn ( fakeWindow . location , 'replace' ) . andCallFake ( function ( newUrl ) {
667
+ fakeWindow . location . href = newUrl ;
668
+ } ) ;
669
+ $provide . value ( '$browser' , browser ) ;
670
+ browser . pollFns = [ ] ;
671
+
672
+ sniffer . history = options . history ;
673
+ $provide . value ( '$sniffer' , sniffer ) ;
674
+
675
+ $locationProvider . html5Mode ( options . html5Mode ) ;
651
676
} ) ;
652
- $provide . value ( '$browser' , browser ) ;
653
- browser . pollFns = [ ] ;
677
+ }
654
678
655
- sniffer . history = true ;
656
- $provide . value ( '$sniffer ', sniffer ) ;
679
+ describe ( 'update $location when it was changed outside of Angular in sync ' +
680
+ 'before $digest was called ', function ( ) {
657
681
658
- $locationProvider . html5Mode ( true ) ;
659
- } ) ) ;
682
+ it ( 'should work with no history support, no html5Mode' , function ( ) {
683
+ setup ( {
684
+ history : false ,
685
+ html5Mode : false
686
+ } ) ;
687
+ inject ( function ( $rootScope , $location ) {
688
+ $rootScope . $apply ( function ( ) {
689
+ $location . path ( '/initialPath' ) ;
690
+ } ) ;
691
+ expect ( fakeWindow . location . href ) . toBe ( 'http://server/#/initialPath' ) ;
660
692
661
- it ( 'should update $location when it was changed outside of Angular in sync ' +
662
- 'before $digest was called' , function ( ) {
663
- inject ( function ( $rootScope , $location ) {
664
- fakeWindow . history . pushState ( null , '' , 'http://server/someTestHash' ) ;
693
+ fakeWindow . location . href = 'http://server/#/someTestHash' ;
665
694
666
- // Verify that infinite digest reported in #6976 no longer occurs
667
- expect ( function ( ) {
668
695
$rootScope . $digest ( ) ;
669
- } ) . not . toThrow ( ) ;
670
696
671
- expect ( $location . path ( ) ) . toBe ( '/someTestHash' ) ;
697
+ expect ( $location . path ( ) ) . toBe ( '/someTestHash' ) ;
698
+ } ) ;
672
699
} ) ;
700
+
701
+ it ( 'should work with history support, no html5Mode' , function ( ) {
702
+ setup ( {
703
+ history : true ,
704
+ html5Mode : false
705
+ } ) ;
706
+ inject ( function ( $rootScope , $location ) {
707
+ $rootScope . $apply ( function ( ) {
708
+ $location . path ( '/initialPath' ) ;
709
+ } ) ;
710
+ expect ( fakeWindow . location . href ) . toBe ( 'http://server/#/initialPath' ) ;
711
+
712
+ fakeWindow . location . href = 'http://server/#/someTestHash' ;
713
+
714
+ $rootScope . $digest ( ) ;
715
+
716
+ expect ( $location . path ( ) ) . toBe ( '/someTestHash' ) ;
717
+ } ) ;
718
+ } ) ;
719
+
720
+ it ( 'should work with no history support, with html5Mode' , function ( ) {
721
+ setup ( {
722
+ history : false ,
723
+ html5Mode : true
724
+ } ) ;
725
+ inject ( function ( $rootScope , $location ) {
726
+ $rootScope . $apply ( function ( ) {
727
+ $location . path ( '/initialPath' ) ;
728
+ } ) ;
729
+ expect ( fakeWindow . location . href ) . toBe ( 'http://server/#/initialPath' ) ;
730
+
731
+ fakeWindow . location . href = 'http://server/#/someTestHash' ;
732
+
733
+ $rootScope . $digest ( ) ;
734
+
735
+ expect ( $location . path ( ) ) . toBe ( '/someTestHash' ) ;
736
+ } ) ;
737
+ } ) ;
738
+
739
+ it ( 'should work with history support, with html5Mode' , function ( ) {
740
+ setup ( {
741
+ history : true ,
742
+ html5Mode : true
743
+ } ) ;
744
+ inject ( function ( $rootScope , $location ) {
745
+ $rootScope . $apply ( function ( ) {
746
+ $location . path ( '/initialPath' ) ;
747
+ } ) ;
748
+ expect ( fakeWindow . location . href ) . toBe ( 'http://server/initialPath' ) ;
749
+
750
+ fakeWindow . location . href = 'http://server/someTestHash' ;
751
+
752
+ $rootScope . $digest ( ) ;
753
+
754
+ expect ( $location . path ( ) ) . toBe ( '/someTestHash' ) ;
755
+ } ) ;
756
+ } ) ;
757
+
673
758
} ) ;
674
759
760
+ it ( 'should not reload the page multiple times when the page will be reloaded due to url rewrite on load' , function ( ) {
761
+ setup ( {
762
+ history : false ,
763
+ html5Mode : true
764
+ } ) ;
765
+ fakeWindow . location . href = 'http://server/some/deep/path' ;
766
+ var changeUrlCount = 0 ;
767
+ var _url = browser . url ;
768
+ browser . url = function ( newUrl , replace ) {
769
+ if ( newUrl ) {
770
+ changeUrlCount ++ ;
771
+ }
772
+ return _url . call ( this , newUrl , replace ) ;
773
+ } ;
774
+ spyOn ( browser , 'url' ) . andCallThrough ( ) ;
775
+ inject ( function ( $rootScope , $location ) {
776
+ $rootScope . $digest ( ) ;
777
+ $rootScope . $digest ( ) ;
778
+ $rootScope . $digest ( ) ;
779
+ $rootScope . $digest ( ) ;
780
+
781
+ // from $location for rewriting the initial url into a hash url
782
+ expect ( browser . url ) . toHaveBeenCalledWith ( 'http://server/#/some/deep/path' , true ) ;
783
+ // from the initial call to the watch in $location for watching $location
784
+ expect ( browser . url ) . toHaveBeenCalledWith ( 'http://server/#/some/deep/path' , false ) ;
785
+ expect ( changeUrlCount ) . toBe ( 2 ) ;
786
+ } ) ;
787
+
788
+ } ) ;
675
789
} ) ;
676
790
677
791
describe ( 'integration test with $rootScope' , function ( ) {
0 commit comments