2020-09-04 08:08:27 -04:00
|
|
|
import { InMemoryCache } from 'apollo-cache-inmemory';
|
2021-04-22 11:09:56 -04:00
|
|
|
import { createMockClient as createMockApolloClient } from 'mock-apollo-client';
|
2020-09-04 08:08:27 -04:00
|
|
|
import VueApollo from 'vue-apollo';
|
|
|
|
|
2021-03-23 08:09:33 -04:00
|
|
|
const defaultCacheOptions = {
|
|
|
|
fragmentMatcher: { match: () => true },
|
|
|
|
addTypename: false,
|
|
|
|
};
|
|
|
|
|
2021-04-22 11:09:56 -04:00
|
|
|
export function createMockClient(handlers = [], resolvers = {}, cacheOptions = {}) {
|
2020-09-04 08:08:27 -04:00
|
|
|
const cache = new InMemoryCache({
|
2021-03-23 08:09:33 -04:00
|
|
|
...defaultCacheOptions,
|
|
|
|
...cacheOptions,
|
2020-09-04 08:08:27 -04:00
|
|
|
});
|
|
|
|
|
2021-04-22 11:09:56 -04:00
|
|
|
const mockClient = createMockApolloClient({ cache, resolvers });
|
2020-09-04 08:08:27 -04:00
|
|
|
|
|
|
|
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-04-22 11:09:56 -04:00
|
|
|
return mockClient;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default function createMockApollo(handlers, resolvers, cacheOptions) {
|
|
|
|
const mockClient = createMockClient(handlers, resolvers, cacheOptions);
|
2020-09-04 08:08:27 -04:00
|
|
|
const apolloProvider = new VueApollo({ defaultClient: mockClient });
|
|
|
|
|
|
|
|
return apolloProvider;
|
2021-04-22 11:09:56 -04:00
|
|
|
}
|