From 122be161f9af2d3c4a51aadc7d94f30a3d9650e2 Mon Sep 17 00:00:00 2001 From: Giordano Ricci Date: Thu, 5 Jan 2023 22:27:43 +0000 Subject: [PATCH] Datasource Onboarding: add tracking (#60918) * Datasource Onboarding: add tracking * remove unused var * set ds logo alt text to empty screenn and remove presentation role * run i18n:extract * Revert "run i18n:extract" This reverts commit 5313ac96618f8ea32717632d9f255bdf358067f1. --- .../EmptyStateNoDatasource.test.tsx | 82 +++++++++++++++++++ .../components/EmptyStateNoDatasource.tsx | 33 +++++--- 2 files changed, 102 insertions(+), 13 deletions(-) create mode 100644 public/app/features/datasources/components/EmptyStateNoDatasource.test.tsx diff --git a/public/app/features/datasources/components/EmptyStateNoDatasource.test.tsx b/public/app/features/datasources/components/EmptyStateNoDatasource.test.tsx new file mode 100644 index 00000000000..e0fd8ce982f --- /dev/null +++ b/public/app/features/datasources/components/EmptyStateNoDatasource.test.tsx @@ -0,0 +1,82 @@ +import { fireEvent, render, screen, waitFor } from '@testing-library/react'; +import { rest } from 'msw'; +import { setupServer } from 'msw/node'; +import React from 'react'; +import { DeepPartial } from 'react-hook-form'; +import { Provider } from 'react-redux'; + +import { DataSourcePluginMeta } from '@grafana/data'; +import * as runtime from '@grafana/runtime'; +import { backendSrv } from 'app/core/services/backend_srv'; +import { contextSrv } from 'app/core/services/context_srv'; +import { configureStore } from 'app/store/configureStore'; +import 'whatwg-fetch'; + +import { EmptyStateNoDatasource } from './EmptyStateNoDatasource'; + +let reportInteractionSpy: jest.SpyInstance; +const server = setupServer(); + +beforeEach(() => { + jest.spyOn(contextSrv, 'hasRole').mockReturnValue(true); + reportInteractionSpy = jest.spyOn(runtime, 'reportInteraction'); + server.resetHandlers(); +}); + +afterEach(() => { + jest.restoreAllMocks(); +}); + +beforeAll(() => { + runtime.setBackendSrv(backendSrv); + + server.listen({ onUnhandledRequest: 'bypass' }); +}); + +afterAll(() => { + server.close(); +}); + +describe('EmptyStateNoDatasource', () => { + it('correctly tracks user interactions', async () => { + server.use( + rest.get('/api/plugins', (_, res, ctx) => { + return res( + ctx.json>>([ + { id: 'prometheus', name: 'Prometheus', info: { logos: { small: 'prometheus.png' } } }, + { id: 'mysql', name: 'MySQL', info: { logos: { small: 'mysql.png' } } }, + { id: 'elasticsearch', name: 'Elasticsearch', info: { logos: { small: 'elasticsearch.png' } } }, + { id: 'influxdb', name: 'InfluxDB', info: { logos: { small: 'influxdb.png' } } }, + { id: 'graphite', name: 'Graphite', info: { logos: { small: 'graphite.png' } } }, + { id: 'stackdriver', name: 'StackDriver', info: { logos: { small: 'stackdriver.png' } } }, + { id: 'cloudwatch', name: 'CloudWatch', info: { logos: { small: 'cloudwatch.png' } } }, + { + id: 'grafana-azure-monitor-datasource', + name: 'Azure Monitor', + info: { logos: { small: 'grafana-azure-monitor-datasource.png' } }, + }, + ]) + ); + }) + ); + render( + + + + ); + + await waitFor(() => { + expect(screen.getByRole('heading', { name: 'A Title' })).toBeInTheDocument(); + expect(screen.getByRole('button', { name: 'Prometheus' })).toBeInTheDocument(); + }); + + fireEvent.click(screen.getByRole('button', { name: 'Prometheus' })); + expect(reportInteractionSpy).toHaveBeenLastCalledWith('dashboards_connectds_ds_clicked'); + + fireEvent.click(screen.getByRole('link', { name: 'View all' })); + expect(reportInteractionSpy).toHaveBeenCalledWith('dashboards_connectds_viewall_clicked'); + + fireEvent.click(screen.getByRole('button', { name: 'CTA' })); + expect(reportInteractionSpy).toHaveBeenLastCalledWith('dashboards_connectds_sampledata_clicked'); + }); +}); diff --git a/public/app/features/datasources/components/EmptyStateNoDatasource.tsx b/public/app/features/datasources/components/EmptyStateNoDatasource.tsx index b0a53fdcab4..39539d90012 100644 --- a/public/app/features/datasources/components/EmptyStateNoDatasource.tsx +++ b/public/app/features/datasources/components/EmptyStateNoDatasource.tsx @@ -3,7 +3,7 @@ import React, { ComponentProps } from 'react'; import { useAsync } from 'react-use'; import { DataSourcePluginMeta, GrafanaTheme2, PageLayoutType } from '@grafana/data'; -import { getBackendSrv } from '@grafana/runtime'; +import { getBackendSrv, reportInteraction } from '@grafana/runtime'; import { Icon, useStyles2 } from '@grafana/ui'; import { Page } from 'app/core/components/Page/Page'; import { contextSrv } from 'app/core/core'; @@ -68,23 +68,24 @@ export function EmptyStateNoDatasource({ onCTAClick, loading = false, title, CTA