Skip to content

Commit 715887a

Browse files
author
Vincent Ardouin-Murat
committed
[PROD-11431] feat: add custom props on SimpleTwoActionsDialog
1 parent 58974b8 commit 715887a

File tree

4 files changed

+193
-6
lines changed

4 files changed

+193
-6
lines changed

src/dialogs/SimpleTwoActionsDialog/SimpleTwoActionsDialog.js

Lines changed: 58 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,53 @@ import React from 'react';
55
import PropTypes from 'prop-types';
66
import { Typography, Button, Dialog, DialogTitle, DialogContent, DialogActions } from '@mui/material';
77

8-
export const SimpleTwoActionsDialog = ({ id, open, labels, handleClickOnButton1, handleClickOnButton2 }) => {
8+
export const SimpleTwoActionsDialog = ({
9+
id,
10+
open,
11+
labels,
12+
handleClickOnButton1,
13+
handleClickOnButton2,
14+
dialogProps,
15+
button1Props,
16+
button2Props,
17+
closeOnBackdropClick,
18+
}) => {
919
const onClose = (event, reason) => {
10-
if (reason !== 'backdropClick') {
20+
if (closeOnBackdropClick || reason !== 'backdropClick') {
1121
handleClickOnButton1();
1222
}
1323
};
1424

1525
return (
16-
<Dialog open={open} aria-labelledby={id + '-dialog-title'} maxWidth={'xs'} fullWidth={true} onClose={onClose}>
26+
<Dialog
27+
open={open}
28+
aria-labelledby={id + '-dialog-title'}
29+
maxWidth={'xs'}
30+
fullWidth={true}
31+
onClose={onClose}
32+
{...dialogProps}
33+
>
1734
<DialogTitle id={id + '-dialog-title'}>{labels.title}</DialogTitle>
1835
<DialogContent>
1936
<Typography variant="body1">{labels.body}</Typography>
2037
</DialogContent>
2138
<DialogActions>
22-
{/* TODO Use button1Props and button2Props to enable variants and colors */}
23-
<Button data-cy={id + '-button1'} id={id + 'id-button1'} onClick={handleClickOnButton1} color="primary">
39+
<Button
40+
data-cy={id + '-button1'}
41+
id={id + 'id-button1'}
42+
onClick={handleClickOnButton1}
43+
color="primary"
44+
{...button1Props}
45+
>
2446
{labels.button1}
2547
</Button>
26-
<Button data-cy={id + '-button2'} id={id + 'id-button2'} onClick={handleClickOnButton2} color="primary">
48+
<Button
49+
data-cy={id + '-button2'}
50+
id={id + 'id-button2'}
51+
onClick={handleClickOnButton2}
52+
color="primary"
53+
{...button2Props}
54+
>
2755
{labels.button2}
2856
</Button>
2957
</DialogActions>
@@ -69,6 +97,26 @@ SimpleTwoActionsDialog.propTypes = {
6997
* ## Function used when button2 is clicked
7098
*/
7199
handleClickOnButton2: PropTypes.func.isRequired,
100+
101+
/**
102+
* Properties to add or override to the dialog component
103+
*/
104+
dialogProps: PropTypes.object,
105+
106+
/**
107+
* Properties to add or override to the button1 component
108+
*/
109+
button1Props: PropTypes.object,
110+
111+
/**
112+
* Properties to add or override to the button2 component
113+
*/
114+
button2Props: PropTypes.object,
115+
116+
/**
117+
* Close dialog on backdrop click
118+
*/
119+
closeOnBackdropClick: PropTypes.bool,
72120
};
73121

74122
SimpleTwoActionsDialog.defaultProps = {
@@ -87,4 +135,8 @@ SimpleTwoActionsDialog.defaultProps = {
87135
button1: 'Cancel',
88136
button2: 'Validate',
89137
},
138+
dialogProps: {},
139+
button1Props: {},
140+
button2Props: {},
141+
closeOnBackdropClick: false,
90142
};
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
// Copyright (c) Cosmo Tech.
2+
// Licensed under the MIT license.
3+
4+
import React from 'react';
5+
import { SimpleTwoActionsDialog } from './SimpleTwoActionsDialog';
6+
import { renderInMuiThemeProvider } from '../../../tests/utils';
7+
import { ButtonTesting, DialogTesting } from '../../../tests/MuiComponentsTesting';
8+
9+
import userEvent from '@testing-library/user-event';
10+
11+
const mockOnButton1Click = jest.fn();
12+
const mockOnButton2Click = jest.fn();
13+
14+
const dialogId = 'dialog-test';
15+
16+
const TwoActionsDialog = new DialogTesting({ labelledby: `${dialogId}-dialog-title` });
17+
const Button1 = new ButtonTesting({ dataCy: `${dialogId}-button1` });
18+
const Button2 = new ButtonTesting({ dataCy: `${dialogId}-button2` });
19+
20+
const defaultProps = {
21+
id: dialogId,
22+
open: true,
23+
handleClickOnButton1: mockOnButton1Click,
24+
handleClickOnButton2: mockOnButton2Click,
25+
};
26+
27+
const setUp = (props) => {
28+
renderInMuiThemeProvider(<SimpleTwoActionsDialog {...props} />);
29+
};
30+
31+
describe('SimpleTwoActionsDialog', () => {
32+
beforeEach(() => {
33+
mockOnButton1Click.mockClear();
34+
mockOnButton2Click.mockClear();
35+
});
36+
37+
describe('On Close', () => {
38+
test('ESC key calls button1 Click', async () => {
39+
setUp(defaultProps);
40+
await userEvent.keyboard('{Escape}');
41+
expect(mockOnButton1Click).toHaveBeenCalled();
42+
});
43+
44+
test(`By default, click on backdrop doesn't call button1 click`, async () => {
45+
setUp(defaultProps);
46+
await userEvent.click(TwoActionsDialog.Backdrop);
47+
expect(mockOnButton1Click).not.toHaveBeenCalled();
48+
});
49+
50+
test('with closeOnBackdropClick, click on backdrop calls button1 click', async () => {
51+
const props = {
52+
...defaultProps,
53+
closeOnBackdropClick: true,
54+
};
55+
56+
setUp(props);
57+
await userEvent.click(TwoActionsDialog.Backdrop);
58+
expect(mockOnButton1Click).toHaveBeenCalled();
59+
});
60+
});
61+
62+
describe('Custom props', () => {
63+
describe('buttonsProps', () => {
64+
test(`by default, buttons haven't error color`, () => {
65+
setUp(defaultProps);
66+
expect(Button1.Button).not.toHaveClass('MuiButton-textError');
67+
expect(Button2.Button).not.toHaveClass('MuiButton-textError');
68+
});
69+
70+
test('force error color on button1', () => {
71+
const props = {
72+
...defaultProps,
73+
button1Props: {
74+
color: 'error',
75+
},
76+
};
77+
78+
setUp(props);
79+
expect(Button1.Button).toHaveClass('MuiButton-textError');
80+
});
81+
82+
test('force error color on button2', () => {
83+
const props = {
84+
...defaultProps,
85+
button2Props: {
86+
color: 'error',
87+
},
88+
};
89+
90+
setUp(props);
91+
expect(Button2.Button).toHaveClass('MuiButton-textError');
92+
});
93+
});
94+
95+
describe('dialogProps', () => {
96+
test('by default dialog has xs size', () => {
97+
setUp(defaultProps);
98+
expect(TwoActionsDialog.Dialog).toHaveClass('MuiDialog-paperWidthXs');
99+
});
100+
101+
test('force sm size on dialog', () => {
102+
const props = {
103+
...defaultProps,
104+
dialogProps: {
105+
maxWidth: 'sm',
106+
},
107+
};
108+
109+
setUp(props);
110+
expect(TwoActionsDialog.Dialog).toHaveClass('MuiDialog-paperWidthSm');
111+
});
112+
});
113+
});
114+
});
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright (c) Cosmo Tech.
2+
// Licensed under the MIT license.
3+
4+
export class DialogTesting {
5+
constructor({ labelledby }) {
6+
this._labelledby = labelledby;
7+
}
8+
9+
get Dialog() {
10+
return document.querySelector(`[aria-labelledby="${this._labelledby}"]`);
11+
}
12+
13+
get DialogRoot() {
14+
return this.Dialog.parentNode.parentNode;
15+
}
16+
17+
get Backdrop() {
18+
return this.DialogRoot.querySelector('.MuiModal-backdrop');
19+
}
20+
}

tests/MuiComponentsTesting/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ export { SelectTesting } from './SelectTesting';
1010
export { SliderTesting } from './SliderTesting';
1111
export { StackContainerTesting } from './StackContainerTesting';
1212
export { default as MockTheme } from './MockTheme';
13+
export { DialogTesting } from './DialogTesting';

0 commit comments

Comments
 (0)