1
- import { createPassThroughPropsTest } from '@shared/tests/utils' ;
2
1
import { fireEvent , render , screen } from '@shared/tests' ;
2
+ import { createPassThroughPropsTest } from '@shared/tests/utils' ;
3
+ import '@ui5/webcomponents-icons/dist/icons/add' ;
4
+ import { Icon } from '@ui5/webcomponents-react/lib/Icon' ;
3
5
import { MessageBox } from '@ui5/webcomponents-react/lib/MessageBox' ;
4
6
import { MessageBoxActions } from '@ui5/webcomponents-react/lib/MessageBoxActions' ;
5
7
import { MessageBoxTypes } from '@ui5/webcomponents-react/lib/MessageBoxTypes' ;
6
8
import React from 'react' ;
7
9
8
10
describe ( 'MessageBox' , ( ) => {
9
- test ( 'Confirm - OK' , ( ) => {
11
+ test . each ( [
12
+ [ MessageBoxTypes . CONFIRM , MessageBoxActions . OK ] ,
13
+ [ MessageBoxTypes . SUCCESS , MessageBoxActions . OK ] ,
14
+ [ MessageBoxTypes . WARNING , MessageBoxActions . OK ] ,
15
+ [ MessageBoxTypes . ERROR , MessageBoxActions . CLOSE ] ,
16
+ [ MessageBoxTypes . INFORMATION , MessageBoxActions . OK ] ,
17
+ [ MessageBoxTypes . HIGHLIGHT , MessageBoxActions . OK ]
18
+ ] ) ( '%s' , ( type , buttonText ) => {
10
19
const callback = jest . fn ( ) ;
11
- const { asFragment } = render (
12
- < MessageBox type = { MessageBoxTypes . CONFIRM } open onClose = { callback } >
13
- Confirm
20
+ const { asFragment, unmount } = render (
21
+ < MessageBox type = { type } open onClose = { callback } >
22
+ My Message Box Content
14
23
</ MessageBox >
15
24
) ;
16
25
17
26
expect ( asFragment ( ) ) . toMatchSnapshot ( ) ;
18
27
19
- fireEvent . click ( screen . getByText ( 'OK' ) ) ;
28
+ fireEvent . click ( screen . getByText ( buttonText ) ) ;
20
29
21
- expect ( callback . mock . calls [ 0 ] [ 0 ] . detail . action ) . toEqual ( MessageBoxActions . OK ) ;
30
+ expect ( callback . mock . calls [ 0 ] [ 0 ] . detail . action ) . toEqual ( buttonText ) ;
31
+ unmount ( ) ;
22
32
} ) ;
23
33
24
34
test ( 'Confirm - Cancel' , ( ) => {
@@ -35,58 +45,6 @@ describe('MessageBox', () => {
35
45
expect ( callback . mock . calls [ 0 ] [ 0 ] . detail . action ) . toEqual ( MessageBoxActions . CANCEL ) ;
36
46
} ) ;
37
47
38
- test ( 'Success' , ( ) => {
39
- const callback = jest . fn ( ) ;
40
- const { asFragment } = render (
41
- < MessageBox type = { MessageBoxTypes . SUCCESS } open onClose = { callback } >
42
- Success
43
- </ MessageBox >
44
- ) ;
45
- expect ( asFragment ( ) ) . toMatchSnapshot ( ) ;
46
-
47
- fireEvent . click ( screen . getByText ( 'OK' ) ) ;
48
- expect ( callback . mock . calls [ 0 ] [ 0 ] . detail . action ) . toEqual ( MessageBoxActions . OK ) ;
49
- } ) ;
50
-
51
- test ( 'Warning' , ( ) => {
52
- const callback = jest . fn ( ) ;
53
- const { asFragment } = render (
54
- < MessageBox type = { MessageBoxTypes . WARNING } open onClose = { callback } >
55
- Warning
56
- </ MessageBox >
57
- ) ;
58
- expect ( asFragment ( ) ) . toMatchSnapshot ( ) ;
59
-
60
- fireEvent . click ( screen . getByText ( 'OK' ) ) ;
61
- expect ( callback . mock . calls [ 0 ] [ 0 ] . detail . action ) . toEqual ( MessageBoxActions . OK ) ;
62
- } ) ;
63
-
64
- test ( 'Error' , ( ) => {
65
- const callback = jest . fn ( ) ;
66
- const { asFragment } = render (
67
- < MessageBox type = { MessageBoxTypes . ERROR } open onClose = { callback } >
68
- Error
69
- </ MessageBox >
70
- ) ;
71
- expect ( asFragment ( ) ) . toMatchSnapshot ( ) ;
72
-
73
- fireEvent . click ( screen . getByText ( 'Close' ) ) ;
74
- expect ( callback . mock . calls [ 0 ] [ 0 ] . detail . action ) . toEqual ( MessageBoxActions . CLOSE ) ;
75
- } ) ;
76
-
77
- test ( 'Information' , ( ) => {
78
- const callback = jest . fn ( ) ;
79
- const { asFragment } = render (
80
- < MessageBox type = { MessageBoxTypes . INFORMATION } open onClose = { callback } >
81
- Information
82
- </ MessageBox >
83
- ) ;
84
- expect ( asFragment ( ) ) . toMatchSnapshot ( ) ;
85
-
86
- fireEvent . click ( screen . getByText ( 'OK' ) ) ;
87
- expect ( callback . mock . calls [ 0 ] [ 0 ] . detail . action ) . toEqual ( MessageBoxActions . OK ) ;
88
- } ) ;
89
-
90
48
test ( 'Show' , ( ) => {
91
49
const callback = jest . fn ( ) ;
92
50
const { asFragment } = render (
@@ -107,7 +65,13 @@ describe('MessageBox', () => {
107
65
test ( 'Success w/ custom title' , ( ) => {
108
66
const callback = jest . fn ( ) ;
109
67
const { asFragment } = render (
110
- < MessageBox type = { MessageBoxTypes . SUCCESS } open onClose = { callback } title = "Custom Success" >
68
+ < MessageBox
69
+ type = { MessageBoxTypes . SUCCESS }
70
+ open
71
+ onClose = { callback }
72
+ title = "Custom Success"
73
+ icon = { < Icon name = "add" /> }
74
+ >
111
75
Custom Success
112
76
</ MessageBox >
113
77
) ;
@@ -162,5 +126,16 @@ describe('MessageBox', () => {
162
126
expect ( onClose . mock . calls [ 1 ] [ 0 ] . detail . action ) . toEqual ( 'My Custom Action' ) ;
163
127
} ) ;
164
128
129
+ test ( "Don't crash on unknown type" , ( ) => {
130
+ const callback = jest . fn ( ) ;
131
+ const { asFragment } = render (
132
+ < MessageBox open onClose = { callback } type = "FOO_BAR" >
133
+ Unknown Type!
134
+ </ MessageBox >
135
+ ) ;
136
+
137
+ expect ( asFragment ( ) ) . toMatchSnapshot ( ) ;
138
+ } ) ;
139
+
165
140
createPassThroughPropsTest ( MessageBox ) ;
166
141
} ) ;
0 commit comments