11import React from 'react' ;
22import userEvent from '@testing-library/user-event' ;
33import { IntlProvider } from '@edx/frontend-platform/i18n' ;
4- import { fireEvent , render , screen } from '@testing-library/react' ;
4+ import { render , screen } from '@testing-library/react' ;
55
66import TypeXToConfirmModal from './TypeXToConfirmModal' ;
77
@@ -11,11 +11,11 @@ const defaultProps = () => ({
1111 confirmLabel : 'Delete' ,
1212 cancelLabel : 'Cancel' ,
1313 X : 'DELETE' ,
14- context : { id : 7 } ,
14+ confirmPayload : { id : 7 } ,
1515 isOpen : true ,
1616 onConfirm : jest . fn ( ) ,
1717 onCancel : jest . fn ( ) ,
18- setContext : jest . fn ( ) ,
18+ setConfirmPayload : jest . fn ( ) ,
1919} ) ;
2020
2121const renderModal = ( props = defaultProps ( ) ) =>
@@ -26,45 +26,58 @@ const renderModal = (props = defaultProps()) =>
2626 ) ;
2727
2828describe ( 'TypeXToConfirmModal' , ( ) => {
29- it ( 'keeps the destructive confirm button disabled until the typed value exactly matches the required confirmation phrase' , ( ) => {
29+ it ( 'renders the required confirmation phrase with strong emphasis' , ( ) => {
30+ renderModal ( ) ;
31+
32+ expect ( screen . getByText ( 'DELETE' , { selector : 'strong' } ) ) . toBeInTheDocument ( ) ;
33+ } ) ;
34+
35+ it ( 'keeps the destructive confirm button disabled until the typed value exactly matches the required confirmation phrase' , async ( ) => {
36+ const user = userEvent . setup ( ) ;
3037 renderModal ( ) ;
3138
3239 const input = screen . getByRole ( 'textbox' ) ;
3340 const confirmButton = screen . getByRole ( 'button' , { name : 'Delete' } ) ;
3441
3542 expect ( confirmButton ) . toBeDisabled ( ) ;
36- fireEvent . change ( input , { target : { value : 'DEL' } } ) ;
43+ await user . type ( input , 'DEL' ) ;
3744 expect ( confirmButton ) . toBeDisabled ( ) ;
38- fireEvent . change ( input , { target : { value : 'DELETE' } } ) ;
45+ await user . type ( input , 'ETE' ) ;
3946 expect ( confirmButton ) . toBeEnabled ( ) ;
4047 } ) ;
4148
42- it ( 'does not enable confirmation for partial, differently cased, or whitespace-padded confirmation text' , ( ) => {
49+ it ( 'does not enable confirmation for partial, differently cased, or whitespace-padded confirmation text' , async ( ) => {
50+ const user = userEvent . setup ( ) ;
4351 renderModal ( ) ;
4452
4553 const input = screen . getByRole ( 'textbox' ) ;
4654 const confirmButton = screen . getByRole ( 'button' , { name : 'Delete' } ) ;
4755
48- [ 'DEL' , 'delete' , ' DELETE' , 'DELETE ' , ' Delete ' ] . forEach ( value => {
49- fireEvent . change ( input , { target : { value } } ) ;
56+ for ( const value of [ 'DEL' , 'delete' , ' DELETE' , 'DELETE ' , ' Delete ' ] ) {
57+ await user . clear ( input ) ;
58+ await user . type ( input , value ) ;
5059 expect ( confirmButton ) . toBeDisabled ( ) ;
51- } ) ;
60+ }
5261 } ) ;
5362
54- it ( 'submits on Enter only after the exact confirmation phrase has been entered ' , async ( ) => {
63+ it ( 'requires explicit activation of the enabled destructive confirm button ' , async ( ) => {
5564 const user = userEvent . setup ( ) ;
5665 const props = defaultProps ( ) ;
5766 renderModal ( props ) ;
5867
5968 const input = screen . getByRole ( 'textbox' ) ;
69+ const confirmButton = screen . getByRole ( 'button' , { name : 'Delete' } ) ;
6070
6171 await user . click ( input ) ;
6272 await user . keyboard ( '{Enter}' ) ;
6373 expect ( props . onConfirm ) . not . toHaveBeenCalled ( ) ;
6474
6575 await user . type ( input , 'DELETE' ) ;
6676 await user . keyboard ( '{Enter}' ) ;
67- expect ( props . onConfirm ) . toHaveBeenCalledWith ( props . context ) ;
77+ expect ( props . onConfirm ) . not . toHaveBeenCalled ( ) ;
78+
79+ await user . click ( confirmButton ) ;
80+ expect ( props . onConfirm ) . toHaveBeenCalledWith ( props . confirmPayload ) ;
6881 } ) ;
6982
7083 it ( 'resets confirmation state when the modal closes so a reopened dialog starts disabled again' , async ( ) => {
@@ -90,7 +103,7 @@ describe('TypeXToConfirmModal', () => {
90103 expect ( screen . getByRole ( 'button' , { name : 'Delete' } ) ) . toBeDisabled ( ) ;
91104 } ) ;
92105
93- it ( 'clears the provided context when the modal is closed without confirming' , ( ) => {
106+ it ( 'clears the provided confirm payload when the modal is closed without confirming' , ( ) => {
94107 const props = defaultProps ( ) ;
95108 const { rerender } = renderModal ( props ) ;
96109
@@ -100,7 +113,7 @@ describe('TypeXToConfirmModal', () => {
100113 </ IntlProvider > ,
101114 ) ;
102115
103- expect ( props . setContext ) . toHaveBeenCalledWith ( null ) ;
116+ expect ( props . setConfirmPayload ) . toHaveBeenCalledWith ( null ) ;
104117 expect ( props . onConfirm ) . not . toHaveBeenCalled ( ) ;
105118 } ) ;
106119} ) ;
0 commit comments