catalog-backend: fix conditional decisions for properties of type array

Signed-off-by: Vincenzo Scamporlino <vincenzos@spotify.com>
This commit is contained in:
Vincenzo Scamporlino
2022-07-28 16:46:29 +02:00
parent c7dd03bf96
commit a679bbcc4a
2 changed files with 73 additions and 0 deletions
@@ -46,6 +46,22 @@ describe('createPropertyRule', () => {
).toBe(false);
});
it('returns false when specified key is an empty array', () => {
expect(
apply(
{
apiVersion: 'backstage.io/v1alpha1',
kind: 'Component',
metadata: {
name: 'test-component',
tags: [],
},
},
'tags',
),
).toBe(false);
});
it('returns true when specified key is present', () => {
expect(
apply(
@@ -63,6 +79,22 @@ describe('createPropertyRule', () => {
),
).toBe(true);
});
it('returns true when specified key is an array containing more than an element', () => {
expect(
apply(
{
apiVersion: 'backstage.io/v1alpha1',
kind: 'Component',
metadata: {
name: 'test-component',
tags: ['java'],
},
},
'tags',
),
).toBe(true);
});
});
describe('key and value', () => {
@@ -101,6 +133,23 @@ describe('createPropertyRule', () => {
).toBe(false);
});
it(`returns false when key is an array and doesn't contain the specified value`, () => {
expect(
apply(
{
apiVersion: 'backstage.io/v1alpha1',
kind: 'Component',
metadata: {
name: 'test-component',
tags: ['java'],
},
},
'tags',
'python',
),
).toBe(false);
});
it('returns true when specified key and value is present', () => {
expect(
apply(
@@ -119,6 +168,23 @@ describe('createPropertyRule', () => {
),
).toBe(true);
});
it(`returns true when key is an array and contains the specified value`, () => {
expect(
apply(
{
apiVersion: 'backstage.io/v1alpha1',
kind: 'Component',
metadata: {
name: 'test-component',
tags: ['java', 'java11'],
},
},
'tags',
'java',
),
).toBe(true);
});
});
});
@@ -26,6 +26,13 @@ export const createPropertyRule = (propertyType: 'metadata' | 'spec') =>
resourceType: RESOURCE_TYPE_CATALOG_ENTITY,
apply: (resource: Entity, key: string, value?: string) => {
const foundValue = get(resource[propertyType], key);
if (Array.isArray(foundValue)) {
if (value !== undefined) {
return foundValue.includes(value);
}
return foundValue.length > 0;
}
if (value !== undefined) {
return value === foundValue;
}