11import React from 'react' ;
22import userEvent from '@testing-library/user-event' ;
33import { render } from '@testing-library/react' ;
4- import BasicModal from './BasicModal' ;
4+ import BasicModal , { BasicModalProps } from './BasicModal' ;
55
66const DEFAULT_BODY_TEXT = 'DEFAULT_TEXT' ;
77const DEFAULT_TEST_ID = 'DEFAULT_TEST_ID' ;
88
9- function makeWrapper (
10- isOpen = true ,
11- bodyText = DEFAULT_BODY_TEXT ,
12- dataTestId = DEFAULT_TEST_ID ,
13- onConfirm = jest . fn ( )
14- ) {
15- return render (
16- < BasicModal
17- data-testid = { dataTestId }
18- isOpen = { isOpen }
19- headerText = ""
20- bodyText = { bodyText }
21- onConfirm = { onConfirm }
22- />
23- ) ;
9+ function makeWrapper ( partialProps : Partial < BasicModalProps > | undefined = { } ) {
10+ const props = {
11+ 'data-testid' : DEFAULT_TEST_ID ,
12+ bodyText : DEFAULT_BODY_TEXT ,
13+ headerText : '' ,
14+ isConfirmDanger : false ,
15+ isOpen : true ,
16+ onCancel : jest . fn ( ) ,
17+ onConfirm : jest . fn ( ) ,
18+ ...partialProps ,
19+ } ;
20+
21+ // eslint-disable-next-line react/jsx-props-no-spreading
22+ return render ( < BasicModal { ...props } /> ) ;
2423}
2524
2625it ( 'mounts and unmounts without failing' , ( ) => {
@@ -32,6 +31,16 @@ it('focuses default button on first render after opening', () => {
3231 expect ( getByTestId ( `${ DEFAULT_TEST_ID } -btn-confirm` ) ) . toHaveFocus ( ) ;
3332} ) ;
3433
34+ it . each ( [ true , false ] ) (
35+ 'sets button style based on isConfirmDanger value: %s' ,
36+ isConfirmDanger => {
37+ const { getByTestId } = makeWrapper ( { isConfirmDanger } ) ;
38+ expect ( getByTestId ( `${ DEFAULT_TEST_ID } -btn-confirm` ) ) . toHaveClass (
39+ isConfirmDanger ? 'btn-danger' : 'btn-primary'
40+ ) ;
41+ }
42+ ) ;
43+
3544it ( 'does not re-focus default button on re-render' , async ( ) => {
3645 const user = userEvent . setup ( ) ;
3746 const { getByTestId, rerender } = makeWrapper ( ) ;
0 commit comments