From 1def17fd023171740e036d0759bb6a422997a860 Mon Sep 17 00:00:00 2001 From: Camila Belo Date: Mon, 27 Nov 2023 16:11:43 +0100 Subject: [PATCH] refactor: use apis provider on the analytics context test Signed-off-by: Camila Belo --- .../src/analytics/useAnalytics.test.tsx | 33 ++++++++----------- 1 file changed, 14 insertions(+), 19 deletions(-) diff --git a/packages/frontend-plugin-api/src/analytics/useAnalytics.test.tsx b/packages/frontend-plugin-api/src/analytics/useAnalytics.test.tsx index 630ce0331a..beb16b1fbb 100644 --- a/packages/frontend-plugin-api/src/analytics/useAnalytics.test.tsx +++ b/packages/frontend-plugin-api/src/analytics/useAnalytics.test.tsx @@ -16,36 +16,31 @@ import { renderHook } from '@testing-library/react'; import { useAnalytics } from './useAnalytics'; -import { useApi } from '@backstage/core-plugin-api'; - -jest.mock('@backstage/core-plugin-api', () => ({ - ...jest.requireActual('@backstage/core-plugin-api'), - useApi: jest.fn(), -})); - -const mocked = (f: Function) => f as jest.Mock; +import { analyticsApiRef } from '@backstage/core-plugin-api'; +import { TestApiProvider } from '@backstage/test-utils'; +import React from 'react'; describe('useAnalytics', () => { it('returns tracker with no implementation defined', () => { - // Simulate useApi() throwing an error. - mocked(useApi).mockImplementation(() => { - throw new Error(); - }); - - // Result should still have a captureEvent method. - const { result } = renderHook(() => useAnalytics()); + // useApi throws an error because the Analytics API is not implemented + // But the result should still have a captureEvent method. + const { result } = renderHook(() => useAnalytics(), {}); expect(result.current.captureEvent).toBeDefined(); }); it('returns tracker from defined analytics api', () => { const captureEvent = jest.fn(); - // Simulate useApi returning a valid tracker. - mocked(useApi).mockReturnValue({ captureEvent }); - // Calling the captureEvent method of the underlying implementation should // pass along the given event as well as the default context. - const { result } = renderHook(() => useAnalytics()); + const { result } = renderHook(() => useAnalytics(), { + wrapper: ({ children }) => ( + // Simulate useApi returning a valid tracker. + + {children} + + ), + }); result.current.captureEvent('an action', 'a subject', { value: 42, attributes: { some: 'value' },