Skip to content

Replace injectIntl with the useIntl() hook #798

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions src/components/FilterBar.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useCallback, useMemo, useState } from 'react';
import { useCallback, useMemo, useState } from 'react';
import PropTypes from 'prop-types';

import {
Expand All @@ -8,7 +8,7 @@ import { Tune } from '@openedx/paragon/icons';
import { capitalize, toString } from 'lodash';
import { useSelector } from 'react-redux';

import { injectIntl, intlShape } from '@edx/frontend-platform/i18n';
import { useIntl } from '@edx/frontend-platform/i18n';

import {
PostsStatusFilter, RequestStatus,
Expand All @@ -19,12 +19,12 @@ import messages from '../discussions/posts/post-filter-bar/messages';
import { ActionItem } from '../discussions/posts/post-filter-bar/PostFilterBar';

const FilterBar = ({
intl,
filters,
selectedFilters,
onFilterChange,
showCohortsFilter,
}) => {
const intl = useIntl();
const [isOpen, setOpen] = useState(false);
const cohorts = useSelector(selectCourseCohorts);
const { status } = useSelector(state => state.cohorts);
Expand Down Expand Up @@ -192,7 +192,6 @@ const FilterBar = ({
};

FilterBar.propTypes = {
intl: intlShape.isRequired,
filters: PropTypes.arrayOf(PropTypes.shape({
name: PropTypes.string,
filters: PropTypes.arrayOf(PropTypes.string),
Expand All @@ -211,4 +210,4 @@ FilterBar.defaultProps = {
showCohortsFilter: false,
};

export default injectIntl(FilterBar);
export default FilterBar;
26 changes: 12 additions & 14 deletions src/components/Head/Head.jsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
import React from 'react';

import { Helmet } from 'react-helmet';

import { getConfig } from '@edx/frontend-platform';
import { injectIntl, intlShape } from '@edx/frontend-platform/i18n';
import { useIntl } from '@edx/frontend-platform/i18n';

import messages from './messages';

const Head = ({ intl }) => (
<Helmet>
<title>
{intl.formatMessage(messages['discussions.page.title'], { siteName: getConfig().SITE_NAME })}
</title>
<link rel="shortcut icon" href={getConfig().FAVICON_URL} type="image/x-icon" />
</Helmet>
);
const Head = () => {
const intl = useIntl();

Head.propTypes = {
intl: intlShape.isRequired,
return (
<Helmet>
<title>
{intl.formatMessage(messages['discussions.page.title'], { siteName: getConfig().SITE_NAME })}
</title>
<link rel="shortcut icon" href={getConfig().FAVICON_URL} type="image/x-icon" />
</Helmet>
);
};

export default injectIntl(Head);
export default Head;
19 changes: 19 additions & 0 deletions src/discussions/common/ActionsDropdown.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,25 @@ describe('ActionsDropdown', () => {
await waitFor(() => expect(screen.queryByText('Copy link')).not.toBeInTheDocument());
});

it('should close the dropdown when pressing escape', async () => {
const discussionObject = buildTestContent({ editable_fields: ['copy_link'] }).discussion;
await mockThreadAndComment(discussionObject);
renderComponent({ ...camelCaseObject(discussionObject) });

const openButton = await findOpenActionsDropdownButton();
await act(async () => {
fireEvent.click(openButton);
});

await waitFor(() => expect(screen.getByRole('button', { name: 'Copy link' })).toBeInTheDocument());

await act(async () => {
fireEvent.keyDown(document.body, { key: 'Escape', code: 'Escape' });
});

await waitFor(() => expect(screen.queryByRole('button', { name: 'Copy link' })).toBeNull());
});

describe.each(canPerformActionTestData)('Actions', ({
testFor, action, label, ...commentOrPost
}) => {
Expand Down
11 changes: 6 additions & 5 deletions src/discussions/in-context-topics/components/BackButton.jsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import React from 'react';
import PropTypes from 'prop-types';

import { Icon, IconButton, Spinner } from '@openedx/paragon';
import { ArrowBack } from '@openedx/paragon/icons';
import { useNavigate } from 'react-router-dom';

import { injectIntl, intlShape } from '@edx/frontend-platform/i18n';
import { useIntl } from '@edx/frontend-platform/i18n';

import messages from '../messages';

const BackButton = ({
intl, path, title, loading,
path,
title,
loading,
}) => {
const intl = useIntl();
const navigate = useNavigate();

return (
Expand All @@ -35,7 +37,6 @@ const BackButton = ({
};

BackButton.propTypes = {
intl: intlShape.isRequired,
path: PropTypes.shape({}).isRequired,
title: PropTypes.string.isRequired,
loading: PropTypes.bool,
Expand All @@ -45,4 +46,4 @@ BackButton.defaultProps = {
loading: false,
};

export default injectIntl(BackButton);
export default BackButton;
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import React from 'react';

import { SearchField } from '@openedx/paragon';
import { useDispatch } from 'react-redux';

import { injectIntl, intlShape } from '@edx/frontend-platform/i18n';
import { useIntl } from '@edx/frontend-platform/i18n';

import { setFilter } from '../data';
import messages from '../messages';

const TopicSearchResultBar = ({ intl }) => {
const TopicSearchResultBar = () => {
const intl = useIntl();
const dispatch = useDispatch();

return (
Expand All @@ -23,8 +22,4 @@ const TopicSearchResultBar = ({ intl }) => {
);
};

TopicSearchResultBar.propTypes = {
intl: intlShape.isRequired,
};

export default injectIntl(TopicSearchResultBar);
export default TopicSearchResultBar;
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { fireEvent, render } from '@testing-library/react';
Copy link
Author

@diana-villalvazo-wgu diana-villalvazo-wgu Aug 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added tests because codecov was failing due to coverage on this file, but i inspected the repo and TopicSearchResultBar.jsx component is never used, should we remove it? @openedx/2u-infinity

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found a reference for TopicSearchBar, but not TopicSearchResultBar.

import { TopicSearchBar as IncontextSearch } from '../../in-context-topics/topic-search';

import { useDispatch } from 'react-redux';

import { IntlProvider } from '@edx/frontend-platform/i18n';

import { setFilter } from '../data';
import messages from '../messages';
import TopicSearchResultBar from './TopicSearchResultBar';

jest.mock('react-redux', () => ({
useDispatch: jest.fn(),
}));

jest.mock('../data', () => ({
setFilter: jest.fn(),
}));

describe('TopicSearchResultBar', () => {
const dispatch = jest.fn();

beforeEach(() => {
useDispatch.mockReturnValue(dispatch);
setFilter.mockReturnValue({ type: 'SET_FILTER' });
});

afterEach(() => {
jest.clearAllMocks();
});

it('should render search field', () => {
const { getByPlaceholderText } = render(
<IntlProvider locale="en">
<TopicSearchResultBar />
</IntlProvider>,
);
expect(getByPlaceholderText(messages.searchTopics.defaultMessage)).toBeInTheDocument();
});

it('should dispatch setFilter on submit', () => {
const { getByPlaceholderText } = render(
<IntlProvider locale="en">
<TopicSearchResultBar />
</IntlProvider>,
);
const searchField = getByPlaceholderText(messages.searchTopics.defaultMessage);
fireEvent.change(searchField, { target: { value: 'test query' } });
fireEvent.submit(searchField);

expect(setFilter).toHaveBeenCalledWith('test query');
expect(dispatch).toHaveBeenCalled();
});

it('should dispatch setFilter on change', () => {
const { getByPlaceholderText } = render(
<IntlProvider locale="en">
<TopicSearchResultBar />
</IntlProvider>,
);
const searchField = getByPlaceholderText(messages.searchTopics.defaultMessage);
fireEvent.change(searchField, { target: { value: 'test' } });

expect(setFilter).toHaveBeenCalledWith('test');
expect(dispatch).toHaveBeenCalled();
});
});