-
Notifications
You must be signed in to change notification settings - Fork 424
feat(clerk-js): Add reset method to Sign[In|Up] resource
#7606
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
Open
alexcarpenter
wants to merge
7
commits into
main
Choose a base branch
from
alexcarpenter/add-sign-up-reset-method
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+212
−0
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3dc0055
feat(clerk-js): Add `reset` method to SignUp resource
alexcarpenter df89c90
set to null to avoid api call
alexcarpenter 931c1e4
fix linting
alexcarpenter 921e646
add changeset
alexcarpenter 7f67200
Update stateProxy.ts
alexcarpenter 7225024
Update stateProxy.ts
alexcarpenter 8fd50e2
feat: Add reset method to signInFuture resource
alexcarpenter 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| '@clerk/clerk-js': patch | ||
| '@clerk/shared': patch | ||
| --- | ||
|
|
||
| Add `reset` method to the sign-in resource. |
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,6 @@ | ||
| --- | ||
| '@clerk/clerk-js': patch | ||
| '@clerk/shared': patch | ||
| --- | ||
|
|
||
| Add `reset` method to the new signUp resource. |
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
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
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 |
|---|---|---|
| @@ -1,5 +1,7 @@ | ||
| import { afterEach, beforeAll, describe, expect, it, vi } from 'vitest'; | ||
|
|
||
| import { eventBus } from '../../events'; | ||
| import { signInErrorSignal, signInResourceSignal } from '../../signals'; | ||
| import { BaseResource } from '../internal'; | ||
| import { SignIn } from '../SignIn'; | ||
|
|
||
|
|
@@ -1890,5 +1892,73 @@ describe('SignIn', () => { | |
| await expect(signIn.__internal_future.finalize()).rejects.toThrow(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('reset', () => { | ||
| afterEach(() => { | ||
| vi.clearAllMocks(); | ||
| vi.restoreAllMocks(); | ||
| // Reset signals to initial state | ||
| signInResourceSignal({ resource: null }); | ||
| signInErrorSignal({ error: null }); | ||
| }); | ||
|
|
||
| it('does NOT emit resource:fetch with status fetching', async () => { | ||
| const emitSpy = vi.spyOn(eventBus, 'emit'); | ||
| const mockFetch = vi.fn(); | ||
| BaseResource._fetch = mockFetch; | ||
|
|
||
| const signIn = new SignIn({ id: 'signin_123', status: 'needs_first_factor' } as any); | ||
| await signIn.__internal_future.reset(); | ||
|
|
||
| // Verify that resource:fetch was NOT called with status: 'fetching' | ||
| const fetchingCalls = emitSpy.mock.calls.filter( | ||
| call => call[0] === 'resource:fetch' && call[1]?.status === 'fetching', | ||
| ); | ||
| expect(fetchingCalls).toHaveLength(0); | ||
| // Verify no API calls were made | ||
| expect(mockFetch).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('clears any previous errors by updating signInErrorSignal', async () => { | ||
| // Set an initial error | ||
| signInErrorSignal({ error: new Error('Previous error') }); | ||
| expect(signInErrorSignal().error).toBeTruthy(); | ||
|
|
||
| const signIn = new SignIn({ id: 'signin_123', status: 'needs_first_factor' } as any); | ||
| await signIn.__internal_future.reset(); | ||
|
|
||
| // Verify that error signal was cleared | ||
| expect(signInErrorSignal().error).toBeNull(); | ||
| }); | ||
|
|
||
| it('returns error: null on success', async () => { | ||
| const signIn = new SignIn({ id: 'signin_123', status: 'needs_first_factor' } as any); | ||
| const result = await signIn.__internal_future.reset(); | ||
|
|
||
| expect(result).toHaveProperty('error', null); | ||
| }); | ||
|
|
||
| it('resets an existing signin with data to a fresh null state', async () => { | ||
| const signIn = new SignIn({ | ||
| id: 'signin_123', | ||
| status: 'needs_first_factor', | ||
| identifier: '[email protected]', | ||
| } as any); | ||
|
|
||
| // Verify initial state | ||
| expect(signIn.id).toBe('signin_123'); | ||
| expect(signIn.status).toBe('needs_first_factor'); | ||
| expect(signIn.identifier).toBe('[email protected]'); | ||
|
|
||
| await signIn.__internal_future.reset(); | ||
|
|
||
| // Verify that signInResourceSignal was updated with a new SignIn(null) instance | ||
| const updatedSignIn = signInResourceSignal().resource; | ||
| expect(updatedSignIn).toBeInstanceOf(SignIn); | ||
| expect(updatedSignIn?.id).toBeUndefined(); | ||
| expect(updatedSignIn?.status).toBeNull(); | ||
| expect(updatedSignIn?.identifier).toBeNull(); | ||
| }); | ||
| }); | ||
| }); | ||
| }); | ||
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 |
|---|---|---|
| @@ -1,5 +1,7 @@ | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; | ||
|
|
||
| import { eventBus } from '../../events'; | ||
| import { signUpErrorSignal, signUpResourceSignal } from '../../signals'; | ||
| import { BaseResource } from '../internal'; | ||
| import { SignUp } from '../SignUp'; | ||
|
|
||
|
|
@@ -701,5 +703,77 @@ describe('SignUp', () => { | |
| expect(result.error).toBeInstanceOf(Error); | ||
| }); | ||
| }); | ||
|
|
||
| describe('reset', () => { | ||
| afterEach(() => { | ||
| vi.clearAllMocks(); | ||
| vi.restoreAllMocks(); | ||
| // Reset signals to initial state | ||
| signUpResourceSignal({ resource: null }); | ||
| signUpErrorSignal({ error: null }); | ||
| }); | ||
|
|
||
| it('does NOT emit resource:fetch with status fetching', async () => { | ||
| const emitSpy = vi.spyOn(eventBus, 'emit'); | ||
| const mockFetch = vi.fn(); | ||
| BaseResource._fetch = mockFetch; | ||
|
|
||
| const signUp = new SignUp({ id: 'signup_123', status: 'missing_requirements' } as any); | ||
| await signUp.__internal_future.reset(); | ||
|
|
||
| // Verify that resource:fetch was NOT called with status: 'fetching' | ||
| const fetchingCalls = emitSpy.mock.calls.filter( | ||
| call => call[0] === 'resource:fetch' && call[1]?.status === 'fetching', | ||
| ); | ||
| expect(fetchingCalls).toHaveLength(0); | ||
| // Verify no API calls were made | ||
| expect(mockFetch).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('clears any previous errors by updating signUpErrorSignal', async () => { | ||
| // Set an initial error | ||
| signUpErrorSignal({ error: new Error('Previous error') }); | ||
| expect(signUpErrorSignal().error).toBeTruthy(); | ||
|
|
||
| const signUp = new SignUp({ id: 'signup_123', status: 'missing_requirements' } as any); | ||
| await signUp.__internal_future.reset(); | ||
|
|
||
| // Verify that error signal was cleared | ||
| expect(signUpErrorSignal().error).toBeNull(); | ||
| }); | ||
|
|
||
| it('returns error: null on success', async () => { | ||
| const signUp = new SignUp({ id: 'signup_123', status: 'missing_requirements' } as any); | ||
| const result = await signUp.__internal_future.reset(); | ||
|
|
||
| expect(result).toHaveProperty('error', null); | ||
| }); | ||
|
|
||
| it('resets an existing signup with data to a fresh null state', async () => { | ||
| const signUp = new SignUp({ | ||
| id: 'signup_123', | ||
| status: 'missing_requirements', | ||
| email_address: '[email protected]', | ||
| first_name: 'John', | ||
| } as any); | ||
|
|
||
| // Verify initial state | ||
| expect(signUp.id).toBe('signup_123'); | ||
| expect(signUp.emailAddress).toBe('[email protected]'); | ||
| expect(signUp.firstName).toBe('John'); | ||
|
|
||
| await signUp.__internal_future.reset(); | ||
|
|
||
| // Verify that signUpResourceSignal was updated with a new SignUp(null) instance | ||
| const updatedSignUp = signUpResourceSignal().resource; | ||
| expect(updatedSignUp).toBeInstanceOf(SignUp); | ||
| expect(updatedSignUp?.id).toBeUndefined(); | ||
| expect(updatedSignUp?.status).toBeNull(); | ||
| expect(updatedSignUp?.emailAddress).toBeNull(); | ||
| expect(updatedSignUp?.firstName).toBeNull(); | ||
| expect(updatedSignUp?.lastName).toBeNull(); | ||
| expect(updatedSignUp?.phoneNumber).toBeNull(); | ||
| }); | ||
| }); | ||
| }); | ||
| }); | ||
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
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
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reset leaves existing
SignInFutureinstances stalereset()only swaps global signals; the currentSignInFuturestill points to the oldSignIn(and retains#hasBeenFinalized). Callers that keep the instance can keep operating on stale state right after reset, which conflicts with the “reset current sign-in attempt” contract. Consider rebinding the future to the fresh resource or explicitly returning/invalidating the old instance to avoid stale usage.🤖 Prompt for AI Agents