frontend-test-utils: remove redundant .factory util
Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
---
|
||||
'@backstage/frontend-test-utils': patch
|
||||
'@backstage/frontend-test-utils': minor
|
||||
---
|
||||
|
||||
Added a new `mockApis` namespace with mock implementations of many core APIs. Mock API instances can be passed directly to `TestApiProvider`, `renderInTestApp`, and `renderTestApp` without needing `[apiRef, impl]` tuples.
|
||||
**BREAKING**: The `mockApis` namespace is no longer a re-export from `@backstage/test-utils`. It's now a standalone namespace with mock implementations of most core APIs. Mock API instances can be passed directly to `TestApiProvider`, `renderInTestApp`, and `renderTestApp` without needing `[apiRef, impl]` tuples. As part of this change, the `.factory()` method on some mocks has been removed, since it's now redundant.
|
||||
|
||||
```tsx
|
||||
// Before
|
||||
|
||||
@@ -9,7 +9,7 @@ When testing frontend components and extensions, you often need to provide mock
|
||||
|
||||
## The `mockApis` namespace
|
||||
|
||||
The `mockApis` namespace is the main entry point for creating mock utility API instances in tests. It provides three usage patterns for each API:
|
||||
The `mockApis` namespace is the main entry point for creating mock utility API instances in tests. It provides two usage patterns for each API:
|
||||
|
||||
### Fake instances
|
||||
|
||||
@@ -42,10 +42,6 @@ const catalogApi = mockApis.permission.mock({
|
||||
expect(catalogApi.authorize).toHaveBeenCalledTimes(1);
|
||||
```
|
||||
|
||||
### API factories
|
||||
|
||||
Call `.factory()` to get an `ApiFactory` suitable for more advanced wiring scenarios.
|
||||
|
||||
## Providing mock APIs in tests
|
||||
|
||||
### With `renderInTestApp`
|
||||
|
||||
@@ -171,7 +171,6 @@ export type MockApiFactorySymbol = typeof mockApiFactorySymbol;
|
||||
export namespace mockApis {
|
||||
export function alert(): MockWithApiFactory<MockAlertApi>;
|
||||
export namespace alert {
|
||||
const factory: () => any;
|
||||
const mock: (
|
||||
partialImpl?:
|
||||
| Partial<{
|
||||
@@ -279,7 +278,6 @@ export namespace mockApis {
|
||||
options?: MockFeatureFlagsApiOptions,
|
||||
): MockWithApiFactory<MockFeatureFlagsApi>;
|
||||
export namespace featureFlags {
|
||||
const factory: (options?: MockFeatureFlagsApiOptions | undefined) => any;
|
||||
const mock: (
|
||||
partialImpl?:
|
||||
| Partial<{
|
||||
|
||||
@@ -91,19 +91,6 @@ function simpleMock<TApi>(
|
||||
};
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
function simpleFactory<TApi, TArgs extends unknown[]>(
|
||||
ref: any,
|
||||
factory: (...args: TArgs) => TApi,
|
||||
): (...args: TArgs) => any {
|
||||
return (...args) =>
|
||||
createApiFactory({
|
||||
api: ref,
|
||||
deps: {},
|
||||
factory: () => factory(...args),
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Mock implementations of the core utility APIs, to be used in tests.
|
||||
*
|
||||
@@ -111,7 +98,7 @@ function simpleFactory<TApi, TArgs extends unknown[]>(
|
||||
* @remarks
|
||||
*
|
||||
* There are some variations among the APIs depending on what needs tests
|
||||
* might have, but overall there are three main usage patterns:
|
||||
* might have, but overall there are two main usage patterns:
|
||||
*
|
||||
* 1: Creating an actual fake API instance, often with a simplified version
|
||||
* of functionality, by calling the mock API itself as a function.
|
||||
@@ -133,14 +120,6 @@ function simpleFactory<TApi, TArgs extends unknown[]>(
|
||||
* expect(foo.someMethod).toHaveBeenCalledTimes(2);
|
||||
* expect(foo.otherMethod).toHaveBeenCalledWith(testData);
|
||||
* ```
|
||||
*
|
||||
* 3: Creating an API factory that behaves similarly to the mock as per above.
|
||||
*
|
||||
* ```ts
|
||||
* const factory = mockApis.foo.factory({
|
||||
* someMethod: () => 'mocked result',
|
||||
* });
|
||||
* ```
|
||||
*/
|
||||
export namespace mockApis {
|
||||
/**
|
||||
@@ -169,13 +148,6 @@ export namespace mockApis {
|
||||
* @public
|
||||
*/
|
||||
export namespace alert {
|
||||
/**
|
||||
* Creates a factory for a fake implementation of
|
||||
* {@link @backstage/frontend-plugin-api#AlertApi}.
|
||||
*
|
||||
* @public
|
||||
*/
|
||||
export const factory = simpleFactory(alertApiRef, alert);
|
||||
/**
|
||||
* Creates a mock implementation of
|
||||
* {@link @backstage/frontend-plugin-api#AlertApi}. All methods are
|
||||
@@ -219,13 +191,6 @@ export namespace mockApis {
|
||||
* @public
|
||||
*/
|
||||
export namespace featureFlags {
|
||||
/**
|
||||
* Creates a factory for a fake implementation of
|
||||
* {@link @backstage/frontend-plugin-api#FeatureFlagsApi}.
|
||||
*
|
||||
* @public
|
||||
*/
|
||||
export const factory = simpleFactory(featureFlagsApiRef, featureFlags);
|
||||
/**
|
||||
* Creates a mock implementation of
|
||||
* {@link @backstage/frontend-plugin-api#FeatureFlagsApi}. All methods are
|
||||
|
||||
Reference in New Issue
Block a user