Skip to main content

Class: InterceptApi

Laika.InterceptApi

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().

Properties

calls

Readonly calls: readonly Record<string, any>[]

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

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

Defined in

src/laika.ts:816

Methods

allowNetworkFallback

allowNetworkFallback(): void

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

Defined in

src/laika.ts:951


disableNetworkFallback

disableNetworkFallback(): void

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

Defined in

src/laika.ts:942


fireSubscriptionUpdate

fireSubscriptionUpdate(resultOrFn, fireMatcher?): InterceptApi

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.

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"

Parameters

NameTypeDescription
resultOrFnResultOrFnThe object to be used as response in the shape of {result: {}, error: {}}. Can be a function that takes operation as the first argument and returns the described object. This may be useful when you wish to customize the mocked response based on the variables from the query.
fireMatcher?MatcherRefine when this mock will fire with an additional {@link typedefs.matcher | matcher} (e.g. only when specific variables are matched)

Returns

InterceptApi

Defined in

src/laika.ts:924


mockReset

mockReset(): InterceptApi

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

Returns

InterceptApi

Defined in

src/laika.ts:955


mockRestore

mockRestore(): void

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

Defined in

src/laika.ts:963


mockResult

mockResult(resultOrFn, matcher?): InterceptApi

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(...).

example 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'}]}}},
);

example 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'}}
);

example 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!`)})
);

Parameters

NameTypeDescription
resultOrFnResultOrFnThe object to be used as response in the shape of {result: {}, error: {}}. Can be a function that takes operation as the first argument and returns the described object. This may be useful when you wish to customize the mocked response based on the variables from the query.
matcher?MatcherRefine when this mock will fire with an additional {@link typedefs.matcher | matcher} (e.g. only when specific variables are matched)

Returns

InterceptApi

Defined in

src/laika.ts:851


mockResultOnce

mockResultOnce(resultOrFn, matcher?): InterceptApi

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.

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'}]}}},
);

Parameters

NameTypeDescription
resultOrFnResultOrFnThe object to be used as response in the shape of {result: {}, error: {}}. Can be a function that takes operation as the first argument and returns the described object. This may be useful when you wish to customize the mocked response based on the variables from the query.
matcher?MatcherRefine when this mock will fire with an additional {@link typedefs.matcher | matcher} (e.g. only when specific variables are matched)

Returns

InterceptApi

Defined in

src/laika.ts:879


onSubscribe

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

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

NameType
callbackOnSubscribeCallback

Returns

void | () => void

Defined in

src/laika.ts:932


waitForActiveSubscription

waitForActiveSubscription(): undefined | Promise<void>

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

undefined | Promise<void>

Defined in

src/laika.ts:888


waitForNextSubscription

waitForNextSubscription(): Promise<{ observer: Observer<FetchResult<Record<string, any>, Record<string, any>, Record<string, any>>> ; operation: Operation }>

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: Observer<FetchResult<Record<string, any>, Record<string, any>, Record<string, any>>> ; operation: Operation }>

Defined in

src/laika.ts:893