allow multiple component types

Signed-off-by: Juan Pablo Garcia Ripa <sarabadu@gmail.com>
This commit is contained in:
Juan Pablo Garcia Ripa
2022-05-17 10:48:18 +02:00
parent 361cebfe1b
commit 449dcef98e
4 changed files with 71 additions and 3 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-catalog': patch
---
Updates the `ìsComponentType` to allow an array of possible types
+3 -1
View File
@@ -371,7 +371,9 @@ export interface HasSystemsCardProps {
}
// @public
export function isComponentType(type: string): (entity: Entity) => boolean;
export function isComponentType(
types: string | string[],
): (entity: Entity) => boolean;
// @public
export function isKind(kind: string): (entity: Entity) => boolean;
@@ -0,0 +1,59 @@
/*
* Copyright 2022 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Entity } from '@backstage/catalog-model';
import { isComponentType } from '.';
const serviceComponent: Entity = {
apiVersion: '',
kind: 'component',
metadata: { name: 'aService' },
spec: { type: 'service' },
};
const websiteComponent: Entity = {
apiVersion: '',
kind: 'component',
metadata: { name: 'aService' },
spec: { type: 'website' },
};
const notComponent: Entity = {
apiVersion: '',
kind: 'not-component',
metadata: { name: 'aService' },
spec: { type: 'service' },
};
describe('isComponentType', () => {
it('should false on non component kinds', () => {
const checkEntity = isComponentType('service');
expect(checkEntity(notComponent)).not.toBeTruthy();
});
it('should check for the intended type', () => {
const checkEntity = isComponentType('service');
expect(checkEntity(websiteComponent)).not.toBeTruthy();
expect(checkEntity(serviceComponent)).toBeTruthy();
});
it('should check for multiple types', () => {
const checkEntity = isComponentType(['service', 'website']);
expect(checkEntity(serviceComponent)).toBeTruthy();
expect(checkEntity(websiteComponent)).toBeTruthy();
});
});
@@ -34,13 +34,15 @@ export function isKind(kind: string) {
* For use in EntitySwitch.Case. Matches if the entity is a Component of a given spec.type.
* @public
*/
export function isComponentType(type: string) {
export function isComponentType(types: string | string[]) {
return (entity: Entity) => {
if (!strCmp(entity.kind, 'component')) {
return false;
}
const componentEntity = entity as ComponentEntity;
return strCmp(componentEntity.spec.type, type);
return typeof types === 'string'
? strCmp(componentEntity.spec.type, types)
: types.some(type => strCmp(componentEntity.spec.type, type));
};
}