Skip to content

Instantly share code, notes, and snippets.

@tvler
Created January 14, 2021 00:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tvler/9c2bb54a42b49cb0c8e67f93149b5673 to your computer and use it in GitHub Desktop.
Save tvler/9c2bb54a42b49cb0c8e67f93149b5673 to your computer and use it in GitHub Desktop.
A mocked version of ApolloClient for Jest tests
import { ApolloCache, ApolloClient, ApolloLink, NormalizedCacheObject } from '@apollo/client';
class MockedApolloLink extends ApolloLink {
split = jest.fn();
concat = jest.fn();
request = jest.fn();
setOnError = jest.fn();
protected onError = jest.fn();
}
class MockedApolloCache extends ApolloCache<NormalizedCacheObject> {
read = jest.fn();
write = jest.fn();
diff = jest.fn();
watch = jest.fn();
reset = jest.fn();
evict = jest.fn();
restore = jest.fn();
extract = jest.fn();
removeOptimistic = jest.fn();
performTransaction = jest.fn();
transformDocument = jest.fn();
gc = jest.fn();
identify = jest.fn();
readQuery = jest.fn();
modify = jest.fn();
readFragment = jest.fn();
writeFragment = jest.fn();
transformForLink = jest.fn();
writeQuery = jest.fn();
recordOptimisticTransaction = jest.fn();
}
class MockedApolloClient extends ApolloClient<NormalizedCacheObject> {
link = new MockedApolloLink();
cache = new MockedApolloCache();
stop = jest.fn();
query = jest.fn();
readFragment = jest.fn();
writeQuery = jest.fn();
readQuery = jest.fn();
writeFragment = jest.fn();
restore = jest.fn();
watchQuery = jest.fn();
mutate = jest.fn();
setLink = jest.fn();
setLocalStateFragmentMatcher = jest.fn();
setResolvers = jest.fn();
subscribe = jest.fn();
resetStore = jest.fn();
clearStore = jest.fn();
onClearStore = jest.fn();
onResetStore = jest.fn();
reFetchObservableQueries = jest.fn();
extract = jest.fn();
add = jest.fn();
getResolvers = jest.fn();
addResolvers = jest.fn();
__actionHookForDevTools = jest.fn();
__requestRaw = jest.fn();
defaultOptions = {};
disableNetworkFetches = false;
queryDeduplication = false;
typeDefs = undefined;
version = '';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment