1
+ import { fireEvent } from '@testing-library/react' ;
1
2
import * as React from 'react' ;
2
3
import { UISref , UIView } from '../../components' ;
3
4
import { UISrefActiveContext } from '../UISrefActive' ;
@@ -21,22 +22,25 @@ const states = [
21
22
] ;
22
23
23
24
describe ( '<UISref>' , ( ) => {
24
- beforeAll ( ( ) => jest . spyOn ( React , 'useEffect' ) . mockImplementation ( React . useLayoutEffect ) ) ;
25
- afterAll ( ( ) => ( React . useEffect as any ) . mockRestore ( ) ) ;
25
+ let mockUseEffect : any ;
26
+ beforeEach ( ( ) => ( mockUseEffect = jest . spyOn ( React , 'useEffect' ) . mockImplementation ( React . useLayoutEffect ) ) ) ;
27
+ afterEach ( ( ) => mockUseEffect . mockRestore ( ) ) ;
26
28
27
29
let { router, routerGo, mountInRouter } = makeTestRouter ( [ ] ) ;
28
30
beforeEach ( ( ) => ( { router, routerGo, mountInRouter } = makeTestRouter ( states ) ) ) ;
29
31
30
32
it ( 'renders its child with injected props' , async ( ) => {
31
33
const wrapper = mountInRouter (
32
34
< UISref to = "state2" >
33
- < a > state2</ a >
35
+ < a data-testid = "anchor" > state2</ a >
34
36
</ UISref >
35
37
) ;
36
38
await routerGo ( 'state' ) ;
37
- const props = wrapper . find ( 'a' ) . props ( ) ;
38
- expect ( typeof props . onClick ) . toBe ( 'function' ) ;
39
- expect ( props . href . includes ( '/state2' ) ) . toBe ( true ) ;
39
+ const goSpy = jest . spyOn ( router . stateService , 'go' ) ;
40
+ const anchor = wrapper . getByTestId ( 'anchor' ) ;
41
+ expect ( anchor . getAttribute ( 'href' ) . includes ( '/state2' ) ) . toBe ( true ) ;
42
+ anchor . click ( ) ;
43
+ expect ( goSpy ) . toHaveBeenCalledTimes ( 1 ) ;
40
44
} ) ;
41
45
42
46
it ( 'throws if state name is not a string' , ( ) => {
@@ -50,27 +54,26 @@ describe('<UISref>', () => {
50
54
const deregisterFn = jest . fn ( ) ;
51
55
const parentUISrefActiveAddStateFn = jest . fn ( ( ) => deregisterFn ) ;
52
56
53
- const wrapper = mountInRouter (
57
+ mountInRouter (
54
58
< UISrefActiveContext . Provider value = { parentUISrefActiveAddStateFn } >
55
59
< UIView />
56
60
</ UISrefActiveContext . Provider >
57
61
) ;
58
62
59
- expect ( wrapper . html ( ) ) . toBe ( '<a href="/state2" class="">state2</a>' ) ;
60
63
expect ( parentUISrefActiveAddStateFn ) . toHaveBeenCalled ( ) ;
61
64
await routerGo ( 'state2' ) ;
62
65
expect ( deregisterFn ) . toHaveBeenCalled ( ) ;
63
66
} ) ;
64
67
65
68
it ( 'triggers a transition to target state' , async ( ) => {
66
69
const goSpy = jest . spyOn ( router . stateService , 'go' ) ;
67
- const wrapper = mountInRouter (
70
+ const rendered = mountInRouter (
68
71
< UISref to = "state2" >
69
- < a />
72
+ < a data-testid = "anchor" />
70
73
</ UISref >
71
74
) ;
72
75
73
- wrapper . find ( 'a ') . simulate ( ' click' ) ;
76
+ rendered . getByTestId ( 'anchor ') . click ( ) ;
74
77
75
78
expect ( goSpy ) . toHaveBeenCalledTimes ( 1 ) ;
76
79
expect ( goSpy ) . toHaveBeenCalledWith ( 'state2' , expect . anything ( ) , expect . anything ( ) ) ;
@@ -80,12 +83,14 @@ describe('<UISref>', () => {
80
83
const log = [ ] ;
81
84
const goSpy = jest . spyOn ( router . stateService , 'go' ) . mockImplementation ( ( ) => log . push ( 'go' ) as any ) ;
82
85
const onClickSpy = jest . fn ( ( ) => log . push ( 'onClick' ) ) ;
83
- const wrapper = mountInRouter (
86
+ const rendered = mountInRouter (
84
87
< UISref to = "state2" >
85
- < a onClick = { onClickSpy } > state2</ a >
88
+ < a data-testid = "anchor" onClick = { onClickSpy } >
89
+ state2
90
+ </ a >
86
91
</ UISref >
87
92
) ;
88
- wrapper . find ( 'a ') . simulate ( ' click' ) ;
93
+ rendered . getByTestId ( 'anchor ') . click ( ) ;
89
94
90
95
expect ( onClickSpy ) . toHaveBeenCalled ( ) ;
91
96
expect ( goSpy ) . toHaveBeenCalled ( ) ;
@@ -95,68 +100,71 @@ describe('<UISref>', () => {
95
100
it ( 'calls the child elements onClick function and honors e.preventDefault()' , async ( ) => {
96
101
const goSpy = jest . spyOn ( router . stateService , 'go' ) ;
97
102
const onClickSpy = jest . fn ( ( e ) => e . preventDefault ( ) ) ;
98
- const wrapper = mountInRouter (
103
+ const rendered = mountInRouter (
99
104
< UISref to = "state2" >
100
- < a onClick = { onClickSpy } > state2</ a >
105
+ < a data-testid = "anchor" onClick = { onClickSpy } >
106
+ state2
107
+ </ a >
101
108
</ UISref >
102
109
) ;
103
- wrapper . find ( 'a ') . simulate ( ' click' ) ;
110
+ rendered . getByTestId ( 'anchor ') . click ( ) ;
104
111
105
112
expect ( onClickSpy ) . toHaveBeenCalled ( ) ;
106
113
expect ( goSpy ) . not . toHaveBeenCalled ( ) ;
107
114
} ) ;
108
115
109
116
it ( "doesn't trigger a transition when middle-clicked" , async ( ) => {
110
117
const stateServiceGoSpy = jest . spyOn ( router . stateService , 'go' ) ;
111
- const wrapper = mountInRouter (
118
+ const rendered = mountInRouter (
112
119
< UISref to = "state2" >
113
- < a > state2</ a >
120
+ < a data-testid = "anchor" > state2</ a >
114
121
</ UISref >
115
122
) ;
116
123
117
- const link = wrapper . find ( 'a ') ;
118
- link . simulate ( ' click' ) ;
124
+ const link = rendered . getByTestId ( 'anchor ') ;
125
+ link . click ( ) ;
119
126
expect ( stateServiceGoSpy ) . toHaveBeenCalledTimes ( 1 ) ;
120
127
121
- link . simulate ( 'click' , { button : 1 } ) ;
128
+ fireEvent ( link , new MouseEvent ( 'click' , { button : 1 } ) ) ;
122
129
expect ( stateServiceGoSpy ) . toHaveBeenCalledTimes ( 1 ) ;
123
130
} ) ;
124
131
125
132
it ( "doesn't trigger a transition when ctrl/meta/shift/alt+clicked" , async ( ) => {
126
133
const stateServiceGoSpy = jest . spyOn ( router . stateService , 'go' ) ;
127
- const wrapper = mountInRouter (
134
+ const rendered = mountInRouter (
128
135
< UISref to = "state2" >
129
- < a > state2</ a >
136
+ < a data-testid = "anchor" > state2</ a >
130
137
</ UISref >
131
138
) ;
132
139
133
- const link = wrapper . find ( 'a ') ;
134
- link . simulate ( ' click' ) ;
140
+ const link = rendered . getByTestId ( 'anchor ') ;
141
+ link . click ( ) ;
135
142
expect ( stateServiceGoSpy ) . toHaveBeenCalledTimes ( 1 ) ;
136
143
137
- link . simulate ( 'click' , { ctrlKey : true } ) ;
144
+ fireEvent ( link , new MouseEvent ( 'click' , { ctrlKey : true } ) ) ;
138
145
expect ( stateServiceGoSpy ) . toHaveBeenCalledTimes ( 1 ) ;
139
146
140
- link . simulate ( 'click' , { metaKey : true } ) ;
147
+ fireEvent ( link , new MouseEvent ( 'click' , { metaKey : true } ) ) ;
141
148
expect ( stateServiceGoSpy ) . toHaveBeenCalledTimes ( 1 ) ;
142
149
143
- link . simulate ( 'click' , { shiftKey : true } ) ;
150
+ fireEvent ( link , new MouseEvent ( 'click' , { shiftKey : true } ) ) ;
144
151
expect ( stateServiceGoSpy ) . toHaveBeenCalledTimes ( 1 ) ;
145
152
146
- link . simulate ( 'click' , { altKey : true } ) ;
153
+ fireEvent ( link , new MouseEvent ( 'click' , { altKey : true } ) ) ;
147
154
expect ( stateServiceGoSpy ) . toHaveBeenCalledTimes ( 1 ) ;
148
155
} ) ;
149
156
150
157
it ( "doesn't trigger a transition when the anchor has a 'target' attribute" , async ( ) => {
151
158
const stateServiceGoSpy = jest . spyOn ( router . stateService , 'go' ) ;
152
- const wrapper = mountInRouter (
159
+ const rendered = mountInRouter (
153
160
< UISref to = "state2" >
154
- < a target = "_blank" > state2</ a >
161
+ < a data-testid = "anchor" target = "_blank" >
162
+ state2
163
+ </ a >
155
164
</ UISref >
156
165
) ;
157
166
158
- const link = wrapper . find ( 'a' ) ;
159
- link . simulate ( 'click' ) ;
167
+ rendered . getByTestId ( 'anchor' ) . click ( ) ;
160
168
expect ( stateServiceGoSpy ) . not . toHaveBeenCalled ( ) ;
161
169
} ) ;
162
170
} ) ;
0 commit comments