|
| 1 | +/* |
| 2 | + * Copyright 2025 The Backstage Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import { default as React } from 'react'; |
| 18 | +import { renderHook } from '@testing-library/react-hooks'; |
| 19 | +import { useTopActiveItems } from './useTopActiveItems'; |
| 20 | +import { rollbarApiRef } from '../api'; |
| 21 | +import { TestApiProvider } from '@backstage/test-utils'; |
| 22 | +import { Entity } from '@backstage/catalog-model'; |
| 23 | +import { configApiRef } from '@backstage/core-plugin-api'; |
| 24 | + |
| 25 | +describe('useTopActiveItems', () => { |
| 26 | + const mockApi = { |
| 27 | + getTopActiveItems: jest.fn(), |
| 28 | + }; |
| 29 | + |
| 30 | + const entity: Entity = { |
| 31 | + apiVersion: 'backstage.io/v1alpha1', |
| 32 | + kind: 'Component', |
| 33 | + metadata: { |
| 34 | + name: 'test-service', |
| 35 | + annotations: { |
| 36 | + 'rollbar.com/project-slug': 'foo/bar', |
| 37 | + 'rollbar.com/environment': 'qa', |
| 38 | + }, |
| 39 | + }, |
| 40 | + spec: {}, |
| 41 | + }; |
| 42 | + |
| 43 | + it('should use the environment annotation', async () => { |
| 44 | + mockApi.getTopActiveItems.mockResolvedValueOnce([ |
| 45 | + { item: { occurrences: 2 } }, |
| 46 | + { item: { occurrences: 5 } }, |
| 47 | + ]); |
| 48 | + const wrapper = ({ children }: { children: React.ReactNode }) => ( |
| 49 | + <TestApiProvider |
| 50 | + apis={[ |
| 51 | + [rollbarApiRef, mockApi], |
| 52 | + [configApiRef, {}], |
| 53 | + ]} |
| 54 | + > |
| 55 | + {children} |
| 56 | + </TestApiProvider> |
| 57 | + ); |
| 58 | + const { result, waitForNextUpdate } = renderHook( |
| 59 | + () => useTopActiveItems(entity), |
| 60 | + { wrapper }, |
| 61 | + ); |
| 62 | + await waitForNextUpdate(); |
| 63 | + expect(mockApi.getTopActiveItems).toHaveBeenCalledWith('bar', 168, 'qa'); |
| 64 | + expect(result.current.items[0].item.occurrences).toBe(5); |
| 65 | + expect(result.current.items[1].item.occurrences).toBe(2); |
| 66 | + }); |
| 67 | +}); |
0 commit comments