1+ import { fireEvent } from '@testing-library/react' ;
12import * as React from 'react' ;
23import { UISref , UIView } from '../../components' ;
34import { UISrefActiveContext } from '../UISrefActive' ;
@@ -21,22 +22,25 @@ const states = [
2122] ;
2223
2324describe ( '<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 ( ) ) ;
2628
2729 let { router, routerGo, mountInRouter } = makeTestRouter ( [ ] ) ;
2830 beforeEach ( ( ) => ( { router, routerGo, mountInRouter } = makeTestRouter ( states ) ) ) ;
2931
3032 it ( 'renders its child with injected props' , async ( ) => {
3133 const wrapper = mountInRouter (
3234 < UISref to = "state2" >
33- < a > state2</ a >
35+ < a data-testid = "anchor" > state2</ a >
3436 </ UISref >
3537 ) ;
3638 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 ) ;
4044 } ) ;
4145
4246 it ( 'throws if state name is not a string' , ( ) => {
@@ -50,27 +54,26 @@ describe('<UISref>', () => {
5054 const deregisterFn = jest . fn ( ) ;
5155 const parentUISrefActiveAddStateFn = jest . fn ( ( ) => deregisterFn ) ;
5256
53- const wrapper = mountInRouter (
57+ mountInRouter (
5458 < UISrefActiveContext . Provider value = { parentUISrefActiveAddStateFn } >
5559 < UIView />
5660 </ UISrefActiveContext . Provider >
5761 ) ;
5862
59- expect ( wrapper . html ( ) ) . toBe ( '<a href="/state2" class="">state2</a>' ) ;
6063 expect ( parentUISrefActiveAddStateFn ) . toHaveBeenCalled ( ) ;
6164 await routerGo ( 'state2' ) ;
6265 expect ( deregisterFn ) . toHaveBeenCalled ( ) ;
6366 } ) ;
6467
6568 it ( 'triggers a transition to target state' , async ( ) => {
6669 const goSpy = jest . spyOn ( router . stateService , 'go' ) ;
67- const wrapper = mountInRouter (
70+ const rendered = mountInRouter (
6871 < UISref to = "state2" >
69- < a />
72+ < a data-testid = "anchor" />
7073 </ UISref >
7174 ) ;
7275
73- wrapper . find ( 'a ') . simulate ( ' click' ) ;
76+ rendered . getByTestId ( 'anchor ') . click ( ) ;
7477
7578 expect ( goSpy ) . toHaveBeenCalledTimes ( 1 ) ;
7679 expect ( goSpy ) . toHaveBeenCalledWith ( 'state2' , expect . anything ( ) , expect . anything ( ) ) ;
@@ -80,12 +83,14 @@ describe('<UISref>', () => {
8083 const log = [ ] ;
8184 const goSpy = jest . spyOn ( router . stateService , 'go' ) . mockImplementation ( ( ) => log . push ( 'go' ) as any ) ;
8285 const onClickSpy = jest . fn ( ( ) => log . push ( 'onClick' ) ) ;
83- const wrapper = mountInRouter (
86+ const rendered = mountInRouter (
8487 < UISref to = "state2" >
85- < a onClick = { onClickSpy } > state2</ a >
88+ < a data-testid = "anchor" onClick = { onClickSpy } >
89+ state2
90+ </ a >
8691 </ UISref >
8792 ) ;
88- wrapper . find ( 'a ') . simulate ( ' click' ) ;
93+ rendered . getByTestId ( 'anchor ') . click ( ) ;
8994
9095 expect ( onClickSpy ) . toHaveBeenCalled ( ) ;
9196 expect ( goSpy ) . toHaveBeenCalled ( ) ;
@@ -95,68 +100,71 @@ describe('<UISref>', () => {
95100 it ( 'calls the child elements onClick function and honors e.preventDefault()' , async ( ) => {
96101 const goSpy = jest . spyOn ( router . stateService , 'go' ) ;
97102 const onClickSpy = jest . fn ( ( e ) => e . preventDefault ( ) ) ;
98- const wrapper = mountInRouter (
103+ const rendered = mountInRouter (
99104 < UISref to = "state2" >
100- < a onClick = { onClickSpy } > state2</ a >
105+ < a data-testid = "anchor" onClick = { onClickSpy } >
106+ state2
107+ </ a >
101108 </ UISref >
102109 ) ;
103- wrapper . find ( 'a ') . simulate ( ' click' ) ;
110+ rendered . getByTestId ( 'anchor ') . click ( ) ;
104111
105112 expect ( onClickSpy ) . toHaveBeenCalled ( ) ;
106113 expect ( goSpy ) . not . toHaveBeenCalled ( ) ;
107114 } ) ;
108115
109116 it ( "doesn't trigger a transition when middle-clicked" , async ( ) => {
110117 const stateServiceGoSpy = jest . spyOn ( router . stateService , 'go' ) ;
111- const wrapper = mountInRouter (
118+ const rendered = mountInRouter (
112119 < UISref to = "state2" >
113- < a > state2</ a >
120+ < a data-testid = "anchor" > state2</ a >
114121 </ UISref >
115122 ) ;
116123
117- const link = wrapper . find ( 'a ') ;
118- link . simulate ( ' click' ) ;
124+ const link = rendered . getByTestId ( 'anchor ') ;
125+ link . click ( ) ;
119126 expect ( stateServiceGoSpy ) . toHaveBeenCalledTimes ( 1 ) ;
120127
121- link . simulate ( 'click' , { button : 1 } ) ;
128+ fireEvent ( link , new MouseEvent ( 'click' , { button : 1 } ) ) ;
122129 expect ( stateServiceGoSpy ) . toHaveBeenCalledTimes ( 1 ) ;
123130 } ) ;
124131
125132 it ( "doesn't trigger a transition when ctrl/meta/shift/alt+clicked" , async ( ) => {
126133 const stateServiceGoSpy = jest . spyOn ( router . stateService , 'go' ) ;
127- const wrapper = mountInRouter (
134+ const rendered = mountInRouter (
128135 < UISref to = "state2" >
129- < a > state2</ a >
136+ < a data-testid = "anchor" > state2</ a >
130137 </ UISref >
131138 ) ;
132139
133- const link = wrapper . find ( 'a ') ;
134- link . simulate ( ' click' ) ;
140+ const link = rendered . getByTestId ( 'anchor ') ;
141+ link . click ( ) ;
135142 expect ( stateServiceGoSpy ) . toHaveBeenCalledTimes ( 1 ) ;
136143
137- link . simulate ( 'click' , { ctrlKey : true } ) ;
144+ fireEvent ( link , new MouseEvent ( 'click' , { ctrlKey : true } ) ) ;
138145 expect ( stateServiceGoSpy ) . toHaveBeenCalledTimes ( 1 ) ;
139146
140- link . simulate ( 'click' , { metaKey : true } ) ;
147+ fireEvent ( link , new MouseEvent ( 'click' , { metaKey : true } ) ) ;
141148 expect ( stateServiceGoSpy ) . toHaveBeenCalledTimes ( 1 ) ;
142149
143- link . simulate ( 'click' , { shiftKey : true } ) ;
150+ fireEvent ( link , new MouseEvent ( 'click' , { shiftKey : true } ) ) ;
144151 expect ( stateServiceGoSpy ) . toHaveBeenCalledTimes ( 1 ) ;
145152
146- link . simulate ( 'click' , { altKey : true } ) ;
153+ fireEvent ( link , new MouseEvent ( 'click' , { altKey : true } ) ) ;
147154 expect ( stateServiceGoSpy ) . toHaveBeenCalledTimes ( 1 ) ;
148155 } ) ;
149156
150157 it ( "doesn't trigger a transition when the anchor has a 'target' attribute" , async ( ) => {
151158 const stateServiceGoSpy = jest . spyOn ( router . stateService , 'go' ) ;
152- const wrapper = mountInRouter (
159+ const rendered = mountInRouter (
153160 < UISref to = "state2" >
154- < a target = "_blank" > state2</ a >
161+ < a data-testid = "anchor" target = "_blank" >
162+ state2
163+ </ a >
155164 </ UISref >
156165 ) ;
157166
158- const link = wrapper . find ( 'a' ) ;
159- link . simulate ( 'click' ) ;
167+ rendered . getByTestId ( 'anchor' ) . click ( ) ;
160168 expect ( stateServiceGoSpy ) . not . toHaveBeenCalled ( ) ;
161169 } ) ;
162170} ) ;
0 commit comments