Skip to content

Commit c58e578

Browse files
Enhance SignInForm tests with improved mocking and query client integration
- Added QueryClientProvider to tests for better React Query handling - Updated mocks for useSignIn, useCurrentUser, and useGetUserTokens hooks - Refactored test cases to utilize custom render function with QueryClient - Improved test coverage by ensuring all form elements are rendered and functional - Cleared mocks before each test for consistent test isolation
1 parent 6fca050 commit c58e578

File tree

3 files changed

+213
-246
lines changed

3 files changed

+213
-246
lines changed

frontend/src/common/components/Auth/__tests__/SignInForm.test.tsx

Lines changed: 57 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ import React from 'react';
22
import { render, screen, fireEvent } from '@testing-library/react';
33
import { vi, expect } from 'vitest';
44
import 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
711
vi.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
197228
vi.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+
217266
describe('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
});

frontend/src/pages/Auth/SignIn/components/SignInForm.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ interface SignInFormProps extends BaseComponentProps {}
4141
* Sign in form values.
4242
* @param {string} email - User's email.
4343
* @param {string} password - A password.
44+
* @param {boolean} rememberMe - Whether to remember the user's credentials.
4445
*/
4546
interface SignInFormValues {
4647
email: string;
@@ -188,7 +189,7 @@ const SignInForm = ({ className, testid = 'form-signin' }: SignInFormProps): JSX
188189
className="ls-signin-form__input"
189190
data-testid={`${testid}-field-password`}
190191
>
191-
<IonInputPasswordToggle slot="end"></IonInputPasswordToggle>
192+
<IonInputPasswordToggle slot="end" />
192193
</Input>
193194

194195
<CheckboxInput
@@ -229,6 +230,7 @@ const SignInForm = ({ className, testid = 'form-signin' }: SignInFormProps): JSX
229230
trigger="signinInfo"
230231
triggerAction="hover"
231232
className="ls-signin-form-popover"
233+
data-testid={`${testid}-popover`}
232234
>
233235
<IonContent className="ion-padding">
234236
<p>

0 commit comments

Comments
 (0)