@@ -4,21 +4,17 @@ import { render, unmountComponentAtNode } from 'react-dom'
4
4
import createHistory from '../createMemoryHistory'
5
5
import Route from '../Route'
6
6
import Router from '../Router'
7
- import routerShape from '../PropTypes'
8
7
import withRouter from '../withRouter'
9
8
10
9
describe ( 'withRouter' , function ( ) {
11
- class App extends Component {
12
- propTypes : {
13
- router : routerShape . isRequired
14
- }
15
- testFunction ( ) {
16
- return 'hello from the test function'
17
- }
18
- render ( ) {
19
- expect ( this . props . router ) . toExist ( )
20
- return < h1 > App</ h1 >
21
- }
10
+ const routerStub = {
11
+ push ( ) { } ,
12
+ replace ( ) { } ,
13
+ go ( ) { } ,
14
+ goBack ( ) { } ,
15
+ goForward ( ) { } ,
16
+ setRouteLeaveHook ( ) { } ,
17
+ isActive ( ) { }
22
18
}
23
19
24
20
let node
@@ -30,51 +26,63 @@ describe('withRouter', function () {
30
26
unmountComponentAtNode ( node )
31
27
} )
32
28
33
- it ( 'puts router on context' , function ( done ) {
34
- const WrappedApp = withRouter ( App )
29
+ it ( 'should put router on props' , function ( done ) {
30
+ const MyComponent = withRouter ( ( { router } ) => {
31
+ expect ( router ) . toExist ( )
32
+ done ( )
33
+ return null
34
+ } )
35
+
36
+ function App ( ) {
37
+ return < MyComponent /> // Ensure no props are passed explicitly.
38
+ }
35
39
36
40
render ( (
37
41
< Router history = { createHistory ( '/' ) } >
38
- < Route path = "/" component = { WrappedApp } />
42
+ < Route path = "/" component = { App } />
39
43
</ Router >
40
- ) , node , function ( ) {
41
- done ( )
42
- } )
44
+ ) , node )
43
45
} )
44
46
45
- it ( 'still uses router prop if provided' , function ( done ) {
46
- const Test = withRouter ( function ( props ) {
47
- props . test ( props )
47
+ it ( 'should set displayName' , function ( ) {
48
+ function MyComponent ( ) {
48
49
return null
49
- } )
50
- const router = {
51
- push ( ) { } ,
52
- replace ( ) { } ,
53
- go ( ) { } ,
54
- goBack ( ) { } ,
55
- goForward ( ) { } ,
56
- setRouteLeaveHook ( ) { } ,
57
- isActive ( ) { }
58
- }
59
- const test = function ( props ) {
60
- expect ( props . router ) . toBe ( router )
61
50
}
62
51
63
- render ( < Test router = { router } test = { test } /> , node , done )
52
+ MyComponent . displayName = 'MyComponent'
53
+
54
+ expect ( withRouter ( MyComponent ) . displayName )
55
+ . toEqual ( 'withRouter(MyComponent)' )
56
+ } )
57
+
58
+ it ( 'should use router prop if specified' , function ( done ) {
59
+ const MyComponent = withRouter ( ( { router } ) => {
60
+ expect ( router ) . toBe ( routerStub )
61
+ done ( )
62
+ return null
63
+ } )
64
+
65
+ render ( < MyComponent router = { routerStub } /> , node )
64
66
} )
65
67
66
- it ( 'should support withRefs as a parameter' , function ( done ) {
67
- const WrappedApp = withRouter ( App , { withRef : true } )
68
- const router = {
69
- push ( ) { } ,
70
- replace ( ) { } ,
71
- go ( ) { } ,
72
- goBack ( ) { } ,
73
- goForward ( ) { } ,
74
- setRouteLeaveHook ( ) { } ,
75
- isActive ( ) { }
68
+ it ( 'should support withRef' , function ( ) {
69
+ const spy = expect . createSpy ( )
70
+
71
+ class MyComponent extends Component {
72
+ invokeSpy ( ) {
73
+ spy ( )
74
+ }
75
+
76
+ render ( ) {
77
+ return null
78
+ }
76
79
}
77
- const component = render ( ( < WrappedApp router = { router } /> ) , node , done )
78
- expect ( component . getWrappedInstance ( ) . testFunction ( ) ) . toEqual ( 'hello from the test function' )
80
+
81
+ const WrappedComponent = withRouter ( MyComponent , { withRef : true } )
82
+
83
+ const instance = render ( < WrappedComponent router = { routerStub } /> , node )
84
+ instance . getWrappedInstance ( ) . invokeSpy ( )
85
+
86
+ expect ( spy ) . toHaveBeenCalled ( )
79
87
} )
80
88
} )
0 commit comments