catalog-react: add useAllEntitiesCount tests

Signed-off-by: Vincenzo Scamporlino <vincenzos@spotify.com>
This commit is contained in:
Vincenzo Scamporlino
2023-06-20 13:58:05 +02:00
parent ead3cb82f6
commit ecf4b77d50
@@ -0,0 +1,106 @@
/*
* Copyright 2023 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 React, { PropsWithChildren } from 'react';
import { CatalogApi } from '@backstage/catalog-client';
import { useAllEntitiesCount } from './useAllEntitiesCount';
import { waitFor } from '@testing-library/react';
import { renderHook } from '@testing-library/react-hooks';
import { EntityListProvider, useEntityList } from '../../hooks';
import { catalogApiRef } from '../../api';
import { ApiRef } from '@backstage/core-plugin-api';
import { MemoryRouter } from 'react-router-dom';
import { EntityOwnerFilter } from '../../filters';
import { useMountEffect } from '@react-hookz/web';
const mockQueryEntities: jest.MockedFn<CatalogApi['queryEntities']> = jest.fn();
const mockCatalogApi: jest.Mocked<Partial<CatalogApi>> = {
queryEntities: mockQueryEntities,
};
jest.mock('@backstage/core-plugin-api', () => {
const actual = jest.requireActual('@backstage/core-plugin-api');
return {
...actual,
useApi: (ref: ApiRef<any>) =>
ref === catalogApiRef ? mockCatalogApi : actual.useApi(ref),
};
});
describe('useAllEntitiesCount', () => {
beforeEach(() => {
jest.clearAllMocks();
});
it('should return the count', async () => {
mockQueryEntities.mockResolvedValue({
items: [],
totalItems: 10,
pageInfo: {},
});
function WrapFilters(props: PropsWithChildren<{}>) {
const { updateFilters } = useEntityList();
useMountEffect(() => {
updateFilters({
owners: new EntityOwnerFilter(['user:default/owner']),
});
});
return <>{props.children}</>;
}
const { result } = renderHook(() => useAllEntitiesCount(), {
wrapper: ({ children }) => (
<MemoryRouter>
<EntityListProvider>
<WrapFilters>{children}</WrapFilters>
</EntityListProvider>
</MemoryRouter>
),
});
await waitFor(() =>
expect(mockQueryEntities).toHaveBeenCalledWith({
filter: {
'relations.ownedBy': ['user:default/owner'],
},
limit: 0,
}),
);
expect(result.current).toEqual({ count: 10, loading: false });
});
it(`shouldn't invoke the endpoint at startup, when filters are missing`, async () => {
mockQueryEntities.mockResolvedValue({
items: [],
totalItems: 10,
pageInfo: {},
});
const { result } = renderHook(() => useAllEntitiesCount(), {
wrapper: ({ children }) => (
<MemoryRouter>
<EntityListProvider>{children}</EntityListProvider>
</MemoryRouter>
),
});
await expect(
waitFor(() => expect(mockQueryEntities).toHaveBeenCalled()),
).rejects.toThrow();
expect(result.current).toEqual({ count: 0, loading: false });
});
});