Supports both Apollo 1 & Apollo 2
Installation
To install the stable version:
npm install --save apollo-mocknetworkinterface@^1.0
What is the purpose of the package?
Please read https://medium.com/p/ef0bbd17e686
Example
import React from 'react';
import { ApolloClient, ApolloProvider, graphql } from 'react-apollo';
import renderer from 'react-test-renderer';
import MockNetworkInterface from 'apollo-mocknetworkinterface';
import query from './query.graphql';
const TestComponent = (props) => {
if (props.data.loading) {
return (<div>loading ... {JSON.stringify(props)}</div>);
}
return (<div>got data ... {JSON.stringify(props)}</div>);
};
const TestComponentWithApollo = graphql(query, {
options: props => ({
variables: { url: props.url },
}),
})(TestComponent);
const createResponse = (request) => {
console.log('creating mocked response for request:', request);
return {
data: {
component: {
id: 1,
name: 'foo',
},
},
};
};
const mockedNetworkFetch = createMockedNetworkFetch(createResponse, { timeout: 100 });
const client = new ApolloClient({
link: createHttpLink({ uri: 'http://localhost:3000', fetch: mockedNetworkFetch }),
cache: new InMemoryCache(),
});
it('should render without exploding', (done) => {
const component = renderer.create((
<ApolloProvider client={client}>
<div>
<TestComponentWithApollo />
</div>
</ApolloProvider>
));
})