Repository navigation
fix(mosaic): resume machine actors after stop instead of resetting them #10341
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
Closed
Closed
Changes from all commits
Commits
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,2 @@ | ||
| --- | ||
| --- |
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
86 changes: 86 additions & 0 deletions
86
packages/mosaic/src/components/form/__tests__/use-form.concurrency.feature.test.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,86 @@ | ||
| import { act, renderHook } from '@testing-library/react'; | ||
| import { Activity, type ReactNode } from 'react'; | ||
| import { describe, expect, it, vi } from 'vitest'; | ||
|
|
||
| import { deferred, tick } from '../../../__tests__/async'; | ||
| import type { FieldFeedback } from '../form-submit-error'; | ||
| import { useForm, type UseFormOptions } from '../use-form'; | ||
|
|
||
| function renderFormInActivity<TValues extends object>(options: UseFormOptions<TValues>) { | ||
| let mode: 'visible' | 'hidden' = 'visible'; | ||
| const wrapper = ({ children }: { children: ReactNode }) => <Activity mode={mode}>{children}</Activity>; | ||
| const view = renderHook(() => useForm(options), { wrapper }); | ||
| const setMode = (next: typeof mode) => { | ||
| mode = next; | ||
| act(() => view.rerender()); | ||
| }; | ||
| return { result: view.result, hide: () => setMode('hidden'), show: () => setMode('visible') }; | ||
| } | ||
|
|
||
| describe.runIf(Activity)('useForm inside Activity', () => { | ||
| it('runs a submit queued behind an async check that settled while hidden', async () => { | ||
| const check = deferred<FieldFeedback | undefined>(); | ||
| const onSubmit = vi.fn(() => Promise.resolve()); | ||
| const form = renderFormInActivity({ | ||
| initialValues: { password: '' }, | ||
| fields: { password: { validateAsync: () => check.promise } }, | ||
| onSubmit, | ||
| }); | ||
| act(() => form.result.current.setValue('password', 'ab')); | ||
| act(() => form.result.current.submit()); | ||
|
|
||
| form.hide(); | ||
| await act(async () => { | ||
| check.resolve(undefined); | ||
| await tick(); | ||
| }); | ||
| form.show(); | ||
| await act(tick); | ||
|
|
||
| expect(onSubmit).toHaveBeenCalledTimes(1); | ||
| expect(onSubmit).toHaveBeenCalledWith({ password: 'ab' }); | ||
| expect(form.result.current.isSubmitting).toBe(false); | ||
| }); | ||
|
|
||
| it('finishes a submit that settled while hidden and keeps the submitted values', async () => { | ||
| const save = deferred<void>(); | ||
| const onSubmit = vi.fn(() => save.promise); | ||
| const form = renderFormInActivity({ initialValues: { name: 'Acme' }, onSubmit }); | ||
| act(() => form.result.current.setValue('name', 'Globex')); | ||
| act(() => form.result.current.submit()); | ||
| expect(form.result.current.isSubmitting).toBe(true); | ||
|
|
||
| form.hide(); | ||
| await act(async () => { | ||
| save.resolve(); | ||
| await tick(); | ||
| }); | ||
| form.show(); | ||
|
|
||
| expect(onSubmit).toHaveBeenCalledTimes(1); | ||
| expect(form.result.current.isSubmitting).toBe(false); | ||
| act(() => form.result.current.submit()); | ||
| expect(onSubmit).toHaveBeenCalledTimes(2); | ||
| expect(onSubmit).toHaveBeenLastCalledWith({ name: 'Globex' }); | ||
| }); | ||
|
|
||
| it('shows a submit error that arrived while hidden', async () => { | ||
| const save = deferred<void>(); | ||
| const form = renderFormInActivity({ | ||
| initialValues: { name: 'Acme' }, | ||
| onSubmit: () => save.promise, | ||
| errorFallback: 'Could not save', | ||
| }); | ||
| act(() => form.result.current.submit()); | ||
|
|
||
| form.hide(); | ||
| await act(async () => { | ||
| save.reject(new Error('boom')); | ||
| await tick(); | ||
| }); | ||
| form.show(); | ||
|
|
||
| expect(form.result.current.isSubmitting).toBe(false); | ||
| expect(form.result.current.error).toBe('Could not save'); | ||
| }); | ||
| }); |
68 changes: 68 additions & 0 deletions
68
...r-profile-email-section/__tests__/user-profile-email-section.concurrency.feature.test.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,68 @@ | ||
| import { act, screen, waitFor } from '@testing-library/react'; | ||
| import userEvent from '@testing-library/user-event'; | ||
| import { Activity } from 'react'; | ||
| import { describe, expect, it } from 'vitest'; | ||
|
|
||
| import { holdRequests, serveFapi, VERIFICATION_CODE } from '../../../../__tests__/feature/fake-fapi'; | ||
| import { fapiClient, fapiSession, fapiUser } from '../../../../__tests__/feature/fapi'; | ||
| import { renderWithClerk } from '../../../../__tests__/feature/render'; | ||
| import { UserProfileEmailSection } from '../user-profile-email-section'; | ||
|
|
||
| function signedIn() { | ||
| const user = fapiUser({ id: 'user_1', first_name: 'Alice', username: 'alicesmith', email_addresses: [] }); | ||
| return { client: fapiClient([fapiSession({ id: 'sess_1', user })]) }; | ||
| } | ||
|
|
||
| function inActivity(mode: 'visible' | 'hidden') { | ||
| return ( | ||
| <Activity mode={mode}> | ||
| <UserProfileEmailSection /> | ||
| </Activity> | ||
| ); | ||
| } | ||
|
|
||
| describe.runIf(Activity)('the user profile email section inside Activity', () => { | ||
| it('keeps the code step after being hidden and shown again', async () => { | ||
| serveFapi(signedIn()); | ||
| const view = await renderWithClerk(inActivity('visible')); | ||
| const actor = userEvent.setup(); | ||
|
|
||
| await actor.click(screen.getByRole('button', { name: 'Add email' })); | ||
| await actor.type(screen.getByRole('textbox', { name: 'Email' }), 'new@example.com'); | ||
| await actor.click(screen.getByRole('button', { name: 'Continue' })); | ||
| const code = await screen.findByRole('textbox', { name: 'Verification code' }); | ||
| await waitFor(() => expect(code).not.toHaveAttribute('aria-disabled')); | ||
|
|
||
| act(() => view.rerender(inActivity('hidden'))); | ||
| act(() => view.rerender(inActivity('visible'))); | ||
|
|
||
| expect(screen.getByRole('dialog')).toBeVisible(); | ||
| await actor.click(screen.getByRole('textbox', { name: 'Verification code' })); | ||
| await actor.keyboard(VERIFICATION_CODE); | ||
| await waitFor(() => expect(screen.queryByRole('dialog')).not.toBeInTheDocument()); | ||
| expect(screen.getByRole('group', { name: 'Email' })).toHaveTextContent('new@example.com'); | ||
| expect(screen.getByRole('group', { name: 'Email' })).not.toHaveTextContent('Unverified'); | ||
| }); | ||
|
|
||
| it('keeps waiting for a code that was in flight when hidden', async () => { | ||
| serveFapi(signedIn()); | ||
| const view = await renderWithClerk(inActivity('visible')); | ||
| const actor = userEvent.setup(); | ||
| const prepare = holdRequests('post', '/v1/me/email_addresses/:id/prepare_verification'); | ||
|
|
||
| await actor.click(screen.getByRole('button', { name: 'Add email' })); | ||
| await actor.type(screen.getByRole('textbox', { name: 'Email' }), 'new@example.com'); | ||
| await actor.click(screen.getByRole('button', { name: 'Continue' })); | ||
| await waitFor(() => expect(prepare.requests).toHaveLength(1)); | ||
|
|
||
| act(() => view.rerender(inActivity('hidden'))); | ||
| prepare.release(); | ||
| act(() => view.rerender(inActivity('visible'))); | ||
|
|
||
| const code = await screen.findByRole('textbox', { name: 'Verification code' }); | ||
| await waitFor(() => expect(code).not.toHaveAttribute('aria-disabled')); | ||
| await actor.click(code); | ||
| await actor.keyboard(VERIFICATION_CODE); | ||
| await waitFor(() => expect(screen.queryByRole('dialog')).not.toBeInTheDocument()); | ||
| }); | ||
| }); |
74 changes: 74 additions & 0 deletions
74
...r-profile-phone-section/__tests__/user-profile-phone-section.concurrency.feature.test.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,74 @@ | ||
| import { act, screen, waitFor } from '@testing-library/react'; | ||
| import userEvent from '@testing-library/user-event'; | ||
| import { Activity } from 'react'; | ||
| import { describe, expect, it } from 'vitest'; | ||
|
|
||
| import { holdRequests, serveFapi, VERIFICATION_CODE } from '../../../../__tests__/feature/fake-fapi'; | ||
| import { fapiClient, fapiEnvironment, fapiSession, fapiUser } from '../../../../__tests__/feature/fapi'; | ||
| import { renderWithClerk } from '../../../../__tests__/feature/render'; | ||
| import { UserProfilePhoneSection } from '../user-profile-phone-section'; | ||
|
|
||
| function signedIn() { | ||
| const user = fapiUser({ id: 'user_1', first_name: 'Alice', username: 'alicesmith', phone_numbers: [] }); | ||
| return { | ||
| environment: fapiEnvironment({ attributes: { phone_number: { enabled: true } } }), | ||
| client: fapiClient([fapiSession({ id: 'sess_1', user })]), | ||
| }; | ||
| } | ||
|
|
||
| function inActivity(mode: 'visible' | 'hidden') { | ||
| return ( | ||
| <Activity mode={mode}> | ||
| <UserProfilePhoneSection /> | ||
| </Activity> | ||
| ); | ||
| } | ||
|
|
||
| async function verify(actor: ReturnType<typeof userEvent.setup>) { | ||
| const code = await screen.findByRole('textbox', { name: 'Verification code' }); | ||
| await waitFor(() => expect(code).not.toHaveAttribute('aria-disabled')); | ||
| await actor.click(code); | ||
| await actor.keyboard(VERIFICATION_CODE); | ||
| await waitFor(() => expect(screen.queryByRole('dialog')).not.toBeInTheDocument()); | ||
| expect(screen.getByRole('group', { name: 'Phone' })).toHaveTextContent('+1 (801) 555-0100'); | ||
| expect(screen.getByRole('group', { name: 'Phone' })).not.toHaveTextContent('Unverified'); | ||
| } | ||
|
|
||
| describe.runIf(Activity)('the user profile phone section inside Activity', () => { | ||
| it('keeps the code step after being hidden and shown again', async () => { | ||
| serveFapi(signedIn()); | ||
| const view = await renderWithClerk(inActivity('visible')); | ||
| const actor = userEvent.setup(); | ||
|
|
||
| await actor.click(screen.getByRole('button', { name: 'Add phone number' })); | ||
| await actor.type(screen.getByRole('textbox', { name: 'Phone' }), '8015550100'); | ||
| await actor.click(screen.getByRole('button', { name: 'Send code' })); | ||
| const code = await screen.findByRole('textbox', { name: 'Verification code' }); | ||
| await waitFor(() => expect(code).not.toHaveAttribute('aria-disabled')); | ||
|
|
||
| act(() => view.rerender(inActivity('hidden'))); | ||
| act(() => view.rerender(inActivity('visible'))); | ||
|
|
||
| expect(screen.getByRole('dialog')).toBeVisible(); | ||
| await verify(actor); | ||
| }); | ||
|
|
||
| it('keeps waiting for a code that was in flight when hidden', async () => { | ||
| serveFapi(signedIn()); | ||
| const view = await renderWithClerk(inActivity('visible')); | ||
| const actor = userEvent.setup(); | ||
| const prepare = holdRequests('post', '/v1/me/phone_numbers/:id/prepare_verification'); | ||
|
|
||
| await actor.click(screen.getByRole('button', { name: 'Add phone number' })); | ||
| await actor.type(screen.getByRole('textbox', { name: 'Phone' }), '8015550100'); | ||
| await actor.click(screen.getByRole('button', { name: 'Send code' })); | ||
| await waitFor(() => expect(prepare.requests).toHaveLength(1)); | ||
|
|
||
| act(() => view.rerender(inActivity('hidden'))); | ||
| prepare.release(); | ||
| act(() => view.rerender(inActivity('visible'))); | ||
|
|
||
| expect(prepare.requests).toHaveLength(1); | ||
| await verify(actor); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
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.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
Repository: clerk/javascript
Length of output: 3411
🏁 Script executed:
Repository: clerk/javascript
Length of output: 41283
🏁 Script executed:
sed -n '840,950p' packages/mosaic/src/__tests__/feature/fake-fapi.tsRepository: clerk/javascript
Length of output: 3182
Wait for request handling before asserting the count.
holdRequestsadds requests when the MSW handler receives them. The current synchronous assertion can run before a duplicate request reaches that handler. Moving the assertion afterverify(actor)improves the ordering, butverify(actor)does not awaitprepare.requests. UsewaitForafter verification.💚 Suggested fix
📝 Committable suggestion
🤖 Prompt for AI Agents