11// Third party dependencies
22import React , { useRef , useEffect } from 'react' ;
3- import { render , waitFor , cleanup } from '@testing-library/react-native' ;
3+ import { render , act , waitFor } from '@testing-library/react-native' ;
44
55// External dependencies.
66import Text from '../../../../Texts/Text' ;
@@ -32,18 +32,6 @@ jest.mock('@react-navigation/native', () => {
3232} ) ;
3333
3434describe ( 'BottomSheetDialog' , ( ) => {
35- beforeEach ( ( ) => {
36- jest . clearAllMocks ( ) ;
37- // Ensure Platform.OS is always reset to 'ios' before each test,
38- // in case a previous test (in a batched run) changed it.
39- Platform . OS = 'ios' ;
40- } ) ;
41-
42- afterEach ( ( ) => {
43- cleanup ( ) ;
44- jest . restoreAllMocks ( ) ;
45- } ) ;
46-
4735 it ( 'should render correctly' , ( ) => {
4836 const wrapper = render ( < BottomSheetDialog /> ) ;
4937 expect ( wrapper . toJSON ( ) ) . toBeDefined ( ) ;
@@ -82,6 +70,8 @@ describe('BottomSheetDialog', () => {
8270 } ) ;
8371
8472 it ( 'should call onClose when onCloseDialog ref is called' , async ( ) => {
73+ Platform . OS = 'ios' ;
74+
8575 const onCloseMock = jest . fn ( ) ;
8676 const TestComponent = ( ) => {
8777 const ref = useRef < BottomSheetDialogRef > ( null ) ;
@@ -105,7 +95,7 @@ describe('BottomSheetDialog', () => {
10595 expect ( onCloseMock ) . toHaveBeenCalled ( ) ;
10696 } ) ;
10797 } ) ;
108- it ( 'calls onClose each time when onCloseDialog is invoked twice' , async ( ) => {
98+ it ( 'calls onClose only once when onCloseDialog is invoked twice rapidly ' , async ( ) => {
10999 const onCloseMock = jest . fn ( ) ;
110100 const TestComponent = ( ) => {
111101 const ref = useRef < BottomSheetDialogRef > ( null ) ;
@@ -124,11 +114,62 @@ describe('BottomSheetDialog', () => {
124114 ) ;
125115 } ;
126116
117+ render ( < TestComponent /> ) ;
118+ await waitFor ( ( ) => {
119+ expect ( onCloseMock ) . toHaveBeenCalledTimes ( 1 ) ;
120+ } ) ;
121+ } ) ;
122+
123+ it ( 'allows closing again after re-opening' , async ( ) => {
124+ const onCloseMock = jest . fn ( ) ;
125+ const onOpenMock = jest . fn ( ) ;
126+ const TestComponent = ( ) => {
127+ const ref = useRef < BottomSheetDialogRef > ( null ) ;
128+
129+ useEffect ( ( ) => {
130+ if ( ref . current ) {
131+ ref . current ?. onCloseDialog ( ) ;
132+ ref . current ?. onOpenDialog ( ) ;
133+ ref . current ?. onCloseDialog ( ) ;
134+ }
135+ } , [ ] ) ;
136+
137+ return (
138+ < BottomSheetDialog ref = { ref } onClose = { onCloseMock } onOpen = { onOpenMock } >
139+ < Text > Test Child</ Text >
140+ </ BottomSheetDialog >
141+ ) ;
142+ } ;
143+
127144 render ( < TestComponent /> ) ;
128145 await waitFor ( ( ) => {
129146 expect ( onCloseMock ) . toHaveBeenCalledTimes ( 2 ) ;
130147 } ) ;
131148 } ) ;
149+ it ( 'calls onClose only once when onCloseDialog is invoked twice rapidly' , async ( ) => {
150+ const onCloseMock = jest . fn ( ) ;
151+ const TestComponent = ( ) => {
152+ const ref = useRef < BottomSheetDialogRef > ( null ) ;
153+
154+ useEffect ( ( ) => {
155+ if ( ref . current ) {
156+ ref . current ?. onCloseDialog ( ) ;
157+ ref . current ?. onCloseDialog ( ) ;
158+ }
159+ } , [ ] ) ;
160+
161+ return (
162+ < BottomSheetDialog ref = { ref } onClose = { onCloseMock } >
163+ < Text > Test Child</ Text >
164+ </ BottomSheetDialog >
165+ ) ;
166+ } ;
167+
168+ render ( < TestComponent /> ) ;
169+ await waitFor ( ( ) => {
170+ expect ( onCloseMock ) . toHaveBeenCalledTimes ( 1 ) ;
171+ } ) ;
172+ } ) ;
132173
133174 it ( 'allows closing again after re-opening' , async ( ) => {
134175 const onCloseMock = jest . fn ( ) ;
0 commit comments