1- import React , { useState } from 'react' ;
2- import { render , screen } from '@testing-library/react' ;
1+ import React , { useEffect , useState } from 'react' ;
2+ import { render , screen , waitFor } from '@testing-library/react' ;
33import {
44 defaultTheme ,
55 Item ,
@@ -20,6 +20,22 @@ function Counter({ label }: { label: string }) {
2020 ) ;
2121}
2222
23+ function OnMountUnmount ( {
24+ onMount,
25+ onUnmount,
26+ } : {
27+ onMount : ( ) => void ;
28+ onUnmount : ( ) => void ;
29+ } ) {
30+ useEffect ( ( ) => {
31+ onMount ( ) ;
32+ return ( ) => {
33+ onUnmount ( ) ;
34+ } ;
35+ } , [ onMount , onUnmount ] ) ;
36+ return null ;
37+ }
38+
2339describe ( 'TabPanels' , ( ) => {
2440 it ( 'should not persist panel state by default when switching tabs' , ( ) => {
2541 render (
@@ -152,4 +168,51 @@ describe('TabPanels', () => {
152168 'background-color: red'
153169 ) ;
154170 } ) ;
171+
172+ it ( 'should still unmount a panel that is not in the tree when using keepMounted' , ( ) => {
173+ const onMount = jest . fn ( ) ;
174+ const onUnmount = jest . fn ( ) ;
175+ const { rerender } = render (
176+ < Provider theme = { defaultTheme } >
177+ < Tabs aria-label = "test" >
178+ < TabList >
179+ < Item key = "1" > Tab 1</ Item >
180+ < Item key = "2" > Tab 2</ Item >
181+ </ TabList >
182+ < DHCTabPanels >
183+ < Item key = "1" >
184+ < Counter label = "foo" />
185+ </ Item >
186+ < Item key = "2" >
187+ < OnMountUnmount onMount = { onMount } onUnmount = { onUnmount } />
188+ </ Item >
189+ </ DHCTabPanels >
190+ </ Tabs >
191+ </ Provider >
192+ ) ;
193+
194+ waitFor ( ( ) => expect ( onMount ) . toHaveBeenCalledTimes ( 1 ) ) ;
195+ expect ( onUnmount ) . toHaveBeenCalledTimes ( 0 ) ;
196+
197+ rerender (
198+ < Provider theme = { defaultTheme } >
199+ < Tabs aria-label = "test" >
200+ < TabList >
201+ < Item key = "1" > Tab 1</ Item >
202+ < Item key = "2" > Tab 2</ Item >
203+ </ TabList >
204+ < DHCTabPanels >
205+ < Item key = "1" >
206+ < Counter label = "foo" />
207+ </ Item >
208+ < Item key = "2" >
209+ < Counter label = "bar" />
210+ </ Item >
211+ </ DHCTabPanels >
212+ </ Tabs >
213+ </ Provider >
214+ ) ;
215+
216+ waitFor ( ( ) => expect ( onUnmount ) . toHaveBeenCalledTimes ( 1 ) ) ;
217+ } ) ;
155218} ) ;
0 commit comments