@@ -1690,6 +1690,95 @@ describe('$location', function() {
1690
1690
expect ( $browser . url ( ) ) . toEqual ( 'http://server/' ) ;
1691
1691
} ) ) ;
1692
1692
1693
+ it ( 'should allow redirect during $locationChangeStart' ,
1694
+ inject ( function ( $location , $browser , $rootScope , $log ) {
1695
+ $rootScope . $on ( '$locationChangeStart' , function ( event , newUrl , oldUrl ) {
1696
+ $log . info ( 'before' , newUrl , oldUrl , $browser . url ( ) ) ;
1697
+ if ( newUrl === 'http://server/#/somePath' ) {
1698
+ $location . url ( '/redirectPath' ) ;
1699
+ }
1700
+ } ) ;
1701
+ $rootScope . $on ( '$locationChangeSuccess' , function ( event , newUrl , oldUrl ) {
1702
+ $log . info ( 'after' , newUrl , oldUrl , $browser . url ( ) ) ;
1703
+ } ) ;
1704
+
1705
+ $location . url ( '/somePath' ) ;
1706
+ $rootScope . $apply ( ) ;
1707
+
1708
+ expect ( $log . info . logs . shift ( ) ) .
1709
+ toEqual ( [ 'before' , 'http://server/#/somePath' , 'http://server/' , 'http://server/' ] ) ;
1710
+ expect ( $log . info . logs . shift ( ) ) .
1711
+ toEqual ( [ 'before' , 'http://server/#/redirectPath' , 'http://server/' , 'http://server/' ] ) ;
1712
+ expect ( $log . info . logs . shift ( ) ) .
1713
+ toEqual ( [ 'after' , 'http://server/#/redirectPath' , 'http://server/' ,
1714
+ 'http://server/#/redirectPath' ] ) ;
1715
+
1716
+ expect ( $location . url ( ) ) . toEqual ( '/redirectPath' ) ;
1717
+ expect ( $browser . url ( ) ) . toEqual ( 'http://server/#/redirectPath' ) ;
1718
+ } )
1719
+ ) ;
1720
+
1721
+ it ( 'should allow redirect during $locationChangeStart even if default prevented' ,
1722
+ inject ( function ( $location , $browser , $rootScope , $log ) {
1723
+ $rootScope . $on ( '$locationChangeStart' , function ( event , newUrl , oldUrl ) {
1724
+ $log . info ( 'before' , newUrl , oldUrl , $browser . url ( ) ) ;
1725
+ if ( newUrl === 'http://server/#/somePath' ) {
1726
+ event . preventDefault ( ) ;
1727
+ $location . url ( '/redirectPath' ) ;
1728
+ }
1729
+ } ) ;
1730
+ $rootScope . $on ( '$locationChangeSuccess' , function ( event , newUrl , oldUrl ) {
1731
+ $log . info ( 'after' , newUrl , oldUrl , $browser . url ( ) ) ;
1732
+ } ) ;
1733
+
1734
+ $location . url ( '/somePath' ) ;
1735
+ $rootScope . $apply ( ) ;
1736
+
1737
+ expect ( $log . info . logs . shift ( ) ) .
1738
+ toEqual ( [ 'before' , 'http://server/#/somePath' , 'http://server/' , 'http://server/' ] ) ;
1739
+ expect ( $log . info . logs . shift ( ) ) .
1740
+ toEqual ( [ 'before' , 'http://server/#/redirectPath' , 'http://server/' , 'http://server/' ] ) ;
1741
+ expect ( $log . info . logs . shift ( ) ) .
1742
+ toEqual ( [ 'after' , 'http://server/#/redirectPath' , 'http://server/' ,
1743
+ 'http://server/#/redirectPath' ] ) ;
1744
+
1745
+ expect ( $location . url ( ) ) . toEqual ( '/redirectPath' ) ;
1746
+ expect ( $browser . url ( ) ) . toEqual ( 'http://server/#/redirectPath' ) ;
1747
+ } )
1748
+ ) ;
1749
+
1750
+ it ( 'should allow multiple redirect during $locationChangeStart' ,
1751
+ inject ( function ( $location , $browser , $rootScope , $log ) {
1752
+ $rootScope . $on ( '$locationChangeStart' , function ( event , newUrl , oldUrl ) {
1753
+ $log . info ( 'before' , newUrl , oldUrl , $browser . url ( ) ) ;
1754
+ if ( newUrl === 'http://server/#/somePath' ) {
1755
+ $location . url ( '/redirectPath' ) ;
1756
+ } else if ( newUrl === 'http://server/#/redirectPath' ) {
1757
+ $location . url ( '/redirectPath2' ) ;
1758
+ }
1759
+ } ) ;
1760
+ $rootScope . $on ( '$locationChangeSuccess' , function ( event , newUrl , oldUrl ) {
1761
+ $log . info ( 'after' , newUrl , oldUrl , $browser . url ( ) ) ;
1762
+ } ) ;
1763
+
1764
+ $location . url ( '/somePath' ) ;
1765
+ $rootScope . $apply ( ) ;
1766
+
1767
+ expect ( $log . info . logs . shift ( ) ) .
1768
+ toEqual ( [ 'before' , 'http://server/#/somePath' , 'http://server/' , 'http://server/' ] ) ;
1769
+ expect ( $log . info . logs . shift ( ) ) .
1770
+ toEqual ( [ 'before' , 'http://server/#/redirectPath' , 'http://server/' , 'http://server/' ] ) ;
1771
+ expect ( $log . info . logs . shift ( ) ) .
1772
+ toEqual ( [ 'before' , 'http://server/#/redirectPath2' , 'http://server/' , 'http://server/' ] ) ;
1773
+ expect ( $log . info . logs . shift ( ) ) .
1774
+ toEqual ( [ 'after' , 'http://server/#/redirectPath2' , 'http://server/' ,
1775
+ 'http://server/#/redirectPath2' ] ) ;
1776
+
1777
+ expect ( $location . url ( ) ) . toEqual ( '/redirectPath2' ) ;
1778
+ expect ( $browser . url ( ) ) . toEqual ( 'http://server/#/redirectPath2' ) ;
1779
+ } )
1780
+ ) ;
1781
+
1693
1782
it ( 'should fire $locationChangeSuccess event when change from browser location bar' ,
1694
1783
inject ( function ( $log , $location , $browser , $rootScope ) {
1695
1784
$rootScope . $apply ( ) ; // clear initial $locationChangeStart
@@ -1715,6 +1804,66 @@ describe('$location', function() {
1715
1804
} )
1716
1805
) ;
1717
1806
1807
+ it ( 'should allow redirect during browser url change' ,
1808
+ inject ( function ( $location , $browser , $rootScope , $log ) {
1809
+ $rootScope . $on ( '$locationChangeStart' , function ( event , newUrl , oldUrl ) {
1810
+ $log . info ( 'before' , newUrl , oldUrl , $browser . url ( ) ) ;
1811
+ if ( newUrl === 'http://server/#/somePath' ) {
1812
+ $location . url ( '/redirectPath' ) ;
1813
+ }
1814
+ } ) ;
1815
+ $rootScope . $on ( '$locationChangeSuccess' , function ( event , newUrl , oldUrl ) {
1816
+ $log . info ( 'after' , newUrl , oldUrl , $browser . url ( ) ) ;
1817
+ } ) ;
1818
+
1819
+ $browser . url ( 'http://server/#/somePath' ) ;
1820
+ $browser . poll ( ) ;
1821
+
1822
+ expect ( $log . info . logs . shift ( ) ) .
1823
+ toEqual ( [ 'before' , 'http://server/#/somePath' , 'http://server/' ,
1824
+ 'http://server/#/somePath' ] ) ;
1825
+ expect ( $log . info . logs . shift ( ) ) .
1826
+ toEqual ( [ 'before' , 'http://server/#/redirectPath' , 'http://server/#/somePath' ,
1827
+ 'http://server/#/somePath' ] ) ;
1828
+ expect ( $log . info . logs . shift ( ) ) .
1829
+ toEqual ( [ 'after' , 'http://server/#/redirectPath' , 'http://server/#/somePath' ,
1830
+ 'http://server/#/redirectPath' ] ) ;
1831
+
1832
+ expect ( $location . url ( ) ) . toEqual ( '/redirectPath' ) ;
1833
+ expect ( $browser . url ( ) ) . toEqual ( 'http://server/#/redirectPath' ) ;
1834
+ } )
1835
+ ) ;
1836
+
1837
+ it ( 'should allow redirect during browser url change even if default prevented' ,
1838
+ inject ( function ( $location , $browser , $rootScope , $log ) {
1839
+ $rootScope . $on ( '$locationChangeStart' , function ( event , newUrl , oldUrl ) {
1840
+ $log . info ( 'before' , newUrl , oldUrl , $browser . url ( ) ) ;
1841
+ if ( newUrl === 'http://server/#/somePath' ) {
1842
+ event . preventDefault ( ) ;
1843
+ $location . url ( '/redirectPath' ) ;
1844
+ }
1845
+ } ) ;
1846
+ $rootScope . $on ( '$locationChangeSuccess' , function ( event , newUrl , oldUrl ) {
1847
+ $log . info ( 'after' , newUrl , oldUrl , $browser . url ( ) ) ;
1848
+ } ) ;
1849
+
1850
+ $browser . url ( 'http://server/#/somePath' ) ;
1851
+ $browser . poll ( ) ;
1852
+
1853
+ expect ( $log . info . logs . shift ( ) ) .
1854
+ toEqual ( [ 'before' , 'http://server/#/somePath' , 'http://server/' ,
1855
+ 'http://server/#/somePath' ] ) ;
1856
+ expect ( $log . info . logs . shift ( ) ) .
1857
+ toEqual ( [ 'before' , 'http://server/#/redirectPath' , 'http://server/#/somePath' ,
1858
+ 'http://server/#/somePath' ] ) ;
1859
+ expect ( $log . info . logs . shift ( ) ) .
1860
+ toEqual ( [ 'after' , 'http://server/#/redirectPath' , 'http://server/#/somePath' ,
1861
+ 'http://server/#/redirectPath' ] ) ;
1862
+
1863
+ expect ( $location . url ( ) ) . toEqual ( '/redirectPath' ) ;
1864
+ expect ( $browser . url ( ) ) . toEqual ( 'http://server/#/redirectPath' ) ;
1865
+ } )
1866
+ ) ;
1718
1867
1719
1868
it ( 'should listen on click events on href and prevent browser default in hashbang mode' , function ( ) {
1720
1869
module ( function ( ) {
0 commit comments