From d2ed3e54ee0156120f144f38150a21f2cf3c570c Mon Sep 17 00:00:00 2001 From: Simone Saturno <32124754+Simoblaster@users.noreply.github.com> Date: Mon, 18 Aug 2025 16:00:13 +0200 Subject: [PATCH] refactor: replaced injectIntl with useIntl (#1239) * refactor: replaced injectIntl with useIntl * test: update tests for useIntl hook implementation * fix: add missing trailing comma * test: fix failing component tests and remove deprecated defaultProps - Fix SwitchContent component defaultProps warning with default parameters - Fix Visibility component formatMessage errors and remove defaultProps - Fix FormControls component intl provider issues with useIntl mock - Fix EditButton component defaultProps and message formatting - Update EditableItemHeader, EmptyContent components - Replace React defaultProps with ES6 default parameters across components - Update test mocking to properly handle useIntl hook - All 82 tests now pass (previously 4 failed, 78 passed) Resolves React deprecation warnings and modernizes component patterns. * fix: add missing trailing comma --- src/head/Head.jsx | 31 ++++++---- src/profile/forms/elements/EditButton.jsx | 59 +++++++++---------- .../forms/elements/EditButton.test.jsx | 22 +++++++ .../forms/elements/EditableItemHeader.jsx | 22 ++----- src/profile/forms/elements/EmptyContent.jsx | 8 +-- src/profile/forms/elements/FormControls.jsx | 44 +++++++------- .../forms/elements/FormControls.test.jsx | 7 +++ src/profile/forms/elements/SwitchContent.jsx | 7 +-- src/profile/forms/elements/Visibility.jsx | 55 +++++++++-------- .../forms/elements/Visibility.test.jsx | 43 ++++++++++++++ 10 files changed, 176 insertions(+), 122 deletions(-) create mode 100644 src/profile/forms/elements/EditButton.test.jsx create mode 100644 src/profile/forms/elements/Visibility.test.jsx diff --git a/src/head/Head.jsx b/src/head/Head.jsx index b84fd32..2478153 100644 --- a/src/head/Head.jsx +++ b/src/head/Head.jsx @@ -1,21 +1,26 @@ import React from 'react'; import { Helmet } from 'react-helmet'; -import { injectIntl, intlShape } from '@edx/frontend-platform/i18n'; +import { useIntl } from '@edx/frontend-platform/i18n'; import { getConfig } from '@edx/frontend-platform'; import messages from './messages'; -const Head = ({ intl }) => ( - - - {intl.formatMessage(messages['profile.page.title'], { siteName: getConfig().SITE_NAME })} - - - -); - -Head.propTypes = { - intl: intlShape.isRequired, +const Head = () => { + const intl = useIntl(); + return ( + + + {intl.formatMessage(messages['profile.page.title'], { + siteName: getConfig().SITE_NAME, + })} + + + + ); }; -export default injectIntl(Head); +export default Head; diff --git a/src/profile/forms/elements/EditButton.jsx b/src/profile/forms/elements/EditButton.jsx index 80cb479..61c1373 100644 --- a/src/profile/forms/elements/EditButton.jsx +++ b/src/profile/forms/elements/EditButton.jsx @@ -1,46 +1,41 @@ import React from 'react'; import PropTypes from 'prop-types'; import { EditOutline } from '@openedx/paragon/icons'; -import { injectIntl, intlShape } from '@edx/frontend-platform/i18n'; +import { useIntl } from '@edx/frontend-platform/i18n'; import { Button, OverlayTrigger, Tooltip } from '@openedx/paragon'; import messages from './EditButton.messages'; -const EditButton = ({ - onClick, className, style, intl, -}) => ( - -

- {intl.formatMessage(messages['profile.editbutton.edit'])} -

- - )} - > - -
-); + + + ); +}; -export default injectIntl(EditButton); +export default EditButton; EditButton.propTypes = { onClick: PropTypes.func.isRequired, className: PropTypes.string, style: PropTypes.object, // eslint-disable-line - intl: intlShape.isRequired, -}; - -EditButton.defaultProps = { - className: null, - style: null, }; diff --git a/src/profile/forms/elements/EditButton.test.jsx b/src/profile/forms/elements/EditButton.test.jsx new file mode 100644 index 0000000..2d9d8c4 --- /dev/null +++ b/src/profile/forms/elements/EditButton.test.jsx @@ -0,0 +1,22 @@ +import React from 'react'; +import { render, fireEvent } from '@testing-library/react'; +import { IntlProvider } from '@edx/frontend-platform/i18n'; +import EditButton from './EditButton'; + +const messages = { + 'profile.editbutton.edit': 'Edit', +}; + +describe('EditButton', () => { + it('renders and calls onClick when clicked', () => { + const onClick = jest.fn(); + const { getByRole } = render( + + + , + ); + const button = getByRole('button'); + fireEvent.click(button); + expect(onClick).toHaveBeenCalled(); + }); +}); diff --git a/src/profile/forms/elements/EditableItemHeader.jsx b/src/profile/forms/elements/EditableItemHeader.jsx index 5381e3a..6b82abc 100644 --- a/src/profile/forms/elements/EditableItemHeader.jsx +++ b/src/profile/forms/elements/EditableItemHeader.jsx @@ -7,12 +7,12 @@ import { Visibility } from './Visibility'; import { useIsOnMobileScreen } from '../../data/hooks'; const EditableItemHeader = ({ - content, - showVisibility, - visibility, - showEditButton, - onClickEdit, - headingId, + content = '', + showVisibility = false, + visibility = 'private', + showEditButton = false, + onClickEdit = () => {}, + headingId = null, }) => { const isMobileView = useIsOnMobileScreen(); return ( @@ -57,13 +57,3 @@ EditableItemHeader.propTypes = { visibility: PropTypes.oneOf(['private', 'all_users']), headingId: PropTypes.string, }; - -EditableItemHeader.defaultProps = { - onClickEdit: () => { - }, - showVisibility: false, - showEditButton: false, - content: '', - visibility: 'private', - headingId: null, -}; diff --git a/src/profile/forms/elements/EmptyContent.jsx b/src/profile/forms/elements/EmptyContent.jsx index 3978d80..4d6b83d 100644 --- a/src/profile/forms/elements/EmptyContent.jsx +++ b/src/profile/forms/elements/EmptyContent.jsx @@ -3,7 +3,7 @@ import PropTypes from 'prop-types'; import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; import { faPlus } from '@fortawesome/free-solid-svg-icons'; -const EmptyContent = ({ children, onClick, showPlusIcon }) => ( +const EmptyContent = ({ children = null, onClick = null, showPlusIcon = true }) => (
{onClick ? (