use the catalog mock in more tests

Signed-off-by: Fredrik Adelöw <freben@gmail.com>
This commit is contained in:
Fredrik Adelöw
2024-10-07 14:17:34 +02:00
parent 3459f9390d
commit 033c957321
65 changed files with 525 additions and 799 deletions
@@ -15,7 +15,6 @@
*/
import {
CatalogApi,
GetEntitiesByRefsRequest,
GetEntitiesRequest,
} from '@backstage/catalog-client';
@@ -25,6 +24,7 @@ import {
GroupEntity,
stringifyEntityRef,
} from '@backstage/catalog-model';
import { catalogApiMock } from '@backstage/plugin-catalog-react/testUtils';
export const groupA: GroupEntity = {
apiVersion: 'backstage.io/v1alpha1',
@@ -308,7 +308,7 @@ const mockedMembersMapping = new Map<string, Entity[]>([
type Nullable<T> = T | undefined;
export const mockedCatalogApiSupportingGroups: Partial<CatalogApi> = {
export const mockedCatalogApiSupportingGroups = catalogApiMock.mock({
getEntities: async (request?: GetEntitiesRequest) => {
const actualFilter = (request?.filter as Nullable<{
'relations.memberof': string[];
@@ -331,4 +331,4 @@ export const mockedCatalogApiSupportingGroups: Partial<CatalogApi> = {
);
return { items };
},
};
});
@@ -14,9 +14,8 @@
* limitations under the License.
*/
import { Entity, GroupEntity } from '@backstage/catalog-model';
import { GroupEntity } from '@backstage/catalog-model';
import {
CatalogApi,
catalogApiRef,
EntityProvider,
entityRouteRef,
@@ -35,6 +34,7 @@ import { EntityLayout, catalogPlugin } from '@backstage/plugin-catalog';
import { screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { Observable } from '@backstage/types';
import { catalogApiMock } from '@backstage/plugin-catalog-react/testUtils';
const mockedStarredEntitiesApi: Partial<StarredEntitiesApi> = {
starredEntitie$: () => {
@@ -68,38 +68,36 @@ describe('MemberTab Test', () => {
},
};
const catalogApi: Partial<CatalogApi> = {
getEntities: () =>
Promise.resolve({
items: [
{
apiVersion: 'backstage.io/v1alpha1',
kind: 'User',
metadata: {
name: 'tara.macgovern',
namespace: 'foo-bar',
uid: 'a5gerth56',
description: 'Super Awesome Developer',
},
relations: [
{
type: 'memberOf',
targetRef: 'group:default/team-d',
},
],
spec: {
profile: {
displayName: 'Tara MacGovern',
email: 'tara-macgovern@example.com',
picture: 'https://example.com/staff/tara.jpeg',
},
memberOf: ['team-d'],
},
const catalogApi = catalogApiMock.mock({
getEntities: async () => ({
items: [
{
apiVersion: 'backstage.io/v1alpha1',
kind: 'User',
metadata: {
name: 'tara.macgovern',
namespace: 'foo-bar',
uid: 'a5gerth56',
description: 'Super Awesome Developer',
},
] as Entity[],
}),
};
const getEntitiesSpy = jest.spyOn(catalogApi, 'getEntities');
relations: [
{
type: 'memberOf',
targetRef: 'group:default/team-d',
},
],
spec: {
profile: {
displayName: 'Tara MacGovern',
email: 'tara-macgovern@example.com',
picture: 'https://example.com/staff/tara.jpeg',
},
memberOf: ['team-d'],
},
},
],
}),
});
it('Display Profile Card', async () => {
await renderInTestApp(
@@ -116,7 +114,7 @@ describe('MemberTab Test', () => {
},
},
);
expect(getEntitiesSpy).toHaveBeenCalledWith({
expect(catalogApi.getEntities).toHaveBeenCalledWith({
filter: {
kind: 'User',
'relations.memberof': ['group:default/team-d'],
@@ -171,7 +169,7 @@ describe('MemberTab Test', () => {
},
);
expect(getEntitiesSpy).toHaveBeenCalledWith({
expect(catalogApi.getEntities).toHaveBeenCalledWith({
filter: {
kind: 'User',
'relations.leaderof': ['group:default/team-d'],
@@ -19,16 +19,13 @@ import {
GetEntitiesResponse,
} from '@backstage/catalog-client';
import { Entity, GroupEntity, UserEntity } from '@backstage/catalog-model';
import {
CatalogApi,
catalogApiRef,
EntityProvider,
} from '@backstage/plugin-catalog-react';
import { catalogApiRef, EntityProvider } from '@backstage/plugin-catalog-react';
import { renderInTestApp, TestApiProvider } from '@backstage/test-utils';
import { queryByText } from '@testing-library/react';
import React from 'react';
import { catalogIndexRouteRef } from '../../../routes';
import { OwnershipCard } from './OwnershipCard';
import { catalogApiMock } from '@backstage/plugin-catalog-react/testUtils';
const items = [
{
@@ -161,11 +158,7 @@ describe('OwnershipCard', () => {
};
it('displays entity counts', async () => {
const catalogApi: jest.Mocked<CatalogApi> = {
getEntities: jest.fn(),
} as any;
catalogApi.getEntities.mockImplementation(getEntitiesMock);
const catalogApi = catalogApiMock.mock({ getEntities: getEntitiesMock });
const { getByText } = await renderInTestApp(
<TestApiProvider apis={[[catalogApiRef, catalogApi]]}>
@@ -215,11 +208,7 @@ describe('OwnershipCard', () => {
});
it('applies CustomFilterDefinition', async () => {
const catalogApi: jest.Mocked<CatalogApi> = {
getEntities: jest.fn(),
} as any;
catalogApi.getEntities.mockImplementation(getEntitiesMock);
const catalogApi = catalogApiMock.mock({ getEntities: getEntitiesMock });
const { getByText } = await renderInTestApp(
<TestApiProvider apis={[[catalogApiRef, catalogApi]]}>
@@ -252,11 +241,7 @@ describe('OwnershipCard', () => {
});
it('links to the catalog with the group filter', async () => {
const catalogApi: jest.Mocked<CatalogApi> = {
getEntities: jest.fn(),
} as any;
catalogApi.getEntities.mockImplementation(getEntitiesMock);
const catalogApi = catalogApiMock.mock({ getEntities: getEntitiesMock });
const { getByText } = await renderInTestApp(
<TestApiProvider apis={[[catalogApiRef, catalogApi]]}>
@@ -279,11 +264,7 @@ describe('OwnershipCard', () => {
});
it('links to the catalog with the user and groups filters from an user profile', async () => {
const catalogApi: jest.Mocked<CatalogApi> = {
getEntities: jest.fn(),
} as any;
catalogApi.getEntities.mockImplementation(getEntitiesMock);
const catalogApi = catalogApiMock.mock({ getEntities: getEntitiesMock });
const { getByText } = await renderInTestApp(
<TestApiProvider apis={[[catalogApiRef, catalogApi]]}>
@@ -308,11 +289,7 @@ describe('OwnershipCard', () => {
describe('OwnershipCard relations', () => {
it('shows relations toggle', async () => {
const catalogApi: jest.Mocked<CatalogApi> = {
getEntities: jest.fn(),
} as any;
catalogApi.getEntities.mockImplementation(getEntitiesMock);
const catalogApi = catalogApiMock.mock({ getEntities: getEntitiesMock });
const { getByTitle } = await renderInTestApp(
<TestApiProvider apis={[[catalogApiRef, catalogApi]]}>
@@ -331,11 +308,7 @@ describe('OwnershipCard', () => {
});
it('hides relations toggle', async () => {
const catalogApi: jest.Mocked<CatalogApi> = {
getEntities: jest.fn(),
} as any;
catalogApi.getEntities.mockImplementation(getEntitiesMock);
const catalogApi = catalogApiMock.mock({ getEntities: getEntitiesMock });
const rendered = await renderInTestApp(
<TestApiProvider apis={[[catalogApiRef, catalogApi]]}>
@@ -352,12 +325,9 @@ describe('OwnershipCard', () => {
expect(rendered.queryByText('Direct Relations')).toBeNull();
});
it('overrides relation type', async () => {
const catalogApi: jest.Mocked<CatalogApi> = {
getEntities: jest.fn(),
} as any;
catalogApi.getEntities.mockImplementation(getEntitiesMock);
it('overrides relation type', async () => {
const catalogApi = catalogApiMock.mock({ getEntities: getEntitiesMock });
const { getByTitle } = await renderInTestApp(
<TestApiProvider apis={[[catalogApiRef, catalogApi]]}>
@@ -376,11 +346,7 @@ describe('OwnershipCard', () => {
});
it('defaults to aggregated for User entity kind', async () => {
const catalogApi: jest.Mocked<CatalogApi> = {
getEntities: jest.fn(),
} as any;
catalogApi.getEntities.mockImplementation(getEntitiesMock);
const catalogApi = catalogApiMock.mock({ getEntities: getEntitiesMock });
const { getByLabelText } = await renderInTestApp(
<TestApiProvider apis={[[catalogApiRef, catalogApi]]}>
@@ -399,11 +365,7 @@ describe('OwnershipCard', () => {
});
it('defaults to direct for all entity kinds except User', async () => {
const catalogApi: jest.Mocked<CatalogApi> = {
getEntities: jest.fn(),
} as any;
catalogApi.getEntities.mockImplementation(getEntitiesMock);
const catalogApi = catalogApiMock.mock({ getEntities: getEntitiesMock });
const { getByLabelText } = await renderInTestApp(
<TestApiProvider apis={[[catalogApiRef, catalogApi]]}>
@@ -422,11 +384,7 @@ describe('OwnershipCard', () => {
});
it('defaults to provided relationsType', async () => {
const catalogApi: jest.Mocked<CatalogApi> = {
getEntities: jest.fn(),
} as any;
catalogApi.getEntities.mockImplementation(getEntitiesMock);
const catalogApi = catalogApiMock.mock({ getEntities: getEntitiesMock });
const { getByLabelText } = await renderInTestApp(
<TestApiProvider apis={[[catalogApiRef, catalogApi]]}>
@@ -19,9 +19,9 @@ import React from 'react';
import { MyGroupsSidebarItem } from './MyGroupsSidebarItem';
import GroupIcon from '@material-ui/icons/People';
import { IdentityApi, identityApiRef } from '@backstage/core-plugin-api';
import { CatalogApi } from '@backstage/catalog-client';
import { Entity } from '@backstage/catalog-model';
import { catalogApiRef, entityRouteRef } from '@backstage/plugin-catalog-react';
import { catalogApiMock } from '@backstage/plugin-catalog-react/testUtils';
describe('MyGroupsSidebarItem Test', () => {
describe('For guests or users with no groups', () => {
@@ -33,12 +33,7 @@ describe('MyGroupsSidebarItem Test', () => {
ownershipEntityRefs: ['user:default/guest'],
}),
};
const catalogApi: Partial<CatalogApi> = {
getEntities: () =>
Promise.resolve({
items: [] as Entity[],
}),
};
const catalogApi = catalogApiMock();
const rendered = await renderInTestApp(
<TestApiProvider
apis={[
@@ -71,26 +66,25 @@ describe('MyGroupsSidebarItem Test', () => {
ownershipEntityRefs: ['user:default/nigel.manning'],
}),
};
const catalogApi: Partial<CatalogApi> = {
getEntities: () =>
Promise.resolve({
items: [
{
apiVersion: 'backstage.io/v1alpha1',
kind: 'Group',
metadata: {
name: 'team-a',
title: 'Team A',
namespace: 'default',
},
spec: {
type: 'team',
children: [],
},
const catalogApi = catalogApiMock.mock({
getEntities: async () => ({
items: [
{
apiVersion: 'backstage.io/v1alpha1',
kind: 'Group',
metadata: {
name: 'team-a',
title: 'Team A',
namespace: 'default',
},
] as Entity[],
}),
};
spec: {
type: 'team',
children: [],
},
},
] as Entity[],
}),
});
const rendered = await renderInTestApp(
<TestApiProvider
apis={[
@@ -127,52 +121,51 @@ describe('MyGroupsSidebarItem Test', () => {
ownershipEntityRefs: ['user:default/nigel.manning'],
}),
};
const catalogApi: Partial<CatalogApi> = {
getEntities: () =>
Promise.resolve({
items: [
{
apiVersion: 'backstage.io/v1alpha1',
kind: 'Group',
metadata: {
name: 'team-a',
title: 'Team A',
namespace: 'default',
},
spec: {
type: 'team',
children: [],
},
const catalogApi = catalogApiMock.mock({
getEntities: async () => ({
items: [
{
apiVersion: 'backstage.io/v1alpha1',
kind: 'Group',
metadata: {
name: 'team-a',
title: 'Team A',
namespace: 'default',
},
{
apiVersion: 'backstage.io/v1alpha1',
kind: 'Group',
metadata: {
name: 'team-b',
title: 'Team B',
namespace: 'default',
},
spec: {
type: 'team',
children: [],
},
spec: {
type: 'team',
children: [],
},
{
apiVersion: 'backstage.io/v1alpha1',
kind: 'Group',
metadata: {
name: 'team-c',
title: 'Team C',
namespace: 'default',
},
spec: {
type: 'team',
children: [],
},
},
{
apiVersion: 'backstage.io/v1alpha1',
kind: 'Group',
metadata: {
name: 'team-b',
title: 'Team B',
namespace: 'default',
},
] as Entity[],
}),
};
spec: {
type: 'team',
children: [],
},
},
{
apiVersion: 'backstage.io/v1alpha1',
kind: 'Group',
metadata: {
name: 'team-c',
title: 'Team C',
namespace: 'default',
},
spec: {
type: 'team',
children: [],
},
},
] as Entity[],
}),
});
const rendered = await renderInTestApp(
<TestApiProvider
apis={[
@@ -206,9 +199,7 @@ describe('MyGroupsSidebarItem Test', () => {
ownershipEntityRefs: ['user:default/guest'],
}),
};
const mockCatalogApi: Partial<CatalogApi> = {
getEntities: jest.fn(),
};
const mockCatalogApi = catalogApiMock.mock();
await renderInTestApp(
<TestApiProvider
apis={[
@@ -249,9 +240,7 @@ describe('MyGroupsSidebarItem Test', () => {
ownershipEntityRefs: ['user:default/guest'],
}),
};
const mockCatalogApi: Partial<CatalogApi> = {
getEntities: jest.fn(),
};
const mockCatalogApi = catalogApiMock.mock();
await renderInTestApp(
<TestApiProvider
apis={[
+3 -4
View File
@@ -14,7 +14,6 @@
* limitations under the License.
*/
import { CatalogApi } from '@backstage/catalog-client';
import {
getAllDesendantMembersForGroupEntity,
getMembersFromGroups,
@@ -42,7 +41,7 @@ import {
describe('Helper functions', () => {
it('getAllDesendantMembersForGroupEntity correctly recursively returns all descendant members', async () => {
const catalogApi = mockedCatalogApiSupportingGroups as CatalogApi;
const catalogApi = mockedCatalogApiSupportingGroups;
const actualGroupADescendantMembers =
await getAllDesendantMembersForGroupEntity(groupA, catalogApi);
@@ -60,7 +59,7 @@ describe('Helper functions', () => {
});
it('getMembersFromGroups correctly returns all members of provided groups', async () => {
const catalogApi = mockedCatalogApiSupportingGroups as CatalogApi;
const catalogApi = mockedCatalogApiSupportingGroups;
const actualNoGroupsMembers = await getMembersFromGroups([], catalogApi);
const actualGroupAMembers = await getMembersFromGroups(
@@ -123,7 +122,7 @@ describe('Helper functions', () => {
it('getDescendantGroupsFromGroup correctly recursively returns descendant groups, ignoring duplicates', async () => {
const actualDescendantGroups = await getDescendantGroupsFromGroup(
groupA,
mockedCatalogApiSupportingGroups as CatalogApi,
mockedCatalogApiSupportingGroups,
);
expect(actualDescendantGroups).toStrictEqual([
groupBRef,