Skip to content

Instantly share code, notes, and snippets.

@steniowagner
Last active April 1, 2020 02:03
Show Gist options
  • Save steniowagner/938860aad836d873bf07a3e9287cb13b to your computer and use it in GitHub Desktop.
Save steniowagner/938860aad836d873bf07a3e9287cb13b to your computer and use it in GitHub Desktop.
// MockedError Provider
import React from 'react';
import { ApolloProvider } from '@apollo/react-hooks';
import { InMemoryCache } from 'apollo-cache-inmemory';
import { ApolloLink, Observable } from 'apollo-link';
import { ApolloClient } from 'apollo-client';
import { GraphQLError } from 'graphql';
type Props = {
errors: Readonly<GraphQLError[]>;
children: JSX.Element;
};
const MockedErrorProvider = ({ errors, children }: Props) => {
const link = new ApolloLink(() => new Observable((observer) => {
observer.next({
errors,
});
observer.complete();
}));
const client = new ApolloClient({
cache: new InMemoryCache(),
link,
});
return (
<ApolloProvider
client={client}
>
{children}
</ApolloProvider>
);
};
export default MockedErrorProvider;
// Usage
it('should render the advise screen when has a network error', () => {
render(
<AuthoMockedErrorProvider
errors={[new GraphQLError(CONSTANTS.ERROR_MESSAGES.NETWORK_FAILED_CONNECTION)]}
>
<ThemeProvider
theme={dark}
>
<News
navigation={navigation}
/>
</ThemeProvider>
</AuthoMockedErrorProvider>,
);
act(() => {
jest.runAllTimers();
});
});
// Verifying the error
if (error && error.message === CONSTANTS.ERROR_MESSAGES.NETWORK_FAILED_CONNECTION) {
// handle the specified error
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment