debian-mirror-gitlab/spec/frontend/__helpers__/mock_apollo_helper.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

33 lines
1.1 KiB
JavaScript
Raw Normal View History

2022-04-04 11:22:00 +05:30
import { InMemoryCache } from '@apollo/client/core';
2021-06-08 01:23:25 +05:30
import { createMockClient as createMockApolloClient } from 'mock-apollo-client';
2020-11-24 15:15:51 +05:30
import VueApollo from 'vue-apollo';
2022-06-21 17:19:12 +05:30
import possibleTypes from '~/graphql_shared/possible_types.json';
2022-04-04 11:22:00 +05:30
import { typePolicies } from '~/lib/graphql';
2021-04-29 21:17:54 +05:30
2021-06-08 01:23:25 +05:30
export function createMockClient(handlers = [], resolvers = {}, cacheOptions = {}) {
2020-11-24 15:15:51 +05:30
const cache = new InMemoryCache({
2022-04-04 11:22:00 +05:30
possibleTypes,
typePolicies,
2021-04-29 21:17:54 +05:30
...cacheOptions,
2020-11-24 15:15:51 +05:30
});
2021-06-08 01:23:25 +05:30
const mockClient = createMockApolloClient({ cache, resolvers });
2020-11-24 15:15:51 +05:30
if (Array.isArray(handlers)) {
2022-04-04 11:22:00 +05:30
handlers.forEach(([query, value]) =>
mockClient.setRequestHandler(query, (...args) =>
Promise.resolve(value(...args)).then((r) => ({ ...r })),
),
);
2020-11-24 15:15:51 +05:30
} else {
throw new Error('You should pass an array of handlers to mock Apollo client');
}
2021-06-08 01:23:25 +05:30
return mockClient;
}
export default function createMockApollo(handlers, resolvers, cacheOptions) {
const mockClient = createMockClient(handlers, resolvers, cacheOptions);
2022-01-26 12:08:38 +05:30
return new VueApollo({ defaultClient: mockClient });
2021-06-08 01:23:25 +05:30
}