Commit 29bd7b79 authored by Lyza Danger Gardner's avatar Lyza Danger Gardner Committed by Lyza Gardner

Replace modal component in `prompts` with shared `ConfirmModal`

parent be29e99a
import { render } from 'preact'; import { render } from 'preact';
import ConfirmDialog from './components/ConfirmDialog'; import { ConfirmModal } from '@hypothesis/frontend-shared';
/** /**
* Show the user a prompt asking them to confirm an action. * Show the user a prompt asking them to confirm an action.
...@@ -11,7 +11,7 @@ import ConfirmDialog from './components/ConfirmDialog'; ...@@ -11,7 +11,7 @@ import ConfirmDialog from './components/ConfirmDialog';
* for the native `window.confirm` dialog) * for the native `window.confirm` dialog)
* - The visual style of the dialog matches the Hypothesis design system * - The visual style of the dialog matches the Hypothesis design system
* *
* @param {object} options - Options for the `ConfirmDialog` * @param {object} options - Options for the `ConfirmModal`
* @prop {string} [title] * @prop {string} [title]
* @prop {string} message * @prop {string} message
* @prop {string} [confirmAction] * @prop {string} [confirmAction]
...@@ -52,7 +52,7 @@ export async function confirm({ ...@@ -52,7 +52,7 @@ export async function confirm({
}; };
render( render(
<ConfirmDialog <ConfirmModal
title={title} title={title}
message={message} message={message}
confirmAction={confirmAction} confirmAction={confirmAction}
......
import { confirm, $imports } from '../prompts'; import { confirm } from '../prompts';
function FakeConfirmDialog({
confirmAction,
message,
onCancel,
onConfirm,
title,
}) {
return (
<div>
<h1>{title}</h1>
<p>{message}</p>
<button data-testid="confirm" onClick={onConfirm}>
{confirmAction}
</button>
<button data-testid="cancel" onClick={onCancel}>
Cancel
</button>
</div>
);
}
describe('shared/prompts', () => { describe('shared/prompts', () => {
describe('confirm', () => { describe('confirm', () => {
beforeEach(() => { beforeEach(() => {
// This will cause the custom ConfirmModal to be used instead of
// window.confirm
sinon.stub(window, 'confirm').returns(false); sinon.stub(window, 'confirm').returns(false);
$imports.$mock({
'./components/ConfirmDialog': FakeConfirmDialog,
});
}); });
afterEach(() => { afterEach(() => {
window.confirm.restore(); window.confirm.restore();
}); });
function getCustomDialog() { function clickClose() {
return document.querySelector('[data-testid="confirm-container"]'); const closeButton = getCustomDialog().querySelector(
'[aria-label="Close"]'
);
closeButton.click();
}
function clickCancel() {
const cancelButton = getCustomDialog().querySelector(
'[data-testid="cancel-button"]'
);
cancelButton.click();
} }
function clickConfirm() { function clickConfirm() {
const confirmButton = getCustomDialog().querySelector( const confirmButton = getCustomDialog().querySelector(
'[data-testid="confirm"]' '[data-testid="confirm-button"]'
); );
confirmButton.click(); confirmButton.click();
} }
function clickCancel() { function getCustomDialog() {
const cancelButton = getCustomDialog().querySelector( return document.querySelector('[data-testid="confirm-container"]');
'[data-testid="cancel"]'
);
cancelButton.click();
} }
it('uses `window.confirm` if available', async () => { it('uses `window.confirm` if available', async () => {
...@@ -68,17 +52,11 @@ describe('shared/prompts', () => { ...@@ -68,17 +52,11 @@ describe('shared/prompts', () => {
const dialog = getCustomDialog(); const dialog = getCustomDialog();
assert.ok(dialog); assert.ok(dialog);
assert.equal(dialog.querySelector('h1').textContent, 'Confirm action?');
assert.equal(dialog.querySelector('p').textContent, 'Do the thing?');
assert.equal(
dialog.querySelector('[data-testid=confirm]').textContent,
'Yeah!'
);
clickConfirm(); clickClose();
await result;
assert.notOk(getCustomDialog()); assert.notOk(getCustomDialog());
assert.isFalse(await result);
}); });
it('returns true if "Confirm" button is clicked', async () => { it('returns true if "Confirm" button is clicked', async () => {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment