1
1
import * as React from 'react' ;
2
2
import * as PropTypes from 'prop-types' ;
3
3
import { mount } from 'enzyme' ;
4
-
4
+ import { pushStateLocationPlugin } from '@uirouter/core' ;
5
5
import {
6
6
UIRouter ,
7
7
UIRouterContext ,
8
8
UIRouterReact ,
9
9
memoryLocationPlugin ,
10
+ ReactStateDeclaration ,
11
+ servicesPlugin ,
10
12
} from '../../index' ;
11
13
12
14
class Child extends React . Component < any , any > {
@@ -20,21 +22,19 @@ class Child extends React.Component<any, any> {
20
22
21
23
describe ( '<UIRouter>' , ( ) => {
22
24
it ( 'throws an error if no plugin or router instance is passed via prop' , ( ) => {
23
- expect ( ( ) => {
24
- const wrapper = mount (
25
+ expect ( ( ) =>
26
+ mount (
25
27
< UIRouter >
26
28
< Child />
27
29
</ UIRouter >
28
- ) ;
29
- } ) . toThrow ( ) ;
30
+ )
31
+ ) . toThrow ( ) ;
30
32
} ) ;
31
33
32
34
it ( 'creates a router instance' , ( ) => {
33
35
const wrapper = mount (
34
36
< UIRouter plugins = { [ memoryLocationPlugin ] } states = { [ ] } >
35
- < UIRouterContext . Consumer >
36
- { router => < Child router = { router } /> }
37
- </ UIRouterContext . Consumer >
37
+ < UIRouterContext . Consumer > { router => < Child router = { router } /> } </ UIRouterContext . Consumer >
38
38
</ UIRouter >
39
39
) ;
40
40
expect ( wrapper . find ( Child ) . props ( ) . router ) . toBeDefined ( ) ;
@@ -45,32 +45,34 @@ describe('<UIRouter>', () => {
45
45
router . plugin ( memoryLocationPlugin ) ;
46
46
const wrapper = mount (
47
47
< UIRouter router = { router } >
48
- < UIRouterContext . Consumer >
49
- { router => < Child router = { router } /> }
50
- </ UIRouterContext . Consumer >
48
+ < UIRouterContext . Consumer > { router => < Child router = { router } /> } </ UIRouterContext . Consumer >
51
49
</ UIRouter >
52
50
) ;
53
51
expect ( wrapper . find ( Child ) . props ( ) . router ) . toBe ( router ) ;
54
52
} ) ;
55
53
56
54
describe ( '<UIRouterCosumer>' , ( ) => {
57
55
it ( 'passes down the router instance' , ( ) => {
58
- const wrapper = mount (
56
+ let router ;
57
+
58
+ mount (
59
59
< UIRouter plugins = { [ memoryLocationPlugin ] } >
60
60
< UIRouterContext . Consumer >
61
- { router => {
62
- expect ( router ) . toBeInstanceOf ( UIRouterReact ) ;
61
+ { _router => {
62
+ router = _router ;
63
63
return null ;
64
64
} }
65
65
</ UIRouterContext . Consumer >
66
66
</ UIRouter >
67
67
) ;
68
+
69
+ expect ( router ) . toBeInstanceOf ( UIRouterReact ) ;
68
70
} ) ;
69
71
70
72
it ( 'passes down the correct router instance when passed via props' , ( ) => {
71
73
const router = new UIRouterReact ( ) ;
72
74
router . plugin ( memoryLocationPlugin ) ;
73
- const wrapper = mount (
75
+ mount (
74
76
< UIRouter router = { router } >
75
77
< UIRouterContext . Consumer >
76
78
{ _router => {
@@ -83,3 +85,14 @@ describe('<UIRouter>', () => {
83
85
} ) ;
84
86
} ) ;
85
87
} ) ;
88
+
89
+ export const makeTestRouter = ( states : ReactStateDeclaration [ ] ) => {
90
+ const router = new UIRouterReact ( ) ;
91
+ router . plugin ( servicesPlugin ) ;
92
+ router . plugin ( pushStateLocationPlugin ) ;
93
+ states . forEach ( state => router . stateRegistry . register ( state ) ) ;
94
+
95
+ const mountInRouter : typeof mount = ( children , opts ) => mount ( < UIRouter router = { router } > { children } </ UIRouter > , opts ) ;
96
+
97
+ return { router, mountInRouter } ;
98
+ } ;
0 commit comments