implement the beginnings of mockApis

Signed-off-by: Fredrik Adelöw <freben@gmail.com>
This commit is contained in:
Fredrik Adelöw
2024-10-08 17:03:22 +02:00
parent f91809d6f6
commit 9cc7dd6cfd
45 changed files with 632 additions and 299 deletions
@@ -16,7 +16,7 @@
import { Entity } from '@backstage/catalog-model';
import { configApiRef } from '@backstage/core-plugin-api';
import { MockConfigApi, TestApiProvider } from '@backstage/test-utils';
import { mockApis, TestApiProvider } from '@backstage/test-utils';
import { makeStyles } from '@material-ui/core/styles';
import { render, screen } from '@testing-library/react';
import { renderHook } from '@testing-library/react';
@@ -46,7 +46,7 @@ const entities: Entity[] = [
},
];
const mockConfigApi = new MockConfigApi({});
const mockConfigApi = mockApis.config();
const apis = [[configApiRef, mockConfigApi]] as const;
describe('<PreviewCatalogInfoComponent />', () => {
@@ -122,10 +122,12 @@ describe('<PreviewCatalogInfoComponent />', () => {
apis={[
[
configApiRef,
new MockConfigApi({
catalog: {
import: {
entityFilename: 'anvil.yaml',
mockApis.config({
data: {
catalog: {
import: {
entityFilename: 'anvil.yaml',
},
},
},
}),
@@ -16,7 +16,7 @@
import { configApiRef, errorApiRef } from '@backstage/core-plugin-api';
import { catalogApiRef } from '@backstage/plugin-catalog-react';
import { TestApiProvider, MockConfigApi } from '@backstage/test-utils';
import { TestApiProvider, mockApis } from '@backstage/test-utils';
import TextField from '@material-ui/core/TextField';
import { render, screen, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
@@ -43,7 +43,7 @@ describe('<StepPrepareCreatePullRequest />', () => {
post: jest.fn(),
};
const configApi = new MockConfigApi({});
const configApi = mockApis.config();
const Wrapper = ({ children }: { children?: React.ReactNode }) => (
<TestApiProvider
+37 -25
View File
@@ -15,15 +15,17 @@
*/
import { readFilterConfig, createFilterByQueryParamFromConfig } from './config';
import { MockConfigApi } from '@backstage/test-utils';
import { mockApis } from '@backstage/test-utils';
describe('config', () => {
describe('readFilterConfig', () => {
it('returns filter data', async () => {
const mockConfig = new MockConfigApi({
field: 'pathname',
operator: '==',
value: '/home',
const mockConfig = mockApis.config({
data: {
field: 'pathname',
operator: '==',
value: '/home',
},
});
const res = readFilterConfig(mockConfig);
expect(res).toEqual({
@@ -34,10 +36,12 @@ describe('config', () => {
});
it('returns undefined for invalid filter', async () => {
const mockInvalidConfig = new MockConfigApi({
myField: 'pathname',
operator: '==',
value: '3',
const mockInvalidConfig = mockApis.config({
data: {
myField: 'pathname',
operator: '==',
value: '3',
},
});
const res = readFilterConfig(mockInvalidConfig);
expect(res).toEqual(undefined);
@@ -46,15 +50,19 @@ describe('config', () => {
describe('createFilterByQueryParamFromConfig', () => {
it('returns filter data', async () => {
const mockConfig1 = new MockConfigApi({
field: 'id',
operator: '==',
value: '3',
const mockConfig1 = mockApis.config({
data: {
field: 'id',
operator: '==',
value: '3',
},
});
const mockConfig2 = new MockConfigApi({
field: 'pathname',
operator: '==',
value: 'path',
const mockConfig2 = mockApis.config({
data: {
field: 'pathname',
operator: '==',
value: 'path',
},
});
const res = createFilterByQueryParamFromConfig([
mockConfig1,
@@ -67,15 +75,19 @@ describe('config', () => {
});
it('returns only valid filters', async () => {
const mockValidConfig = new MockConfigApi({
field: 'id',
operator: '==',
value: 3,
const mockValidConfig = mockApis.config({
data: {
field: 'id',
operator: '==',
value: 3,
},
});
const mockInvalidConfig = new MockConfigApi({
myField: 'pathname',
operator: '==',
value: 'path',
const mockInvalidConfig = mockApis.config({
data: {
myField: 'pathname',
operator: '==',
value: 'path',
},
});
const res = createFilterByQueryParamFromConfig([
mockValidConfig,
@@ -19,7 +19,7 @@ import { Content } from './Content';
import {
TestApiProvider,
renderInTestApp,
MockConfigApi,
mockApis,
} from '@backstage/test-utils';
import { visitsApiRef } from '../../api';
import { ContextProvider } from './Context';
@@ -138,16 +138,18 @@ describe('<Content kind="recent"/>', () => {
});
it('allows recent items to be filtered using config', async () => {
const configApiMock = new MockConfigApi({
home: {
recentVisits: {
filterBy: [
{
field: 'pathname',
operator: '==',
value: '/tech-radar',
},
],
const configApiMock = mockApis.config({
data: {
home: {
recentVisits: {
filterBy: [
{
field: 'pathname',
operator: '==',
value: '/tech-radar',
},
],
},
},
},
});
@@ -206,20 +208,22 @@ describe('<Content kind="recent"/>', () => {
});
it('allows recent items to have no filter if the filter config is not valid', async () => {
const configApiMock = new MockConfigApi({
home: {
recentVisits: {
filterBy: [
{
operator: '==',
value: '/tech-radar',
},
{
field: 'pathname',
operator: '==',
value: '/explore',
},
],
const configApiMock = mockApis.config({
data: {
home: {
recentVisits: {
filterBy: [
{
operator: '==',
value: '/tech-radar',
},
{
field: 'pathname',
operator: '==',
value: '/explore',
},
],
},
},
},
});
@@ -283,16 +287,18 @@ describe('<Content kind="top"/>', () => {
});
it('allows top items to be filtered using config', async () => {
const configApiMock = new MockConfigApi({
home: {
topVisits: {
filterBy: [
{
field: 'pathname',
operator: '==',
value: '/explore',
},
],
const configApiMock = mockApis.config({
data: {
home: {
topVisits: {
filterBy: [
{
field: 'pathname',
operator: '==',
value: '/explore',
},
],
},
},
},
});
@@ -14,7 +14,7 @@
* limitations under the License.
*/
import { MockConfigApi, TestApiProvider } from '@backstage/test-utils';
import { mockApis, TestApiProvider } from '@backstage/test-utils';
import { screen, render, waitFor, within } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import React from 'react';
@@ -35,10 +35,12 @@ const SearchContextFilterSpy = ({ name }: { name: string }) => {
};
describe('SearchFilter.Autocomplete', () => {
const configApiMock = new MockConfigApi({
search: {
query: {
pageLimit: 100,
const configApiMock = mockApis.config({
data: {
search: {
query: {
pageLimit: 100,
},
},
},
});
@@ -22,7 +22,7 @@ import { configApiRef } from '@backstage/core-plugin-api';
import { SearchContextProvider } from '../../context';
import { SearchFilter } from './SearchFilter';
import { MockConfigApi, TestApiProvider } from '@backstage/test-utils';
import { mockApis, TestApiProvider } from '@backstage/test-utils';
import { searchApiRef } from '../../api';
describe('SearchFilter', () => {
@@ -37,10 +37,12 @@ describe('SearchFilter', () => {
const values = ['value1', 'value2'];
const filters = { unrelated: 'unrelated' };
const configApiMock = new MockConfigApi({
search: {
query: {
pagelimit: 10,
const configApiMock = mockApis.config({
data: {
search: {
query: {
pagelimit: 10,
},
},
},
});
@@ -13,9 +13,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import React from 'react';
import { ApiProvider } from '@backstage/core-app-api';
import { MockConfigApi, TestApiRegistry } from '@backstage/test-utils';
import { mockApis, TestApiRegistry } from '@backstage/test-utils';
import { act, renderHook, waitFor } from '@testing-library/react';
import { searchApiRef } from '../../api';
@@ -27,17 +28,19 @@ jest.useFakeTimers();
describe('SearchFilter.hooks', () => {
describe('useDefaultFilterValue', () => {
const configApiMock = new MockConfigApi({
search: {
query: {
pageLimit: 100,
const configApiMock = mockApis.config({
data: {
search: {
query: {
pageLimit: 100,
},
},
},
});
const searchApiMock = {
query: jest.fn().mockResolvedValue({ results: [] }),
};
const mockApis = TestApiRegistry.from(
const apis = TestApiRegistry.from(
[searchApiRef, searchApiMock],
[configApiRef, configApiMock],
);
@@ -54,7 +57,7 @@ describe('SearchFilter.hooks', () => {
filters: {},
};
return (
<ApiProvider apis={mockApis}>
<ApiProvider apis={apis}>
<SearchContextProvider
initialState={{ ...emptySearchContext, ...overrides }}
>
@@ -19,7 +19,7 @@ import { screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import {
MockConfigApi,
mockApis,
renderWithEffects,
TestApiProvider,
} from '@backstage/test-utils';
@@ -31,10 +31,12 @@ import { SearchPagination } from './SearchPagination';
import { configApiRef } from '@backstage/core-plugin-api';
describe('SearchPagination', () => {
const configApiMock = new MockConfigApi({
search: {
query: {
pagelimit: 10,
const configApiMock = mockApis.config({
data: {
search: {
query: {
pagelimit: 10,
},
},
},
});
@@ -22,7 +22,7 @@ import {
act,
renderHook,
} from '@testing-library/react';
import { MockConfigApi, TestApiProvider } from '@backstage/test-utils';
import { mockApis, TestApiProvider } from '@backstage/test-utils';
import React from 'react';
import {
SearchContextProvider,
@@ -37,7 +37,7 @@ describe('SearchContext', () => {
} satisfies typeof searchApiRef.T;
const wrapper = ({ children, initialState, config = {} }: any) => {
const configApiMock = new MockConfigApi(config);
const configApiMock = mockApis.config({ data: config });
return (
<TestApiProvider
apis={[
@@ -433,7 +433,7 @@ describe('SearchContext', () => {
const { result } = renderHook(() => useSearch(), {
wrapper: ({ children }) => {
const configApiMock = new MockConfigApi({});
const configApiMock = mockApis.config();
return (
<TestApiProvider
apis={[
@@ -491,7 +491,7 @@ describe('SearchContext', () => {
const { result } = renderHook(() => useSearch(), {
wrapper: ({ children }) => {
const configApiMock = new MockConfigApi({});
const configApiMock = mockApis.config();
return (
<TestApiProvider
apis={[
@@ -15,7 +15,7 @@
*/
import React from 'react';
import { MockConfigApi, TestApiProvider } from '@backstage/test-utils';
import { mockApis, TestApiProvider } from '@backstage/test-utils';
import { act, render, waitFor } from '@testing-library/react';
import user from '@testing-library/user-event';
import {
@@ -41,10 +41,12 @@ jest.mock('@backstage/plugin-search-react', () => ({
}));
describe('SearchType.Accordion', () => {
const configApiMock = new MockConfigApi({
search: {
query: {
pagelimit: 10,
const configApiMock = mockApis.config({
data: {
search: {
query: {
pagelimit: 10,
},
},
},
});
@@ -15,7 +15,7 @@
*/
import React from 'react';
import { MockConfigApi, TestApiProvider } from '@backstage/test-utils';
import { mockApis, TestApiProvider } from '@backstage/test-utils';
import { act, render } from '@testing-library/react';
import user from '@testing-library/user-event';
import {
@@ -40,10 +40,12 @@ jest.mock('@backstage/plugin-search-react', () => ({
describe('SearchType.Tabs', () => {
const searchApiMock = { query: jest.fn().mockResolvedValue({ results: [] }) };
const configApiMock = new MockConfigApi({
search: {
query: {
pageLimit: 100,
const configApiMock = mockApis.config({
data: {
search: {
query: {
pageLimit: 100,
},
},
},
});
@@ -23,7 +23,7 @@ import {
searchApiRef,
} from '@backstage/plugin-search-react';
import { SearchType } from './SearchType';
import { MockConfigApi, TestApiProvider } from '@backstage/test-utils';
import { mockApis, TestApiProvider } from '@backstage/test-utils';
describe('SearchType', () => {
const initialState = {
@@ -36,10 +36,12 @@ describe('SearchType', () => {
const values = ['value1', 'value2'];
const typeValues = ['preselected'];
const configApiMock = new MockConfigApi({
search: {
query: {
pagelimit: 10,
const configApiMock = mockApis.config({
data: {
search: {
query: {
pagelimit: 10,
},
},
},
});
+3 -2
View File
@@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import React from 'react';
import { renderHook, act, waitFor } from '@testing-library/react';
@@ -21,7 +22,7 @@ import { ThemeProvider } from '@material-ui/core/styles';
import { lightTheme } from '@backstage/theme';
import {
MockAnalyticsApi,
MockConfigApi,
mockApis,
TestApiProvider,
} from '@backstage/test-utils';
import { Entity, CompoundEntityRef } from '@backstage/catalog-model';
@@ -84,7 +85,7 @@ const wrapper = ({
<TestApiProvider
apis={[
[analyticsApiRef, analyticsApiMock],
[configApiRef, new MockConfigApi(config ?? {})],
[configApiRef, mockApis.config({ data: config ?? {} })],
[techdocsApiRef, techdocsApiMock],
]}
>
+2 -2
View File
@@ -18,7 +18,7 @@ import { UrlPatternDiscovery } from '@backstage/core-app-api';
import { IdentityApi } from '@backstage/core-plugin-api';
import { NotFoundError } from '@backstage/errors';
import { fetchEventSource } from '@microsoft/fetch-event-source';
import { MockConfigApi, MockFetchApi } from '@backstage/test-utils';
import { mockApis, MockFetchApi } from '@backstage/test-utils';
import { TechDocsStorageClient } from './client';
jest.mock('@microsoft/fetch-event-source');
@@ -34,7 +34,7 @@ const mockEntity = {
describe('TechDocsStorageClient', () => {
const mockBaseUrl = 'http://backstage:9191/api/techdocs';
const configApi = new MockConfigApi({});
const configApi = mockApis.config();
const discoveryApi = UrlPatternDiscovery.compile(mockBaseUrl);
const identityApi: jest.Mocked<IdentityApi> = {
getCredentials: jest.fn(),
@@ -13,12 +13,13 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import React from 'react';
import { scmIntegrationsApiRef } from '@backstage/integration-react';
import { entityRouteRef } from '@backstage/plugin-catalog-react';
import {
MockConfigApi,
mockApis,
renderInTestApp,
TestApiProvider,
} from '@backstage/test-utils';
@@ -106,10 +107,8 @@ jest.mock('@backstage/core-components', () => ({
Page: jest.fn(),
}));
const configApi = new MockConfigApi({
app: {
baseUrl: 'http://localhost:3000',
},
const configApi = mockApis.config({
data: { app: { baseUrl: 'http://localhost:3000' } },
});
const Wrapper = ({ children }: { children: React.ReactNode }) => {
@@ -13,9 +13,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import React from 'react';
import {
MockConfigApi,
mockApis,
renderInTestApp,
TestApiProvider,
} from '@backstage/test-utils';
@@ -55,16 +56,7 @@ describe('useNavigateUrl', () => {
const baseUrl = 'http://localhost:3000';
await renderInTestApp(
<TestApiProvider
apis={[
[
configApiRef,
new MockConfigApi({
app: {
baseUrl,
},
}),
],
]}
apis={[[configApiRef, mockApis.config({ data: { app: { baseUrl } } })]]}
>
<Component to={`${baseUrl}/test`} />
</TestApiProvider>,
@@ -75,16 +67,7 @@ describe('useNavigateUrl', () => {
const baseUrl = 'http://localhost:3000/instance';
await renderInTestApp(
<TestApiProvider
apis={[
[
configApiRef,
new MockConfigApi({
app: {
baseUrl,
},
}),
],
]}
apis={[[configApiRef, mockApis.config({ data: { app: { baseUrl } } })]]}
>
<Component to={`${baseUrl}/test`} />
</TestApiProvider>,
@@ -95,16 +78,7 @@ describe('useNavigateUrl', () => {
const baseUrl = 'http://localhost:3000';
await renderInTestApp(
<TestApiProvider
apis={[
[
configApiRef,
new MockConfigApi({
app: {
baseUrl,
},
}),
],
]}
apis={[[configApiRef, mockApis.config({ data: { app: { baseUrl } } })]]}
>
<Component to="/test" />
</TestApiProvider>,