Skip to main content

Abstract Class: InterceptApi<TData>

Defined in: src/laika.ts:955

This is the mocking API that is returned after running `intercept()` on the Laika.

The API is chainable, with the exception of mockRestore().

Inspired by jest.fn().

Type Parameters

TData

TData = unknown

Properties

calls

readonly calls: readonly OperationVariables[]

Defined in: src/laika.ts:963

An array containing the variables from subsequent operations that passed through this intercept.

Similar to jest.fn().mock.calls.

Methods

allowNetworkFallback()

allowNetworkFallback(): void

Defined in: src/laika.ts:1098

This restores the default behavior: both queries and mutations will be passed to future links (e.g. your backend) and back to the components.

Does not affect subscription operations which will not reach the backend regardless of this setting (unless the connectFutureLinksOrMitmFn argument was set).

Opposite of `disableNetworkFallback()`.

Returns

void


disableNetworkFallback()

disableNetworkFallback(): void

Defined in: src/laika.ts:1089

If you invoke this and do not setup any mocked results, your intercepted queries will not respond, i.e. hang in a "loading" state, until you fire the data event manually (e.g. in a custom callback defined in `onSubscribe(callback)`.

Does not affect subscription operations which will not reach the backend regardless of this setting (unless the connectFutureLinksOrMitmFn argument was set).

Opposite of `allowNetworkFallback()`.

Returns

void


fireSubscriptionUpdate()

fireSubscriptionUpdate<TNextData>(resultOrFn, fireMatcher?): InterceptApi<TNextData>

Defined in: src/laika.ts:1071

Push data to an already active subscription-type operation. Will throw if there are no subscribers (e.g. active useQuery hooks).

Works similarly to `mockResult(...)`, but the listener is being fed the new data upon execution.

Combine with `waitForActiveSubscription()` to ensure a subscription is active before calling.

Type Parameters

TNextData

TNextData = TData

Parameters

resultOrFn

ResultOrFn<NoInfer<TNextData>>

The object to be used as response in the shape of {result: {}, error: {}, delay?: number}. Can be a function that takes operation as the first argument and returns that object synchronously or asynchronously. This may be useful when you wish to customize the mocked response based on the variables from the query.

fireMatcher?

Matcher

Refine when this mock will fire with an additional matcher (e.g. only when specific variables are matched).

Returns

InterceptApi<TNextData>

Example

Push new information to a live feed:

const intercept = laika.intercept({operationName: 'getActiveUsersCount'});
await intercept.waitForActiveSubscription();
intercept.fireSubscriptionUpdate(
{result: {data: {count: 10}}},
);
// e.g. assert the count displayed on the page is in fact 10
intercept.fireSubscriptionUpdate(
{result: {data: {count: 0}}},
);
// e.g. assert the page shows "there are no active users currently on the page"

mockReset()

mockReset(): InterceptApi<TData>

Defined in: src/laika.ts:1102

Resets the mock configuration to its initial state and reenables the intercept if disabled by `mockRestore()`.

Returns

InterceptApi<TData>


mockRestore()

mockRestore(): void

Defined in: src/laika.ts:1110

Removes the intercept completely and re-establishes connectivity in current and future intercepted operations. Note the word future. Any connections that were established prior to running this command, will not automatically switch over to other mocks. This will mostly affect subscriptions. Ideally, keep a reference to the original intercept throughout the duration of your session and simply intercept.reset() if you need to restore connectivity or setup a different scenario.

Returns

void


mockResult()

mockResult<TNextData>(resultOrFn, matcher?): InterceptApi<TNextData>

Defined in: src/laika.ts:998

Sets the mock data that will be used as a default response to intercepted queries and mutations. If used for subscriptions, will push data immediately.

Similar to jest.fn().mockReturnValue(...).

Type Parameters

TNextData

TNextData = TData

Parameters

resultOrFn

ResultOrFn<NoInfer<TNextData>>

The object to be used as response in the shape of {result: {}, error: {}, delay?: number}. Can be a function that takes operation as the first argument and returns that object synchronously or asynchronously. This may be useful when you wish to customize the mocked response based on the variables from the query.

matcher?

Matcher

Refine when this mock will fire with an additional matcher (e.g. only when specific variables are matched).

Returns

InterceptApi<TNextData>

Examples

Always respond with the mock to all queries/mutations intercepted

const intercept = laika.intercept({operationName: 'getUsers'});
intercept.mockResult(
{result: {data: {users: [{id: 1, name: 'Mouse'}, {id: 2, name: 'Bamboo'}]}}},
);

Respond with an error, but only when the operations's variables contain {userGroup: 'elephants'}

const intercept = laika.intercept({operationName: 'getUsers'});
intercept.mockResult(
{error: new Error(`oops, server blew up from all the elephants stomping!`)},
{variables: {userGroup: 'elephants'}}
);

Respond with a customized error based on the variables:

const intercept = laika.intercept({operationName: 'getUsers'});
intercept.mockResult(
({variables}) => ({error: new Error(`oops, server blew up from all the ${variables.userGroup} stomping!`)})
);

mockResultOnce()

mockResultOnce<TNextData>(resultOrFn, matcher?): InterceptApi<TNextData>

Defined in: src/laika.ts:1026

Sets the mock data that will be used as the next response to matching intercepted queries/mutations. If used for subscription operations, will immediately push provided data to the next matching request. Works the same as `mockResult`, except that as soon as a matching result is found in the queue of mocks, it will not be sent again.

Can be run multiple times and will send responses in order in which mockResultOnce was called.

Type Parameters

TNextData

TNextData = TData

Parameters

resultOrFn

ResultOrFn<NoInfer<TNextData>>

The object to be used as response in the shape of {result: {}, error: {}, delay?: number}. Can be a function that takes operation as the first argument and returns that object synchronously or asynchronously. This may be useful when you wish to customize the mocked response based on the variables from the query.

matcher?

Matcher

Refine when this mock will fire with an additional matcher (e.g. only when specific variables are matched).

Returns

InterceptApi<TNextData>

Example

Respond with the mock to the first intercepted operation with the name getUsers, then with a different mock the second time that operation is intercepted.

const intercept = laika.intercept({operationName: 'getUsers'});
intercept
.mockResultOnce(
{result: {data: {users: [{id: 1, name: 'Mouse'}, {id: 2, name: 'Bamboo'}]}}},
);
.mockResultOnce(
{result: {data: {users: [{id: 9, name: 'Ox'}, {id: 10, name: 'Fox'}]}}},
);

onSubscribe()

onSubscribe(callback): void | (() => void)

Defined in: src/laika.ts:1079

Add a callback that will fire every time a component connects to the query (i.e. mounts). You may return a clean-up function which will be run when the query disconnects.

Parameters

callback

OnSubscribeCallback

Returns

void | (() => void)


waitForActiveSubscription()

waitForActiveSubscription(): Promise<void> | undefined

Defined in: src/laika.ts:1035

In case of GraphQL subscriptions, will return synchronously if at least one intercepted subscription is already active. In other cases returns a Promise and behaves the same way as `waitForNextSubscription()`.

Returns

Promise<void> | undefined


waitForNextSubscription()

waitForNextSubscription(): Promise<{ observer: FetchResultSubscriptionObserver; operation: Operation; }>

Defined in: src/laika.ts:1040

Returns a Promise that will resolve when the next operation is run. This translates to whenever a query/mutation is run, or whenever the next subscription is made.

Returns

Promise<{ observer: FetchResultSubscriptionObserver; operation: Operation; }>