diff --git a/.changeset/fluffy-candles-learn.md b/.changeset/fluffy-candles-learn.md new file mode 100644 index 0000000000..f3ed109071 --- /dev/null +++ b/.changeset/fluffy-candles-learn.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-catalog': patch +--- + +Updates the `ìsComponentType` to allow an array of possible types diff --git a/plugins/catalog/api-report.md b/plugins/catalog/api-report.md index 556016941a..e95a8a638c 100644 --- a/plugins/catalog/api-report.md +++ b/plugins/catalog/api-report.md @@ -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; diff --git a/plugins/catalog/src/components/EntitySwitch/conditions.test.ts b/plugins/catalog/src/components/EntitySwitch/conditions.test.ts new file mode 100644 index 0000000000..05df2d6bc5 --- /dev/null +++ b/plugins/catalog/src/components/EntitySwitch/conditions.test.ts @@ -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(); + }); +}); diff --git a/plugins/catalog/src/components/EntitySwitch/conditions.ts b/plugins/catalog/src/components/EntitySwitch/conditions.ts index 907aa45afe..314c05d14b 100644 --- a/plugins/catalog/src/components/EntitySwitch/conditions.ts +++ b/plugins/catalog/src/components/EntitySwitch/conditions.ts @@ -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)); }; }