|
| 1 | +/* |
| 2 | + * Copyright Red Hat, Inc. |
| 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 | +import React from 'react'; |
| 17 | +import { screen, fireEvent } from '@testing-library/react'; |
| 18 | +import { |
| 19 | + useStarredEntities, |
| 20 | + useEntityPresentation, |
| 21 | +} from '@backstage/plugin-catalog-react'; |
| 22 | +import { renderInTestApp } from '@backstage/test-utils'; |
| 23 | +import { StarredDropdown } from './StarredDropdown'; |
| 24 | +import { useDropdownManager } from '../../hooks'; |
| 25 | + |
| 26 | +jest.mock('@backstage/plugin-catalog-react', () => ({ |
| 27 | + useStarredEntities: jest.fn(), |
| 28 | + useEntityPresentation: jest.fn(), |
| 29 | +})); |
| 30 | + |
| 31 | +jest.mock('../../hooks', () => ({ |
| 32 | + useDropdownManager: jest.fn(), |
| 33 | +})); |
| 34 | + |
| 35 | +describe('StarredDropdown', () => { |
| 36 | + beforeEach(() => { |
| 37 | + jest.clearAllMocks(); |
| 38 | + |
| 39 | + (useStarredEntities as jest.Mock).mockReturnValue({ |
| 40 | + starredEntities: new Set(), |
| 41 | + toggleStarredEntity: jest.fn(), |
| 42 | + isStarredEntity: jest.fn(), |
| 43 | + }); |
| 44 | + |
| 45 | + (useEntityPresentation as jest.Mock).mockReturnValue({ |
| 46 | + Icon: () => <svg />, // Mock an icon component |
| 47 | + primaryTitle: 'Mock Entity', |
| 48 | + secondaryTitle: 'Mock Kind', |
| 49 | + }); |
| 50 | + |
| 51 | + (useDropdownManager as jest.Mock).mockReturnValue({ |
| 52 | + anchorEl: null, |
| 53 | + handleOpen: jest.fn(), |
| 54 | + handleClose: jest.fn(), |
| 55 | + }); |
| 56 | + |
| 57 | + jest.spyOn(console, 'warn').mockImplementation(message => { |
| 58 | + if (message.includes('⚠️ React Router Future Flag Warning')) { |
| 59 | + return; // Suppress React Router warning |
| 60 | + } |
| 61 | + // eslint-disable-next-line no-console |
| 62 | + console.warn(message); // Log other warnings |
| 63 | + }); |
| 64 | + |
| 65 | + jest.spyOn(console, 'error').mockImplementation(message => { |
| 66 | + if ( |
| 67 | + typeof message === 'string' && |
| 68 | + message.includes('findDOMNode is deprecated') |
| 69 | + ) { |
| 70 | + return; // Suppress findDOMNode warning in tests |
| 71 | + } |
| 72 | + // eslint-disable-next-line no-console |
| 73 | + console.error(message); // Allow other errors to be logged |
| 74 | + }); |
| 75 | + }); |
| 76 | + |
| 77 | + it('renders an empty state when there are no starred entities', async () => { |
| 78 | + await renderInTestApp(<StarredDropdown />); |
| 79 | + |
| 80 | + // Replace this with the actual empty state message in your component |
| 81 | + expect(screen.getByText(/No starred items yet/i)).toBeInTheDocument(); |
| 82 | + }); |
| 83 | + |
| 84 | + it('renders starred items when entities exist', async () => { |
| 85 | + (useStarredEntities as jest.Mock).mockReturnValue({ |
| 86 | + starredEntities: new Set(['component:default/my-entity']), |
| 87 | + toggleStarredEntity: jest.fn(), |
| 88 | + isStarredEntity: jest.fn(), |
| 89 | + }); |
| 90 | + |
| 91 | + await renderInTestApp(<StarredDropdown />); |
| 92 | + expect(screen.getByText(/Your starred items/i)).toBeInTheDocument(); |
| 93 | + }); |
| 94 | + |
| 95 | + it('calls handleOpen when dropdown button is clicked', async () => { |
| 96 | + const handleOpen = jest.fn(); |
| 97 | + (useStarredEntities as jest.Mock).mockReturnValue({ |
| 98 | + starredEntities: new Set(['component:default/my-entity']), |
| 99 | + toggleStarredEntity: jest.fn(), |
| 100 | + isStarredEntity: jest.fn(), |
| 101 | + }); |
| 102 | + |
| 103 | + (useDropdownManager as jest.Mock).mockReturnValue({ |
| 104 | + anchorEl: null, |
| 105 | + handleOpen, |
| 106 | + handleClose: jest.fn(), |
| 107 | + }); |
| 108 | + |
| 109 | + await renderInTestApp(<StarredDropdown />); |
| 110 | + fireEvent.click(screen.getByRole('button')); |
| 111 | + expect(handleOpen).toHaveBeenCalled(); |
| 112 | + }); |
| 113 | +}); |
0 commit comments