From f7b56156600d718ca27caa36a797dec5dc1bacd1 Mon Sep 17 00:00:00 2001 From: bydawen Date: Tue, 10 Feb 2026 17:17:20 +0200 Subject: [PATCH] fix: matching styles for report/delete post buttons (#794) * fix: matching styles for report/delete post buttons * feat: add tests for the Post.jsx --- src/discussions/posts/post/Post.jsx | 2 +- src/discussions/posts/post/Post.test.jsx | 157 +++++++++++++++++++++++ 2 files changed, 158 insertions(+), 1 deletion(-) create mode 100644 src/discussions/posts/post/Post.test.jsx diff --git a/src/discussions/posts/post/Post.jsx b/src/discussions/posts/post/Post.jsx index 736be665..e1a026a6 100644 --- a/src/discussions/posts/post/Post.jsx +++ b/src/discussions/posts/post/Post.jsx @@ -143,6 +143,7 @@ const Post = ({ handleAddResponseButton, openRestrictionDialogue }) => { onClose={hideDeleteConfirmation} confirmAction={handleDeleteConfirmation} closeButtonVariant="tertiary" + confirmButtonVariant="danger" confirmButtonText={intl.formatMessage(messages.deleteConfirmationDelete)} /> {!abuseFlagged && ( @@ -152,7 +153,6 @@ const Post = ({ handleAddResponseButton, openRestrictionDialogue }) => { description={intl.formatMessage(messages.reportPostDescription)} onClose={hideReportConfirmation} confirmAction={handleReportConfirmation} - confirmButtonVariant="danger" /> )} ({ + ...jest.requireActual('react-redux'), + useDispatch: () => jest.fn(), + useSelector: jest.fn(), +})); + +jest.mock('react-router-dom', () => ({ + ...jest.requireActual('react-router-dom'), + useLocation: () => ({ pathname: '/test' }), + useNavigate: () => jest.fn(), +})); + +jest.mock('@edx/frontend-platform/i18n', () => ({ + useIntl: () => ({ + formatMessage: (msg) => ((msg && msg.defaultMessage) ? msg.defaultMessage : 'test-message'), + }), + defineMessages: (msgs) => msgs, +})); + +jest.mock('@openedx/paragon', () => { + const actual = jest.requireActual('@openedx/paragon'); + // eslint-disable-next-line global-require + const PropTypes = require('prop-types'); + + const MockHyperlink = ({ children }) =>
{children}
; + MockHyperlink.propTypes = { children: PropTypes.node.isRequired }; + + return { + ...actual, + Hyperlink: MockHyperlink, + useToggle: actual.useToggle, + }; +}); + +jest.mock('../../common', () => { + // eslint-disable-next-line global-require + const PropTypes = require('prop-types'); + + const MockConfirmation = ({ confirmButtonVariant, isOpen }) => { + if (!isOpen) { return null; } + return ( +
+ Mock Confirmation +
+ ); + }; + + MockConfirmation.propTypes = { + confirmButtonVariant: PropTypes.string, + isOpen: PropTypes.bool.isRequired, + }; + + // eslint-disable-next-line react/prop-types + const MockAlertBanner = () =>
; + + return { + Confirmation: MockConfirmation, + AlertBanner: MockAlertBanner, + }; +}); + +jest.mock('./PostHeader', () => function MockPostHeader() { return
PostHeader
; }); +jest.mock('./PostFooter', () => function MockPostFooter() { return
PostFooter
; }); +jest.mock('./ClosePostReasonModal', () => function MockCloseModal() { return
; }); +jest.mock('../../../components/HTMLLoader', () => function MockLoader() { return
Body Content
; }); + +jest.mock('../../common/HoverCard', () => { + // eslint-disable-next-line global-require + const { ContentActions } = require('../../../data/constants'); + // eslint-disable-next-line global-require + const PropTypes = require('prop-types'); + + const MockHoverCard = ({ actionHandlers }) => ( +
+ + +
+ ); + + MockHoverCard.propTypes = { + actionHandlers: PropTypes.shape({}).isRequired, + }; + + return MockHoverCard; +}); + +describe('Post Component - Delete/Report Confirmation', () => { + const mockPostId = '123'; + + beforeEach(() => { + useSelector.mockReturnValue({ + topicId: 'topic-1', + abuseFlagged: false, + closed: false, + pinned: false, + voted: false, + following: false, + author: {}, + title: 'Test Post', + renderedBody: '
Hello
', + users: {}, + }); + }); + + const renderPost = () => { + // eslint-disable-next-line global-require + const DiscussionContext = require('../../common/context').default; + + return render( + + + , + ); + }; + + it('passes "danger" variant to Confirmation modal when deleting a post', () => { + renderPost(); + + const deleteBtn = screen.getByTestId('trigger-delete'); + fireEvent.click(deleteBtn); + + const confirmation = screen.getByTestId('mock-confirmation'); + expect(confirmation).toHaveAttribute('data-variant', 'danger'); + }); + + it('does NOT pass "danger" variant to Confirmation modal when reporting a post', () => { + renderPost(); + + const reportBtn = screen.getByTestId('trigger-report'); + fireEvent.click(reportBtn); + + const confirmation = screen.getByTestId('mock-confirmation'); + expect(confirmation).not.toHaveAttribute('data-variant', 'danger'); + }); +});