Merge pull request #27026 from backstage/freben/mock-apis-config
Implement the beginnings of `mockApis`
This commit is contained in:
@@ -20,7 +20,7 @@ import userEvent from '@testing-library/user-event';
|
||||
import { configApiRef } from '@backstage/core-plugin-api';
|
||||
import { ConfigReader } from '@backstage/core-app-api';
|
||||
import {
|
||||
MockAnalyticsApi,
|
||||
mockApis,
|
||||
TestApiProvider,
|
||||
renderInTestApp,
|
||||
} from '@backstage/test-utils';
|
||||
@@ -273,7 +273,7 @@ describe('SearchBar', () => {
|
||||
});
|
||||
|
||||
it('Does not capture analytics event if not enabled in app', async () => {
|
||||
const analyticsApiMock = new MockAnalyticsApi();
|
||||
const analyticsApiMock = mockApis.analytics();
|
||||
|
||||
await renderInTestApp(
|
||||
<TestApiProvider
|
||||
@@ -296,7 +296,7 @@ describe('SearchBar', () => {
|
||||
|
||||
await waitFor(() => expect(textbox).toHaveValue(value));
|
||||
|
||||
expect(analyticsApiMock.getEvents()).toHaveLength(0);
|
||||
expect(analyticsApiMock.captureEvent).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('Renders custom search icon', async () => {
|
||||
|
||||
@@ -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,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
@@ -24,7 +24,7 @@ import DocsIcon from '@material-ui/icons/InsertDriveFile';
|
||||
import {
|
||||
renderInTestApp,
|
||||
TestApiProvider,
|
||||
MockAnalyticsApi,
|
||||
mockApis,
|
||||
} from '@backstage/test-utils';
|
||||
import { createPlugin, analyticsApiRef } from '@backstage/core-plugin-api';
|
||||
|
||||
@@ -40,7 +40,7 @@ import {
|
||||
|
||||
const query = jest.fn().mockResolvedValue({ results: [] });
|
||||
const searchApiMock = { query };
|
||||
const analyticsApiMock = new MockAnalyticsApi();
|
||||
const analyticsApiMock = mockApis.analytics();
|
||||
|
||||
describe('SearchResultGroup', () => {
|
||||
const results = [
|
||||
|
||||
@@ -20,7 +20,7 @@ import { screen, waitFor } from '@testing-library/react';
|
||||
import {
|
||||
TestApiProvider,
|
||||
renderInTestApp,
|
||||
MockAnalyticsApi,
|
||||
mockApis,
|
||||
} from '@backstage/test-utils';
|
||||
import { analyticsApiRef, createPlugin } from '@backstage/core-plugin-api';
|
||||
|
||||
@@ -31,7 +31,7 @@ import { SearchResultList } from './SearchResultList';
|
||||
|
||||
const query = jest.fn().mockResolvedValue({ results: [] });
|
||||
const searchApiMock = { query };
|
||||
const analyticsApiMock = new MockAnalyticsApi();
|
||||
const analyticsApiMock = mockApis.analytics();
|
||||
|
||||
describe('SearchResultList', () => {
|
||||
const results = [
|
||||
|
||||
@@ -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={[
|
||||
@@ -422,9 +422,7 @@ describe('SearchContext', () => {
|
||||
|
||||
describe('analytics', () => {
|
||||
it('captures analytics events if enabled in app', async () => {
|
||||
const analyticsApiMock = {
|
||||
captureEvent: jest.fn(),
|
||||
} satisfies typeof analyticsApiRef.T;
|
||||
const analyticsApiMock = mockApis.analytics();
|
||||
|
||||
searchApiMock.query.mockResolvedValue({
|
||||
results: [],
|
||||
@@ -433,7 +431,7 @@ describe('SearchContext', () => {
|
||||
|
||||
const { result } = renderHook(() => useSearch(), {
|
||||
wrapper: ({ children }) => {
|
||||
const configApiMock = new MockConfigApi({});
|
||||
const configApiMock = mockApis.config();
|
||||
return (
|
||||
<TestApiProvider
|
||||
apis={[
|
||||
@@ -481,9 +479,7 @@ describe('SearchContext', () => {
|
||||
});
|
||||
|
||||
it('captures analytics events even if number of results does not exist', async () => {
|
||||
const analyticsApiMock = {
|
||||
captureEvent: jest.fn(),
|
||||
} satisfies typeof analyticsApiRef.T;
|
||||
const analyticsApiMock = mockApis.analytics();
|
||||
|
||||
searchApiMock.query.mockResolvedValue({
|
||||
results: [],
|
||||
@@ -491,7 +487,7 @@ describe('SearchContext', () => {
|
||||
|
||||
const { result } = renderHook(() => useSearch(), {
|
||||
wrapper: ({ children }) => {
|
||||
const configApiMock = new MockConfigApi({});
|
||||
const configApiMock = mockApis.config();
|
||||
return (
|
||||
<TestApiProvider
|
||||
apis={[
|
||||
|
||||
@@ -23,7 +23,7 @@ import ListItemText from '@material-ui/core/ListItemText';
|
||||
import {
|
||||
renderInTestApp,
|
||||
TestApiProvider,
|
||||
MockAnalyticsApi,
|
||||
mockApis,
|
||||
} from '@backstage/test-utils';
|
||||
import {
|
||||
createPlugin,
|
||||
@@ -38,7 +38,7 @@ import {
|
||||
SearchResultListItemExtensionOptions,
|
||||
} from './extensions';
|
||||
|
||||
const analyticsApiMock = new MockAnalyticsApi();
|
||||
const analyticsApiMock = mockApis.analytics();
|
||||
|
||||
const results = [
|
||||
{
|
||||
@@ -118,7 +118,7 @@ describe('extensions', () => {
|
||||
screen.getByRole('link', { name: /Search Result 1/ }),
|
||||
);
|
||||
|
||||
expect(analyticsApiMock.getEvents()[0]).toMatchObject({
|
||||
expect(analyticsApiMock.captureEvent).toHaveBeenCalledWith({
|
||||
action: 'discover',
|
||||
subject: 'Search Result 1',
|
||||
context: { routeRef: 'unknown', pluginId: 'root', extension: 'App' },
|
||||
@@ -141,7 +141,7 @@ describe('extensions', () => {
|
||||
|
||||
await userEvent.click(screen.getByRole('listitem'));
|
||||
|
||||
expect(analyticsApiMock.getEvents()[0]).toMatchObject({
|
||||
expect(analyticsApiMock.captureEvent).toHaveBeenCalledWith({
|
||||
action: 'discover',
|
||||
subject: 'Search Result 1',
|
||||
context: { routeRef: 'unknown', pluginId: 'root', extension: 'App' },
|
||||
|
||||
Reference in New Issue
Block a user