From: Guillaume Peoc'h Date: Thu, 6 Oct 2022 09:56:50 +0000 (+0200) Subject: SONAR-17096 Display announcement on window focus X-Git-Tag: 9.7.0.61563~128 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=537245f93e0b1f51fb84ecbbc9efffc65c91aceb;p=sonarqube.git SONAR-17096 Display announcement on window focus --- diff --git a/server/sonar-web/src/main/js/app/components/SystemAnnouncement.tsx b/server/sonar-web/src/main/js/app/components/SystemAnnouncement.tsx index 89e23b08ce6..b889cd478dc 100644 --- a/server/sonar-web/src/main/js/app/components/SystemAnnouncement.tsx +++ b/server/sonar-web/src/main/js/app/components/SystemAnnouncement.tsx @@ -18,7 +18,7 @@ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -import { isEmpty, keyBy } from 'lodash'; +import { isEmpty, keyBy, throttle } from 'lodash'; import * as React from 'react'; import { getValues } from '../../api/settings'; import { Alert } from '../../components/ui/Alert'; @@ -29,6 +29,8 @@ import withAvailableFeatures, { } from './available-features/withAvailableFeatures'; import './SystemAnnouncement.css'; +const THROTTLE_TIME_MS = 10000; + interface State { displayMessage: boolean; message: string; @@ -40,13 +42,13 @@ export class SystemAnnouncement extends React.PureComponent { + // eslint-disable-next-line react/sort-comp + handleVisibilityChange = throttle(() => { if (document.visibilityState === 'visible') { this.getSettings(); } - }; + }, THROTTLE_TIME_MS); render() { const { displayMessage, message } = this.state; diff --git a/server/sonar-web/src/main/js/app/components/__tests__/SystemAnnouncement-test.tsx b/server/sonar-web/src/main/js/app/components/__tests__/SystemAnnouncement-test.tsx index 1b44b1a34bb..e958ef8ae47 100644 --- a/server/sonar-web/src/main/js/app/components/__tests__/SystemAnnouncement-test.tsx +++ b/server/sonar-web/src/main/js/app/components/__tests__/SystemAnnouncement-test.tsx @@ -29,6 +29,12 @@ jest.mock('../../../api/settings', () => ({ getValues: jest.fn() })); +jest.mock('lodash', () => { + const lodash = jest.requireActual('lodash'); + lodash.throttle = (fn: any) => () => fn(); + return lodash; +}); + it('should display system announcement', async () => { (getValues as jest.Mock) .mockResolvedValueOnce([ @@ -75,13 +81,13 @@ it('should display system announcement', async () => { renderSystemAnnouncement(); expect(screen.queryByRole('alert')).not.toBeInTheDocument(); - fireEvent(document, new Event('visibilitychange')); + fireEvent(window, new Event('focus')); expect(screen.queryByRole('alert')).not.toBeInTheDocument(); - fireEvent(document, new Event('visibilitychange')); + fireEvent(window, new Event('focus')); expect(screen.queryByRole('alert')).not.toBeInTheDocument(); - fireEvent(document, new Event('visibilitychange')); + fireEvent(window, new Event('focus')); expect(screen.queryByRole('alert')).not.toBeInTheDocument(); - fireEvent(document, new Event('visibilitychange')); + fireEvent(window, new Event('focus')); expect(await screen.findByRole('alert')).toBeInTheDocument(); expect(screen.getByText('Foo')).toBeInTheDocument(); });