-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
873e7de
commit 664edc2
Showing
12 changed files
with
553 additions
and
19 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,12 @@ | ||
import React, { Suspense, SuspenseProps, useContext } from 'react'; | ||
import { SSRContext } from './internal/SSRContext'; | ||
|
||
export function unstable_SuspenseSSR({ children, fallback }: SuspenseProps) { | ||
const ssrManager = useContext(SSRContext); | ||
|
||
return ssrManager ? ( | ||
<>{children}</> | ||
) : ( | ||
<Suspense fallback={fallback}>{children}</Suspense> | ||
); | ||
} |
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,110 @@ | ||
import ApolloClient from 'apollo-client'; | ||
import { ApolloLink } from 'apollo-link'; | ||
import { MockedResponse } from 'apollo-link-mock'; | ||
import gql from 'graphql-tag'; | ||
import * as React from 'react'; | ||
import { renderToString } from 'react-dom/server'; | ||
import { render } from 'react-testing-library'; | ||
|
||
import { ApolloProvider } from '../ApolloContext'; | ||
import { unstable_SuspenseSSR as SuspenseSSR } from '../SuspenseSSR'; | ||
import createClient from '../__testutils__/createClient'; | ||
import flushEffectsAndWait from '../__testutils__/flushEffectsAndWait'; | ||
import { getMarkupFromTree } from '../getMarkupFromTree'; | ||
import { QueryHookOptions, useQuery } from '../useQuery'; | ||
|
||
const USER_QUERY = gql` | ||
{ | ||
currentUser { | ||
firstName | ||
} | ||
} | ||
`; | ||
|
||
interface UserQueryResult { | ||
currentUser: { firstName: string }; | ||
} | ||
|
||
const MOCKS: MockedResponse[] = [ | ||
{ | ||
request: { query: USER_QUERY }, | ||
result: { data: { currentUser: { firstName: 'James' } } }, | ||
}, | ||
]; | ||
|
||
function createMockClient(link?: ApolloLink) { | ||
return createClient({ link, mocks: MOCKS, addTypename: false }); | ||
} | ||
|
||
function UserDetails(props: QueryHookOptions<{}>) { | ||
const { data, loading } = useQuery<UserQueryResult>(USER_QUERY, props); | ||
|
||
return ( | ||
<> | ||
{loading | ||
? 'Loading' | ||
: !data | ||
? 'No Data' | ||
: !data.currentUser | ||
? 'No Current User' | ||
: data.currentUser.firstName} | ||
</> | ||
); | ||
} | ||
|
||
interface UserWrapperProps extends QueryHookOptions<{}> { | ||
readonly client: ApolloClient<object>; | ||
} | ||
|
||
function UserDetailsWrapper({ client, ...props }: UserWrapperProps) { | ||
return ( | ||
<ApolloProvider client={client}> | ||
<SuspenseSSR fallback={<>Loading with suspense</>}> | ||
<UserDetails {...props} /> | ||
</SuspenseSSR> | ||
</ApolloProvider> | ||
); | ||
} | ||
|
||
describe.each([[true], [false]])('SuspenseSSR with "suspend: %s"', suspend => { | ||
it('not throws in react-dom', async () => { | ||
const client = createMockClient(); | ||
|
||
const { container } = render( | ||
<UserDetailsWrapper client={client} suspend={suspend} /> | ||
); | ||
|
||
if (suspend) { | ||
expect(container).toMatchInlineSnapshot(` | ||
<div> | ||
Loading with suspense | ||
</div> | ||
`); | ||
} else { | ||
expect(container).toMatchInlineSnapshot(` | ||
<div> | ||
Loading | ||
</div> | ||
`); | ||
} | ||
|
||
await flushEffectsAndWait(); | ||
|
||
expect(container).toMatchInlineSnapshot(` | ||
<div> | ||
James | ||
</div> | ||
`); | ||
}); | ||
|
||
it('not throws in react-dom/server', async () => { | ||
const client = createMockClient(); | ||
|
||
await expect( | ||
getMarkupFromTree({ | ||
renderFunction: renderToString, | ||
tree: <UserDetailsWrapper client={client} suspend={suspend} />, | ||
}) | ||
).resolves.toMatchInlineSnapshot(`"James"`); | ||
}); | ||
}); |
Oops, something went wrong.