Simplify OwnerEntityColumn to use EntityRefLink directly
Remove the catalog API lookup and manual title resolution. The EntityRefLink already uses the entity presentation API to resolve display names, so the component only needs the entity ref string. This removes unused imports of useApi, useAsync, catalogApiRef, parseEntityRef, and UserEntity. Signed-off-by: Fredrik Adelöw <freben@spotify.com> Made-with: Cursor
This commit is contained in:
+21
-41
@@ -14,48 +14,15 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { Entity } from '@backstage/catalog-model';
|
||||
import {
|
||||
renderInTestApp,
|
||||
TestApiProvider,
|
||||
mockApis,
|
||||
} from '@backstage/test-utils';
|
||||
import { catalogApiRef, entityRouteRef } from '@backstage/plugin-catalog-react';
|
||||
import { catalogApiMock } from '@backstage/plugin-catalog-react/testUtils';
|
||||
import { renderInTestApp, TestApiProvider } from '@backstage/test-utils';
|
||||
import { entityRouteRef } from '@backstage/plugin-catalog-react';
|
||||
import { OwnerEntityColumn } from './OwnerEntityColumn';
|
||||
import { identityApiRef } from '@backstage/core-plugin-api';
|
||||
|
||||
describe('<OwnerEntityColumn />', () => {
|
||||
const catalogApi = catalogApiMock.mock();
|
||||
const identityApi = mockApis.identity();
|
||||
|
||||
it('should render the column with the user', async () => {
|
||||
const props = {
|
||||
entityRef: 'user:default/foo',
|
||||
};
|
||||
|
||||
const entity: Entity = {
|
||||
apiVersion: 'v1',
|
||||
kind: 'User',
|
||||
metadata: {
|
||||
name: 'test',
|
||||
},
|
||||
spec: {
|
||||
profile: {
|
||||
displayName: 'BackUser',
|
||||
},
|
||||
},
|
||||
};
|
||||
catalogApi.getEntityByRef.mockResolvedValue(entity);
|
||||
|
||||
const { getByText, getByRole } = await renderInTestApp(
|
||||
<TestApiProvider
|
||||
apis={[
|
||||
[catalogApiRef, catalogApi],
|
||||
[identityApiRef, identityApi],
|
||||
]}
|
||||
>
|
||||
<OwnerEntityColumn {...props} />
|
||||
it('should render a link to the user entity', async () => {
|
||||
const { getByRole } = await renderInTestApp(
|
||||
<TestApiProvider apis={[]}>
|
||||
<OwnerEntityColumn entityRef="user:default/foo" />
|
||||
</TestApiProvider>,
|
||||
{
|
||||
mountedRoutes: {
|
||||
@@ -68,7 +35,20 @@ describe('<OwnerEntityColumn />', () => {
|
||||
'href',
|
||||
'/catalog/default/user/foo',
|
||||
);
|
||||
const text = getByText('BackUser');
|
||||
expect(text).toBeDefined();
|
||||
});
|
||||
|
||||
it('should render Unknown when entityRef is missing', async () => {
|
||||
const { getByText } = await renderInTestApp(
|
||||
<TestApiProvider apis={[]}>
|
||||
<OwnerEntityColumn />
|
||||
</TestApiProvider>,
|
||||
{
|
||||
mountedRoutes: {
|
||||
'/catalog/:namespace/:kind/:name': entityRouteRef,
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
expect(getByText('Unknown')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -13,37 +13,13 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
import { useApi } from '@backstage/core-plugin-api';
|
||||
|
||||
import useAsync from 'react-use/esm/useAsync';
|
||||
|
||||
import { catalogApiRef, EntityRefLink } from '@backstage/plugin-catalog-react';
|
||||
import { parseEntityRef, UserEntity } from '@backstage/catalog-model';
|
||||
import { EntityRefLink } from '@backstage/plugin-catalog-react';
|
||||
import Typography from '@material-ui/core/Typography';
|
||||
|
||||
export const OwnerEntityColumn = ({ entityRef }: { entityRef?: string }) => {
|
||||
const catalogApi = useApi(catalogApiRef);
|
||||
|
||||
const { value, loading, error } = useAsync(
|
||||
() => catalogApi.getEntityByRef(entityRef || ''),
|
||||
[catalogApi, entityRef],
|
||||
);
|
||||
|
||||
if (!entityRef) {
|
||||
return <Typography paragraph>Unknown</Typography>;
|
||||
return <Typography>Unknown</Typography>;
|
||||
}
|
||||
|
||||
if (loading || error) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return (
|
||||
<EntityRefLink
|
||||
entityRef={parseEntityRef(entityRef)}
|
||||
title={
|
||||
(value as UserEntity)?.spec?.profile?.displayName ??
|
||||
value?.metadata.name
|
||||
}
|
||||
/>
|
||||
);
|
||||
return <EntityRefLink entityRef={entityRef} />;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user