1
1
import * as React from 'react' ;
2
- import { CoreAdminContext } from 'ra-core' ;
2
+ import { CoreAdminContext , TestMemoryRouter } from 'ra-core' ;
3
3
import { render , waitFor } from '@testing-library/react' ;
4
- import { createMemoryHistory } from 'history' ;
5
4
import {
6
5
useRedirectIfAuthenticated ,
7
6
UseRedirectIfAuthenticatedOptions ,
@@ -29,19 +28,29 @@ describe('useRedirectIfAuthenticated', () => {
29
28
getPermissions : jest . fn ( ) ,
30
29
setPassword : jest . fn ( ) ,
31
30
} ;
32
- const history = createMemoryHistory ( { initialEntries : [ '/login' ] } ) ;
33
- const push = jest . spyOn ( history , 'push' ) ;
34
31
32
+ let location ;
35
33
render (
36
- < CoreAdminContext authProvider = { authProvider } history = { history } >
37
- < UseRedirectIfAuthenticated />
38
- </ CoreAdminContext >
34
+ < TestMemoryRouter
35
+ initialEntries = { [ '/login' ] }
36
+ locationCallback = { l => {
37
+ location = l ;
38
+ } }
39
+ >
40
+ < CoreAdminContext authProvider = { authProvider } >
41
+ < UseRedirectIfAuthenticated />
42
+ </ CoreAdminContext >
43
+ </ TestMemoryRouter >
39
44
) ;
40
45
41
46
expect ( authProvider . checkAuth ) . toHaveBeenCalled ( ) ;
42
- await waitFor ( ( ) => {
43
- expect ( push ) . toHaveBeenCalledTimes ( 0 ) ;
44
- } ) ;
47
+ expect ( location ) . toEqual (
48
+ expect . objectContaining ( {
49
+ hash : '' ,
50
+ pathname : '/login' ,
51
+ search : '' ,
52
+ } )
53
+ ) ;
45
54
} ) ;
46
55
47
56
test ( 'should redirect users if they are authenticated' , async ( ) => {
@@ -53,25 +62,29 @@ describe('useRedirectIfAuthenticated', () => {
53
62
getPermissions : jest . fn ( ) ,
54
63
setPassword : jest . fn ( ) ,
55
64
} ;
56
- const history = createMemoryHistory ( { initialEntries : [ '/login' ] } ) ;
57
- const push = jest . spyOn ( history , 'push' ) ;
58
65
66
+ let location ;
59
67
render (
60
- < CoreAdminContext authProvider = { authProvider } history = { history } >
61
- < UseRedirectIfAuthenticated />
62
- </ CoreAdminContext >
68
+ < TestMemoryRouter
69
+ initialEntries = { [ '/login' ] }
70
+ locationCallback = { l => {
71
+ location = l ;
72
+ } }
73
+ >
74
+ < CoreAdminContext authProvider = { authProvider } >
75
+ < UseRedirectIfAuthenticated />
76
+ </ CoreAdminContext >
77
+ </ TestMemoryRouter >
63
78
) ;
64
79
65
80
expect ( authProvider . checkAuth ) . toHaveBeenCalled ( ) ;
66
81
await waitFor ( ( ) => {
67
- expect ( push ) . toHaveBeenCalledWith (
68
- {
82
+ expect ( location ) . toEqual (
83
+ expect . objectContaining ( {
69
84
hash : '' ,
70
85
pathname : '/' ,
71
86
search : '' ,
72
- } ,
73
- undefined ,
74
- { }
87
+ } )
75
88
) ;
76
89
} ) ;
77
90
} ) ;
@@ -85,21 +98,29 @@ describe('useRedirectIfAuthenticated', () => {
85
98
getPermissions : jest . fn ( ) ,
86
99
setPassword : jest . fn ( ) ,
87
100
} ;
88
- const history = createMemoryHistory ( { initialEntries : [ '/login' ] } ) ;
89
- const push = jest . spyOn ( history , 'push' ) ;
90
101
102
+ let location ;
91
103
render (
92
- < CoreAdminContext authProvider = { authProvider } history = { history } >
93
- < UseRedirectIfAuthenticated redirectTo = "/dashboard" />
94
- </ CoreAdminContext >
104
+ < TestMemoryRouter
105
+ initialEntries = { [ '/login' ] }
106
+ locationCallback = { l => {
107
+ location = l ;
108
+ } }
109
+ >
110
+ < CoreAdminContext authProvider = { authProvider } >
111
+ < UseRedirectIfAuthenticated redirectTo = "/dashboard" />
112
+ </ CoreAdminContext >
113
+ </ TestMemoryRouter >
95
114
) ;
96
115
97
116
expect ( authProvider . checkAuth ) . toHaveBeenCalled ( ) ;
98
117
await waitFor ( ( ) => {
99
- expect ( push ) . toHaveBeenCalledWith (
100
- { hash : '' , pathname : '/dashboard' , search : '' } ,
101
- undefined ,
102
- { }
118
+ expect ( location ) . toEqual (
119
+ expect . objectContaining ( {
120
+ hash : '' ,
121
+ pathname : '/dashboard' ,
122
+ search : '' ,
123
+ } )
103
124
) ;
104
125
} ) ;
105
126
} ) ;
0 commit comments