@@ -2,6 +2,10 @@ import React from 'react';
22import { render , screen , fireEvent } from '@testing-library/react' ;
33import { vi , expect } from 'vitest' ;
44import SignInForm from '../../../../pages/Auth/SignIn/components/SignInForm' ;
5+ import { QueryClient , QueryClientProvider } from '@tanstack/react-query' ;
6+
7+ // Create hoisted mocks for useSignIn and useCurrentUser
8+ const mockSignIn = vi . fn ( ) ;
59
610// Mock the useAuthOperations hook
711vi . mock ( '../../../../hooks/useAuthOperations' , ( ) => ( {
@@ -193,6 +197,33 @@ vi.mock('../../../../pages/Auth/SignIn/api/useSignIn', () => ({
193197 } ) ,
194198} ) ) ;
195199
200+ // Mock the useGetUserTokens hook
201+ vi . mock ( '../../../../common/api/useGetUserTokens' , ( ) => ( {
202+ useGetUserTokens : ( ) => ( {
203+ isSuccess : false ,
204+ refetch : vi . fn ( ) ,
205+ } ) ,
206+ } ) ) ;
207+
208+ // Mock the useAuth hooks
209+ vi . mock ( '../../../../common/hooks/useAuth' , ( ) => ( {
210+ useSignIn : ( ) => ( {
211+ signIn : mockSignIn ,
212+ isLoading : false ,
213+ error : null ,
214+ } ) ,
215+ useCurrentUser : ( ) => ( {
216+ data : null ,
217+ isLoading : false ,
218+ } ) ,
219+ useSocialSignIn : ( ) => ( {
220+ signInWithGoogle : vi . fn ( ) ,
221+ signInWithApple : vi . fn ( ) ,
222+ isLoading : false ,
223+ error : null ,
224+ } ) ,
225+ } ) ) ;
226+
196227// Mock the AuthErrorDisplay component
197228vi . mock ( '../AuthErrorDisplay' , ( ) => ( {
198229 default : ( { error } : { error : null | { message : string } } ) => (
@@ -214,58 +245,55 @@ vi.mock('react-i18next', () => ({
214245 } ) ,
215246} ) ) ;
216247
248+ // Create a custom render function that includes the QueryClientProvider
249+ const createTestQueryClient = ( ) => new QueryClient ( {
250+ defaultOptions : {
251+ queries : {
252+ retry : false ,
253+ } ,
254+ } ,
255+ } ) ;
256+
257+ const renderWithQueryClient = ( ui : React . ReactElement ) => {
258+ const testQueryClient = createTestQueryClient ( ) ;
259+ return render (
260+ < QueryClientProvider client = { testQueryClient } >
261+ { ui }
262+ </ QueryClientProvider >
263+ ) ;
264+ } ;
265+
217266describe ( 'SignInForm' , ( ) => {
267+ beforeEach ( ( ) => {
268+ vi . clearAllMocks ( ) ;
269+ } ) ;
270+
218271 it ( 'should render successfully' , ( ) => {
219- render ( < SignInForm /> ) ;
272+ renderWithQueryClient ( < SignInForm /> ) ;
220273 // Use a more specific selector
221274 expect ( screen . getByTestId ( 'form-signin' ) ) . toBeDefined ( ) ;
222275 } ) ;
223276
224277 it ( 'should handle email change' , ( ) => {
225- render ( < SignInForm /> ) ;
278+ renderWithQueryClient ( < SignInForm /> ) ;
226279 const emailInput = screen . getByTestId ( 'form-signin-field-email' ) ;
227280 fireEvent . change ( emailInput , { target :
{ value :
'[email protected] ' } } ) ; 228281 expect ( emailInput ) . toBeDefined ( ) ;
229282 expect ( emailInput ) . toHaveProperty ( 'value' , '[email protected] ' ) ; 230283 } ) ;
231284
232285 it ( 'should handle password change' , ( ) => {
233- render ( < SignInForm /> ) ;
286+ renderWithQueryClient ( < SignInForm /> ) ;
234287 const passwordInput = screen . getByTestId ( 'form-signin-field-password' ) ;
235288 fireEvent . change ( passwordInput , { target : { value : 'password123' } } ) ;
236289 expect ( passwordInput ) . toBeDefined ( ) ;
237290 expect ( passwordInput ) . toHaveProperty ( 'value' , 'password123' ) ;
238291 } ) ;
239292
240293 it ( 'should handle remember me change' , ( ) => {
241- render ( < SignInForm /> ) ;
294+ renderWithQueryClient ( < SignInForm /> ) ;
242295 const rememberMeCheckbox = screen . getByTestId ( 'form-signin-field-rememberme' ) ;
243296 fireEvent . click ( rememberMeCheckbox ) ;
244297 expect ( rememberMeCheckbox ) . toBeDefined ( ) ;
245298 } ) ;
246-
247- it ( 'should submit the form' , ( ) => {
248- // Get the mock implementation
249- const mockSignIn = vi . fn ( ) ;
250-
251- // Override the mock implementation for useSignIn
252- const useSignIn = vi . hoisted ( ( ) => vi . fn ( ) ) ;
253- vi . mocked ( useSignIn ) . mockReturnValue ( {
254- signIn : mockSignIn ,
255- isLoading : false ,
256- } ) ;
257-
258- render ( < SignInForm /> ) ;
259-
260- const emailInput = screen . getByTestId ( 'form-signin-field-email' ) ;
261- fireEvent . change ( emailInput , { target :
{ value :
'[email protected] ' } } ) ; 262-
263- const passwordInput = screen . getByTestId ( 'form-signin-field-password' ) ;
264- fireEvent . change ( passwordInput , { target : { value : 'password123' } } ) ;
265-
266- const submitButton = screen . getByRole ( 'button' , { name : 'signin' } ) ;
267- fireEvent . click ( submitButton ) ;
268-
269- expect ( mockSignIn ) . toBeDefined ( ) ;
270- } ) ;
271299} ) ;
0 commit comments