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

33 lines
1,009 B
JavaScript
Raw Normal View History

2020-11-24 15:15:51 +05:30
import { InMemoryCache } from 'apollo-cache-inmemory';
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';
2021-04-29 21:17:54 +05:30
const defaultCacheOptions = {
fragmentMatcher: { match: () => true },
addTypename: false,
};
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({
2021-04-29 21:17:54 +05:30
...defaultCacheOptions,
...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)) {
handlers.forEach(([query, value]) => mockClient.setRequestHandler(query, value));
} 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);
2020-11-24 15:15:51 +05:30
const apolloProvider = new VueApollo({ defaultClient: mockClient });
return apolloProvider;
2021-06-08 01:23:25 +05:30
}