test: fix route tracker skipped test

Co-authored-by: Patrik Oldsberg <poldsberg@gmail.com>

Signed-off-by: Camila Belo <camilaibs@gmail.com>
This commit is contained in:
Camila Belo
2023-11-27 15:26:42 +01:00
parent 5c794cd583
commit 5ef37a9c39
3 changed files with 103 additions and 16 deletions
@@ -14,7 +14,7 @@
* limitations under the License.
*/
import { TestApiProvider } from '@backstage/test-utils';
import React from 'react';
import React, { useEffect } from 'react';
import { BackstageRouteObject } from './types';
import { fireEvent, render } from '@testing-library/react';
import { RouteTracker } from './RouteTracker';
@@ -25,6 +25,7 @@ import {
AnalyticsApi,
analyticsApiRef,
AppNode,
useAnalytics,
} from '@backstage/frontend-plugin-api';
import { MATCH_ALL_ROUTE } from './extractRouteInfoFromAppNode';
@@ -186,31 +187,46 @@ describe('RouteTracker', () => {
});
});
it.skip('should return default context when it would have otherwise matched on the root path', async () => {
it('should return default context when it would have otherwise matched on the root path', async () => {
const Dummy = () => {
const analytics = useAnalytics();
useEffect(() => {
analytics.captureEvent('click', 'test', {});
}, [analytics]);
return <div>dummy</div>;
};
render(
<MemoryRouter initialEntries={['/not-routable-extension']}>
<TestApiProvider apis={[[analyticsApiRef, mockedAnalytics]]}>
<RouteTracker routeObjects={routeObjects} />
<Routes>
<Route
path="/not-routable-extension"
element={<>Non-extension</>}
/>
<Route path="/not-routable-extension" element={<Dummy />} />
</Routes>
</TestApiProvider>
</MemoryRouter>,
);
expect(mockedAnalytics.captureEvent).toHaveBeenCalledWith({
expect(mockedAnalytics.captureEvent).toHaveBeenNthCalledWith(1, {
action: 'navigate',
attributes: {},
context: {
extension: 'App',
extensionId: 'App',
pluginId: 'root',
},
subject: '/not-routable-extension',
value: undefined,
});
expect(mockedAnalytics.captureEvent).toHaveBeenNthCalledWith(2, {
action: 'click',
attributes: undefined,
context: {
extensionId: 'App',
pluginId: 'root',
},
subject: 'test',
value: undefined,
});
});
it('should return parent route context on navigating to a sub-route', async () => {
@@ -20,7 +20,7 @@ import {
useAnalytics,
AnalyticsContext,
AnalyticsEventAttributes,
} from '@backstage/core-plugin-api';
} from '@backstage/frontend-plugin-api';
import { BackstageRouteObject } from './types';
/**
@@ -19,6 +19,7 @@ import { BackstagePlugin } from '@backstage/core-plugin-api';
import { extractRouteInfoFromAppNode } from './extractRouteInfoFromAppNode';
import {
AnyRouteRefParams,
AppNode,
Extension,
RouteRef,
coreExtensionData,
@@ -99,8 +100,10 @@ function routeObj(
children: any[] = [],
type: 'mounted' | 'gathered' = 'mounted',
backstagePlugin?: BackstagePlugin,
appNode?: AppNode,
) {
return {
appNode,
path: path,
caseSensitive: false,
element: type,
@@ -171,7 +174,14 @@ describe('discovery', () => {
[ref5, ref1],
]);
expect(info.routeObjects).toEqual([
routeObj('nothing', []),
routeObj(
'nothing',
[],
undefined,
undefined,
undefined,
expect.any(Object),
),
routeObj(
'foo',
[ref1],
@@ -179,16 +189,41 @@ describe('discovery', () => {
routeObj(
'bar/:id',
[ref2],
[routeObj('baz', [ref3], undefined, undefined, expect.any(Object))],
[
routeObj(
'baz',
[ref3],
undefined,
undefined,
expect.any(Object),
expect.any(Object),
),
],
undefined,
expect.any(Object),
expect.any(Object),
),
routeObj(
'blop',
[ref5],
undefined,
undefined,
expect.any(Object),
expect.any(Object),
),
routeObj('blop', [ref5], undefined, undefined, expect.any(Object)),
],
undefined,
expect.any(Object),
expect.any(Object),
),
routeObj(
'divsoup',
[ref4],
undefined,
undefined,
expect.any(Object),
expect.any(Object),
),
routeObj('divsoup', [ref4], undefined, undefined, expect.any(Object)),
]);
});
@@ -349,13 +384,30 @@ describe('discovery', () => {
[ref5, ref3],
]);
expect(info.routeObjects).toEqual([
routeObj('foo', [ref1, ref2], [], 'mounted', expect.any(Object)),
routeObj(
'foo',
[ref1, ref2],
[],
'mounted',
expect.any(Object),
expect.any(Object),
),
routeObj(
'bar',
[ref3],
[routeObj('', [ref4, ref5], [], 'mounted', expect.any(Object))],
[
routeObj(
'',
[ref4, ref5],
[],
'mounted',
expect.any(Object),
expect.any(Object),
),
],
'mounted',
expect.any(Object),
expect.any(Object),
),
]);
});
@@ -429,18 +481,22 @@ describe('discovery', () => {
undefined,
undefined,
expect.any(Object),
expect.any(Object),
),
],
undefined,
expect.any(Object),
expect.any(Object),
),
],
'mounted',
expect.any(Object),
expect.any(Object),
),
],
undefined,
expect.any(Object),
expect.any(Object),
),
]);
});
@@ -499,7 +555,14 @@ describe('discovery', () => {
'r',
[],
[
routeObj('x', [ref1], [], 'mounted', expect.any(Object)),
routeObj(
'x',
[ref1],
[],
'mounted',
expect.any(Object),
expect.any(Object),
),
routeObj(
'y',
[],
@@ -514,6 +577,7 @@ describe('discovery', () => {
undefined,
'mounted',
expect.any(Object),
expect.any(Object),
),
routeObj(
'b',
@@ -521,15 +585,22 @@ describe('discovery', () => {
undefined,
'mounted',
expect.any(Object),
expect.any(Object),
),
],
'mounted',
expect.any(Object),
expect.any(Object),
),
],
'mounted',
undefined,
expect.any(Object),
),
],
undefined,
undefined,
expect.any(Object),
),
]);
});