-
Notifications
You must be signed in to change notification settings - Fork 446
feat(ui): Add drag to upload to AvatarUploader #8348
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
2
commits into
main
Choose a base branch
from
carp/avatar-uploader-drag-to-upload
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.
+236
−8
Open
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
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
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
174 changes: 174 additions & 0 deletions
174
packages/ui/src/elements/__tests__/AvatarUploader.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,174 @@ | ||
| import { fireEvent, render, waitFor } from '@testing-library/react'; | ||
| import userEvent from '@testing-library/user-event'; | ||
| import { describe, expect, it, vi } from 'vitest'; | ||
|
|
||
| import { bindCreateFixtures } from '@/test/create-fixtures'; | ||
|
|
||
| import { localizationKeys } from '../../customizables'; | ||
| import { AvatarUploader, type AvatarUploaderProps } from '../AvatarUploader'; | ||
| import { useCardState, withCardStateProvider } from '../contexts'; | ||
|
|
||
| const { createFixtures } = bindCreateFixtures('UserProfile'); | ||
|
|
||
| const StubPreview = (_props: { imageUrl?: string }) => <span data-testid='avatar-preview' />; | ||
|
|
||
| type HarnessProps = Omit<AvatarUploaderProps, 'title' | 'avatarPreview'>; | ||
|
|
||
| const Harness = withCardStateProvider((props: HarnessProps) => { | ||
| const card = useCardState(); | ||
| return ( | ||
| <> | ||
| <AvatarUploader | ||
| {...props} | ||
| title={localizationKeys('userProfile.profilePage.imageFormTitle')} | ||
| avatarPreview={<StubPreview />} | ||
| /> | ||
| {card.error ? <div data-testid='card-error'>{card.error}</div> : null} | ||
| </> | ||
| ); | ||
| }); | ||
|
|
||
| const makeImageFile = (size = 1024, type = 'image/png') => { | ||
| const file = new File([new Uint8Array(size)], 'logo.png', { type }); | ||
| Object.defineProperty(file, 'size', { value: size }); | ||
| return file; | ||
| }; | ||
|
|
||
| const makeDataTransfer = (files: File[] = [], types: string[] = ['Files']) => | ||
| ({ | ||
| files, | ||
| types, | ||
| items: files.map(f => ({ kind: 'file', type: f.type, getAsFile: () => f })), | ||
| dropEffect: 'none', | ||
| effectAllowed: 'all', | ||
| }) as unknown as DataTransfer; | ||
|
|
||
| const findFileInput = (container: HTMLElement) => { | ||
| const input = container.querySelector<HTMLInputElement>('input[type="file"]'); | ||
| if (!input) throw new Error('Could not find hidden file input'); | ||
| return input; | ||
| }; | ||
|
|
||
| const findDropZone = (container: HTMLElement) => { | ||
| // The outer Flex registered with the drop handlers is the file input's next sibling. | ||
| const sibling = findFileInput(container).nextElementSibling; | ||
| if (!sibling) throw new Error('Could not find drop zone element'); | ||
| return sibling as HTMLElement; | ||
| }; | ||
|
|
||
| describe('AvatarUploader', () => { | ||
| describe('click-upload', () => { | ||
| it('calls onAvatarChange with the selected file', async () => { | ||
| const { wrapper } = await createFixtures(); | ||
| const onAvatarChange = vi.fn().mockResolvedValue(undefined); | ||
| const file = makeImageFile(); | ||
| const { container } = render(<Harness onAvatarChange={onAvatarChange} />, { wrapper }); | ||
|
|
||
| fireEvent.change(findFileInput(container), { target: { files: [file] } }); | ||
|
|
||
| await waitFor(() => expect(onAvatarChange).toHaveBeenCalledWith(file)); | ||
| }); | ||
| }); | ||
|
|
||
| describe('drag-and-drop', () => { | ||
| it('calls onAvatarChange when a valid image file is dropped', async () => { | ||
| const { wrapper } = await createFixtures(); | ||
| const onAvatarChange = vi.fn().mockResolvedValue(undefined); | ||
| const file = makeImageFile(); | ||
| const { container } = render(<Harness onAvatarChange={onAvatarChange} />, { wrapper }); | ||
|
|
||
| fireEvent.drop(findDropZone(container), { dataTransfer: makeDataTransfer([file]) }); | ||
|
|
||
| await waitFor(() => expect(onAvatarChange).toHaveBeenCalledWith(file)); | ||
| }); | ||
|
|
||
| it('rejects unsupported file types', async () => { | ||
| const { wrapper } = await createFixtures(); | ||
| const onAvatarChange = vi.fn(); | ||
| const pdf = makeImageFile(1024, 'application/pdf'); | ||
| const { container, findByTestId } = render(<Harness onAvatarChange={onAvatarChange} />, { wrapper }); | ||
|
|
||
| fireEvent.drop(findDropZone(container), { dataTransfer: makeDataTransfer([pdf]) }); | ||
|
|
||
| const error = await findByTestId('card-error'); | ||
| expect(error).toHaveTextContent(/file type not supported/i); | ||
| expect(onAvatarChange).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('rejects files exceeding the max size', async () => { | ||
| const { wrapper } = await createFixtures(); | ||
| const onAvatarChange = vi.fn(); | ||
| const oversized = makeImageFile(11 * 1000 * 1000); | ||
| const { container, findByTestId } = render(<Harness onAvatarChange={onAvatarChange} />, { wrapper }); | ||
|
|
||
| fireEvent.drop(findDropZone(container), { dataTransfer: makeDataTransfer([oversized]) }); | ||
|
|
||
| const error = await findByTestId('card-error'); | ||
| expect(error).toHaveTextContent(/file size exceeds/i); | ||
| expect(onAvatarChange).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('ignores drops that do not contain files (e.g. text drags)', async () => { | ||
| const { wrapper } = await createFixtures(); | ||
| const onAvatarChange = vi.fn(); | ||
| const { container } = render(<Harness onAvatarChange={onAvatarChange} />, { wrapper }); | ||
|
|
||
| fireEvent.drop(findDropZone(container), { | ||
| dataTransfer: makeDataTransfer([], ['text/plain']), | ||
| }); | ||
|
|
||
| expect(onAvatarChange).not.toHaveBeenCalled(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('remove button', () => { | ||
| it('is hidden when onAvatarRemove is not provided', async () => { | ||
| const { wrapper } = await createFixtures(); | ||
| const { queryByRole } = render(<Harness onAvatarChange={vi.fn().mockResolvedValue(undefined)} />, { wrapper }); | ||
|
|
||
| expect(queryByRole('button', { name: /^remove$/i })).not.toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('stays visible after a successful upload', async () => { | ||
| // Regression: previously `showUpload` was toggled inside handleFileDrop and the remove | ||
| // button was gated on `!showUpload`, so it disappeared after each successful upload. | ||
| const { wrapper } = await createFixtures(); | ||
| const onAvatarChange = vi.fn().mockResolvedValue(undefined); | ||
| const onAvatarRemove = vi.fn(); | ||
| const { container, getByRole } = render( | ||
| <Harness | ||
| onAvatarChange={onAvatarChange} | ||
| onAvatarRemove={onAvatarRemove} | ||
| />, | ||
| { wrapper }, | ||
| ); | ||
|
|
||
| expect(getByRole('button', { name: /^remove$/i })).toBeInTheDocument(); | ||
|
|
||
| fireEvent.change(findFileInput(container), { target: { files: [makeImageFile()] } }); | ||
|
|
||
| await waitFor(() => expect(onAvatarChange).toHaveBeenCalledTimes(1)); | ||
| await waitFor(() => { | ||
| expect(getByRole('button', { name: /^remove$/i })).not.toBeDisabled(); | ||
| }); | ||
| expect(getByRole('button', { name: /^remove$/i })).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('invokes onAvatarRemove when clicked', async () => { | ||
| const user = userEvent.setup(); | ||
| const { wrapper } = await createFixtures(); | ||
| const onAvatarRemove = vi.fn(); | ||
| const { getByRole } = render( | ||
| <Harness | ||
| onAvatarChange={vi.fn().mockResolvedValue(undefined)} | ||
| onAvatarRemove={onAvatarRemove} | ||
| />, | ||
| { wrapper }, | ||
| ); | ||
|
|
||
| await user.click(getByRole('button', { name: /^remove$/i })); | ||
|
|
||
| await waitFor(() => expect(onAvatarRemove).toHaveBeenCalledTimes(1)); | ||
| }); | ||
| }); | ||
| }); |
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.
fixed a bug where the remove button was showing inconsistently