|
| 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 type { ReactNode } from 'react'; |
| 17 | + |
| 18 | +import { render, screen } from '@testing-library/react'; |
| 19 | +import { MemoryRouter } from 'react-router-dom'; |
| 20 | +import { ThemeProvider, createTheme } from '@mui/material/styles'; |
| 21 | + |
| 22 | +import { EntitySection } from './EntitySection'; |
| 23 | +import { useEntities } from '../../hooks/useEntities'; |
| 24 | + |
| 25 | +jest.mock('../../hooks/useEntities'); |
| 26 | +const mockUseEntities = useEntities as jest.MockedFunction<typeof useEntities>; |
| 27 | + |
| 28 | +jest.mock('@backstage/plugin-user-settings', () => ({ |
| 29 | + useUserProfile: () => ({ |
| 30 | + displayName: 'Test User', |
| 31 | + loading: false, |
| 32 | + }), |
| 33 | +})); |
| 34 | + |
| 35 | +jest.mock('@backstage/plugin-catalog-react', () => ({ |
| 36 | + EntityRefLink: ({ children }: { children: ReactNode }) => ( |
| 37 | + <span>{children}</span> |
| 38 | + ), |
| 39 | +})); |
| 40 | + |
| 41 | +jest.mock('@backstage/core-components', () => ({ |
| 42 | + CodeSnippet: ({ text }: { text: string }) => <pre>{text}</pre>, |
| 43 | + WarningPanel: ({ children }: { children: ReactNode }) => ( |
| 44 | + <div>{children}</div> |
| 45 | + ), |
| 46 | + Link: ({ children, to }: { children: ReactNode; to: string }) => ( |
| 47 | + <a href={to}>{children}</a> |
| 48 | + ), |
| 49 | + MarkdownContent: ({ content }: { content: string }) => <span>{content}</span>, |
| 50 | +})); |
| 51 | + |
| 52 | +jest.mock('../../hooks/useTranslation', () => ({ |
| 53 | + useTranslation: () => ({ |
| 54 | + t: (key: string) => key, |
| 55 | + }), |
| 56 | +})); |
| 57 | + |
| 58 | +jest.mock('../Trans', () => ({ |
| 59 | + Trans: ({ message }: { message: string }) => <span>{message}</span>, |
| 60 | +})); |
| 61 | + |
| 62 | +jest.mock('./ViewMoreLink', () => ({ |
| 63 | + ViewMoreLink: ({ children }: { children: ReactNode }) => ( |
| 64 | + <span>{children}</span> |
| 65 | + ), |
| 66 | +})); |
| 67 | + |
| 68 | +jest.mock('../../utils/utils', () => ({ |
| 69 | + hasEntityIllustrationUserDismissed: () => true, |
| 70 | + addDismissedEntityIllustrationUsers: jest.fn(), |
| 71 | +})); |
| 72 | + |
| 73 | +jest.mock('../../images/homepage-entities-1.svg', () => 'mock-image.svg'); |
| 74 | + |
| 75 | +const theme = createTheme(); |
| 76 | + |
| 77 | +const renderComponent = () => |
| 78 | + render( |
| 79 | + <ThemeProvider theme={theme}> |
| 80 | + <MemoryRouter> |
| 81 | + <EntitySection /> |
| 82 | + </MemoryRouter> |
| 83 | + </ThemeProvider>, |
| 84 | + ); |
| 85 | + |
| 86 | +describe('<EntitySection />', () => { |
| 87 | + beforeEach(() => { |
| 88 | + jest.clearAllMocks(); |
| 89 | + }); |
| 90 | + |
| 91 | + it('displays entity title when metadata.title is set', () => { |
| 92 | + mockUseEntities.mockReturnValue({ |
| 93 | + data: { |
| 94 | + items: [ |
| 95 | + { |
| 96 | + apiVersion: 'backstage.io/v1alpha1', |
| 97 | + kind: 'Component', |
| 98 | + metadata: { |
| 99 | + name: 'my-component-name', |
| 100 | + title: 'My Component Title', |
| 101 | + namespace: 'default', |
| 102 | + }, |
| 103 | + spec: {}, |
| 104 | + }, |
| 105 | + ], |
| 106 | + totalItems: 1, |
| 107 | + pageInfo: {}, |
| 108 | + }, |
| 109 | + isLoading: false, |
| 110 | + error: undefined, |
| 111 | + }); |
| 112 | + |
| 113 | + renderComponent(); |
| 114 | + |
| 115 | + expect(screen.getByText('My Component Title')).toBeInTheDocument(); |
| 116 | + expect(screen.queryByText('my-component-name')).not.toBeInTheDocument(); |
| 117 | + }); |
| 118 | + |
| 119 | + it('displays entity name when metadata.title is not set', () => { |
| 120 | + mockUseEntities.mockReturnValue({ |
| 121 | + data: { |
| 122 | + items: [ |
| 123 | + { |
| 124 | + apiVersion: 'backstage.io/v1alpha1', |
| 125 | + kind: 'Component', |
| 126 | + metadata: { |
| 127 | + name: 'my-component-name', |
| 128 | + namespace: 'default', |
| 129 | + }, |
| 130 | + spec: {}, |
| 131 | + }, |
| 132 | + ], |
| 133 | + totalItems: 1, |
| 134 | + pageInfo: {}, |
| 135 | + }, |
| 136 | + isLoading: false, |
| 137 | + error: undefined, |
| 138 | + }); |
| 139 | + |
| 140 | + renderComponent(); |
| 141 | + |
| 142 | + expect(screen.getByText('my-component-name')).toBeInTheDocument(); |
| 143 | + }); |
| 144 | + |
| 145 | + it('displays title for entities with title and name for entities without', () => { |
| 146 | + mockUseEntities.mockReturnValue({ |
| 147 | + data: { |
| 148 | + items: [ |
| 149 | + { |
| 150 | + apiVersion: 'backstage.io/v1alpha1', |
| 151 | + kind: 'Component', |
| 152 | + metadata: { |
| 153 | + name: 'component-with-title', |
| 154 | + title: 'Component With Title', |
| 155 | + namespace: 'default', |
| 156 | + }, |
| 157 | + spec: {}, |
| 158 | + }, |
| 159 | + { |
| 160 | + apiVersion: 'backstage.io/v1alpha1', |
| 161 | + kind: 'API', |
| 162 | + metadata: { |
| 163 | + name: 'api-without-title', |
| 164 | + namespace: 'default', |
| 165 | + }, |
| 166 | + spec: {}, |
| 167 | + }, |
| 168 | + ], |
| 169 | + totalItems: 2, |
| 170 | + pageInfo: {}, |
| 171 | + }, |
| 172 | + isLoading: false, |
| 173 | + error: undefined, |
| 174 | + }); |
| 175 | + |
| 176 | + renderComponent(); |
| 177 | + |
| 178 | + expect(screen.getByText('Component With Title')).toBeInTheDocument(); |
| 179 | + expect(screen.queryByText('component-with-title')).not.toBeInTheDocument(); |
| 180 | + expect(screen.getByText('api-without-title')).toBeInTheDocument(); |
| 181 | + }); |
| 182 | + |
| 183 | + it('shows loading state', () => { |
| 184 | + mockUseEntities.mockReturnValue({ |
| 185 | + data: undefined, |
| 186 | + isLoading: true, |
| 187 | + error: undefined, |
| 188 | + }); |
| 189 | + |
| 190 | + renderComponent(); |
| 191 | + |
| 192 | + expect(screen.getByRole('progressbar')).toBeInTheDocument(); |
| 193 | + }); |
| 194 | +}); |
0 commit comments