This repository has been archived by the owner on Jan 10, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 227
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix cache-less queries in SSR (#770)
- Loading branch information
Showing
2 changed files
with
90 additions
and
4 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/** | ||
* @jest-environment node | ||
*/ | ||
|
||
import React from 'react'; | ||
import {renderToString} from 'react-dom/server'; | ||
import gql from 'graphql-tag'; | ||
import {FetchPolicy} from 'apollo-client'; | ||
import {createGraphQLFactory} from '@shopify/graphql-testing'; | ||
import {extract} from '@shopify/react-effect/server'; | ||
|
||
import useQuery from '../query'; | ||
import {ApolloProvider} from '../../ApolloProvider'; | ||
|
||
const petQuery = gql` | ||
query PetQuery { | ||
pets { | ||
name | ||
} | ||
} | ||
`; | ||
|
||
const createGraphQL = createGraphQLFactory(); | ||
const mockData = { | ||
pets: [ | ||
{ | ||
__typename: 'Cat', | ||
name: 'Garfield', | ||
}, | ||
], | ||
}; | ||
|
||
function MockQuery({fetchPolicy = 'network-only' as FetchPolicy}) { | ||
const {data, loading} = useQuery(petQuery, {fetchPolicy}); | ||
return loading ? <div>loading</div> : <pre>{JSON.stringify(data)}</pre>; | ||
} | ||
|
||
describe('useQuery', () => { | ||
it('does not loop infinitely when a query has network-only during SSR', async () => { | ||
const graphQL = createGraphQL({PetQuery: mockData}); | ||
const afterEachSpy = jest.fn(); | ||
|
||
const extractPromise = extract( | ||
<ApolloProvider client={graphQL.client}> | ||
<MockQuery fetchPolicy="network-only" /> | ||
</ApolloProvider>, | ||
{ | ||
afterEachPass: afterEachSpy, | ||
}, | ||
); | ||
|
||
await graphQL.resolveAll(); | ||
await extractPromise; | ||
|
||
// One call for the first pass, another call after the GraphQL | ||
// resolves | ||
expect(afterEachSpy).toHaveBeenCalledTimes(2); | ||
}); | ||
|
||
it('does not run a query with the no-cache fetch policy during SSR', async () => { | ||
const graphQL = createGraphQL({PetQuery: mockData}); | ||
const afterEachSpy = jest.fn(); | ||
const element = ( | ||
<ApolloProvider client={graphQL.client}> | ||
<MockQuery fetchPolicy="no-cache" /> | ||
</ApolloProvider> | ||
); | ||
|
||
const extractPromise = extract(element, { | ||
afterEachPass: afterEachSpy, | ||
}); | ||
|
||
await graphQL.resolveAll(); | ||
await extractPromise; | ||
|
||
// One call for the first pass, which is the only one because there are | ||
// no GraphQL queries to resolve. | ||
expect(afterEachSpy).toHaveBeenCalledTimes(1); | ||
|
||
expect(renderToString(element)).toContain('loading'); | ||
}); | ||
}); |