From da156f2831888a0a474f99899b11d7792b54b05d Mon Sep 17 00:00:00 2001 From: Jan Vilimek Date: Sat, 11 Dec 2021 12:46:17 +0100 Subject: [PATCH 01/17] let's support also entities without spec.type Signed-off-by: Jan Vilimek --- .../src/components/Cards/OwnershipCard/OwnershipCard.tsx | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/plugins/org/src/components/Cards/OwnershipCard/OwnershipCard.tsx b/plugins/org/src/components/Cards/OwnershipCard/OwnershipCard.tsx index fe23191779..091e8037ca 100644 --- a/plugins/org/src/components/Cards/OwnershipCard/OwnershipCard.tsx +++ b/plugins/org/src/components/Cards/OwnershipCard/OwnershipCard.tsx @@ -162,17 +162,15 @@ export const OwnershipCard = ({ const counts = ownedEntitiesList.reduce( (acc: EntityTypeProps[], ownedEntity) => { - if (typeof ownedEntity.spec?.type !== 'string') return acc; - const match = acc.find( - x => x.kind === ownedEntity.kind && x.type === ownedEntity.spec?.type, + x => x.kind === ownedEntity.kind && x.type === (ownedEntity.spec?.type ?? ownedEntity.kind), ); if (match) { match.count += 1; } else { acc.push({ kind: ownedEntity.kind, - type: ownedEntity.spec?.type, + type: ownedEntity.spec?.type?.toString() ?? ownedEntity.kind, count: 1, }); } From 26b04d2341739d65dc8145e8e0b3b08617757867 Mon Sep 17 00:00:00 2001 From: Jan Vilimek Date: Sat, 11 Dec 2021 12:48:14 +0100 Subject: [PATCH 02/17] New arg entityFilterKind for ownershipcard Signed-off-by: Jan Vilimek --- .../org/src/components/Cards/OwnershipCard/OwnershipCard.tsx | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/plugins/org/src/components/Cards/OwnershipCard/OwnershipCard.tsx b/plugins/org/src/components/Cards/OwnershipCard/OwnershipCard.tsx index 091e8037ca..56e87a26f6 100644 --- a/plugins/org/src/components/Cards/OwnershipCard/OwnershipCard.tsx +++ b/plugins/org/src/components/Cards/OwnershipCard/OwnershipCard.tsx @@ -128,10 +128,12 @@ const getQueryParams = ( export const OwnershipCard = ({ variant, + entityFilterKind }: { /** @deprecated The entity is now grabbed from context instead */ entity?: Entity; variant?: InfoCardVariants; + entityFilterKind?: string[]; }) => { const { entity } = useEntity(); const catalogApi = useApi(catalogApiRef); @@ -142,7 +144,7 @@ export const OwnershipCard = ({ error, value: componentsWithCounters, } = useAsync(async () => { - const kinds = ['Component', 'API']; + const kinds = entityFilterKind ?? ['Component', 'API']; const entitiesList = await catalogApi.getEntities({ filter: { kind: kinds, From 455da34c98ce6fc60e414ce4aec80ce1a3ac9cf6 Mon Sep 17 00:00:00 2001 From: Jan Vilimek Date: Sat, 11 Dec 2021 12:49:39 +0100 Subject: [PATCH 03/17] jest tests for ownershipCard improved Signed-off-by: Jan Vilimek --- .../OwnershipCard/OwnershipCard.test.tsx | 152 ++++++++++++++++++ 1 file changed, 152 insertions(+) diff --git a/plugins/org/src/components/Cards/OwnershipCard/OwnershipCard.test.tsx b/plugins/org/src/components/Cards/OwnershipCard/OwnershipCard.test.tsx index 8d6fc1a002..e5bf36ba8e 100644 --- a/plugins/org/src/components/Cards/OwnershipCard/OwnershipCard.test.tsx +++ b/plugins/org/src/components/Cards/OwnershipCard/OwnershipCard.test.tsx @@ -108,6 +108,22 @@ describe('OwnershipCard', () => { }, ], }, + { + kind: 'system', + metadata: { + name: 'my-systen', + }, + relations: [ + { + type: 'ownedBy', + target: { + name: 'my-team', + namespace: 'default', + kind: 'Group', + }, + }, + ], + }, ] as any; it('displays entity counts', async () => { @@ -144,6 +160,7 @@ describe('OwnershipCard', () => { expect( queryByText(getByText('LIBRARY').parentElement!, '1'), ).toBeInTheDocument(); + expect(getByText('SYSTEM')).not.toBeInTheDocument(); }); it('links to the catalog with the group filter', async () => { @@ -222,3 +239,138 @@ describe('OwnershipCard', () => { ); }); }); + +describe('OwnershipCardWithCustomFilterDefinition', () => { + const groupEntity: GroupEntity = { + apiVersion: 'backstage.io/v1alpha1', + kind: 'Group', + metadata: { + name: 'my-team', + }, + spec: { + type: 'team', + children: [], + }, + relations: [ + { + type: 'memberOf', + target: { + kind: 'group', + name: 'ExampleGroup', + namespace: 'default', + }, + }, + ], + }; + + const items = [ + { + kind: 'API', + metadata: { + name: 'my-api', + }, + spec: { + type: 'openapi', + }, + relations: [ + { + type: 'ownedBy', + target: { + name: 'my-team', + namespace: 'default', + kind: 'Group', + }, + }, + ], + }, + { + kind: 'Component', + metadata: { + name: 'my-service', + }, + spec: { + type: 'service', + }, + relations: [ + { + type: 'ownedBy', + target: { + name: 'my-team', + namespace: 'default', + kind: 'Group', + }, + }, + ], + }, + { + kind: 'Component', + metadata: { + name: 'my-library', + namespace: 'other-namespace', + }, + spec: { + type: 'library', + }, + relations: [ + { + type: 'ownedBy', + target: { + name: 'my-team', + namespace: 'default', + kind: 'Group', + }, + }, + ], + }, + { + kind: 'system', + metadata: { + name: 'my-systen', + }, + relations: [ + { + type: 'ownedBy', + target: { + name: 'my-team', + namespace: 'default', + kind: 'Group', + }, + }, + ], + }, + ] as any; + + it('displays entity counts', async () => { + const catalogApi: jest.Mocked = { + getEntities: jest.fn(), + } as any; + + catalogApi.getEntities.mockResolvedValue({ + items, + }); + + const { getByText } = await renderInTestApp( + + + + + , + { + mountedRoutes: { + '/create': catalogRouteRef, + }, + }, + ); + + expect(getByText('OPENAPI')).toBeInTheDocument(); + expect( + queryByText(getByText('OPENAPI').parentElement!, '1'), + ).toBeInTheDocument(); + expect(getByText('SERVICE')).not.toBeInTheDocument(); + expect(getByText('LIBRARY')).not.toBeInTheDocument(); + expect(getByText('SYSTEM')).toBeInTheDocument(); + expect( + queryByText(getByText('SYSTEM').parentElement!, '1'), + ).toBeInTheDocument(); + }); +}); From 5835456f6f52d784f778db4c919e40b6db1e645c Mon Sep 17 00:00:00 2001 From: Jan Vilimek Date: Sat, 11 Dec 2021 12:52:37 +0100 Subject: [PATCH 04/17] Example for entityFilterKind Signed-off-by: Jan Vilimek --- packages/app/src/components/catalog/EntityPage.tsx | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/app/src/components/catalog/EntityPage.tsx b/packages/app/src/components/catalog/EntityPage.tsx index 9810f49386..e6cb43efdc 100644 --- a/packages/app/src/components/catalog/EntityPage.tsx +++ b/packages/app/src/components/catalog/EntityPage.tsx @@ -126,6 +126,8 @@ import { } from '@roadiehq/backstage-plugin-travis-ci'; import React, { ReactNode, useMemo, useState } from 'react'; +const customEntityFilterKind = ['Component', 'API', 'System']; + const EntityLayoutWrapper = (props: { children?: ReactNode }) => { const [badgesDialogOpen, setBadgesDialogOpen] = useState(false); @@ -523,7 +525,7 @@ const userPage = ( - + @@ -539,7 +541,7 @@ const groupPage = ( - + From fc2924b23e4a96c768f4234104b173261cfc0d3d Mon Sep 17 00:00:00 2001 From: Jan Vilimek Date: Sat, 11 Dec 2021 12:58:53 +0100 Subject: [PATCH 05/17] org plugin changelog Signed-off-by: Jan Vilimek --- plugins/org/CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/plugins/org/CHANGELOG.md b/plugins/org/CHANGELOG.md index 0576d644da..e409087f1c 100644 --- a/plugins/org/CHANGELOG.md +++ b/plugins/org/CHANGELOG.md @@ -1,5 +1,11 @@ # @backstage/plugin-org +## 0.3.31 + +### Patch Changes + +- added `entityFilterKind` property for `EntityOwnershipCard` + ## 0.3.30 ### Patch Changes From 47755c2a60ffddcf94442fa3483f4c113b3eb176 Mon Sep 17 00:00:00 2001 From: Jan Vilimek Date: Sat, 11 Dec 2021 12:59:16 +0100 Subject: [PATCH 06/17] package.json 0.3.31 bump Signed-off-by: Jan Vilimek --- plugins/org/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/org/package.json b/plugins/org/package.json index 4e7f556547..3554b5657c 100644 --- a/plugins/org/package.json +++ b/plugins/org/package.json @@ -1,7 +1,7 @@ { "name": "@backstage/plugin-org", "description": "A Backstage plugin that helps you create entity pages for your organization", - "version": "0.3.30", + "version": "0.3.31", "main": "src/index.ts", "types": "src/index.ts", "license": "Apache-2.0", From fe86adbcd24282dfae4d4fcd9b2aeb8c4db8c390 Mon Sep 17 00:00:00 2001 From: Jan Vilimek Date: Sat, 11 Dec 2021 13:05:36 +0100 Subject: [PATCH 07/17] root changelog Signed-off-by: Jan Vilimek --- .changeset/org-ownershipcard-filteradded.md | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .changeset/org-ownershipcard-filteradded.md diff --git a/.changeset/org-ownershipcard-filteradded.md b/.changeset/org-ownershipcard-filteradded.md new file mode 100644 index 0000000000..8f6c1bc37d --- /dev/null +++ b/.changeset/org-ownershipcard-filteradded.md @@ -0,0 +1,6 @@ +--- +"example-app": patch +"@backstage/plugin-org": patch +--- + +Added `entityFilterKind` property for `EntityOwnershipCard` From 75c47f4ee6724348260f03e74c279639e446348a Mon Sep 17 00:00:00 2001 From: Jan Vilimek Date: Sat, 11 Dec 2021 13:13:37 +0100 Subject: [PATCH 08/17] revert plugin changelog Signed-off-by: Jan Vilimek --- plugins/org/CHANGELOG.md | 6 ------ 1 file changed, 6 deletions(-) diff --git a/plugins/org/CHANGELOG.md b/plugins/org/CHANGELOG.md index e409087f1c..0576d644da 100644 --- a/plugins/org/CHANGELOG.md +++ b/plugins/org/CHANGELOG.md @@ -1,11 +1,5 @@ # @backstage/plugin-org -## 0.3.31 - -### Patch Changes - -- added `entityFilterKind` property for `EntityOwnershipCard` - ## 0.3.30 ### Patch Changes From 2fd0010a152fb1b0610e4d8c7bc0511c05a210cf Mon Sep 17 00:00:00 2001 From: Jan Vilimek Date: Sat, 11 Dec 2021 13:13:58 +0100 Subject: [PATCH 09/17] revert package.json bump Signed-off-by: Jan Vilimek --- plugins/org/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/org/package.json b/plugins/org/package.json index 3554b5657c..4e7f556547 100644 --- a/plugins/org/package.json +++ b/plugins/org/package.json @@ -1,7 +1,7 @@ { "name": "@backstage/plugin-org", "description": "A Backstage plugin that helps you create entity pages for your organization", - "version": "0.3.31", + "version": "0.3.30", "main": "src/index.ts", "types": "src/index.ts", "license": "Apache-2.0", From 64dc7ed8c6568e29fb9e22c34febc2695b8172ca Mon Sep 17 00:00:00 2001 From: Jan Vilimek Date: Mon, 13 Dec 2021 11:11:20 +0100 Subject: [PATCH 10/17] ownershipcard: prettier Signed-off-by: Jan Vilimek --- .../components/Cards/OwnershipCard/OwnershipCard.test.tsx | 2 +- .../src/components/Cards/OwnershipCard/OwnershipCard.tsx | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/plugins/org/src/components/Cards/OwnershipCard/OwnershipCard.test.tsx b/plugins/org/src/components/Cards/OwnershipCard/OwnershipCard.test.tsx index e5bf36ba8e..e49720a9d4 100644 --- a/plugins/org/src/components/Cards/OwnershipCard/OwnershipCard.test.tsx +++ b/plugins/org/src/components/Cards/OwnershipCard/OwnershipCard.test.tsx @@ -352,7 +352,7 @@ describe('OwnershipCardWithCustomFilterDefinition', () => { const { getByText } = await renderInTestApp( - + , { diff --git a/plugins/org/src/components/Cards/OwnershipCard/OwnershipCard.tsx b/plugins/org/src/components/Cards/OwnershipCard/OwnershipCard.tsx index 56e87a26f6..1e2d514359 100644 --- a/plugins/org/src/components/Cards/OwnershipCard/OwnershipCard.tsx +++ b/plugins/org/src/components/Cards/OwnershipCard/OwnershipCard.tsx @@ -128,7 +128,7 @@ const getQueryParams = ( export const OwnershipCard = ({ variant, - entityFilterKind + entityFilterKind, }: { /** @deprecated The entity is now grabbed from context instead */ entity?: Entity; @@ -165,7 +165,9 @@ export const OwnershipCard = ({ const counts = ownedEntitiesList.reduce( (acc: EntityTypeProps[], ownedEntity) => { const match = acc.find( - x => x.kind === ownedEntity.kind && x.type === (ownedEntity.spec?.type ?? ownedEntity.kind), + x => + x.kind === ownedEntity.kind && + x.type === (ownedEntity.spec?.type ?? ownedEntity.kind), ); if (match) { match.count += 1; From 9ed3dfb4d5ff25c676c23a016a6901d812c4ee97 Mon Sep 17 00:00:00 2001 From: Jan Vilimek Date: Mon, 13 Dec 2021 11:11:42 +0100 Subject: [PATCH 11/17] changelog:prettier Signed-off-by: Jan Vilimek --- .changeset/org-ownershipcard-filteradded.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.changeset/org-ownershipcard-filteradded.md b/.changeset/org-ownershipcard-filteradded.md index 8f6c1bc37d..d95428ae02 100644 --- a/.changeset/org-ownershipcard-filteradded.md +++ b/.changeset/org-ownershipcard-filteradded.md @@ -1,6 +1,6 @@ --- -"example-app": patch -"@backstage/plugin-org": patch +'example-app': patch +'@backstage/plugin-org': patch --- Added `entityFilterKind` property for `EntityOwnershipCard` From 8390fff862d6206f6b38bb6ad34b55f3a471fb2b Mon Sep 17 00:00:00 2001 From: Jan Vilimek Date: Mon, 13 Dec 2021 11:11:56 +0100 Subject: [PATCH 12/17] entitypage: prettier Signed-off-by: Jan Vilimek --- packages/app/src/components/catalog/EntityPage.tsx | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/packages/app/src/components/catalog/EntityPage.tsx b/packages/app/src/components/catalog/EntityPage.tsx index e6cb43efdc..a7de64e89a 100644 --- a/packages/app/src/components/catalog/EntityPage.tsx +++ b/packages/app/src/components/catalog/EntityPage.tsx @@ -525,7 +525,10 @@ const userPage = ( - + @@ -541,7 +544,10 @@ const groupPage = ( - + From 9f5a08de13b0b8d925601b98e32bff49fd70b914 Mon Sep 17 00:00:00 2001 From: Jan Vilimek Date: Mon, 13 Dec 2021 13:29:33 +0100 Subject: [PATCH 13/17] updated api-report Signed-off-by: Jan Vilimek --- plugins/org/api-report.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/plugins/org/api-report.md b/plugins/org/api-report.md index 88ce00fd52..60caa45134 100644 --- a/plugins/org/api-report.md +++ b/plugins/org/api-report.md @@ -33,9 +33,11 @@ export const EntityMembersListCard: (_props: { // @public (undocumented) export const EntityOwnershipCard: ({ variant, + entityFilterKind, }: { entity?: Entity | undefined; variant?: InfoCardVariants | undefined; + entityFilterKind?: string[] | undefined; }) => JSX.Element; // Warning: (ae-missing-release-tag) "EntityUserProfileCard" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal) @@ -75,9 +77,11 @@ export { orgPlugin as plugin }; // @public (undocumented) export const OwnershipCard: ({ variant, + entityFilterKind, }: { entity?: Entity | undefined; variant?: InfoCardVariants | undefined; + entityFilterKind?: string[] | undefined; }) => JSX.Element; // Warning: (ae-missing-release-tag) "UserProfileCard" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal) From 0be6cb1111b401b8171fa3342c0c9a3a45140f6d Mon Sep 17 00:00:00 2001 From: Jan Vilimek Date: Mon, 13 Dec 2021 16:17:52 +0100 Subject: [PATCH 14/17] no bump for example-app Signed-off-by: Jan Vilimek --- .changeset/org-ownershipcard-filteradded.md | 1 - 1 file changed, 1 deletion(-) diff --git a/.changeset/org-ownershipcard-filteradded.md b/.changeset/org-ownershipcard-filteradded.md index d95428ae02..cbd1475bb1 100644 --- a/.changeset/org-ownershipcard-filteradded.md +++ b/.changeset/org-ownershipcard-filteradded.md @@ -1,5 +1,4 @@ --- -'example-app': patch '@backstage/plugin-org': patch --- From bc1e2f20bd6a0f3a95aa2a755da0946bd041c1d5 Mon Sep 17 00:00:00 2001 From: Jan Vilimek Date: Mon, 13 Dec 2021 17:36:09 +0100 Subject: [PATCH 15/17] unit test fixed Signed-off-by: Jan Vilimek --- .../OwnershipCard/OwnershipCard.test.tsx | 225 ++++++------------ 1 file changed, 71 insertions(+), 154 deletions(-) diff --git a/plugins/org/src/components/Cards/OwnershipCard/OwnershipCard.test.tsx b/plugins/org/src/components/Cards/OwnershipCard/OwnershipCard.test.tsx index e49720a9d4..912014092f 100644 --- a/plugins/org/src/components/Cards/OwnershipCard/OwnershipCard.test.tsx +++ b/plugins/org/src/components/Cards/OwnershipCard/OwnershipCard.test.tsx @@ -14,7 +14,7 @@ * limitations under the License. */ -import { GroupEntity, UserEntity } from '@backstage/catalog-model'; +import { Entity, GroupEntity, UserEntity } from '@backstage/catalog-model'; import { CatalogApi, catalogApiRef, @@ -51,6 +51,7 @@ describe('OwnershipCard', () => { const items = [ { + apiVersion: 'backstage.io/v1alpha1', kind: 'API', metadata: { name: 'my-api', @@ -108,23 +109,7 @@ describe('OwnershipCard', () => { }, ], }, - { - kind: 'system', - metadata: { - name: 'my-systen', - }, - relations: [ - { - type: 'ownedBy', - target: { - name: 'my-team', - namespace: 'default', - kind: 'Group', - }, - }, - ], - }, - ] as any; + ] as Entity[]; it('displays entity counts', async () => { const catalogApi: jest.Mocked = { @@ -148,6 +133,17 @@ describe('OwnershipCard', () => { }, ); + expect(catalogApi.getEntities).toHaveBeenCalledWith({ + filter: { kind: ['Component', 'API'] }, + fields: [ + 'kind', + 'metadata.name', + 'metadata.namespace', + 'spec.type', + 'relations', + ], + }); + expect(getByText('OPENAPI')).toBeInTheDocument(); expect( queryByText(getByText('OPENAPI').parentElement!, '1'), @@ -160,7 +156,63 @@ describe('OwnershipCard', () => { expect( queryByText(getByText('LIBRARY').parentElement!, '1'), ).toBeInTheDocument(); - expect(getByText('SYSTEM')).not.toBeInTheDocument(); + }); + + it('applies CustomFilterDefinition', async () => { + const catalogApi: jest.Mocked = { + getEntities: jest.fn(), + } as any; + + catalogApi.getEntities.mockResolvedValue({ + items: [ + { + apiVersion: 'backstage.io/v1alpha1', + kind: 'system', + metadata: { + name: 'my-systen', + }, + relations: [ + { + type: 'ownedBy', + target: { + name: 'my-team', + namespace: 'default', + kind: 'Group', + }, + }, + ], + }, + ], + }); + + const { getByText } = await renderInTestApp( + + + + + , + { + mountedRoutes: { + '/create': catalogRouteRef, + }, + }, + ); + + expect(catalogApi.getEntities).toHaveBeenCalledWith({ + filter: { kind: ['API', 'System'] }, + fields: [ + 'kind', + 'metadata.name', + 'metadata.namespace', + 'spec.type', + 'relations', + ], + }); + + expect(getByText('SYSTEM')).toBeInTheDocument(); + expect( + queryByText(getByText('SYSTEM').parentElement!, '1'), + ).toBeInTheDocument(); }); it('links to the catalog with the group filter', async () => { @@ -239,138 +291,3 @@ describe('OwnershipCard', () => { ); }); }); - -describe('OwnershipCardWithCustomFilterDefinition', () => { - const groupEntity: GroupEntity = { - apiVersion: 'backstage.io/v1alpha1', - kind: 'Group', - metadata: { - name: 'my-team', - }, - spec: { - type: 'team', - children: [], - }, - relations: [ - { - type: 'memberOf', - target: { - kind: 'group', - name: 'ExampleGroup', - namespace: 'default', - }, - }, - ], - }; - - const items = [ - { - kind: 'API', - metadata: { - name: 'my-api', - }, - spec: { - type: 'openapi', - }, - relations: [ - { - type: 'ownedBy', - target: { - name: 'my-team', - namespace: 'default', - kind: 'Group', - }, - }, - ], - }, - { - kind: 'Component', - metadata: { - name: 'my-service', - }, - spec: { - type: 'service', - }, - relations: [ - { - type: 'ownedBy', - target: { - name: 'my-team', - namespace: 'default', - kind: 'Group', - }, - }, - ], - }, - { - kind: 'Component', - metadata: { - name: 'my-library', - namespace: 'other-namespace', - }, - spec: { - type: 'library', - }, - relations: [ - { - type: 'ownedBy', - target: { - name: 'my-team', - namespace: 'default', - kind: 'Group', - }, - }, - ], - }, - { - kind: 'system', - metadata: { - name: 'my-systen', - }, - relations: [ - { - type: 'ownedBy', - target: { - name: 'my-team', - namespace: 'default', - kind: 'Group', - }, - }, - ], - }, - ] as any; - - it('displays entity counts', async () => { - const catalogApi: jest.Mocked = { - getEntities: jest.fn(), - } as any; - - catalogApi.getEntities.mockResolvedValue({ - items, - }); - - const { getByText } = await renderInTestApp( - - - - - , - { - mountedRoutes: { - '/create': catalogRouteRef, - }, - }, - ); - - expect(getByText('OPENAPI')).toBeInTheDocument(); - expect( - queryByText(getByText('OPENAPI').parentElement!, '1'), - ).toBeInTheDocument(); - expect(getByText('SERVICE')).not.toBeInTheDocument(); - expect(getByText('LIBRARY')).not.toBeInTheDocument(); - expect(getByText('SYSTEM')).toBeInTheDocument(); - expect( - queryByText(getByText('SYSTEM').parentElement!, '1'), - ).toBeInTheDocument(); - }); -}); From 60abfd536aaf070d4249f1c383a3719abb8f45ea Mon Sep 17 00:00:00 2001 From: Jan Vilimek Date: Tue, 14 Dec 2021 19:04:51 +0100 Subject: [PATCH 16/17] Improved unit tests Signed-off-by: Jan Vilimek --- plugins/org/package.json | 1 + .../OwnershipCard/OwnershipCard.test.tsx | 208 +++++++++--------- 2 files changed, 106 insertions(+), 103 deletions(-) diff --git a/plugins/org/package.json b/plugins/org/package.json index 4e7f556547..8df865f386 100644 --- a/plugins/org/package.json +++ b/plugins/org/package.json @@ -40,6 +40,7 @@ "devDependencies": { "@backstage/cli": "^0.10.1", "@backstage/core-app-api": "^0.2.0", + "@backstage/catalog-client": "^0.5.2", "@backstage/dev-utils": "^0.2.14", "@backstage/test-utils": "^0.1.24", "@testing-library/jest-dom": "^5.10.1", diff --git a/plugins/org/src/components/Cards/OwnershipCard/OwnershipCard.test.tsx b/plugins/org/src/components/Cards/OwnershipCard/OwnershipCard.test.tsx index 912014092f..c4710c14ab 100644 --- a/plugins/org/src/components/Cards/OwnershipCard/OwnershipCard.test.tsx +++ b/plugins/org/src/components/Cards/OwnershipCard/OwnershipCard.test.tsx @@ -14,6 +14,10 @@ * limitations under the License. */ +import { + CatalogEntitiesRequest, + CatalogListResponse, +} from '@backstage/catalog-client'; import { Entity, GroupEntity, UserEntity } from '@backstage/catalog-model'; import { CatalogApi, @@ -26,6 +30,97 @@ import { queryByText } from '@testing-library/react'; import React from 'react'; import { OwnershipCard } from './OwnershipCard'; +const items = [ + { + apiVersion: 'backstage.io/v1alpha1', + kind: 'API', + metadata: { + name: 'my-api', + }, + spec: { + type: 'openapi', + }, + relations: [ + { + type: 'ownedBy', + target: { + name: 'my-team', + namespace: 'default', + kind: 'Group', + }, + }, + ], + }, + { + kind: 'Component', + metadata: { + name: 'my-service', + }, + spec: { + type: 'service', + }, + relations: [ + { + type: 'ownedBy', + target: { + name: 'my-team', + namespace: 'default', + kind: 'Group', + }, + }, + ], + }, + { + kind: 'Component', + metadata: { + name: 'my-library', + namespace: 'other-namespace', + }, + spec: { + type: 'library', + }, + relations: [ + { + type: 'ownedBy', + target: { + name: 'my-team', + namespace: 'default', + kind: 'Group', + }, + }, + ], + }, + { + apiVersion: 'backstage.io/v1alpha1', + kind: 'System', + metadata: { + name: 'my-system', + }, + relations: [ + { + type: 'ownedBy', + target: { + name: 'my-team', + namespace: 'default', + kind: 'Group', + }, + }, + ], + }, +] as Entity[]; + +const getEntitiesMock = ( + request?: CatalogEntitiesRequest, +): Promise> => { + const filterKinds = + !Array.isArray(request?.filter) && Array.isArray(request?.filter?.kind) + ? request?.filter?.kind ?? [] + : []; // we expect the request to be like { filter: { kind: ['API','System'], .... } + return Promise.resolve({ + items: items.filter(item => filterKinds.find(k => k === item.kind)), + } as CatalogListResponse); +}; + describe('OwnershipCard', () => { const groupEntity: GroupEntity = { apiVersion: 'backstage.io/v1alpha1', @@ -49,76 +144,12 @@ describe('OwnershipCard', () => { ], }; - const items = [ - { - apiVersion: 'backstage.io/v1alpha1', - kind: 'API', - metadata: { - name: 'my-api', - }, - spec: { - type: 'openapi', - }, - relations: [ - { - type: 'ownedBy', - target: { - name: 'my-team', - namespace: 'default', - kind: 'Group', - }, - }, - ], - }, - { - kind: 'Component', - metadata: { - name: 'my-service', - }, - spec: { - type: 'service', - }, - relations: [ - { - type: 'ownedBy', - target: { - name: 'my-team', - namespace: 'default', - kind: 'Group', - }, - }, - ], - }, - { - kind: 'Component', - metadata: { - name: 'my-library', - namespace: 'other-namespace', - }, - spec: { - type: 'library', - }, - relations: [ - { - type: 'ownedBy', - target: { - name: 'my-team', - namespace: 'default', - kind: 'Group', - }, - }, - ], - }, - ] as Entity[]; - it('displays entity counts', async () => { const catalogApi: jest.Mocked = { getEntities: jest.fn(), } as any; - catalogApi.getEntities.mockResolvedValue({ - items, - }); + catalogApi.getEntities.mockImplementation(getEntitiesMock); const { getByText } = await renderInTestApp( @@ -156,6 +187,7 @@ describe('OwnershipCard', () => { expect( queryByText(getByText('LIBRARY').parentElement!, '1'), ).toBeInTheDocument(); + expect(() => getByText('SYSTEM')).toThrowError(); }); it('applies CustomFilterDefinition', async () => { @@ -163,27 +195,7 @@ describe('OwnershipCard', () => { getEntities: jest.fn(), } as any; - catalogApi.getEntities.mockResolvedValue({ - items: [ - { - apiVersion: 'backstage.io/v1alpha1', - kind: 'system', - metadata: { - name: 'my-systen', - }, - relations: [ - { - type: 'ownedBy', - target: { - name: 'my-team', - namespace: 'default', - kind: 'Group', - }, - }, - ], - }, - ], - }); + catalogApi.getEntities.mockImplementation(getEntitiesMock); const { getByText } = await renderInTestApp( @@ -198,21 +210,15 @@ describe('OwnershipCard', () => { }, ); - expect(catalogApi.getEntities).toHaveBeenCalledWith({ - filter: { kind: ['API', 'System'] }, - fields: [ - 'kind', - 'metadata.name', - 'metadata.namespace', - 'spec.type', - 'relations', - ], - }); - expect(getByText('SYSTEM')).toBeInTheDocument(); expect( queryByText(getByText('SYSTEM').parentElement!, '1'), ).toBeInTheDocument(); + expect(getByText('OPENAPI')).toBeInTheDocument(); + expect( + queryByText(getByText('OPENAPI').parentElement!, '1'), + ).toBeInTheDocument(); + expect(() => getByText('LIBRARY')).toThrowError(); }); it('links to the catalog with the group filter', async () => { @@ -220,9 +226,7 @@ describe('OwnershipCard', () => { getEntities: jest.fn(), } as any; - catalogApi.getEntities.mockResolvedValue({ - items, - }); + catalogApi.getEntities.mockImplementation(getEntitiesMock); const { getByText } = await renderInTestApp( @@ -268,9 +272,7 @@ describe('OwnershipCard', () => { getEntities: jest.fn(), } as any; - catalogApi.getEntities.mockResolvedValue({ - items, - }); + catalogApi.getEntities.mockImplementation(getEntitiesMock); const { getByText } = await renderInTestApp( From 95284ba57210aed75cf223bf96fc1bacc8b35e04 Mon Sep 17 00:00:00 2001 From: Jan Vilimek Date: Wed, 15 Dec 2021 09:45:15 +0100 Subject: [PATCH 17/17] improved tests / comment Signed-off-by: Jan Vilimek --- .../src/components/Cards/OwnershipCard/OwnershipCard.test.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/org/src/components/Cards/OwnershipCard/OwnershipCard.test.tsx b/plugins/org/src/components/Cards/OwnershipCard/OwnershipCard.test.tsx index c4710c14ab..439365e583 100644 --- a/plugins/org/src/components/Cards/OwnershipCard/OwnershipCard.test.tsx +++ b/plugins/org/src/components/Cards/OwnershipCard/OwnershipCard.test.tsx @@ -115,7 +115,7 @@ const getEntitiesMock = ( const filterKinds = !Array.isArray(request?.filter) && Array.isArray(request?.filter?.kind) ? request?.filter?.kind ?? [] - : []; // we expect the request to be like { filter: { kind: ['API','System'], .... } + : []; // we expect the request to be like { filter: { kind: ['API','System'], .... }. If changed in OwnerShipCard, let's change in also here return Promise.resolve({ items: items.filter(item => filterKinds.find(k => k === item.kind)), } as CatalogListResponse);