Skip to content
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

Add onClick prop to FormFileUpload #39268

Merged
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

### Enhancements

- Add `onClick` event on `FormFileUpload` to emit `onChange` event when selecting the same file again.
mirka marked this conversation as resolved.
Show resolved Hide resolved
mirka marked this conversation as resolved.
Show resolved Hide resolved
- `ConfirmDialog`: Add support for custom label text on the confirmation and cancelation buttons ([#38994](https://github.com/WordPress/gutenberg/pull/38994))
- `InputControl`: Allow `onBlur` for empty values to commit the change when `isPressEnterToChange` is true, and move reset behavior to the ESCAPE key. ([#39109](https://github.com/WordPress/gutenberg/pull/39109)).
- `TreeGrid`: Add tests for Home/End keyboard navigation. Add `onFocusRow` callback for Home/End keyboard navigation, this was missed in the implementation PR. Modify test for expanding/collapsing a row as row 1 implements this now. Update README with latest changes. ([#39302](https://github.com/WordPress/gutenberg/pull/39302))
Expand Down
3 changes: 3 additions & 0 deletions packages/components/src/form-file-upload/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ function FormFileUpload( {
children,
multiple = false,
onChange,
onClick,
render,
...props
} ) {
Expand All @@ -38,6 +39,8 @@ function FormFileUpload( {
style={ { display: 'none' } }
accept={ accept }
onChange={ onChange }
onClick={ onClick }
data-testid="form-file-upload-input"
/>
</div>
);
Expand Down
83 changes: 70 additions & 13 deletions packages/components/src/form-file-upload/test/index.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,87 @@
/**
* 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 ),
};
}

describe( 'FormFileUpload', () => {
it( 'should show an Icon Button and a hidden input', () => {
const wrapper = shallow(
<FormFileUpload
instanceId={ 1 }
blocks={ [] }
recentlyUsedBlocks={ [] }
debouncedSpeak={ noop }
>
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 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 );

// await last upload event propagation
setTimeout( async () => {
await user.upload( input, file );

expect( onChange ).toHaveBeenCalledTimes( 1 );
}, 0 );
} );

it( 'should fire a change event after selecting the same file if there is a onClick', async () => {
mirka marked this conversation as resolved.
Show resolved Hide resolved
mirka marked this conversation as resolved.
Show resolved Hide resolved
const onChange = jest.fn();

const { user } = render(
<FormFileUpload onClick={ jest.fn() } 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 );

expect( onChange ).toHaveBeenCalledTimes( 1 );

// await last upload event propagation and onClick event run
setTimeout( async () => {
await user.upload( input, file );

expect( onChange ).toHaveBeenCalledTimes( 2 );
}, 0 );
} );
} );