Last active
December 16, 2017 13:35
-
-
Save adambisek/491869a5c730e49abe9d6215d0d4dd1b to your computer and use it in GitHub Desktop.
This file contains hidden or 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 { ApolloClient, ApolloProvider } from 'react-apollo'; | |
| import renderer from 'react-test-renderer'; | |
| import MockNetworkInterface from 'apollo-mocknetworkinterface'; | |
| import TestedComponentWithApollo from './TestedComponentWithApollo'; | |
| // create mocked network interface, to simulate communication with graphQL server | |
| const networkInterface = new MockNetworkInterface(...); | |
| const client = new ApolloClient({ networkInterface, addTypename: false }); | |
| // test in Jest | |
| it('render without exploding', (done) => { | |
| const component = renderer.create(( | |
| <ApolloProvider client={client}> | |
| <div> | |
| <TestedComponentWithApollo /> | |
| </div> | |
| </ApolloProvider> | |
| )); | |
| // first is loading | |
| const tree = component.toJSON(); | |
| expect(tree.children[0].children[0]).toMatch('loading ...'); | |
| expect(tree).toMatchSnapshot(); | |
| // wait until data arrive | |
| setTimeout(() => { | |
| try { | |
| const tree2 = component.toJSON(); | |
| expect(tree2.children[0].children).toMatchObject([ | |
| 'got data ... ', | |
| ]); | |
| expect(tree2).toMatchSnapshot(); | |
| } catch (e) { | |
| return done.fail(e); | |
| } | |
| return done(); | |
| }, 2500); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment