Skip to content

Instantly share code, notes, and snippets.

@nthock
Created April 24, 2018 14:29
Show Gist options
  • Save nthock/71d1ff35695a43c480c87ca5ef8e7029 to your computer and use it in GitHub Desktop.
Save nthock/71d1ff35695a43c480c87ca5ef8e7029 to your computer and use it in GitHub Desktop.
Example of Mocking GraphQL Apollo
// project_root/__mocks__/react-apollo.js
// Mock for node_modules must be adjacent to the node_modules folder
import React from "react";
const actualReactApollo = require.requireActual("react-apollo");
let mockProps = {};
const setMockGraphQLProps = (props) => { mockProps = props; };
const graphql = () => Component => props => <Component {...mockProps} {...props} />
const { compose, createBatchingNetworkInterface, ApolloClient } = actualReactApollo;
export {
graphql,
compose,
setMockGraphQLProps,
createBatchingNetworkInterface,
ApolloClient
};
import React from "react";
import { graphql } from "react-apollo";
import { compose } from "recompose";
import { someQuery } from "SOMEPATH";
export const VersionNumber = ({ data: { config: { version } } }) => (
<div>
v{version}
</div>
);
const enhancedComponent = compose(
graphql(someQuery),
)(VersionNumber);
export default enhancedComponent;
import React from "react";
import renderer from "react-test-renderer";
import { setMockGraphQLProps } from "react-apollo";
import VersionNumber from "../helpers/VersionNumber";
const mockGraphQLProps = {
data: {
config: { version: '3.5.2' }
}
};
describe.only("when configuration is fetched", () => {
test("it should properly rendered", () => {
setMockGraphQLProps(mockGraphQLProps);
const tree = renderer.create(<VersionNumber />).toJSON();
console.log(tree);
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment