Skip to content

Commit

Permalink
Test that the Confirm closes when pressing Escape
Browse files Browse the repository at this point in the history
  • Loading branch information
fullofcaffeine committed Aug 27, 2021
1 parent 7d629b8 commit 12f0b18
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 10 deletions.
10 changes: 4 additions & 6 deletions packages/components/src/confirm/component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,10 @@ function Confirm(
props: PolymorphicComponentProps< OwnProps, 'div', false >,
forwardedRef: Ref< any >
) {
const {
message,
onConfirm,
onCancel,
...otherProps
} = useContextSystem( props, 'Confirm' );
const { message, onConfirm, onCancel, ...otherProps } = useContextSystem(
props,
'Confirm'
);

const [ isOpen, setIsOpen ] = useState( true );

Expand Down
64 changes: 64 additions & 0 deletions packages/components/src/confirm/test/__snapshots__/index.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,70 @@ Object {
}
`;

exports[`Confirm Confirm component should not render if dialog is closed by pressing Escape 1`] = `
Object {
"asFragment": [Function],
"baseElement": <body
class=""
>
<div />
<div />
</body>,
"container": <div />,
"debug": [Function],
"findAllByAltText": [Function],
"findAllByDisplayValue": [Function],
"findAllByLabelText": [Function],
"findAllByPlaceholderText": [Function],
"findAllByRole": [Function],
"findAllByTestId": [Function],
"findAllByText": [Function],
"findAllByTitle": [Function],
"findByAltText": [Function],
"findByDisplayValue": [Function],
"findByLabelText": [Function],
"findByPlaceholderText": [Function],
"findByRole": [Function],
"findByTestId": [Function],
"findByText": [Function],
"findByTitle": [Function],
"getAllByAltText": [Function],
"getAllByDisplayValue": [Function],
"getAllByLabelText": [Function],
"getAllByPlaceholderText": [Function],
"getAllByRole": [Function],
"getAllByTestId": [Function],
"getAllByText": [Function],
"getAllByTitle": [Function],
"getByAltText": [Function],
"getByDisplayValue": [Function],
"getByLabelText": [Function],
"getByPlaceholderText": [Function],
"getByRole": [Function],
"getByTestId": [Function],
"getByText": [Function],
"getByTitle": [Function],
"queryAllByAltText": [Function],
"queryAllByDisplayValue": [Function],
"queryAllByLabelText": [Function],
"queryAllByPlaceholderText": [Function],
"queryAllByRole": [Function],
"queryAllByTestId": [Function],
"queryAllByText": [Function],
"queryAllByTitle": [Function],
"queryByAltText": [Function],
"queryByDisplayValue": [Function],
"queryByLabelText": [Function],
"queryByPlaceholderText": [Function],
"queryByRole": [Function],
"queryByTestId": [Function],
"queryByText": [Function],
"queryByTitle": [Function],
"rerender": [Function],
"unmount": [Function],
}
`;

exports[`Confirm Confirm component should render correctly 1`] = `
Object {
"asFragment": [Function],
Expand Down
27 changes: 23 additions & 4 deletions packages/components/src/confirm/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
/**
* External dependencies
*/
import { render, fireEvent, waitForElementToBeRemoved } from '@testing-library/react';
import {
render,
fireEvent,
waitForElementToBeRemoved,
} from '@testing-library/react';

/**
* Internal dependencies
Expand Down Expand Up @@ -63,16 +67,31 @@ describe( 'Confirm', () => {
<Confirm onConfirm={ noop } onCancel={ noop } />
);

const frame = wrapper.baseElement.querySelector( '.components-modal__frame.components-confirm' );
const frame = wrapper.baseElement.querySelector(
'.components-modal__frame.components-confirm'
);
/**
* The overlay click is handled by detecting an onBlur from the modal frame.
* See the `handeFocusOutside` function in the `ModalFrame` component
* and the `withFocusOutside` HOC for moret details.
*/
fireEvent.blur( frame );

await waitForElementToBeRemoved( frame );

expect( wrapper ).toMatchSnapshot();
} );

fireEvent.blur(frame);
it( 'should not render if dialog is closed by pressing Escape', async () => {
const wrapper = render(
<Confirm onConfirm={ noop } onCancel={ noop } />
);

const frame = wrapper.baseElement.querySelector(
'.components-modal__frame.components-confirm'
);

await waitForElementToBeRemoved(frame);
fireEvent.keyDown( frame, { keyCode: 27 } );

expect( wrapper ).toMatchSnapshot();
} );
Expand Down

0 comments on commit 12f0b18

Please sign in to comment.