11import {
2- cleanup ,
32 render ,
43 screen ,
54 waitFor ,
@@ -10,85 +9,30 @@ import {
109import { expect } from 'chai' ;
1110import React from 'react' ;
1211
13- import {
14- ConfirmationModalArea ,
15- useConfirmationModal ,
16- showConfirmation ,
17- } from './use-confirmation' ;
18-
19- const OpenConfirmationModalButton = ( ) => {
20- const { showConfirmation } = useConfirmationModal ( ) ;
21- return (
22- < button
23- type = "button"
24- onClick = { ( ) =>
25- void showConfirmation ( {
26- title : 'Are you sure?' ,
27- description : 'This action can not be undone.' ,
28- buttonText : 'Yes' ,
29- } )
30- }
31- >
32- Open Modal
33- </ button >
34- ) ;
35- } ;
12+ import { ConfirmationModalArea , showConfirmation } from './use-confirmation' ;
3613
3714describe ( 'use-confirmation' , function ( ) {
38- afterEach ( cleanup ) ;
39-
40- context ( 'useConfirmationModal hook' , function ( ) {
41- let modal : HTMLElement ;
42- beforeEach ( function ( ) {
43- render (
44- < ConfirmationModalArea >
45- < OpenConfirmationModalButton />
46- </ ConfirmationModalArea >
47- ) ;
48-
49- userEvent . click ( screen . getByText ( 'Open Modal' ) ) ;
50- modal = screen . getByTestId ( 'confirmation-modal' ) ;
51- expect ( modal ) . to . exist ;
52- } ) ;
53-
54- it ( 'renders modal contents' , function ( ) {
55- expect ( within ( modal ) . getByText ( 'Are you sure?' ) ) . to . exist ;
56- expect (
57- within ( modal ) . getByText ( 'This action can not be undone.' )
58- ) . to . exist ;
59- expect ( within ( modal ) . getByText ( 'Yes' ) ) . to . exist ;
60- expect ( within ( modal ) . getByText ( 'Cancel' ) ) . to . exist ;
61- } ) ;
62-
63- it ( 'handles cancel action' , async function ( ) {
64- userEvent . click ( within ( modal ) . getByText ( 'Cancel' ) ) ;
65- await waitForElementToBeRemoved ( ( ) =>
66- screen . getByTestId ( 'confirmation-modal' )
67- ) ;
68- } ) ;
69-
70- it ( 'handles confirm action' , async function ( ) {
71- userEvent . click ( within ( modal ) . getByText ( 'Yes' ) ) ;
72- await waitForElementToBeRemoved ( ( ) =>
73- screen . getByTestId ( 'confirmation-modal' )
74- ) ;
75- } ) ;
76- } ) ;
77-
7815 context ( 'showConfirmation global function' , function ( ) {
7916 let modal : HTMLElement ;
8017 let response : Promise < boolean > ;
8118 beforeEach ( async function ( ) {
8219 render (
8320 < ConfirmationModalArea >
84- < div />
21+ < button
22+ type = "button"
23+ onClick = { ( ) => {
24+ response = showConfirmation ( {
25+ title : 'Are you sure?' ,
26+ description : 'This action can not be undone.' ,
27+ buttonText : 'Yes' ,
28+ } ) ;
29+ } }
30+ >
31+ Open Modal
32+ </ button >
8533 </ ConfirmationModalArea >
8634 ) ;
87- response = showConfirmation ( {
88- title : 'Are you sure?' ,
89- description : 'This action can not be undone.' ,
90- buttonText : 'Yes' ,
91- } ) ;
35+ userEvent . click ( screen . getByText ( 'Open Modal' ) ) ;
9236 await waitFor ( ( ) => {
9337 modal = screen . getByTestId ( 'confirmation-modal' ) ;
9438 } ) ;
@@ -105,12 +49,18 @@ describe('use-confirmation', function () {
10549
10650 it ( 'handles cancel action' , async function ( ) {
10751 userEvent . click ( within ( modal ) . getByText ( 'Cancel' ) ) ;
52+ await waitForElementToBeRemoved ( ( ) =>
53+ screen . getByTestId ( 'confirmation-modal' )
54+ ) ;
10855 const confirmed = await response ;
10956 expect ( confirmed ) . to . be . false ;
11057 } ) ;
11158
11259 it ( 'handles confirm action' , async function ( ) {
11360 userEvent . click ( within ( modal ) . getByText ( 'Yes' ) ) ;
61+ await waitForElementToBeRemoved ( ( ) =>
62+ screen . getByTestId ( 'confirmation-modal' )
63+ ) ;
11464 const confirmed = await response ;
11565 expect ( confirmed ) . to . be . true ;
11666 } ) ;
0 commit comments