Created
September 18, 2020 15:14
-
-
Save boris317/2f8675bb121d8aa95eae13fea0e10be7 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
export function renderComponent(component, query, variables) { | |
let {loading, error, data} = useQuery(query, {variables}); | |
if (loading) { | |
return null; | |
} | |
if (error) { | |
console.log(error); | |
} | |
return React.createElement(component, data); | |
} |
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 {gql} from '@apollo/client'; | |
import {MockedProvider} from '@apollo/client/testing'; | |
import assert from 'assert'; | |
import {renderComponent} from '../graphql'; | |
import {renderToStringWithData} from '@apollo/client/react/ssr'; | |
const TEST_QUERY = gql` | |
{ | |
getUser(username: "foo") { | |
} | |
} | |
`; | |
function TestComponent(data) { | |
return <p>{data.user.email}</p>; | |
} | |
function TestRender({component, query, variables}) { | |
return renderComponent(component, query, variables); | |
} | |
const mocks = [ | |
{ | |
request: { | |
query: TEST_QUERY, | |
variables: { | |
username: 'foo', | |
}, | |
}, | |
result: { | |
data: { | |
user: {id: '1', email: '[email protected]', username: 'foo'}, | |
}, | |
}, | |
}, | |
]; | |
describe('graphql', () => { | |
it('renderComponent fetches data for a component', async () => { | |
const component = ( | |
<MockedProvider mocks={mocks} addTypename={false}> | |
<TestRender component={TestComponent} query={TEST_QUERY} variables={{username: 'foo'}} /> | |
</MockedProvider> | |
); | |
const content = await renderToStringWithData(component); | |
// This fails because content = <p></p> | |
assert.deepStrictEqual(content, "<p>[email protected]</p>"); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment