1
-
2
- import { services , UrlMatcher } from '../src/index' ;
1
+ import { UrlMatcher } from "../src/index" ;
3
2
import { UIRouter } from "../src/router" ;
4
- import * as vanilla from "../src/vanilla"
3
+ import { UrlService } from "../src/url/urlService" ;
4
+ import * as vanilla from "../src/vanilla" ;
5
5
6
6
describe ( 'browserHistory implementation' , ( ) => {
7
7
8
8
let router : UIRouter ;
9
- let locationProvider ;
9
+ let urlService : UrlService ;
10
10
let makeMatcher ;
11
11
12
- // Replace the `history` reference because PhantomJS does not support spying on it.
13
- function mockHistoryObject ( ) {
14
- let plugin : any = router . getPlugin ( 'vanilla.pushStateLocation' ) ;
12
+ let mockHistory , mockLocation ;
13
+
14
+ // Replace the history and location
15
+ function mockPushState ( _router ) {
16
+ let plugin : any = _router . getPlugin ( 'vanilla.pushStateLocation' ) ;
17
+
18
+ mockHistory = {
19
+ replaceState : ( a , b , url ) => mockLocation . href = url ,
20
+ pushState : ( a , b , url ) => mockLocation . href = url
21
+ } ;
15
22
16
- plugin . service . _history = {
17
- replaceState : ( ) => null ,
18
- pushState : ( ) => null
23
+ mockLocation = {
24
+ _href : "/" ,
25
+ pathname : "/" ,
26
+ search : "" ,
27
+ get href ( ) {
28
+ return this . _href
29
+ } ,
30
+ set href ( val ) {
31
+ this . _href = val ;
32
+ var [ pathname , search ] = val . split ( "?" ) ;
33
+ this . pathname = pathname ;
34
+ this . search = search ? "?" + search : "" ;
35
+ }
19
36
} ;
20
37
38
+ plugin . service . _history = mockHistory ;
39
+ plugin . service . _location = mockLocation ;
40
+
21
41
return plugin . service ;
22
42
}
23
43
@@ -26,7 +46,7 @@ describe('browserHistory implementation', () => {
26
46
router . plugin ( vanilla . servicesPlugin ) ;
27
47
router . plugin ( vanilla . pushStateLocationPlugin ) ;
28
48
router . stateRegistry . stateQueue . autoFlush ( router . stateService ) ;
29
- locationProvider = router . urlService ;
49
+ urlService = router . urlService ;
30
50
makeMatcher = ( url , config ?) => {
31
51
return new UrlMatcher ( url , router . urlMatcherFactory . paramTypes , config )
32
52
} ;
@@ -38,34 +58,41 @@ describe('browserHistory implementation', () => {
38
58
} ) ;
39
59
40
60
it ( 'uses history.pushState when setting a url' , ( ) => {
41
- let service = mockHistoryObject ( ) ;
61
+ let service = mockPushState ( router ) ;
42
62
expect ( router . urlService . config . html5Mode ( ) ) . toBe ( true ) ;
43
63
let stub = spyOn ( service . _history , 'pushState' ) ;
44
64
router . urlRouter . push ( makeMatcher ( '/hello/:name' ) , { name : 'world' } , { } ) ;
45
65
expect ( stub . calls . first ( ) . args [ 2 ] ) . toBe ( '/hello/world' ) ;
46
66
} ) ;
47
67
48
68
it ( 'uses history.replaceState when setting a url with replace' , ( ) => {
49
- let service = mockHistoryObject ( ) ;
69
+ let service = mockPushState ( router ) ;
50
70
let stub = spyOn ( service . _history , 'replaceState' ) ;
51
71
router . urlRouter . push ( makeMatcher ( '/hello/:name' ) , { name : 'world' } , { replace : true } ) ;
52
72
expect ( stub . calls . first ( ) . args [ 2 ] ) . toBe ( '/hello/world' ) ;
53
73
} ) ;
54
74
55
- it ( 'returns the correct url query' , ( ) => {
75
+ it ( 'returns the correct url query' , async ( done ) => {
76
+ let service = mockPushState ( router ) ;
56
77
expect ( router . urlService . config . html5Mode ( ) ) . toBe ( true ) ;
57
- return router . stateService . go ( 'path' , { urlParam : 'bar' } ) . then ( ( ) => {
58
- expect ( window . location . toString ( ) . includes ( '/path/bar' ) ) . toBe ( true ) ;
59
- expect ( window . location . toString ( ) . includes ( '/#/path/bar' ) ) . toBe ( false ) ;
60
- expect ( locationProvider . path ( ) ) . toBe ( '/path/bar' ) ;
61
- expect ( locationProvider . search ( ) ) . toEqual ( { '' :'' } ) ;
62
- return router . stateService . go ( 'path' , { urlParam : 'bar' , queryParam : 'query' } ) ;
63
- } ) . then ( ( ) => {
64
- expect ( window . location . toString ( ) . includes ( '/path/bar?queryParam=query' ) ) . toBe ( true ) ;
65
- expect ( window . location . toString ( ) . includes ( '/#/path/bar?queryParam=query' ) ) . toBe ( false ) ;
66
- expect ( locationProvider . path ( ) ) . toBe ( '/path/bar' ) ;
67
- expect ( locationProvider . search ( ) ) . toEqual ( { queryParam :'query' } ) ;
68
- } ) ;
78
+
79
+ await router . stateService . go ( 'path' , { urlParam : 'bar' } ) ;
80
+
81
+ expect ( mockLocation . href . includes ( '/path/bar' ) ) . toBe ( true ) ;
82
+ expect ( mockLocation . href . includes ( '#' ) ) . toBe ( false ) ;
83
+
84
+ expect ( urlService . path ( ) ) . toBe ( '/path/bar' ) ;
85
+ expect ( urlService . search ( ) ) . toEqual ( { } ) ;
86
+
87
+ await router . stateService . go ( 'path' , { urlParam : 'bar' , queryParam : 'query' } ) ;
88
+
89
+ expect ( mockLocation . href . includes ( '/path/bar?queryParam=query' ) ) . toBe ( true ) ;
90
+ expect ( mockLocation . href . includes ( '#' ) ) . toBe ( false ) ;
91
+
92
+ expect ( urlService . path ( ) ) . toBe ( '/path/bar' ) ;
93
+ expect ( urlService . search ( ) ) . toEqual ( { queryParam : 'query' } ) ;
94
+
95
+ done ( ) ;
69
96
} ) ;
70
97
71
98
} ) ;
0 commit comments