write test for capturing rank prop

Signed-off-by: Emma Indal <emma.indahl@gmail.com>
This commit is contained in:
Emma Indal
2022-11-14 10:40:50 +01:00
parent 38c3c4931a
commit bab2ae85a5
@@ -15,9 +15,15 @@
*/
import React from 'react';
import { renderInTestApp } from '@backstage/test-utils';
import {
MockAnalyticsApi,
renderInTestApp,
TestApiProvider,
} from '@backstage/test-utils';
import userEvent from '@testing-library/user-event';
import { screen } from '@testing-library/react';
import { StackOverflowSearchResultListItem } from './StackOverflowSearchResultListItem';
import { analyticsApiRef } from '@backstage/core-plugin-api';
describe('<StackOverflowSearchResultListItem/>', () => {
it('should render without exploding', async () => {
@@ -40,4 +46,36 @@ describe('<StackOverflowSearchResultListItem/>', () => {
screen.getByText(/Customizing Spotify backstage UI/i).closest('a'),
).toHaveAttribute('href', 'https://stackoverflow.com/questions/7');
});
it('should capture analytics for rank', async () => {
const analyticsSpy = new MockAnalyticsApi();
await renderInTestApp(
<TestApiProvider apis={[[analyticsApiRef, analyticsSpy]]}>
<StackOverflowSearchResultListItem
result={{
title: 'Customizing Spotify backstage UI',
text: 'Name of Author',
location: 'https://stackoverflow.com/questions/7',
answers: 0,
tags: ['backstage'],
}}
rank={1}
/>
,
</TestApiProvider>,
);
await userEvent.click(
screen.getByText(/Customizing Spotify backstage UI/i),
);
expect(analyticsSpy.getEvents()[0]).toMatchObject({
action: 'discover',
attributes: { to: 'https://stackoverflow.com/questions/7' },
context: { extension: 'App', pluginId: 'root', routeRef: 'unknown' },
subject: 'Customizing Spotify backstage UI',
value: 1,
});
});
});