@@ -77,23 +77,24 @@ describe.each(table)('FocusWithin responder', hasPointerEvents => {
77
77
describe ( 'onFocusWithinChange' , ( ) => {
78
78
let onFocusWithinChange , ref , innerRef , innerRef2 ;
79
79
80
+ const Component = ( { show} ) => {
81
+ const listener = useFocusWithin ( {
82
+ onFocusWithinChange,
83
+ } ) ;
84
+ return (
85
+ < div ref = { ref } listeners = { listener } >
86
+ { show && < input ref = { innerRef } /> }
87
+ < div ref = { innerRef2 } />
88
+ </ div >
89
+ ) ;
90
+ } ;
91
+
80
92
beforeEach ( ( ) => {
81
93
onFocusWithinChange = jest . fn ( ) ;
82
94
ref = React . createRef ( ) ;
83
95
innerRef = React . createRef ( ) ;
84
96
innerRef2 = React . createRef ( ) ;
85
- const Component = ( ) => {
86
- const listener = useFocusWithin ( {
87
- onFocusWithinChange,
88
- } ) ;
89
- return (
90
- < div ref = { ref } listeners = { listener } >
91
- < div ref = { innerRef } />
92
- < div ref = { innerRef2 } />
93
- </ div >
94
- ) ;
95
- } ;
96
- ReactDOM . render ( < Component /> , container ) ;
97
+ ReactDOM . render ( < Component show = { true } /> , container ) ;
97
98
} ) ;
98
99
99
100
it ( 'is called after "blur" and "focus" events on focus target' , ( ) => {
@@ -140,28 +141,39 @@ describe.each(table)('FocusWithin responder', hasPointerEvents => {
140
141
expect ( onFocusWithinChange ) . toHaveBeenCalledTimes ( 2 ) ;
141
142
expect ( onFocusWithinChange ) . toHaveBeenCalledWith ( false ) ;
142
143
} ) ;
144
+
145
+ it ( 'is called after a focused element is unmounted' , ( ) => {
146
+ const target = createEventTarget ( innerRef . current ) ;
147
+ target . focus ( ) ;
148
+ expect ( onFocusWithinChange ) . toHaveBeenCalledTimes ( 1 ) ;
149
+ expect ( onFocusWithinChange ) . toHaveBeenCalledWith ( true ) ;
150
+ ReactDOM . render ( < Component show = { false } /> , container ) ;
151
+ expect ( onFocusWithinChange ) . toHaveBeenCalledTimes ( 2 ) ;
152
+ expect ( onFocusWithinChange ) . toHaveBeenCalledWith ( false ) ;
153
+ } ) ;
143
154
} ) ;
144
155
145
156
describe ( 'onFocusWithinVisibleChange' , ( ) => {
146
157
let onFocusWithinVisibleChange , ref , innerRef , innerRef2 ;
147
158
159
+ const Component = ( { show} ) => {
160
+ const listener = useFocusWithin ( {
161
+ onFocusWithinVisibleChange,
162
+ } ) ;
163
+ return (
164
+ < div ref = { ref } listeners = { listener } >
165
+ { show && < input ref = { innerRef } /> }
166
+ < div ref = { innerRef2 } />
167
+ </ div >
168
+ ) ;
169
+ } ;
170
+
148
171
beforeEach ( ( ) => {
149
172
onFocusWithinVisibleChange = jest . fn ( ) ;
150
173
ref = React . createRef ( ) ;
151
174
innerRef = React . createRef ( ) ;
152
175
innerRef2 = React . createRef ( ) ;
153
- const Component = ( ) => {
154
- const listener = useFocusWithin ( {
155
- onFocusWithinVisibleChange,
156
- } ) ;
157
- return (
158
- < div ref = { ref } listeners = { listener } >
159
- < div ref = { innerRef } />
160
- < div ref = { innerRef2 } />
161
- </ div >
162
- ) ;
163
- } ;
164
- ReactDOM . render ( < Component /> , container ) ;
176
+ ReactDOM . render ( < Component show = { true } /> , container ) ;
165
177
} ) ;
166
178
167
179
it ( 'is called after "focus" and "blur" on focus target if keyboard was used' , ( ) => {
@@ -258,6 +270,18 @@ describe.each(table)('FocusWithin responder', hasPointerEvents => {
258
270
expect ( onFocusWithinVisibleChange ) . toHaveBeenCalledTimes ( 2 ) ;
259
271
expect ( onFocusWithinVisibleChange ) . toHaveBeenCalledWith ( false ) ;
260
272
} ) ;
273
+
274
+ it ( 'is called after a focused element is unmounted' , ( ) => {
275
+ const inner = innerRef . current ;
276
+ const target = createEventTarget ( inner ) ;
277
+ target . keydown ( { key : 'Tab' } ) ;
278
+ target . focus ( ) ;
279
+ expect ( onFocusWithinVisibleChange ) . toHaveBeenCalledTimes ( 1 ) ;
280
+ expect ( onFocusWithinVisibleChange ) . toHaveBeenCalledWith ( true ) ;
281
+ ReactDOM . render ( < Component show = { false } /> , container ) ;
282
+ expect ( onFocusWithinVisibleChange ) . toHaveBeenCalledTimes ( 2 ) ;
283
+ expect ( onFocusWithinVisibleChange ) . toHaveBeenCalledWith ( false ) ;
284
+ } ) ;
261
285
} ) ;
262
286
263
287
it ( 'expect displayName to show up for event component' , ( ) => {
0 commit comments