Skip to content

Commit 12b46df

Browse files
author
Konstantinos Familonidis
committed
Fixes #38822: update pf3 buttons to pf5
Upgrade ActionButtons. Replace Enzyme tests with React Testing Library Fix style. Upgrade ForemanForm ActionsButtons, DeleteButton, DiffModal ForemanModalFooter, ExportButton, SubmitOrCancel, SubmitBtn, CancelBtn PersonalAccessToken
1 parent 5d6a82c commit 12b46df

File tree

27 files changed

+356
-477
lines changed

27 files changed

+356
-477
lines changed

webpack/assets/javascripts/react_app/components/ConfigReports/DiffModal/DiffModal.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React from 'react';
2-
import { Modal, Icon, Button } from 'patternfly-react';
2+
import { Modal, Icon } from 'patternfly-react';
3+
import { Button } from '@patternfly/react-core';
34
import PropTypes from 'prop-types';
45

56
import { noop } from '../../../common/helpers';
@@ -22,9 +23,10 @@ const DiffModal = ({
2223
<Modal.Header>
2324
<h4 id="diff-modal-h4">{title}</h4>
2425
<Button
26+
ouiaId="diff-modal-close-button"
2527
className="close diff-modal-close"
2628
onClick={toggleModal}
27-
bsStyle="link"
29+
variant="link"
2830
>
2931
<Icon type="pf" name="close" />
3032
</Button>

webpack/assets/javascripts/react_app/components/ConfigReports/DiffModal/__tests__/DiffModal.test.js

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,41 @@
11
import React from 'react';
2-
import { mount } from 'enzyme';
2+
import { screen, fireEvent, render, act } from '@testing-library/react';
3+
import { configure } from '@testing-library/react';
4+
import '@testing-library/jest-dom/extend-expect';
35

4-
import { testComponentSnapshotsWithFixtures } from '../../../../common/testHelpers';
56
import DiffModal from '../DiffModal';
67
import { diffModalMock } from '../DiffModal.fixtures';
78

9+
const toggleModal = jest.fn();
10+
const changeState = jest.fn();
811
const fixtures = {
9-
'renders diffModal': diffModalMock,
12+
renders: {
13+
...diffModalMock,
14+
toggleModal: toggleModal,
15+
changeViewType: changeState,
16+
},
1017
};
18+
configure({ testIdAttribute: 'data-ouia-component-id'})
1119

1220
describe('DiffModal', () => {
13-
describe('rendering...', () =>
14-
testComponentSnapshotsWithFixtures(DiffModal, fixtures));
21+
describe('rendering', () => {
22+
it('should render modal with title and close button', () => {
23+
render(<DiffModal {...fixtures.renders} />);
24+
25+
expect(screen.getByText('log1')).toBeInTheDocument();
26+
const closeButton = document.querySelector('.diff-modal-close');
27+
expect(closeButton).toBeInTheDocument();
28+
expect(closeButton).toHaveClass('close', 'diff-modal-close');
29+
});
30+
});
1531

1632
describe('triggering..', () => {
17-
it('should trigger onHide', () => {
18-
const toggleModal = jest.fn();
19-
const changeState = jest.fn();
20-
const component = mount(
21-
<DiffModal
22-
{...diffModalMock}
23-
toggleModal={toggleModal}
24-
changeViewType={changeState}
25-
/>
26-
);
27-
component
28-
.find('.close')
29-
.at(0)
30-
.simulate('click');
33+
it('should trigger onHide', async () => {
34+
render(<DiffModal {...fixtures.renders} />);
35+
const closeButton = screen.getByTestId('diff-modal-close-button');
36+
37+
await act(async () => await fireEvent.click(closeButton));
38+
3139
expect(toggleModal).toHaveBeenCalled();
3240
});
3341
});

webpack/assets/javascripts/react_app/components/ConfigReports/DiffModal/__tests__/__snapshots__/DiffModal.test.js.snap

Lines changed: 0 additions & 86 deletions
This file was deleted.

webpack/assets/javascripts/react_app/components/ForemanModal/subcomponents/ForemanModalFooter.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React from 'react';
22
import PropTypes from 'prop-types';
3-
import { Modal, Button } from 'patternfly-react';
3+
import { Modal } from 'patternfly-react';
4+
import { Button } from '@patternfly/react-core';
45
import { useModalContext } from '../ForemanModalHooks';
56
import { translate as __ } from '../../../common/I18n';
67

@@ -12,7 +13,7 @@ const ForemanModalFooter = props => {
1213

1314
// Render the provided children, or default markup if none given
1415
const closeButton = childCount === 0 && (
15-
<Button bsStyle="default" onClick={onClose}>
16+
<Button ouiaId="close-modal-button" variant="secondary" onClick={onClose}>
1617
{__('Close')}
1718
</Button>
1819
);

webpack/assets/javascripts/react_app/components/ForemanModal/subcomponents/SubmitOrCancel/CancelBtn.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,31 @@
11
import React from 'react';
2-
import { Button } from 'patternfly-react';
2+
import { Button } from '@patternfly/react-core';
33
import PropTypes from 'prop-types';
44

55
import { translate as __ } from '../../../../common/I18n';
66

7-
const CancelBtn = ({ onCancel, disabled, bsStyle, btnText }) => (
8-
<Button bsStyle={bsStyle} onClick={onCancel} disabled={disabled}>
7+
const CancelBtn = ({ onCancel, disabled, variant, btnText }) => (
8+
<Button
9+
ouiaId="cancel-button"
10+
variant={variant}
11+
onClick={onCancel}
12+
isDisabled={disabled}
13+
isDanger
14+
>
915
{btnText}
1016
</Button>
1117
);
1218

1319
CancelBtn.propTypes = {
1420
onCancel: PropTypes.func.isRequired,
1521
disabled: PropTypes.bool,
16-
bsStyle: PropTypes.string,
22+
variant: PropTypes.oneOf(['secondary', 'danger']),
1723
btnText: PropTypes.string,
1824
};
1925

2026
CancelBtn.defaultProps = {
2127
disabled: false,
22-
bsStyle: 'default',
28+
variant: 'secondary',
2329
btnText: __('Cancel'),
2430
};
2531

webpack/assets/javascripts/react_app/components/ForemanModal/subcomponents/SubmitOrCancel/SubmitBtn.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
import React from 'react';
2-
import { Button } from 'patternfly-react';
2+
import { Button } from '@patternfly/react-core';
33
import PropTypes from 'prop-types';
44
import { translate as __ } from '../../../../common/I18n';
55
import { simpleLoader } from '../../../common/Loader';
66

7-
const SubmitBtn = ({ isSubmitting, onSubmit, bsStyle, btnText }) => (
8-
<Button bsStyle={bsStyle} disabled={isSubmitting} onClick={onSubmit}>
7+
const SubmitBtn = ({ isSubmitting, onSubmit, variant, btnText }) => (
8+
<Button
9+
ouiaId="submit-button"
10+
variant={variant}
11+
isDisabled={isSubmitting}
12+
onClick={onSubmit}
13+
>
914
&nbsp;
1015
{btnText}
1116
&nbsp;
@@ -16,12 +21,12 @@ const SubmitBtn = ({ isSubmitting, onSubmit, bsStyle, btnText }) => (
1621
SubmitBtn.propTypes = {
1722
isSubmitting: PropTypes.bool.isRequired,
1823
onSubmit: PropTypes.func.isRequired,
19-
bsStyle: PropTypes.string,
24+
variant: PropTypes.oneOf(['primary']),
2025
btnText: PropTypes.string,
2126
};
2227

2328
SubmitBtn.defaultProps = {
24-
bsStyle: 'primary',
29+
variant: 'primary',
2530
btnText: __('Submit'),
2631
};
2732

Lines changed: 55 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,75 @@
1-
import { testComponentSnapshotsWithFixtures } from 'foremanReact/common/testHelpers';
1+
import React from 'react';
2+
import { screen, fireEvent, render, act } from '@testing-library/react';
3+
import '@testing-library/jest-dom/extend-expect';
24

35
import SubmitOrCancel from './SubmitOrCancel';
6+
import SubmitBtn from './SubmitBtn';
7+
import CancelBtn from './CancelBtn';
48

5-
const handlers = {
9+
// Helper functions for creating props
10+
const createHandlers = () => ({
611
submitProps: {
712
submitBtnProps: {
8-
bsStyle: 'default',
13+
variant: 'primary',
914
btnText: 'Confirm',
1015
},
1116
cancelBtnProps: {
12-
bsStyle: 'danger',
17+
variant: 'danger',
1318
btnText: 'Deny',
1419
},
1520
},
1621
onCancel: jest.fn(),
1722
onSubmit: jest.fn(),
1823
id: 'test-modal',
19-
};
24+
});
25+
26+
const createBaseProps = (isSubmitting = false) => ({
27+
isSubmitting,
28+
...createHandlers(),
29+
});
2030

2131
const fixtures = {
22-
'should render': {
23-
isSubmitting: false,
24-
...handlers,
25-
},
26-
'should render when isSubmitting': {
27-
isSubmitting: true,
28-
...handlers,
29-
},
32+
render: createBaseProps(false),
33+
whenSubmitting: createBaseProps(true),
3034
};
3135

36+
const createSubmitBtnProps = (isSubmitting = false) => ({
37+
...fixtures.render.submitProps.submitBtnProps,
38+
...createHandlers(),
39+
isSubmitting,
40+
});
41+
42+
const createCancelBtnProps = (isSubmitting = false) => ({
43+
...fixtures.render.submitProps.cancelBtnProps,
44+
...createHandlers(),
45+
isSubmitting,
46+
});
47+
3248
describe('SubmitOrCancel', () => {
33-
testComponentSnapshotsWithFixtures(SubmitOrCancel, fixtures);
49+
it('renders', () => {
50+
render(<SubmitOrCancel {...fixtures.render} />);
51+
expect(screen.getByRole('button', { name: /submit/i })).toBeInTheDocument();
52+
expect(screen.getByRole('button', { name: /cancel/i })).toBeInTheDocument();
53+
});
54+
55+
it('renders when submitting', () => {
56+
render(<SubmitOrCancel {...fixtures.whenSubmitting} />);
57+
58+
expect(screen.getByRole('button', { name: /submit/i })).toBeDisabled();
59+
expect(screen.getByRole('button', { name: /cancel/i })).toBeDisabled();
60+
});
61+
62+
describe('SubmitBtn', () => {
63+
it('renders', () => {
64+
render(<SubmitBtn {...createSubmitBtnProps()} />);
65+
expect(screen.getByRole('button', { name: /Confirm/i })).toBeInTheDocument();
66+
});
67+
});
68+
69+
describe('CancelBtn', () => {
70+
it('renders', () => {
71+
render(<CancelBtn {...createCancelBtnProps()} />);
72+
expect(screen.getByRole('button', { name: /Deny/i })).toBeInTheDocument();
73+
});
74+
});
3475
});

webpack/assets/javascripts/react_app/components/ForemanModal/subcomponents/SubmitOrCancel/__snapshots__/SubmitOrCancel.test.js.snap

Lines changed: 0 additions & 35 deletions
This file was deleted.

0 commit comments

Comments
 (0)