Bring back queryParameterKind effect.

Signed-off-by: Gustaf Lundh <gustaf.lundh@axis.com>
This commit is contained in:
Gustaf Lundh
2022-11-30 15:48:09 +01:00
parent f79395c16c
commit 54b5c430c5
3 changed files with 53 additions and 1 deletions
@@ -44,6 +44,14 @@ function useEntityKindFilter(opts: { initialFilter: string }): {
queryParamKind ?? filters.kind?.value ?? opts.initialFilter,
);
// Set selected kinds on query parameter updates; this happens at initial page load and from
// external updates to the page location.
useEffect(() => {
if (queryParamKind) {
setSelectedKind(queryParamKind);
}
}, [queryParamKind]);
// Set selected kind from filters; this happens when the kind filter is
// updated from another component
useEffect(() => {
@@ -15,7 +15,7 @@
*/
import React from 'react';
import { fireEvent, screen } from '@testing-library/react';
import { fireEvent, screen, waitFor } from '@testing-library/react';
import { GetEntityFacetsResponse } from '@backstage/catalog-client';
import { Entity } from '@backstage/catalog-model';
import {
@@ -137,6 +137,42 @@ describe('<CatalogKindHeader />', () => {
});
});
it('responds to external queryParameters changes', async () => {
const updateFilters = jest.fn();
const rendered = await renderWithEffects(
<ApiProvider apis={apis}>
<MockEntityListContextProvider
value={{
updateFilters,
queryParameters: { kind: ['components'] },
}}
>
<CatalogKindHeader />
</MockEntityListContextProvider>
</ApiProvider>,
);
expect(updateFilters).toHaveBeenLastCalledWith({
kind: new EntityKindFilter('components'),
});
rendered.rerender(
<ApiProvider apis={apis}>
<MockEntityListContextProvider
value={{
updateFilters,
queryParameters: { kind: ['template'] },
}}
>
<CatalogKindHeader />
</MockEntityListContextProvider>
</ApiProvider>,
);
await waitFor(() =>
expect(updateFilters).toHaveBeenLastCalledWith({
kind: new EntityKindFilter('template'),
})
);
});
it('limits kinds when allowedKinds is set', async () => {
await renderWithEffects(
<ApiProvider apis={apis}>
@@ -76,6 +76,14 @@ export function CatalogKindHeader(props: CatalogKindHeaderProps) {
queryParamKind ?? filters.kind?.value ?? initialFilter,
);
// Set selected kinds on query parameter updates; this happens at initial page load and from
// external updates to the page location.
useEffect(() => {
if (queryParamKind) {
setSelectedKind(queryParamKind);
}
}, [queryParamKind]);
// Set selected kind from filters; this happens when the kind filter is
// updated from another component
useEffect(() => {