Skip to content

Commit

Permalink
added test for clearing prompt
Browse files Browse the repository at this point in the history
  • Loading branch information
jonbnewman committed Jan 8, 2022
1 parent d91e25f commit ce801d5
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "use-prompt",
"version": "1.0.1",
"version": "1.1.0",
"license": "MIT",
"author": "Jonathan Newman",
"description": "Display a react component to a user asynchronously",
Expand Down
12 changes: 7 additions & 5 deletions stories/components/Prompter/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import Message from './Message';
import Button from './Button';
import Output from './Output';

export interface Props extends HTMLAttributes<HTMLDivElement> {}
export interface Props extends HTMLAttributes<HTMLDivElement> {
clearable?: boolean;
}

export const Prompter: FC<Props> = () => {
const [prompt, showPrompt, visible] = usePrompt();
export const Prompter: FC<Props> = ({ clearable = false }) => {
const [prompt, showPrompt, visible, clear] = usePrompt();
const [resolveReason, setResolveReason] = useState<null | string>(null);
const [rejectReason, setRejectReason] = useState<null | string>(null);

Expand All @@ -34,8 +36,8 @@ export const Prompter: FC<Props> = () => {
<div>
<Button
variant="contained"
onClick={triggerPrompt}
disabled={visible}
onClick={visible ? clear : triggerPrompt}
disabled={visible && !clearable}
data-testid="show-prompt"
>
Show prompt
Expand Down
22 changes: 22 additions & 0 deletions test/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,28 @@ describe('Modal Prompt', () => {
});
});

it('can clear a prompt', async () => {
const { queryByText, getByTestId } = render(<Prompter clearable />);

await waitFor(() => {
expect(queryByText('Prompt is currently hidden')).toBeInTheDocument();
});

const button = getByTestId('show-prompt');
expect(button).not.toHaveAttribute('disabled');
expect(queryByText('Are you sure?')).not.toBeInTheDocument();

fireEvent.click(button);
await waitFor(() => {
expect(queryByText('Are you sure?')).toBeInTheDocument();
});

fireEvent.click(button);
await waitFor(() => {
expect(queryByText('Are you sure?')).not.toBeInTheDocument();
});
});

it('can resolve a prompt', async () => {
const { queryByText, getByTestId } = render(<Prompter />);

Expand Down

0 comments on commit ce801d5

Please sign in to comment.