From f36eb59f215ef4e81e9541dadfeebd13a5ba6279 Mon Sep 17 00:00:00 2001 From: MeHow25 <61119231+MeHow25@users.noreply.github.com> Date: Mon, 1 Jul 2024 09:33:52 +0200 Subject: [PATCH] [MM-58405] Migrate tooltips of 'components/announcement_bar/default_announcement_bar/announcement_bar' to WithTooltip (#27244) * Migrate tooltips of 'components/announcement_bar/default_announcement_bar/announcement_bar' to WithTooltip * Review fixes * Review fix * Fix imports * Update snapshots * Test fix --------- Co-authored-by: Mattermost Build --- .../announcement_bar.test.tsx.snap | 320 +++++------------- .../announcement_bar.test.tsx | 43 +++ .../announcement_bar.tsx | 90 ++--- .../src/components/with_tooltip/index.tsx | 3 + 4 files changed, 182 insertions(+), 274 deletions(-) create mode 100644 webapp/channels/src/components/announcement_bar/default_announcement_bar/announcement_bar.test.tsx diff --git a/webapp/channels/src/components/announcement_bar/__snapshots__/announcement_bar.test.tsx.snap b/webapp/channels/src/components/announcement_bar/__snapshots__/announcement_bar.test.tsx.snap index 0db07a0d78..a1de01252f 100644 --- a/webapp/channels/src/components/announcement_bar/__snapshots__/announcement_bar.test.tsx.snap +++ b/webapp/channels/src/components/announcement_bar/__snapshots__/announcement_bar.test.tsx.snap @@ -86,31 +86,17 @@ exports[`components/AnnouncementBar should match snapshot, bar not showing 1`] = } } > - } - placement="bottom" - trigger={ - Array [ - "hover", - "focus", - ] - } +
-
- - - -
- + + +
`; @@ -124,31 +110,17 @@ exports[`components/AnnouncementBar should match snapshot, bar showing 1`] = ` } } > - } - placement="bottom" - trigger={ - Array [ - "hover", - "focus", - ] - } +
-
- - - -
- + + +
`; @@ -162,31 +134,17 @@ exports[`components/AnnouncementBar should match snapshot, bar showing, no dismi } } > - } - placement="bottom" - trigger={ - Array [ - "hover", - "focus", - ] - } +
-
- - - -
- + + +
`; @@ -200,31 +158,17 @@ exports[`components/AnnouncementBar should match snapshot, dismissal 1`] = ` } } > - } - placement="bottom" - trigger={ - Array [ - "hover", - "focus", - ] - } +
-
- - - -
- + + +
`; @@ -238,31 +182,17 @@ exports[`components/AnnouncementBar should match snapshot, dismissal 2`] = ` } } > - } - placement="bottom" - trigger={ - Array [ - "hover", - "focus", - ] - } +
-
- - - -
- + + +
`; @@ -276,31 +206,17 @@ exports[`components/AnnouncementBar should match snapshot, dismissal 3`] = ` } } > - } - placement="bottom" - trigger={ - Array [ - "hover", - "focus", - ] - } +
-
- - - -
- + + +
`; @@ -314,31 +230,17 @@ exports[`components/AnnouncementBar should match snapshot, props change 1`] = ` } } > - } - placement="bottom" - trigger={ - Array [ - "hover", - "focus", - ] - } +
-
- - - -
- + + +
`; @@ -352,31 +254,17 @@ exports[`components/AnnouncementBar should match snapshot, props change 2`] = ` } } > - } - placement="bottom" - trigger={ - Array [ - "hover", - "focus", - ] - } +
-
- - - -
- + + +
`; @@ -390,31 +278,17 @@ exports[`components/AnnouncementBar should match snapshot, props change 3`] = ` } } > - } - placement="bottom" - trigger={ - Array [ - "hover", - "focus", - ] - } +
-
- - - -
- + + +
`; @@ -428,30 +302,16 @@ exports[`components/AnnouncementBar should match snapshot, props change 4`] = ` } } > - } - placement="bottom" - trigger={ - Array [ - "hover", - "focus", - ] - } +
-
- - - -
- + + +
`; diff --git a/webapp/channels/src/components/announcement_bar/default_announcement_bar/announcement_bar.test.tsx b/webapp/channels/src/components/announcement_bar/default_announcement_bar/announcement_bar.test.tsx new file mode 100644 index 0000000000..e7ef62a3e5 --- /dev/null +++ b/webapp/channels/src/components/announcement_bar/default_announcement_bar/announcement_bar.test.tsx @@ -0,0 +1,43 @@ +// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. +// See LICENSE.txt for license information. + +import {screen} from '@testing-library/react'; +import React from 'react'; + +import AnnouncementBar from 'components/announcement_bar/default_announcement_bar'; + +import {renderWithContext, userEvent} from 'tests/react_testing_utils'; + +describe('components/announcement_bar/default_announcement_bar', () => { + const originalOffsetWidth = Object.getOwnPropertyDescriptor( + HTMLElement.prototype, + 'offsetWidth', + ) as PropertyDescriptor; + + beforeAll(() => { + Object.defineProperty(HTMLElement.prototype, 'offsetWidth', { + configurable: true, + value: 20, + }); + }); + + afterAll(() => { + Object.defineProperty(HTMLElement.prototype, 'offsetWidth', originalOffsetWidth); + }); + + test('should not show tooltip by default', () => { + const wrapper = renderWithContext({'Lorem Ipsum'}}/>); + + wrapper.getByText('Lorem Ipsum'); + + expect(wrapper.queryByRole('tooltip')).toBeNull(); + }); + + test('should show tooltip on hover', async () => { + const wrapper = renderWithContext({'Lorem Ipsum'}}/>); + + userEvent.hover(wrapper.getByText('Lorem Ipsum')); + + expect(screen.findByRole('tooltip')).not.toBeNull(); + }); +}); diff --git a/webapp/channels/src/components/announcement_bar/default_announcement_bar/announcement_bar.tsx b/webapp/channels/src/components/announcement_bar/default_announcement_bar/announcement_bar.tsx index 9660682e19..bcf45df550 100644 --- a/webapp/channels/src/components/announcement_bar/default_announcement_bar/announcement_bar.tsx +++ b/webapp/channels/src/components/announcement_bar/default_announcement_bar/announcement_bar.tsx @@ -7,10 +7,9 @@ import type {MessageDescriptor} from 'react-intl'; import {FormattedMessage} from 'react-intl'; import FormattedMarkdownMessage from 'components/formatted_markdown_message'; -import OverlayTrigger from 'components/overlay_trigger'; -import Tooltip from 'components/tooltip'; +import WithTooltip from 'components/with_tooltip'; -import {Constants, AnnouncementBarTypes} from 'utils/constants'; +import {AnnouncementBarTypes} from 'utils/constants'; import {isStringContainingUrl} from 'utils/url'; type Props = { @@ -156,17 +155,55 @@ export default class AnnouncementBar extends React.PureComponent { ); } - const announcementTooltip = this.state.showTooltip ? ( - - {this.props.tooltipMsg ? this.props.tooltipMsg : message} - - ) : <>; const announcementIcon = () => { return this.props.showLinkAsButton && (this.props.showCloseButton ? : ); }; + let barContent = (
+ {this.props.icon ? this.props.icon : announcementIcon()} + + {message} + + { + this.props.showLinkAsButton && this.props.showCTA && this.props.modalButtonText && + + } + { + this.props.showLinkAsButton && this.props.showCTA && this.props.ctaText && + + } +
); + + if (this.state.showTooltip) { + barContent = ( + + {barContent} + + ); + } + return (
{ css={{gridArea: 'announcement'}} data-testid={this.props.id} > - -
- {this.props.icon ? this.props.icon : announcementIcon()} - - {message} - - { - this.props.showLinkAsButton && this.props.showCTA && this.props.modalButtonText && - - } - { - this.props.showLinkAsButton && this.props.showCTA && this.props.ctaText && - - } -
-
+ {barContent} {closeButton}
); diff --git a/webapp/channels/src/components/with_tooltip/index.tsx b/webapp/channels/src/components/with_tooltip/index.tsx index b432e27e2b..6fac771a70 100644 --- a/webapp/channels/src/components/with_tooltip/index.tsx +++ b/webapp/channels/src/components/with_tooltip/index.tsx @@ -17,6 +17,7 @@ type WithTooltipProps = { children: OverlayTriggerProps['children']; placement: OverlayTriggerProps['placement']; onShow?: () => void; + delayHide?: number; onExit?: () => void; } & CommonTooltipProps; const WithTooltip = ({ @@ -28,6 +29,7 @@ const WithTooltip = ({ shortcut, placement, onShow, + delayHide, children, onExit, shouldUpdatePosition, @@ -47,6 +49,7 @@ const WithTooltip = ({ overlay={} placement={placement} onEnter={onShow} + delayHide={delayHide} onExit={onExit} shouldUpdatePosition={shouldUpdatePosition} >