From ec12bbe64b5060a87f521bf2f81baf5174ed4aad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Tunkl?= Date: Thu, 8 Sep 2022 13:04:03 +0200 Subject: [PATCH] Implemented new tests for humanize update. Fix of logic MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Tomáš Tunkl --- .../components/EntityRefLink/humanize.test.ts | 80 ++++++++++++++++++- .../src/components/EntityRefLink/humanize.ts | 18 ++++- 2 files changed, 92 insertions(+), 6 deletions(-) diff --git a/plugins/catalog-react/src/components/EntityRefLink/humanize.test.ts b/plugins/catalog-react/src/components/EntityRefLink/humanize.test.ts index 9f08290095..80ce2100f6 100644 --- a/plugins/catalog-react/src/components/EntityRefLink/humanize.test.ts +++ b/plugins/catalog-react/src/components/EntityRefLink/humanize.test.ts @@ -47,7 +47,7 @@ describe('humanizeEntityRef', () => { lifecycle: 'production', }, }; - const title = humanizeEntityRef(entity, { skipDefaultNamespace: false }); + const title = humanizeEntityRef(entity, { defaultNamespace: false }); expect(title).toEqual('component:default/software'); }); @@ -69,6 +69,24 @@ describe('humanizeEntityRef', () => { expect(title).toEqual('component:test/software'); }); + it('formats entity in other namespace and hides this namespace', () => { + const entity = { + apiVersion: 'v1', + kind: 'Component', + metadata: { + name: 'software', + namespace: 'test', + }, + spec: { + owner: 'guest', + type: 'service', + lifecycle: 'production', + }, + }; + const title = humanizeEntityRef(entity, { defaultNamespace: 'test' }); + expect(title).toEqual('component:software'); + }); + it('formats entity and hides default kind', () => { const entity = { apiVersion: 'v1', @@ -87,6 +105,27 @@ describe('humanizeEntityRef', () => { expect(title).toEqual('test/software'); }); + it('formats entity and hides default kind and hiding namespace', () => { + const entity = { + apiVersion: 'v1', + kind: 'Component', + metadata: { + name: 'software', + namespace: 'test', + }, + spec: { + owner: 'guest', + type: 'service', + lifecycle: 'production', + }, + }; + const title = humanizeEntityRef(entity, { + defaultKind: 'Component', + defaultNamespace: 'test', + }); + expect(title).toEqual('software'); + }); + it('formats entity name in default namespace', () => { const entityName = { kind: 'Component', @@ -97,6 +136,16 @@ describe('humanizeEntityRef', () => { expect(title).toEqual('component:software'); }); + it('formats entity name in default namespace and does not skip default namespace', () => { + const entityName = { + kind: 'Component', + namespace: 'default', + name: 'software', + }; + const title = humanizeEntityRef(entityName, { defaultNamespace: false }); + expect(title).toEqual('component:default/software'); + }); + it('formats entity name in other namespace', () => { const entityName = { kind: 'Component', @@ -108,6 +157,19 @@ describe('humanizeEntityRef', () => { expect(title).toEqual('component:test/software'); }); + it('formats entity name in other namespace with skipping this namespace', () => { + const entityName = { + kind: 'Component', + namespace: 'test', + name: 'software', + }; + + const title = humanizeEntityRef(entityName, { + defaultNamespace: 'test', + }); + expect(title).toEqual('component:software'); + }); + it('renders link for entity name and hides default kind', () => { const entityName = { kind: 'Component', @@ -121,6 +183,20 @@ describe('humanizeEntityRef', () => { expect(title).toEqual('test/software'); }); + it('renders link for entity name and hides default kind with skipping namespace', () => { + const entityName = { + kind: 'Component', + namespace: 'test', + name: 'software', + }; + + const title = humanizeEntityRef(entityName, { + defaultKind: 'component', + defaultNamespace: 'test', + }); + expect(title).toEqual('software'); + }); + it('formats entity name in default namespace without skip of default namespace', () => { const entityName = { kind: 'Component', @@ -129,7 +205,7 @@ describe('humanizeEntityRef', () => { }; const title = humanizeEntityRef(entityName, { - skipDefaultNamespace: false, + defaultNamespace: false, }); expect(title).toEqual('component:default/software'); }); diff --git a/plugins/catalog-react/src/components/EntityRefLink/humanize.ts b/plugins/catalog-react/src/components/EntityRefLink/humanize.ts index bd97ce2548..536b9a3a6e 100644 --- a/plugins/catalog-react/src/components/EntityRefLink/humanize.ts +++ b/plugins/catalog-react/src/components/EntityRefLink/humanize.ts @@ -20,16 +20,19 @@ import { DEFAULT_NAMESPACE, } from '@backstage/catalog-model'; -/** @public */ +/** + * @property defaultNamespace - if set to false then namespace is never ommited, + * if set to string which matches namespace of entity then omited + * + * @public */ export function humanizeEntityRef( entityRef: Entity | CompoundEntityRef, opts?: { defaultKind?: string; - skipDefaultNamespace?: boolean; + defaultNamespace?: string | boolean; }, ) { const defaultKind = opts?.defaultKind; - const skipDefaultNamespace = opts?.skipDefaultNamespace ?? true; let kind; let namespace; let name; @@ -44,7 +47,14 @@ export function humanizeEntityRef( name = entityRef.name; } - if (skipDefaultNamespace === true && namespace === DEFAULT_NAMESPACE) { + if (namespace === undefined || namespace === '') { + namespace = DEFAULT_NAMESPACE; + } + if (opts?.defaultNamespace !== undefined) { + if (opts?.defaultNamespace === namespace) { + namespace = undefined; + } + } else if (namespace === DEFAULT_NAMESPACE) { namespace = undefined; }