From 19348c5990b73d2989c0e831925784de91cad355 Mon Sep 17 00:00:00 2001 From: Vincenzo Scamporlino Date: Wed, 26 Apr 2023 23:08:56 +0200 Subject: [PATCH] core-app-api: add route params test Signed-off-by: Vincenzo Scamporlino --- .../src/routing/RouteTracker.test.tsx | 115 ++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 packages/core-app-api/src/routing/RouteTracker.test.tsx diff --git a/packages/core-app-api/src/routing/RouteTracker.test.tsx b/packages/core-app-api/src/routing/RouteTracker.test.tsx new file mode 100644 index 0000000000..9472b523aa --- /dev/null +++ b/packages/core-app-api/src/routing/RouteTracker.test.tsx @@ -0,0 +1,115 @@ +/* + * Copyright 2023 The Backstage Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import { TestApiProvider } from '@backstage/test-utils'; +import React from 'react'; +import { BackstageRouteObject } from './types'; +import { fireEvent, render, waitFor } from '@testing-library/react'; +import { RouteTracker } from './RouteTracker'; +import { Link, MemoryRouter, Route, Routes } from 'react-router-dom'; +import { + AnalyticsApi, + analyticsApiRef, + createRouteRef, +} from '@backstage/core-plugin-api'; + +describe('RouteTracker', () => { + const routeRef1 = createRouteRef({ + id: 'route1', + }); + const routeRef2 = createRouteRef({ + id: 'route2', + }); + + const routeObjects: BackstageRouteObject[] = [ + { + path: '/path/:p1/:p2', + element: go, + routeRefs: new Set([routeRef1]), + caseSensitive: false, + }, + { + path: '/path2/:param', + element:
hi there
, + routeRefs: new Set([routeRef2]), + caseSensitive: false, + }, + ]; + + const mockedAnalytics: jest.Mocked = { + captureEvent: jest.fn(), + }; + + beforeEach(() => { + jest.clearAllMocks(); + }); + + it('should capture the navigate event on load', async () => { + render( + + + + + , + ); + + expect(mockedAnalytics.captureEvent).toHaveBeenCalledWith({ + action: 'navigate', + attributes: { + p1: 'foo', + p2: 'bar', + }, + context: { + extension: 'App', + pluginId: 'root', + routeRef: 'route1', + }, + subject: '/path/foo/bar', + value: undefined, + }); + }); + + it('should capture the navigate event on route change', async () => { + const { getByText } = render( + + + + + + {routeObjects.map(({ routeRefs, ...props }) => ( + + ))} + + + , + ); + + fireEvent.click(getByText('go')); + + expect(mockedAnalytics.captureEvent).toHaveBeenCalledWith({ + action: 'navigate', + attributes: { + param: 'hello', + }, + context: { + extension: 'App', + pluginId: 'root', + routeRef: 'route2', + }, + subject: '/path2/hello', + value: undefined, + }); + }); +});