-
Notifications
You must be signed in to change notification settings - Fork 462
fix(clerk-js): Remove duplicate prepare first factor calls #6134
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
jacekradko
merged 5 commits into
main
from
fix/remove-duplicate-prepare-first-factor-call
Jun 17, 2025
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3f91967
fix(clerk-js): Remove duplicate prepare first factor fetch
jacekradko 514e807
Merge branch 'main' into fix/remove-duplicate-prepare-first-factor-call
jacekradko b849413
stable useMemo deps
jacekradko 05dda3d
wip
jacekradko 59828e8
changeset
jacekradko File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
205 changes: 205 additions & 0 deletions
205
packages/clerk-js/src/ui/components/SignIn/__tests__/SignInFactorOneCodeForm.spec.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,205 @@ | ||
| import { beforeEach, describe, expect, it, vi } from 'vitest'; | ||
|
|
||
| import { render } from '../../../../vitestUtils'; | ||
| import { CardStateProvider } from '../../../elements/contexts'; | ||
| import { clearFetchCache, useFetch } from '../../../hooks'; | ||
| import { localizationKeys } from '../../../localization'; | ||
| import { bindCreateFixtures } from '../../../utils/vitest/createFixtures'; | ||
| import { SignInFactorOneCodeForm } from '../SignInFactorOneCodeForm'; | ||
|
|
||
| const { createFixtures } = bindCreateFixtures('SignIn'); | ||
|
|
||
| vi.mock('../../../hooks', async () => { | ||
| const actual = await vi.importActual('../../../hooks'); | ||
| return { | ||
| ...actual, | ||
| useFetch: vi.fn(), | ||
| }; | ||
| }); | ||
|
|
||
| describe('SignInFactorOneCodeForm', () => { | ||
| beforeEach(() => { | ||
| clearFetchCache(); | ||
| vi.mocked(useFetch).mockClear(); | ||
| }); | ||
|
|
||
| const renderWithProviders = (component: React.ReactElement, options?: any) => { | ||
| return render(<CardStateProvider>{component}</CardStateProvider>, options); | ||
| }; | ||
|
|
||
| const defaultProps = { | ||
| factor: { | ||
| strategy: 'phone_code' as const, | ||
| phoneNumberId: 'idn_123', | ||
| safeIdentifier: '+1234567890', | ||
| }, | ||
| factorAlreadyPrepared: false, | ||
| onFactorPrepare: vi.fn(), | ||
| cardTitle: localizationKeys('signIn.phoneCode.title'), | ||
| cardSubtitle: localizationKeys('signIn.phoneCode.subtitle'), | ||
| inputLabel: localizationKeys('signIn.phoneCode.formTitle'), | ||
| resendButton: localizationKeys('signIn.phoneCode.resendButton'), | ||
| }; | ||
|
|
||
| describe('Cache Key Generation', () => { | ||
| it('generates cache key without signIn.id to prevent extra API calls', async () => { | ||
| const { wrapper } = await createFixtures(f => { | ||
| f.withPhoneNumber(); | ||
| f.startSignInWithPhoneNumber({ supportPhoneCode: true }); | ||
| }); | ||
|
|
||
| renderWithProviders(<SignInFactorOneCodeForm {...defaultProps} />, { wrapper }); | ||
|
|
||
| expect(vi.mocked(useFetch)).toHaveBeenCalledWith( | ||
| expect.any(Function), | ||
| { | ||
| name: 'signIn.prepareFirstFactor', | ||
| factorKey: 'phone_code_idn_123', | ||
| }, | ||
| { | ||
| staleTime: 100, | ||
| }, | ||
| ); | ||
| }); | ||
|
|
||
| it('includes channel in cache key for phone code with WhatsApp', async () => { | ||
| const { wrapper } = await createFixtures(f => { | ||
| f.withPhoneNumber(); | ||
| f.startSignInWithPhoneNumber({ supportPhoneCode: true }); | ||
| }); | ||
|
|
||
| const phonePropsWithChannel = { | ||
| factor: { | ||
| strategy: 'phone_code' as const, | ||
| phoneNumberId: 'idn_123', | ||
| safeIdentifier: '+1234567890', | ||
| channel: 'whatsapp' as const, | ||
| }, | ||
| factorAlreadyPrepared: false, | ||
| onFactorPrepare: vi.fn(), | ||
| cardTitle: localizationKeys('signIn.phoneCode.title'), | ||
| cardSubtitle: localizationKeys('signIn.phoneCode.subtitle'), | ||
| inputLabel: localizationKeys('signIn.phoneCode.formTitle'), | ||
| resendButton: localizationKeys('signIn.phoneCode.resendButton'), | ||
| }; | ||
|
|
||
| renderWithProviders(<SignInFactorOneCodeForm {...phonePropsWithChannel} />, { wrapper }); | ||
|
|
||
| expect(vi.mocked(useFetch)).toHaveBeenCalledWith( | ||
| expect.any(Function), | ||
| { | ||
| name: 'signIn.prepareFirstFactor', | ||
| factorKey: 'phone_code_idn_123_whatsapp', | ||
| }, | ||
| { | ||
| staleTime: 100, | ||
| }, | ||
| ); | ||
| }); | ||
|
|
||
| it('still calls prepare when factor is already prepared but verification not verified', async () => { | ||
| const { wrapper } = await createFixtures(f => { | ||
| f.withPhoneNumber(); | ||
| f.startSignInWithPhoneNumber({ supportPhoneCode: true }); | ||
| }); | ||
|
|
||
| const props = { | ||
| factor: { | ||
| strategy: 'phone_code' as const, | ||
| phoneNumberId: 'idn_123', | ||
| safeIdentifier: '+1234567890', | ||
| }, | ||
| factorAlreadyPrepared: true, | ||
| onFactorPrepare: vi.fn(), | ||
| cardTitle: localizationKeys('signIn.phoneCode.title'), | ||
| cardSubtitle: localizationKeys('signIn.phoneCode.subtitle'), | ||
| inputLabel: localizationKeys('signIn.phoneCode.formTitle'), | ||
| resendButton: localizationKeys('signIn.phoneCode.resendButton'), | ||
| }; | ||
|
|
||
| renderWithProviders(<SignInFactorOneCodeForm {...props} />, { wrapper }); | ||
|
|
||
| expect(vi.mocked(useFetch)).toHaveBeenCalledWith(expect.any(Function), expect.any(Object), expect.any(Object)); | ||
| }); | ||
| }); | ||
|
|
||
| describe('shouldAvoidPrepare Logic', () => { | ||
| it('still calls prepare when factor is already prepared but verification not verified', async () => { | ||
| const { wrapper } = await createFixtures(f => { | ||
| f.withPhoneNumber(); | ||
| f.startSignInWithPhoneNumber({ supportPhoneCode: true }); | ||
| }); | ||
|
|
||
| const propsWithFactorPrepared = { | ||
| ...defaultProps, | ||
| factorAlreadyPrepared: true, | ||
| }; | ||
|
|
||
| renderWithProviders(<SignInFactorOneCodeForm {...propsWithFactorPrepared} />, { wrapper }); | ||
|
|
||
| expect(vi.mocked(useFetch)).toHaveBeenCalledWith( | ||
| expect.any(Function), // fetcher should still be a function because shouldAvoidPrepare requires BOTH conditions | ||
| expect.any(Object), | ||
| expect.any(Object), | ||
| ); | ||
| }); | ||
|
|
||
| it('allows prepare when factor is not already prepared', async () => { | ||
| const { wrapper } = await createFixtures(f => { | ||
| f.withPhoneNumber(); | ||
| f.startSignInWithPhoneNumber({ supportPhoneCode: true }); | ||
| }); | ||
|
|
||
| const propsWithFactorNotPrepared = { | ||
| ...defaultProps, | ||
| factorAlreadyPrepared: false, | ||
| }; | ||
|
|
||
| renderWithProviders(<SignInFactorOneCodeForm {...propsWithFactorNotPrepared} />, { wrapper }); | ||
|
|
||
| expect(vi.mocked(useFetch)).toHaveBeenCalledWith( | ||
| expect.any(Function), // fetcher should be a function when prepare is allowed | ||
| expect.any(Object), | ||
| expect.any(Object), | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| describe('Component Rendering', () => { | ||
| it('renders phone code verification form', async () => { | ||
| const { wrapper } = await createFixtures(f => { | ||
| f.withPhoneNumber(); | ||
| f.startSignInWithPhoneNumber({ supportPhoneCode: true }); | ||
| }); | ||
|
|
||
| renderWithProviders(<SignInFactorOneCodeForm {...defaultProps} />, { wrapper }); | ||
|
|
||
| expect(document.querySelector('input[type="text"]')).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('renders email code verification form', async () => { | ||
| const { wrapper } = await createFixtures(f => { | ||
| f.withEmailAddress(); | ||
| f.startSignInWithEmailAddress({ supportEmailCode: true }); | ||
| }); | ||
|
|
||
| const emailProps = { | ||
| factor: { | ||
| strategy: 'email_code' as const, | ||
| emailAddressId: 'idn_456', | ||
| safeIdentifier: 'test@example.com', | ||
| }, | ||
| factorAlreadyPrepared: false, | ||
| onFactorPrepare: vi.fn(), | ||
| cardTitle: localizationKeys('signIn.emailCode.title'), | ||
| cardSubtitle: localizationKeys('signIn.emailCode.subtitle'), | ||
| inputLabel: localizationKeys('signIn.emailCode.formTitle'), | ||
| resendButton: localizationKeys('signIn.emailCode.resendButton'), | ||
| }; | ||
|
|
||
| renderWithProviders(<SignInFactorOneCodeForm {...emailProps} />, { wrapper }); | ||
|
|
||
| expect(document.querySelector('input[type="text"]')).toBeInTheDocument(); | ||
| }); | ||
| }); | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.