Created
April 24, 2018 14:29
-
-
Save nthock/71d1ff35695a43c480c87ca5ef8e7029 to your computer and use it in GitHub Desktop.
Example of Mocking GraphQL Apollo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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