Merge pull request #11768 from kielosz/my-groups-filter

Add filter property to MyGroupsSidebarItem
This commit is contained in:
Johan Haals
2022-06-02 15:14:09 +02:00
committed by GitHub
4 changed files with 124 additions and 2 deletions
+26
View File
@@ -0,0 +1,26 @@
---
'@backstage/plugin-org': patch
---
Added the ability to use an additional `filter` when fetching groups in `MyGroupsSidebarItem` component. Example:
```diff
// app/src/components/Root/Root.tsx
<SidebarPage>
<Sidebar>
//...
<SidebarGroup label="Menu" icon={<MenuIcon />}>
{/* Global nav, not org-specific */}
//...
<SidebarItem icon={HomeIcon} to="catalog" text="Home" />
<MyGroupsSidebarItem
singularTitle="My Squad"
pluralTitle="My Squads"
icon={GroupIcon}
+ filter={{ 'spec.type': 'team' }}
/>
//...
</SidebarGroup>
</ Sidebar>
</SidebarPage>
```
+1
View File
@@ -50,6 +50,7 @@ export const MyGroupsSidebarItem: (props: {
singularTitle: string;
pluralTitle: string;
icon: IconComponent;
filter?: Record<string, string | symbol | (string | symbol)[]>;
}) => JSX.Element | null;
// @public (undocumented)
@@ -196,4 +196,92 @@ describe('MyGroupsSidebarItem Test', () => {
expect(rendered.getByLabelText('My Squads')).toBeInTheDocument();
});
});
describe('When an additional filter is not provided', () => {
it('catalogApi.getEntities() should be called with the default filter', async () => {
const identityApi: Partial<IdentityApi> = {
getBackstageIdentity: async () => ({
type: 'user',
userEntityRef: 'user:default/guest',
ownershipEntityRefs: ['user:default/guest'],
}),
};
const mockCatalogApi: Partial<CatalogApi> = {
getEntities: jest.fn(),
};
await renderInTestApp(
<TestApiProvider
apis={[
[identityApiRef, identityApi],
[catalogApiRef, mockCatalogApi],
]}
>
<MyGroupsSidebarItem
singularTitle="My Squad"
pluralTitle="My Squads"
icon={GroupIcon}
/>
</TestApiProvider>,
{
mountedRoutes: {
'/catalog/:namespace/:kind/:name': entityRouteRef,
},
},
);
expect(mockCatalogApi.getEntities).toHaveBeenCalledWith({
filter: [
{
kind: 'group',
'relations.hasMember': 'user:default/guest',
},
],
fields: ['metadata', 'kind'],
});
});
});
describe('When an additional filter is provided', () => {
it('catalogApi.getEntities() should be called with an additional filter item', async () => {
const identityApi: Partial<IdentityApi> = {
getBackstageIdentity: async () => ({
type: 'user',
userEntityRef: 'user:default/guest',
ownershipEntityRefs: ['user:default/guest'],
}),
};
const mockCatalogApi: Partial<CatalogApi> = {
getEntities: jest.fn(),
};
await renderInTestApp(
<TestApiProvider
apis={[
[identityApiRef, identityApi],
[catalogApiRef, mockCatalogApi],
]}
>
<MyGroupsSidebarItem
singularTitle="My Squad"
pluralTitle="My Squads"
icon={GroupIcon}
filter={{ 'spec.type': 'team' }}
/>
</TestApiProvider>,
{
mountedRoutes: {
'/catalog/:namespace/:kind/:name': entityRouteRef,
},
},
);
expect(mockCatalogApi.getEntities).toHaveBeenCalledWith({
filter: [
{
kind: 'group',
'relations.hasMember': 'user:default/guest',
'spec.type': 'team',
},
],
fields: ['metadata', 'kind'],
});
});
});
});
@@ -45,8 +45,9 @@ export const MyGroupsSidebarItem = (props: {
singularTitle: string;
pluralTitle: string;
icon: IconComponent;
filter?: Record<string, string | symbol | (string | symbol)[]>;
}) => {
const { singularTitle, pluralTitle, icon } = props;
const { singularTitle, pluralTitle, icon, filter } = props;
const identityApi = useApi(identityApiRef);
const catalogApi: CatalogApi = useApi(catalogApiRef);
@@ -56,7 +57,13 @@ export const MyGroupsSidebarItem = (props: {
const profile = await identityApi.getBackstageIdentity();
const response = await catalogApi.getEntities({
filter: [{ kind: 'group', 'relations.hasMember': profile.userEntityRef }],
filter: [
{
kind: 'group',
'relations.hasMember': profile.userEntityRef,
...(filter ?? {}),
},
],
fields: ['metadata', 'kind'],
});