-
Notifications
You must be signed in to change notification settings - Fork 474
feat(ui): Add an edit username to user profile #9712
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
+657
−35
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
Some comments aren't visible on the classic Files Changed page.
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 @@ | ||
| --- | ||
| --- |
28 changes: 28 additions & 0 deletions
28
packages/swingset/src/stories/fixtures/user-profile-edit-username.ts
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,28 @@ | ||
| import type { UserProfileFormError } from '@clerk/ui/mosaic/user-profile/user-profile-account-section/user-profile-account-section.types'; | ||
| import { UserProfileSaveError } from '@clerk/ui/mosaic/user-profile/user-profile-account-section/user-profile-account-section.types'; | ||
| import { useState } from 'react'; | ||
|
|
||
| export interface UserProfileEditUsernameFixtureOptions { | ||
| username?: string; | ||
| latency?: number; | ||
| failWith?: UserProfileFormError; | ||
| } | ||
|
|
||
| export function useUserProfileEditUsernameFixture({ | ||
| username: initialUsername = 'prestonxyz', | ||
| latency = 800, | ||
| failWith, | ||
| }: UserProfileEditUsernameFixtureOptions = {}) { | ||
| const [username, setUsername] = useState(initialUsername); | ||
|
|
||
| return { | ||
| username, | ||
| onSubmitUsername: async (value: string) => { | ||
| await new Promise(resolve => setTimeout(resolve, latency)); | ||
| if (failWith) { | ||
| throw new UserProfileSaveError(failWith.message ?? 'Something went wrong.', failWith.fields); | ||
| } | ||
| setUsername(value); | ||
| }, | ||
| }; | ||
| } | ||
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
142 changes: 142 additions & 0 deletions
142
packages/ui/src/mosaic/user-profile/__tests__/user-profile-edit-username.view.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,142 @@ | ||
| import { render, screen, waitFor } from '@testing-library/react'; | ||
| import userEvent from '@testing-library/user-event'; | ||
| import { describe, expect, it, vi } from 'vitest'; | ||
|
|
||
| import { Button } from '../../components/button'; | ||
| import { MosaicProvider } from '../../MosaicProvider'; | ||
| import type { UserProfileEditUsernameViewProps } from '../user-profile-account-section/user-profile-edit-username.view'; | ||
| import { UserProfileEditUsernameView } from '../user-profile-account-section/user-profile-edit-username.view'; | ||
|
|
||
| function renderView(overrides: Partial<UserProfileEditUsernameViewProps> = {}) { | ||
| const props: UserProfileEditUsernameViewProps = { | ||
| open: true, | ||
| onOpenChange: vi.fn(), | ||
| username: 'prestonxyz', | ||
| onUsernameChange: vi.fn(), | ||
| onSubmit: vi.fn(), | ||
| ...overrides, | ||
| }; | ||
| return { | ||
| props, | ||
| ...render( | ||
| <MosaicProvider> | ||
| <UserProfileEditUsernameView {...props} /> | ||
| </MosaicProvider>, | ||
| ), | ||
| }; | ||
| } | ||
|
|
||
| const usernameField = () => screen.getByLabelText('Username'); | ||
| const saveButton = () => screen.getByRole('button', { name: 'Save changes' }); | ||
|
|
||
| describe('UserProfileEditUsernameView', () => { | ||
| it('renders nothing until the caller opens it', () => { | ||
| renderView({ open: false }); | ||
|
|
||
| expect(screen.queryByRole('dialog')).not.toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('names the dialog and shows the value it was given', () => { | ||
| renderView(); | ||
|
|
||
| expect(screen.getByRole('dialog', { name: 'Edit username' })).toBeInTheDocument(); | ||
| expect(usernameField()).toHaveValue('prestonxyz'); | ||
| }); | ||
|
|
||
| it('opens on the field rather than the corner dismiss', async () => { | ||
| renderView(); | ||
|
|
||
| // `FloatingFocusManager` moves focus in an effect, hence the wait. | ||
| await waitFor(() => expect(usernameField()).toHaveFocus()); | ||
| }); | ||
|
|
||
| it('asks to open from the trigger', async () => { | ||
| const onOpenChange = vi.fn(); | ||
| const user = userEvent.setup(); | ||
| renderView({ open: false, onOpenChange, trigger: <Button>Edit username</Button> }); | ||
|
|
||
| await user.click(screen.getByRole('button', { name: 'Edit username' })); | ||
|
|
||
| expect(onOpenChange).toHaveBeenCalledWith(true, expect.anything()); | ||
| }); | ||
|
|
||
| it('reports each keystroke, holding nothing itself', async () => { | ||
| const onUsernameChange = vi.fn(); | ||
| const user = userEvent.setup(); | ||
| renderView({ onUsernameChange }); | ||
|
|
||
| await user.type(usernameField(), 'x'); | ||
|
|
||
| expect(onUsernameChange).toHaveBeenCalledWith('prestonxyzx'); | ||
| // Controlled: the rendered value only moves when the caller says so. | ||
| expect(usernameField()).toHaveValue('prestonxyz'); | ||
| }); | ||
|
|
||
| it('submits from the action and from enter in the field', async () => { | ||
| const onSubmit = vi.fn(); | ||
| const user = userEvent.setup(); | ||
| renderView({ onSubmit }); | ||
|
|
||
| await user.click(saveButton()); | ||
| // One field, so native implicit submission carries Enter with no submit button in the form. | ||
| await user.type(usernameField(), '{Enter}'); | ||
|
|
||
| expect(onSubmit).toHaveBeenCalledTimes(2); | ||
| }); | ||
|
|
||
| it('asks to close from cancel', async () => { | ||
| const onOpenChange = vi.fn(); | ||
| const user = userEvent.setup(); | ||
| renderView({ onOpenChange }); | ||
|
|
||
| await user.click(screen.getByRole('button', { name: 'Cancel' })); | ||
|
|
||
| expect(onOpenChange).toHaveBeenCalledWith(false, expect.anything()); | ||
| }); | ||
|
|
||
| it('announces the failure in a negative banner', () => { | ||
| renderView({ error: { message: 'Your username could not be updated.' } }); | ||
|
|
||
| const banner = screen.getByRole('alert'); | ||
| expect(banner).toHaveAttribute('data-color', 'negative'); | ||
| expect(banner).toHaveTextContent('Your username could not be updated.'); | ||
| expect(usernameField()).not.toHaveAttribute('aria-invalid', 'true'); | ||
| }); | ||
|
|
||
| it('renders a field-scoped failure with no banner', () => { | ||
| renderView({ error: { fields: { username: 'That username is taken.' } } }); | ||
|
|
||
| expect(screen.queryByRole('alert')).not.toBeInTheDocument(); | ||
| expect(screen.getByText('That username is taken.')).toBeInTheDocument(); | ||
| expect(usernameField()).toHaveAttribute('aria-invalid', 'true'); | ||
| }); | ||
|
|
||
| it('withholds the save while the caller says the value is unacceptable', async () => { | ||
| const onSubmit = vi.fn(); | ||
| const user = userEvent.setup(); | ||
| renderView({ canSave: false, onSubmit }); | ||
|
|
||
| // Inert but still reachable, so the reason stays discoverable by keyboard. | ||
| expect(saveButton()).toHaveAttribute('aria-disabled', 'true'); | ||
| await user.click(saveButton()); | ||
| await user.type(usernameField(), '{Enter}'); | ||
|
|
||
| expect(onSubmit).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('stays inert while the save runs', async () => { | ||
| const onSubmit = vi.fn(); | ||
| const onUsernameChange = vi.fn(); | ||
| const user = userEvent.setup(); | ||
| renderView({ isSaving: true, onSubmit, onUsernameChange }); | ||
|
|
||
| await user.type(usernameField(), 'ada'); | ||
|
|
||
| expect(usernameField()).toBeDisabled(); | ||
| expect(onUsernameChange).not.toHaveBeenCalled(); | ||
| // Busy, not unavailable: the pending affordance is `isPending`, not a second disabled state. | ||
| expect(saveButton()).toHaveAttribute('aria-busy', 'true'); | ||
| await user.click(saveButton()); | ||
| expect(onSubmit).not.toHaveBeenCalled(); | ||
| }); | ||
| }); |
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.
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.
📐 Maintainability & Code Quality | 🟠 Major | ⚡ Quick win
Define the exported hook's return type.
useUserProfileEditUsernameFixtureexposes a shared fixture contract. Declare its result type so changes tousernameoronSaveUsernamecannot silently alter consumers.Proposed change
Based on learnings, “enforce explicit return type annotations for exported functions and public APIs.” As per coding guidelines, “Always define explicit return types for functions, especially public APIs.”
📝 Committable suggestion
🤖 Prompt for AI Agents
Sources: Coding guidelines, Learnings