@@ -624,6 +624,87 @@ describe('New URL Parsing', () => {
624
624
} ) ;
625
625
} ) ;
626
626
627
+ describe ( '$location.onChange()' , ( ) => {
628
+
629
+ let $location : $locationShim ;
630
+ let upgradeModule : UpgradeModule ;
631
+
632
+ beforeEach ( ( ) => {
633
+ TestBed . configureTestingModule ( {
634
+ imports : [
635
+ CommonModule ,
636
+ LocationUpgradeTestModule . config ( { useHash : false , startUrl : 'http://host.com/' } ) ,
637
+ ] ,
638
+ providers : [ UpgradeModule ] ,
639
+ } ) ;
640
+
641
+ upgradeModule = TestBed . get ( UpgradeModule ) ;
642
+ upgradeModule . $injector = { get : injectorFactory ( ) } ;
643
+ } ) ;
644
+
645
+ beforeEach ( inject ( [ $locationShim ] , ( loc : $locationShim ) => { $location = loc ; } ) ) ;
646
+
647
+ it ( 'should have onChange method' , ( ) => { expect ( typeof $location . onChange ) . toBe ( 'function' ) ; } ) ;
648
+
649
+ it ( 'should add registered functions to changeListeners' , ( ) => {
650
+
651
+ function changeListener ( url : string , state : unknown ) { return undefined ; }
652
+ function errorHandler ( e : Error ) { }
653
+
654
+ expect ( ( $location as any ) . $$changeListeners . length ) . toBe ( 0 ) ;
655
+
656
+ $location . onChange ( changeListener , errorHandler ) ;
657
+
658
+ expect ( ( $location as any ) . $$changeListeners . length ) . toBe ( 1 ) ;
659
+ expect ( ( $location as any ) . $$changeListeners [ 0 ] [ 0 ] ) . toEqual ( changeListener ) ;
660
+ expect ( ( $location as any ) . $$changeListeners [ 0 ] [ 1 ] ) . toEqual ( errorHandler ) ;
661
+ } ) ;
662
+
663
+ it ( 'should call changeListeners when URL is updated' , ( ) => {
664
+
665
+ const onChangeVals =
666
+ { url : 'url' , state : 'state' as unknown , oldUrl : 'oldUrl' , oldState : 'oldState' as unknown } ;
667
+
668
+ function changeListener ( url : string , state : unknown , oldUrl : string , oldState : unknown ) {
669
+ onChangeVals . url = url ;
670
+ onChangeVals . state = state ;
671
+ onChangeVals . oldUrl = oldUrl ;
672
+ onChangeVals . oldState = oldState ;
673
+ }
674
+
675
+ $location . onChange ( changeListener ) ;
676
+
677
+ // Mock out setting browserUrl
678
+ ( $location as any ) . browserUrl = ( url : string , replace : boolean , state : unknown ) => { } ;
679
+
680
+ const newState = { foo : 'bar' } ;
681
+ ( $location as any ) . setBrowserUrlWithFallback ( '/newUrl' , false , newState ) ;
682
+ expect ( onChangeVals . url ) . toBe ( '/newUrl' ) ;
683
+ expect ( onChangeVals . state ) . toBe ( newState ) ;
684
+ expect ( onChangeVals . oldUrl ) . toBe ( '/' ) ;
685
+ expect ( onChangeVals . oldState ) . toBe ( null ) ;
686
+ } ) ;
687
+
688
+ it ( 'should call forward errors to error handler' , ( ) => {
689
+
690
+ let error ! : Error ;
691
+
692
+ function changeListener ( url : string , state : unknown , oldUrl : string , oldState : unknown ) {
693
+ throw new Error ( 'Handle error' ) ;
694
+ }
695
+ function errorHandler ( e : Error ) { error = e ; }
696
+
697
+ $location . onChange ( changeListener , errorHandler ) ;
698
+
699
+ // Mock out setting browserUrl
700
+ ( $location as any ) . browserUrl = ( url : string , replace : boolean , state : unknown ) => { } ;
701
+
702
+ ( $location as any ) . setBrowserUrlWithFallback ( '/newUrl' ) ;
703
+ expect ( error . message ) . toBe ( 'Handle error' ) ;
704
+ } ) ;
705
+
706
+ } ) ;
707
+
627
708
function parseLinkAndReturn ( location : $locationShim , toUrl : string , relHref ?: string ) {
628
709
const resetUrl = location . $$parseLinkUrl ( toUrl , relHref ) ;
629
710
return resetUrl && location . absUrl ( ) || undefined ;
0 commit comments