1- import { cleanup , render } from '@testing-library/react' ;
1+ import { cleanup , fireEvent , render , waitFor } from '@testing-library/react' ;
22import { afterEach , beforeEach , describe , expect , jest , test } from 'bun:test' ;
33import React from 'react' ;
44
@@ -121,55 +121,52 @@ describe('Web3AuthContext', () => {
121121 }
122122 } ) ;
123123
124- test ( 'isSigning state management works correctly' , ( ) => {
124+ test ( 'isSigning state management works correctly' , async ( ) => {
125125 let setSigningFunction : ( ( isSigning : boolean ) => void ) | undefined ;
126- let isSigningValue : boolean | undefined ;
127126
128- const TestComponent = ( ) => {
127+ const TestComponentWithState = ( ) => {
129128 const context = React . useContext ( Web3AuthContext ) ;
130129 setSigningFunction = context . setIsSigning ;
131- isSigningValue = context . isSigning ;
132130 return (
133131 < div >
134132 < span data-testid = "signing-state" > { context . isSigning . toString ( ) } </ span >
133+ < button
134+ data-testid = "toggle-signing"
135+ onClick = { ( ) => context . setIsSigning ( ! context . isSigning ) }
136+ >
137+ Toggle
138+ </ button >
135139 </ div >
136140 ) ;
137141 } ;
138142
139- const { getByTestId, rerender } = render (
143+ const { getByTestId } = render (
140144 < Web3AuthProvider >
141- < TestComponent />
145+ < TestComponentWithState />
142146 </ Web3AuthProvider >
143147 ) ;
144148
145149 // Initial state should be false
146150 expect ( getByTestId ( 'signing-state' ) . textContent ) . toBe ( 'false' ) ;
147151
148- // Set signing to true
152+ // Test state changes by triggering the function
149153 if ( setSigningFunction ) {
150154 setSigningFunction ( true ) ;
151- rerender (
152- < Web3AuthProvider >
153- < TestComponent />
154- </ Web3AuthProvider >
155- ) ;
156- expect ( getByTestId ( 'signing-state' ) . textContent ) . toBe ( 'true' ) ;
155+ await waitFor ( ( ) => {
156+ expect ( getByTestId ( 'signing-state' ) . textContent ) . toBe ( 'true' ) ;
157+ } ) ;
157158
158- // Set signing back to false
159159 setSigningFunction ( false ) ;
160- rerender (
161- < Web3AuthProvider >
162- < TestComponent />
163- </ Web3AuthProvider >
164- ) ;
165- expect ( getByTestId ( 'signing-state' ) . textContent ) . toBe ( 'false' ) ;
160+ await waitFor ( ( ) => {
161+ expect ( getByTestId ( 'signing-state' ) . textContent ) . toBe ( 'false' ) ;
162+ } ) ;
166163 }
167164 } ) ;
168165
169- test ( 'promptId state management works correctly' , ( ) => {
166+ test ( 'promptId state management works correctly' , async ( ) => {
170167 let setPromptIdFunction : ( ( promptId : string | undefined ) => void ) | undefined ;
171168
172- const TestComponent = ( ) => {
169+ const TestComponentWithState = ( ) => {
173170 const context = React . useContext ( Web3AuthContext ) ;
174171 setPromptIdFunction = context . setPromptId ;
175172 return (
@@ -179,33 +176,27 @@ describe('Web3AuthContext', () => {
179176 ) ;
180177 } ;
181178
182- const { getByTestId, rerender } = render (
179+ const { getByTestId } = render (
183180 < Web3AuthProvider >
184- < TestComponent />
181+ < TestComponentWithState />
185182 </ Web3AuthProvider >
186183 ) ;
187184
188185 // Initial state should be undefined
189186 expect ( getByTestId ( 'prompt-id' ) . textContent ) . toBe ( 'undefined' ) ;
190187
191- // Set prompt ID
188+ // Test state changes by triggering the function
192189 if ( setPromptIdFunction ) {
193190 setPromptIdFunction ( 'test-prompt' ) ;
194- rerender (
195- < Web3AuthProvider >
196- < TestComponent />
197- </ Web3AuthProvider >
198- ) ;
199- expect ( getByTestId ( 'prompt-id' ) . textContent ) . toBe ( 'test-prompt' ) ;
191+ await waitFor ( ( ) => {
192+ expect ( getByTestId ( 'prompt-id' ) . textContent ) . toBe ( 'test-prompt' ) ;
193+ } ) ;
200194
201195 // Clear prompt ID
202196 setPromptIdFunction ( undefined ) ;
203- rerender (
204- < Web3AuthProvider >
205- < TestComponent />
206- </ Web3AuthProvider >
207- ) ;
208- expect ( getByTestId ( 'prompt-id' ) . textContent ) . toBe ( 'undefined' ) ;
197+ await waitFor ( ( ) => {
198+ expect ( getByTestId ( 'prompt-id' ) . textContent ) . toBe ( 'undefined' ) ;
199+ } ) ;
209200 }
210201 } ) ;
211202} ) ;
0 commit comments