-
Notifications
You must be signed in to change notification settings - Fork 13.4k
fix: add image/* filter for custom emoji file input #39175
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
dodaa08
wants to merge
5
commits into
RocketChat:develop
Choose a base branch
from
dodaa08:fix/FileUploadCustomEmoji
base: develop
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.
+137
−8
Open
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
75dec42
Added checks for image and removed webm format
dodaa08 db8a786
fix: improve useSingleFileInput and add test coverage
dodaa08 3f6a0eb
Update Unit tests
dodaa08 0a3b0a9
Merge branch 'develop' into fix/FileUploadCustomEmoji
dodaa08 53941e1
Merge branch 'develop' into fix/FileUploadCustomEmoji
dodaa08 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,126 @@ | ||
| import { renderHook, act } from '@testing-library/react'; | ||
| import { useSingleFileInput } from './useSingleFileInput'; | ||
|
|
||
| describe('useSingleFileInput', () => { | ||
| afterEach(() => { | ||
| document.body.innerHTML = ''; | ||
| }); | ||
|
|
||
| it('should create an input element and append it to the body', () => { | ||
| const onSetFile = jest.fn(); | ||
| renderHook(() => useSingleFileInput(onSetFile)); | ||
|
|
||
| const inputs = document.querySelectorAll('input[type="file"]'); | ||
| expect(inputs).toHaveLength(1); | ||
| expect((inputs[0] as HTMLInputElement).style.display).toBe('none'); | ||
| }); | ||
|
|
||
| it('should set the accept attribute based on fileType option', () => { | ||
| const onSetFile = jest.fn(); | ||
| renderHook(() => useSingleFileInput(onSetFile, 'image', { fileType: 'video/*' })); | ||
|
|
||
| const input = document.querySelector('input[type="file"]') as HTMLInputElement; | ||
| expect(input.getAttribute('accept')).toBe('video/*'); | ||
| }); | ||
|
|
||
| it('should use default accept attribute if not provided', () => { | ||
| const onSetFile = jest.fn(); | ||
| renderHook(() => useSingleFileInput(onSetFile)); | ||
|
|
||
| const input = document.querySelector('input[type="file"]') as HTMLInputElement; | ||
| expect(input.getAttribute('accept')).toBe('image/*'); | ||
| }); | ||
|
|
||
| it('should trigger click on the input when calling onClick', () => { | ||
| const onSetFile = jest.fn(); | ||
| const { result } = renderHook(() => useSingleFileInput(onSetFile)); | ||
| const [onClick] = result.current; | ||
|
|
||
| const input = document.querySelector('input[type="file"]') as HTMLInputElement; | ||
| const clickSpy = jest.spyOn(input, 'click'); | ||
|
|
||
| act(() => { | ||
| onClick(); | ||
| }); | ||
|
|
||
| expect(clickSpy).toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('should call onSetFile when a file is selected', () => { | ||
| const onSetFile = jest.fn(); | ||
| renderHook(() => useSingleFileInput(onSetFile, 'test-field')); | ||
|
|
||
| const input = document.querySelector('input[type="file"]') as HTMLInputElement; | ||
| const file = new File(['foo'], 'foo.txt', { type: 'text/plain' }); | ||
|
|
||
| Object.defineProperty(input, 'files', { | ||
| value: [file], | ||
| }); | ||
|
|
||
| act(() => { | ||
| input.dispatchEvent(new Event('change')); | ||
| }); | ||
|
|
||
| expect(onSetFile).toHaveBeenCalledWith(file, expect.any(FormData)); | ||
| const formData = onSetFile.mock.calls[0][1] as FormData; | ||
| expect(formData.get('test-field')).toBe(file); | ||
| }); | ||
|
|
||
| it('should not call onSetFile when no file is selected', () => { | ||
| const onSetFile = jest.fn(); | ||
| renderHook(() => useSingleFileInput(onSetFile, 'test-field')); | ||
|
|
||
| const input = document.querySelector('input[type="file"]') as HTMLInputElement; | ||
|
|
||
| Object.defineProperty(input, 'files', { | ||
| value: [], | ||
| }); | ||
|
|
||
| act(() => { | ||
| input.dispatchEvent(new Event('change')); | ||
| }); | ||
|
|
||
| expect(onSetFile).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('should remove input element on unmount', () => { | ||
| const onSetFile = jest.fn(); | ||
| const { unmount } = renderHook(() => useSingleFileInput(onSetFile)); | ||
|
|
||
| expect(document.querySelectorAll('input[type="file"]')).toHaveLength(1); | ||
|
|
||
| unmount(); | ||
|
|
||
| expect(document.querySelectorAll('input[type="file"]')).toHaveLength(0); | ||
| }); | ||
|
|
||
| it('should reset input value when calling reset', () => { | ||
| const onSetFile = jest.fn(); | ||
| const { result } = renderHook(() => useSingleFileInput(onSetFile)); | ||
| const [, reset] = result.current; | ||
|
|
||
| const input = document.querySelector('input[type="file"]') as HTMLInputElement; | ||
|
|
||
| const setterSpy = jest.spyOn(input, 'value', 'set'); | ||
|
|
||
| act(() => { | ||
| reset(); | ||
| }); | ||
|
|
||
| expect(setterSpy).toHaveBeenCalledWith(''); | ||
| }); | ||
|
|
||
| it('should update accept attribute when fileType option changes', () => { | ||
| const onSetFile = jest.fn(); | ||
| const { rerender } = renderHook( | ||
| ({ fileType }: { fileType?: string }) => | ||
| useSingleFileInput(onSetFile, 'image', { fileType }), | ||
| { initialProps: { fileType: 'image/*' } } | ||
| ); | ||
|
|
||
| rerender({ fileType: 'video/*' }); | ||
|
|
||
| const input = document.querySelector('input[type="file"]') as HTMLInputElement; | ||
| expect(input.getAttribute('accept')).toBe('video/*'); | ||
| }); | ||
| }); | ||
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
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
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.