fix unit test

Signed-off-by: mario ma <mario.ma.node@gmail.com>
This commit is contained in:
mario ma
2025-03-28 15:27:19 +08:00
parent c20b2285d6
commit d827c84fba
5 changed files with 62 additions and 44 deletions
@@ -14,14 +14,18 @@
* limitations under the License.
*/
import { screen, render, waitFor } from '@testing-library/react';
import { screen, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { configApiRef } from '@backstage/core-plugin-api';
import { SearchContextProvider } from '../../context';
import { SearchFilter } from './SearchFilter';
import { mockApis, TestApiProvider } from '@backstage/test-utils';
import {
mockApis,
renderInTestApp,
TestApiProvider,
} from '@backstage/test-utils';
import { searchApiRef } from '../../api';
describe('SearchFilter', () => {
@@ -55,14 +59,16 @@ describe('SearchFilter', () => {
it('Check that element was rendered and received props', async () => {
const CustomFilter = (props: { name: string }) => <h6>{props.name}</h6>;
render(<SearchFilter name={name} component={CustomFilter} />);
await renderInTestApp(
<SearchFilter name={name} component={CustomFilter} />,
);
expect(screen.getByRole('heading', { name })).toBeInTheDocument();
});
describe('Checkbox', () => {
it('Renders field name and values when provided as props', async () => {
render(
await renderInTestApp(
<TestApiProvider
apis={[
[searchApiRef, searchApiMock],
@@ -88,7 +94,7 @@ describe('SearchFilter', () => {
});
it('Renders correctly based on filter state', async () => {
render(
await renderInTestApp(
<TestApiProvider
apis={[
[searchApiRef, searchApiMock],
@@ -119,7 +125,7 @@ describe('SearchFilter', () => {
});
it('Renders correctly based on defaultValue', async () => {
render(
await renderInTestApp(
<TestApiProvider
apis={[
[searchApiRef, searchApiMock],
@@ -148,7 +154,7 @@ describe('SearchFilter', () => {
});
it('Checking / unchecking a value sets filter state', async () => {
render(
await renderInTestApp(
<TestApiProvider
apis={[
[searchApiRef, searchApiMock],
@@ -185,7 +191,7 @@ describe('SearchFilter', () => {
});
it('Checking / unchecking a value maintains unrelated filter state', async () => {
render(
await renderInTestApp(
<TestApiProvider
apis={[
[searchApiRef, searchApiMock],
@@ -226,7 +232,7 @@ describe('SearchFilter', () => {
describe('Select', () => {
it('Renders field name and values when provided as props', async () => {
render(
await renderInTestApp(
<TestApiProvider
apis={[
[searchApiRef, searchApiMock],
@@ -258,7 +264,7 @@ describe('SearchFilter', () => {
});
it('Renders values when provided asynchronously', async () => {
render(
await renderInTestApp(
<TestApiProvider
apis={[
[searchApiRef, searchApiMock],
@@ -299,7 +305,7 @@ describe('SearchFilter', () => {
});
it('Renders correctly based on filter state', async () => {
render(
await renderInTestApp(
<TestApiProvider
apis={[
[searchApiRef, searchApiMock],
@@ -342,7 +348,7 @@ describe('SearchFilter', () => {
});
it('Renders correctly based on defaultValue', async () => {
render(
await renderInTestApp(
<TestApiProvider
apis={[
[searchApiRef, searchApiMock],
@@ -383,7 +389,7 @@ describe('SearchFilter', () => {
});
it('Selecting a value sets filter state', async () => {
render(
await renderInTestApp(
<TestApiProvider
apis={[
[searchApiRef, searchApiMock],
@@ -436,7 +442,7 @@ describe('SearchFilter', () => {
});
it('Selecting a value maintains unrelated filter state', async () => {
render(
await renderInTestApp(
<TestApiProvider
apis={[
[searchApiRef, searchApiMock],
@@ -19,7 +19,7 @@ import userEvent from '@testing-library/user-event';
import {
mockApis,
renderWithEffects,
renderInTestApp,
TestApiProvider,
} from '@backstage/test-utils';
@@ -53,7 +53,7 @@ describe('SearchPagination', () => {
});
it('Renders without exploding', async () => {
await renderWithEffects(
await renderInTestApp(
<TestApiProvider
apis={[
[searchApiRef, searchApiMock],
@@ -74,7 +74,7 @@ describe('SearchPagination', () => {
});
it('Define default page limit options', async () => {
await renderWithEffects(
await renderInTestApp(
<TestApiProvider
apis={[
[searchApiRef, searchApiMock],
@@ -99,7 +99,7 @@ describe('SearchPagination', () => {
it('Accept custom page limit label', async () => {
const label = 'Page limit:';
await renderWithEffects(
await renderInTestApp(
<TestApiProvider
apis={[
[searchApiRef, searchApiMock],
@@ -116,7 +116,7 @@ describe('SearchPagination', () => {
});
it('Show the total in text', async () => {
await renderWithEffects(
await renderInTestApp(
<TestApiProvider
apis={[
[searchApiRef, searchApiMock],
@@ -133,7 +133,7 @@ describe('SearchPagination', () => {
});
it('Accept custom page limit text', async () => {
await renderWithEffects(
await renderInTestApp(
<TestApiProvider
apis={[
[searchApiRef, searchApiMock],
@@ -152,7 +152,7 @@ describe('SearchPagination', () => {
});
it('Accept custom page limit options', async () => {
await renderWithEffects(
await renderInTestApp(
<TestApiProvider
apis={[
[searchApiRef, searchApiMock],
@@ -176,7 +176,7 @@ describe('SearchPagination', () => {
});
it('Set page limit in the context', async () => {
await renderWithEffects(
await renderInTestApp(
<TestApiProvider
apis={[
[searchApiRef, searchApiMock],
@@ -208,7 +208,7 @@ describe('SearchPagination', () => {
pageCursor: 'MQ==', // page: 1
};
await renderWithEffects(
await renderInTestApp(
<TestApiProvider
apis={[
[searchApiRef, searchApiMock],
@@ -250,7 +250,7 @@ describe('SearchPagination', () => {
pageCursor: 'Mg==', // page: 2
};
await renderWithEffects(
await renderInTestApp(
<TestApiProvider
apis={[
[searchApiRef, searchApiMock],
@@ -15,8 +15,12 @@
*/
import { ReactNode } from 'react';
import { mockApis, TestApiProvider } from '@backstage/test-utils';
import { act, render, waitFor } from '@testing-library/react';
import {
mockApis,
renderInTestApp,
TestApiProvider,
} from '@backstage/test-utils';
import { act, waitFor } from '@testing-library/react';
import user from '@testing-library/user-event';
import {
searchApiRef,
@@ -81,7 +85,7 @@ describe('SearchType.Accordion', () => {
};
it('should render as expected', async () => {
const { getByText } = render(
const { getByText } = await renderInTestApp(
<Wrapper>
<SearchType.Accordion name={expectedLabel} types={[expectedType]} />
</Wrapper>,
@@ -103,7 +107,7 @@ describe('SearchType.Accordion', () => {
});
it('should set entire types array when a type is selected', async () => {
const { getByText } = render(
const { getByText } = await renderInTestApp(
<Wrapper>
<SearchType.Accordion name={expectedLabel} types={[expectedType]} />
</Wrapper>,
@@ -115,7 +119,7 @@ describe('SearchType.Accordion', () => {
});
it('should reset types array when all is selected', async () => {
const { getByText } = render(
const { getByText } = await renderInTestApp(
<Wrapper>
<SearchType.Accordion
name={expectedLabel}
@@ -131,7 +135,7 @@ describe('SearchType.Accordion', () => {
});
it('should reset page cursor when a new type is selected', async () => {
const { getByText } = render(
const { getByText } = await renderInTestApp(
<Wrapper>
<SearchType.Accordion name={expectedLabel} types={[expectedType]} />
</Wrapper>,
@@ -143,7 +147,7 @@ describe('SearchType.Accordion', () => {
});
it('should show result counts if enabled', async () => {
const { getAllByText } = render(
const { getAllByText } = await renderInTestApp(
<Wrapper>
<SearchType.Accordion
name={expectedLabel}
@@ -15,8 +15,12 @@
*/
import { ReactNode } from 'react';
import { mockApis, TestApiProvider } from '@backstage/test-utils';
import { act, render } from '@testing-library/react';
import {
mockApis,
renderInTestApp,
TestApiProvider,
} from '@backstage/test-utils';
import { act } from '@testing-library/react';
import user from '@testing-library/user-event';
import {
SearchContextProvider,
@@ -69,7 +73,7 @@ describe('SearchType.Tabs', () => {
};
it('should render as expected', async () => {
const { getByText } = render(
const { getByText } = await renderInTestApp(
<Wrapper>
<SearchType.Tabs types={[expectedType]} />
</Wrapper>,
@@ -85,7 +89,7 @@ describe('SearchType.Tabs', () => {
});
it('should set entire types array when a type is selected', async () => {
const { getByText } = render(
const { getByText } = await renderInTestApp(
<Wrapper>
<SearchType.Tabs types={[expectedType]} />
</Wrapper>,
@@ -97,7 +101,7 @@ describe('SearchType.Tabs', () => {
});
it('should reset types array when all is selected', async () => {
const { getByText } = render(
const { getByText } = await renderInTestApp(
<Wrapper>
<SearchType.Tabs
defaultValue={expectedType.value}
@@ -112,7 +116,7 @@ describe('SearchType.Tabs', () => {
});
it('should reset page cursor when a new type is selected', async () => {
const { getByText } = render(
const { getByText } = await renderInTestApp(
<Wrapper>
<SearchType.Tabs types={[expectedType]} />
</Wrapper>,
@@ -15,14 +15,18 @@
*/
import { configApiRef } from '@backstage/core-plugin-api';
import { render, screen, waitFor } from '@testing-library/react';
import { screen, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import {
SearchContextProvider,
searchApiRef,
} from '@backstage/plugin-search-react';
import { SearchType } from './SearchType';
import { mockApis, TestApiProvider } from '@backstage/test-utils';
import {
mockApis,
renderInTestApp,
TestApiProvider,
} from '@backstage/test-utils';
describe('SearchType', () => {
const initialState = {
@@ -53,7 +57,7 @@ describe('SearchType', () => {
describe('Type Filter', () => {
it('Renders field name and values when provided as props', async () => {
render(
await renderInTestApp(
<TestApiProvider
apis={[
[configApiRef, configApiMock],
@@ -85,7 +89,7 @@ describe('SearchType', () => {
});
it('Renders correctly based on type filter state', async () => {
render(
await renderInTestApp(
<TestApiProvider
apis={[
[configApiRef, configApiMock],
@@ -123,7 +127,7 @@ describe('SearchType', () => {
});
it('Renders correctly based on type filter defaultValue', async () => {
render(
await renderInTestApp(
<TestApiProvider
apis={[
[configApiRef, configApiMock],
@@ -156,7 +160,7 @@ describe('SearchType', () => {
});
it('Selecting a value sets type filter state', async () => {
render(
await renderInTestApp(
<TestApiProvider
apis={[
[configApiRef, configApiMock],
@@ -199,7 +203,7 @@ describe('SearchType', () => {
});
it('Selecting none defaults to empty state', async () => {
render(
await renderInTestApp(
<TestApiProvider
apis={[
[configApiRef, configApiMock],