-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add onClick prop to FormFileUpload (#39268)
* Add onClick prop to FormFileUpload It will allow to the parent component clear the file input when adding the same file. * Rename onInputFileClick to onClick * Test file change on FormFileUpload component * Add changelog entry * Update changelog entry * Change FormFileUpload test comments * Use user-event@14 recommended setup * Update unit tests * Update unit tests comments * Update unit tests * Fix typos * Update unit tests * Update changelog * Update packages/components/CHANGELOG.md * Fixup changelog Moved to "Enhancements" subsection * Add `onClick` prop to readme Co-authored-by: Marco Ciampini <[email protected]> Co-authored-by: Lena Morita <[email protected]>
- Loading branch information
1 parent
3ef88d4
commit 8699e17
Showing
4 changed files
with
95 additions
and
11 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,30 +1,92 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { shallow } from 'enzyme'; | ||
import { noop } from 'lodash'; | ||
import { render as RTLrender, screen } from '@testing-library/react'; | ||
import userEvent from '@testing-library/user-event'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import FormFileUpload from '../'; | ||
|
||
/** | ||
* Browser dependencies | ||
*/ | ||
const { File } = window; | ||
|
||
function render( jsx ) { | ||
return { | ||
user: userEvent.setup( { | ||
// Avoids timeout errors (https://github.com/testing-library/user-event/issues/565#issuecomment-1064579531). | ||
delay: null, | ||
} ), | ||
...RTLrender( jsx ), | ||
}; | ||
} | ||
|
||
// @testing-library/user-event considers changing <input type="file"> to a string as a change, but it do not occur on real browsers, so the comparisons will be against this result | ||
const fakePath = expect.objectContaining( { | ||
target: expect.objectContaining( { | ||
value: 'C:\\fakepath\\hello.png', | ||
} ), | ||
} ); | ||
|
||
describe( 'FormFileUpload', () => { | ||
it( 'should show an Icon Button and a hidden input', () => { | ||
const wrapper = shallow( | ||
render( <FormFileUpload>My Upload Button</FormFileUpload> ); | ||
|
||
const button = screen.getByText( 'My Upload Button' ); | ||
const input = screen.getByTestId( 'form-file-upload-input' ); | ||
expect( button ).toBeInTheDocument(); | ||
expect( input.style.display ).toBe( 'none' ); | ||
} ); | ||
|
||
it( 'should not fire a change event after selecting the same file', async () => { | ||
const onChange = jest.fn(); | ||
|
||
const { user } = render( | ||
<FormFileUpload onChange={ onChange }> | ||
My Upload Button | ||
</FormFileUpload> | ||
); | ||
|
||
const file = new File( [ 'hello' ], 'hello.png', { | ||
type: 'image/png', | ||
} ); | ||
|
||
const input = screen.getByTestId( 'form-file-upload-input' ); | ||
|
||
await user.upload( input, file ); | ||
|
||
await user.upload( input, file ); | ||
|
||
expect( onChange ).toHaveBeenCalledTimes( 1 ); | ||
expect( onChange ).toHaveBeenCalledWith( fakePath ); | ||
} ); | ||
|
||
it( 'should fire a change event after selecting the same file if the value was reset in between', async () => { | ||
const onChange = jest.fn(); | ||
|
||
const { user } = render( | ||
<FormFileUpload | ||
instanceId={ 1 } | ||
blocks={ [] } | ||
recentlyUsedBlocks={ [] } | ||
debouncedSpeak={ noop } | ||
onClick={ jest.fn( ( e ) => ( e.target.value = '' ) ) } | ||
onChange={ onChange } | ||
> | ||
My Upload Button | ||
</FormFileUpload> | ||
); | ||
|
||
const button = wrapper.find( 'ForwardRef(Button)' ); | ||
const input = wrapper.find( 'input' ); | ||
expect( button.prop( 'children' ) ).toBe( 'My Upload Button' ); | ||
expect( input.prop( 'style' ).display ).toBe( 'none' ); | ||
const file = new File( [ 'hello' ], 'hello.png', { | ||
type: 'image/png', | ||
} ); | ||
|
||
const input = screen.getByTestId( 'form-file-upload-input' ); | ||
await user.upload( input, file ); | ||
|
||
expect( onChange ).toHaveBeenNthCalledWith( 1, fakePath ); | ||
|
||
await user.upload( input, file ); | ||
|
||
expect( onChange ).toHaveBeenNthCalledWith( 2, fakePath ); | ||
} ); | ||
} ); |