@@ -4,17 +4,16 @@ import {
4
4
ConfirmationDialogProps ,
5
5
} from "@/components/ConfirmationDialog/ConfirmationDialog" ;
6
6
import { fireEvent } from "@testing-library/dom" ;
7
- import { Button } from "@/components" ;
8
7
9
8
describe ( "Dialog Component" , ( ) => {
10
9
const renderDialog = (
11
10
{
12
11
title = "Dialog Title" ,
13
12
message = "Are you sure you want to proceed?" ,
14
- onPrimaryActionClick = ( ) => {
13
+ onConfirm = ( ) => {
15
14
void undefined ;
16
15
} ,
17
- onOpenChange ,
16
+ onCancel ,
18
17
open,
19
18
primaryActionLabel = "Confirm" ,
20
19
secondaryActionLabel = "Cancel" ,
@@ -25,10 +24,10 @@ describe("Dialog Component", () => {
25
24
< ConfirmationDialog
26
25
title = { title }
27
26
message = { message }
28
- onPrimaryActionClick = { onPrimaryActionClick }
27
+ onConfirm = { onConfirm }
29
28
primaryActionLabel = { primaryActionLabel }
30
29
secondaryActionLabel = { secondaryActionLabel }
31
- onOpenChange = { onOpenChange }
30
+ onCancel = { onCancel }
32
31
open = { open }
33
32
children = { children }
34
33
/>
@@ -65,40 +64,34 @@ describe("Dialog Component", () => {
65
64
it ( "closes the dialog on primary action click" , ( ) => {
66
65
const title = "Dialog Title" ;
67
66
const primaryActionLabel = "PrimaryAction" ;
68
- const triggerLabel = "Open Dialog" ;
69
- const children = < Button label = { triggerLabel } /> ;
70
67
68
+ let open = true ;
71
69
const { getByText, queryAllByText } = renderDialog ( {
72
70
title,
73
71
primaryActionLabel,
74
- children,
72
+ open,
73
+ onCancel : ( ) => open = false
75
74
} ) ;
76
75
77
- const triggerBtn = getByText ( triggerLabel ) ;
78
- fireEvent . click ( triggerBtn ) ;
79
76
expect ( queryAllByText ( title ) . length ) . toEqual ( 1 ) ;
80
77
const primaryBtn = getByText ( primaryActionLabel ) ;
81
78
fireEvent . click ( primaryBtn ) ;
82
- expect ( queryAllByText ( title ) . length ) . toEqual ( 0 ) ;
79
+ expect ( open ) . toEqual ( false ) ;
83
80
} ) ;
84
81
85
82
it ( "executes the primary action on primary action click" , ( ) => {
86
83
let counter = 0 ;
87
84
const title = "Dialog Title" ;
88
85
const primaryActionLabel = "PrimaryAction" ;
89
- const triggerLabel = "Open Dialog" ;
90
- const onPrimaryActionClick = ( ) => counter ++ ;
91
- const children = < Button label = { triggerLabel } /> ;
86
+ const onConfirm = ( ) => counter ++ ;
92
87
93
88
const { getByText } = renderDialog ( {
94
89
title,
95
90
primaryActionLabel,
96
- onPrimaryActionClick ,
97
- children ,
91
+ onConfirm ,
92
+ open : true ,
98
93
} ) ;
99
94
100
- const triggerBtn = getByText ( triggerLabel ) ;
101
- fireEvent . click ( triggerBtn ) ;
102
95
const primaryBtn = getByText ( primaryActionLabel ) ;
103
96
fireEvent . click ( primaryBtn ) ;
104
97
expect ( counter ) . toEqual ( 1 ) ;
@@ -114,20 +107,25 @@ describe("Dialog Component", () => {
114
107
it ( "closes the dialog on secondary action click" , ( ) => {
115
108
const title = "Dialog Title" ;
116
109
const secondaryActionLabel = "SecondaryAction" ;
117
- const triggerLabel = "Open Dialog" ;
118
- const children = < Button label = { triggerLabel } /> ;
119
110
111
+ let open = true ;
120
112
const { getByText, queryAllByText } = renderDialog ( {
121
113
title,
122
114
secondaryActionLabel,
123
- children,
115
+ open,
116
+ onCancel : ( ) => open = false
124
117
} ) ;
125
118
126
- const triggerBtn = getByText ( triggerLabel ) ;
127
- fireEvent . click ( triggerBtn ) ;
128
119
expect ( queryAllByText ( title ) . length ) . toEqual ( 1 ) ;
129
120
const secondaryActionBtn = getByText ( secondaryActionLabel ) ;
130
121
fireEvent . click ( secondaryActionBtn ) ;
131
- expect ( queryAllByText ( title ) . length ) . toEqual ( 0 ) ;
122
+ expect ( open ) . toEqual ( false ) ;
123
+ } ) ;
124
+
125
+ it ( "fails to render in case you provide both children and message props" , ( ) => {
126
+ const children = < div > test</ div > ;
127
+ const message = "this is a test message" ;
128
+
129
+ expect ( ( ) => renderDialog ( { children, message, open : true } ) ) . toThrowError ( "You can't pass children and message props at the same time" ) ;
132
130
} ) ;
133
131
} ) ;
0 commit comments