From 2184592174b8ec1ff20381349773fef1533d4b9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Laura=20Fern=C3=A1ndez?= Date: Fri, 1 Mar 2024 19:01:27 +0100 Subject: [PATCH] ReturnToPrevious: create tests to check `reportInteraction` (#83757) --- .../ReturnToPrevious.test.tsx | 75 +++++++++++++++++++ .../ReturnToPrevious/ReturnToPrevious.tsx | 5 +- 2 files changed, 77 insertions(+), 3 deletions(-) create mode 100644 public/app/core/components/AppChrome/ReturnToPrevious/ReturnToPrevious.test.tsx diff --git a/public/app/core/components/AppChrome/ReturnToPrevious/ReturnToPrevious.test.tsx b/public/app/core/components/AppChrome/ReturnToPrevious/ReturnToPrevious.test.tsx new file mode 100644 index 00000000000..8ff6c986e85 --- /dev/null +++ b/public/app/core/components/AppChrome/ReturnToPrevious/ReturnToPrevious.test.tsx @@ -0,0 +1,75 @@ +import { render, screen } from '@testing-library/react'; +import userEvent from '@testing-library/user-event'; +import React from 'react'; +import { TestProvider } from 'test/helpers/TestProvider'; +import { getGrafanaContextMock } from 'test/mocks/getGrafanaContextMock'; + +import { config, reportInteraction } from '@grafana/runtime'; + +import { ReturnToPrevious, ReturnToPreviousProps } from './ReturnToPrevious'; + +const mockReturnToPreviousProps: ReturnToPreviousProps = { + title: 'Dashboards Page', + href: '/dashboards', +}; +const reportInteractionMock = jest.mocked(reportInteraction); +jest.mock('@grafana/runtime', () => { + return { + ...jest.requireActual('@grafana/runtime'), + reportInteraction: reportInteractionMock, + }; +}); +jest.mock('@grafana/runtime', () => { + return { + ...jest.requireActual('@grafana/runtime'), + reportInteraction: jest.fn(), + }; +}); +const setup = () => { + const grafanaContext = getGrafanaContextMock(); + grafanaContext.chrome.setReturnToPrevious(mockReturnToPreviousProps); + return render( + + + + ); +}; + +describe('ReturnToPrevious', () => { + beforeEach(() => { + /* We enabled the feature toggle */ + config.featureToggles.returnToPrevious = true; + }); + afterEach(() => { + window.sessionStorage.clear(); + jest.resetAllMocks(); + config.featureToggles.returnToPrevious = false; + }); + it('should render component', async () => { + setup(); + expect(await screen.findByTitle('Back to Dashboards Page')).toBeInTheDocument(); + }); + + it('should trigger event once when clicking on the RTP button', async () => { + setup(); + const returnButton = await screen.findByTitle('Back to Dashboards Page'); + expect(returnButton).toBeInTheDocument(); + await userEvent.click(returnButton); + const mockCalls = reportInteractionMock.mock.calls; + /* The report is called 'grafana_return_to_previous_button_dismissed' but the action is 'clicked' */ + const mockReturn = mockCalls.filter((call) => call[0] === 'grafana_return_to_previous_button_dismissed'); + expect(mockReturn).toHaveLength(1); + }); + + it('should trigger event once when clicking on the Close button', async () => { + setup(); + + const closeBtn = await screen.findByRole('button', { name: 'Close' }); + expect(closeBtn).toBeInTheDocument(); + await userEvent.click(closeBtn); + const mockCalls = reportInteractionMock.mock.calls; + /* The report is called 'grafana_return_to_previous_button_dismissed' but the action is 'dismissed' */ + const mockDismissed = mockCalls.filter((call) => call[0] === 'grafana_return_to_previous_button_dismissed'); + expect(mockDismissed).toHaveLength(1); + }); +}); diff --git a/public/app/core/components/AppChrome/ReturnToPrevious/ReturnToPrevious.tsx b/public/app/core/components/AppChrome/ReturnToPrevious/ReturnToPrevious.tsx index 58d7dbf654a..693aa72b83f 100644 --- a/public/app/core/components/AppChrome/ReturnToPrevious/ReturnToPrevious.tsx +++ b/public/app/core/components/AppChrome/ReturnToPrevious/ReturnToPrevious.tsx @@ -2,7 +2,7 @@ import { css } from '@emotion/css'; import React, { useCallback } from 'react'; import { GrafanaTheme2 } from '@grafana/data'; -import { locationService, reportInteraction } from '@grafana/runtime'; +import { locationService } from '@grafana/runtime'; import { useStyles2 } from '@grafana/ui'; import { useGrafana } from 'app/core/context/GrafanaContext'; import { t } from 'app/core/internationalization'; @@ -24,9 +24,8 @@ export const ReturnToPrevious = ({ href, title }: ReturnToPreviousProps) => { }, [href, chrome]); const handleOnDismiss = useCallback(() => { - reportInteraction('grafana_return_to_previous_button_dismissed', { action: 'dismissed', page: href }); chrome.clearReturnToPrevious('dismissed'); - }, [href, chrome]); + }, [chrome]); return (