Implemented new tests for humanize update. Fix of logic

Signed-off-by: Tomáš Tunkl <tomas.tunkl@avast.com>
This commit is contained in:
Tomáš Tunkl
2022-09-08 13:04:03 +02:00
parent 727e66aeb6
commit ec12bbe64b
2 changed files with 92 additions and 6 deletions
@@ -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');
});
@@ -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;
}