@@ -77,23 +77,24 @@ describe.each(table)('FocusWithin responder', hasPointerEvents => {
7777 describe ( 'onFocusWithinChange' , ( ) => {
7878 let onFocusWithinChange , ref , innerRef , innerRef2 ;
7979
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+
8092 beforeEach ( ( ) => {
8193 onFocusWithinChange = jest . fn ( ) ;
8294 ref = React . createRef ( ) ;
8395 innerRef = React . createRef ( ) ;
8496 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 ) ;
9798 } ) ;
9899
99100 it ( 'is called after "blur" and "focus" events on focus target' , ( ) => {
@@ -140,28 +141,39 @@ describe.each(table)('FocusWithin responder', hasPointerEvents => {
140141 expect ( onFocusWithinChange ) . toHaveBeenCalledTimes ( 2 ) ;
141142 expect ( onFocusWithinChange ) . toHaveBeenCalledWith ( false ) ;
142143 } ) ;
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+ } ) ;
143154 } ) ;
144155
145156 describe ( 'onFocusWithinVisibleChange' , ( ) => {
146157 let onFocusWithinVisibleChange , ref , innerRef , innerRef2 ;
147158
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+
148171 beforeEach ( ( ) => {
149172 onFocusWithinVisibleChange = jest . fn ( ) ;
150173 ref = React . createRef ( ) ;
151174 innerRef = React . createRef ( ) ;
152175 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 ) ;
165177 } ) ;
166178
167179 it ( 'is called after "focus" and "blur" on focus target if keyboard was used' , ( ) => {
@@ -258,6 +270,18 @@ describe.each(table)('FocusWithin responder', hasPointerEvents => {
258270 expect ( onFocusWithinVisibleChange ) . toHaveBeenCalledTimes ( 2 ) ;
259271 expect ( onFocusWithinVisibleChange ) . toHaveBeenCalledWith ( false ) ;
260272 } ) ;
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+ } ) ;
261285 } ) ;
262286
263287 it ( 'expect displayName to show up for event component' , ( ) => {
0 commit comments