From 266b771a5bb856720552d953337d25073cfa521f Mon Sep 17 00:00:00 2001 From: Antoine Moreaux Date: Wed, 15 Jan 2025 18:07:06 +0100 Subject: [PATCH] [test(auth)]: Add unit tests for useSignInUpForm hook (#9648) Introduce unit tests to validate the behavior of the useSignInUpForm hook. Tests cover default initialization, handling of developer defaults, and prefilled values based on state. --- .../hooks/__tests__/useSignInUpForm.test.tsx | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 packages/twenty-front/src/modules/auth/sign-in-up/hooks/__tests__/useSignInUpForm.test.tsx diff --git a/packages/twenty-front/src/modules/auth/sign-in-up/hooks/__tests__/useSignInUpForm.test.tsx b/packages/twenty-front/src/modules/auth/sign-in-up/hooks/__tests__/useSignInUpForm.test.tsx new file mode 100644 index 000000000000..9c626b43d5b0 --- /dev/null +++ b/packages/twenty-front/src/modules/auth/sign-in-up/hooks/__tests__/useSignInUpForm.test.tsx @@ -0,0 +1,68 @@ +import { RecoilRoot, useSetRecoilState } from 'recoil'; +import { renderHook } from '@testing-library/react'; +import { useSignInUpForm } from '@/auth/sign-in-up/hooks/useSignInUpForm'; +import { ReactNode } from 'react'; +import { MemoryRouter } from 'react-router-dom'; +import { isDeveloperDefaultSignInPrefilledState } from '@/client-config/states/isDeveloperDefaultSignInPrefilledState'; + +describe('useSignInUpForm', () => { + beforeEach(() => { + jest.clearAllMocks(); + }); + + it('should initialize the form with default values', async () => { + const { result } = renderHook(() => useSignInUpForm(), { + wrapper: ({ children }: { children: ReactNode }) => ( + + {children} + + ), + }); + expect(result.current.form).toBeDefined(); + }); + + it('should not prefill sign-in developer defaults when state is false', () => { + const { result } = renderHook(() => useSignInUpForm(), { + wrapper: ({ children }: { children: ReactNode }) => ( + + {children} + + ), + }); + + expect(result.current.form.getValues()).toEqual({ + exist: false, + email: 'test@test.com', + password: '', + captchaToken: '', + }); + }); + + it('should prefill developer defaults when the state is true', () => { + const { result } = renderHook( + () => { + const setIsDeveloperDefaultSignInPrefilledState = useSetRecoilState( + isDeveloperDefaultSignInPrefilledState, + ); + + setIsDeveloperDefaultSignInPrefilledState(true); + + return useSignInUpForm(); + }, + { + wrapper: ({ children }: { children: ReactNode }) => ( + + {children} + + ), + }, + ); + + expect(result.current.form.getValues()).toEqual({ + exist: false, + email: 'test@test.com', + password: 'Applecar2025', + captchaToken: '', + }); + }); +});