summaryrefslogtreecommitdiff
path: root/spec/frontend/__helpers__/mock_apollo_helper.js
blob: bd97a06071a4b14623f7801f20bbc8dce7b01222 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import { InMemoryCache } from 'apollo-cache-inmemory';
import { createMockClient } from 'mock-apollo-client';
import VueApollo from 'vue-apollo';

const defaultCacheOptions = {
  fragmentMatcher: { match: () => true },
  addTypename: false,
};

export default (handlers = [], resolvers = {}, cacheOptions = {}) => {
  const cache = new InMemoryCache({
    ...defaultCacheOptions,
    ...cacheOptions,
  });

  const mockClient = createMockClient({ cache, resolvers });

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

  const apolloProvider = new VueApollo({ defaultClient: mockClient });

  return apolloProvider;
};