address comments

Signed-off-by: Fredrik Adelöw <freben@gmail.com>
This commit is contained in:
Fredrik Adelöw
2023-08-18 13:25:19 +02:00
parent 1e5b7d993a
commit e5ce16aa44
6 changed files with 49 additions and 42 deletions
+1 -1
View File
@@ -8,7 +8,7 @@ iconography etc) are represented in the user interface.
Usage of this API is initially added to the `EntityRefLink` and `EntityRefLinks`
components, so that they can render richer, more correct representation of
entity refs. There's also a new `EntityName` component, which works just like
entity refs. There's also a new `EntityDisplayName` component, which works just like
the `EntityRefLink` but without the link.
Along with that change, the `fetchEntities` and `getTitle` props of
@@ -85,9 +85,9 @@ export function defaultEntityPresentation(
const shortRef = getShortRef({ kind, namespace, name, context });
const primary = [displayName, title, shortRef].filter(
const primary = [displayName, title, shortRef].find(
candidate => candidate && typeof candidate === 'string',
)[0]!;
)!;
const secondary = [
primary !== entityRef ? entityRef : undefined,
@@ -134,35 +134,35 @@ function getParts(entityOrRef: Entity | CompoundEntityRef | string): {
}
if (typeof entityOrRef === 'object' && entityOrRef !== null) {
const kind = [get(entityOrRef, 'kind')].filter(
const kind = [get(entityOrRef, 'kind')].find(
candidate => candidate && typeof candidate === 'string',
)[0];
);
const namespace = [
get(entityOrRef, 'metadata.namespace'),
get(entityOrRef, 'namespace'),
].filter(candidate => candidate && typeof candidate === 'string')[0];
].find(candidate => candidate && typeof candidate === 'string');
const name = [
get(entityOrRef, 'metadata.name'),
get(entityOrRef, 'name'),
].filter(candidate => candidate && typeof candidate === 'string')[0];
].find(candidate => candidate && typeof candidate === 'string');
const title = [get(entityOrRef, 'metadata.title')].filter(
const title = [get(entityOrRef, 'metadata.title')].find(
candidate => candidate && typeof candidate === 'string',
)[0];
);
const description = [get(entityOrRef, 'metadata.description')].filter(
const description = [get(entityOrRef, 'metadata.description')].find(
candidate => candidate && typeof candidate === 'string',
)[0];
);
const displayName = [get(entityOrRef, 'spec.profile.displayName')].filter(
const displayName = [get(entityOrRef, 'spec.profile.displayName')].find(
candidate => candidate && typeof candidate === 'string',
)[0];
);
const type = [get(entityOrRef, 'spec.type')].filter(
const type = [get(entityOrRef, 'spec.type')].find(
candidate => candidate && typeof candidate === 'string',
)[0];
);
return { kind, namespace, name, title, description, displayName, type };
}
@@ -177,23 +177,27 @@ function getShortRef(options: {
context?: { defaultKind?: string; defaultNamespace?: string };
}): string {
const kind = options.kind?.toLocaleLowerCase('en-US') || 'unknown';
const namespace =
options.namespace?.toLocaleLowerCase('en-US') || DEFAULT_NAMESPACE;
const name = options.name?.toLocaleLowerCase('en-US') || 'unknown';
const defaultKind = options.context?.defaultKind?.toLocaleLowerCase('en-US');
const defaultNamespace =
const namespace = options.namespace || DEFAULT_NAMESPACE;
const name = options.name || 'unknown';
const defaultKindLower =
options.context?.defaultKind?.toLocaleLowerCase('en-US');
const defaultNamespaceLower =
options.context?.defaultNamespace?.toLocaleLowerCase('en-US');
let result = name;
if (
(defaultNamespace && namespace !== defaultNamespace) ||
(defaultNamespaceLower &&
namespace.toLocaleLowerCase('en-US') !== defaultNamespaceLower) ||
namespace !== DEFAULT_NAMESPACE
) {
result = `${namespace}/${result}`;
}
if (defaultKind && kind !== defaultKind) {
if (
defaultKindLower &&
kind.toLocaleLowerCase('en-US') !== defaultKindLower
) {
result = `${kind}:${result}`;
}
@@ -40,7 +40,7 @@ describe('<EntityRefLink />', () => {
'/catalog/:namespace/:kind/:name/*': entityRouteRef,
},
});
expect(screen.getByText('component:software').closest('a')).toHaveAttribute(
expect(screen.getByText('software').closest('a')).toHaveAttribute(
'href',
'/catalog/default/component/software',
);
@@ -65,9 +65,10 @@ describe('<EntityRefLink />', () => {
'/catalog/:namespace/:kind/:name/*': entityRouteRef,
},
});
expect(
screen.getByText('component:test/software').closest('a'),
).toHaveAttribute('href', '/catalog/test/component/software');
expect(screen.getByText('test/software').closest('a')).toHaveAttribute(
'href',
'/catalog/test/component/software',
);
});
it('renders link for entity and hides default kind', async () => {
@@ -109,7 +110,7 @@ describe('<EntityRefLink />', () => {
'/catalog/:namespace/:kind/:name/*': entityRouteRef,
},
});
expect(screen.getByText('component:software').closest('a')).toHaveAttribute(
expect(screen.getByText('software').closest('a')).toHaveAttribute(
'href',
'/catalog/default/component/software',
);
@@ -126,9 +127,10 @@ describe('<EntityRefLink />', () => {
'/catalog/:namespace/:kind/:name/*': entityRouteRef,
},
});
expect(
screen.getByText('component:test/software').closest('a'),
).toHaveAttribute('href', '/catalog/test/component/software');
expect(screen.getByText('test/software').closest('a')).toHaveAttribute(
'href',
'/catalog/test/component/software',
);
});
it('renders link for entity name and hides default kind', async () => {
@@ -285,8 +285,8 @@ describe('UnregisterEntityDialog', () => {
expect(
screen.getByText(/will unregister the following entities/),
).toBeInTheDocument();
expect(screen.getByText(/k1:ns1\/n1/)).toBeInTheDocument();
expect(screen.getByText(/k2:ns2\/n2/)).toBeInTheDocument();
expect(screen.getByText(/ns1\/n1/)).toBeInTheDocument();
expect(screen.getByText(/ns2\/n2/)).toBeInTheDocument();
});
await userEvent.click(screen.getByText('Unregister Location'));
@@ -333,8 +333,8 @@ describe('UnregisterEntityDialog', () => {
expect(
screen.getByText(/will unregister the following entities/),
).toBeInTheDocument();
expect(screen.getByText(/k1:ns1\/n1/)).toBeInTheDocument();
expect(screen.getByText(/k2:ns2\/n2/)).toBeInTheDocument();
expect(screen.getByText(/ns1\/n1/)).toBeInTheDocument();
expect(screen.getByText(/ns2\/n2/)).toBeInTheDocument();
});
await userEvent.click(screen.getByText('Advanced Options'));
@@ -89,7 +89,7 @@ describe('TemplateCard', () => {
expect(description.querySelector('strong')).toBeInTheDocument();
});
it('should render no descroption if none is provided through the template', async () => {
it('should render no description if none is provided through the template', async () => {
const mockTemplate: TemplateEntityV1beta3 = {
apiVersion: 'scaffolder.backstage.io/v1beta3',
kind: 'Template',
@@ -186,11 +186,12 @@ describe('TemplateCard', () => {
},
);
expect(getByRole('link', { name: 'my-test-user' })).toBeInTheDocument();
expect(getByRole('link', { name: 'my-test-user' })).toHaveAttribute(
'href',
'/catalog/group/default/my-test-user',
);
expect(
getByRole('link', { name: 'group:default/my-test-user' }),
).toBeInTheDocument();
expect(
getByRole('link', { name: 'group:default/my-test-user' }),
).toHaveAttribute('href', '/catalog/group/default/my-test-user');
});
it('should call the onSelected handler when clicking the choose button', async () => {
@@ -45,7 +45,7 @@ describe('<UserSettingsIdentityCard />', () => {
},
);
expect(screen.getByText('user:test-ownership')).toBeInTheDocument();
expect(screen.getByText('foo:bar/foobar')).toBeInTheDocument();
expect(screen.getByText('test-ownership')).toBeInTheDocument();
expect(screen.getByText('bar/foobar')).toBeInTheDocument();
});
});