Skip to content

Commit

Permalink
Add a failing test of the deferred fetchMore query.
Browse files Browse the repository at this point in the history
  • Loading branch information
jerelmiller committed Jul 7, 2023
1 parent b047048 commit 34a2dd7
Showing 1 changed file with 288 additions and 1 deletion.
289 changes: 288 additions & 1 deletion src/react/hooks/__tests__/useSuspenseQuery.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7056,8 +7056,12 @@ describe('useSuspenseQuery', () => {
// This test reflects the behavior as it exists today, but will need
// to be updated once the core bug is fixed.
//
// NOTE: A duplicate it.failng test has been added right below this one with
// the expected behavior added in (i.e. the commented code in this test). Once
// the core bug is fixed, this test can be removed in favor of the other test.
//
// https://github.com/apollographql/apollo-client/issues/11034
it('incrementally rerenders data returned by a `fetchMore` for a deferred query', async () => {
it('rerenders data returned by `fetchMore` for a deferred query', async () => {
const query = gql`
query ($offset: Int) {
greetings(offset: $offset) {
Expand Down Expand Up @@ -7332,6 +7336,289 @@ describe('useSuspenseQuery', () => {
]);
});

// TODO: This is a duplicate of the test above, but with the expected behavior
// added (hence the `it.failing`). Remove the previous test once issue #11034
// is fixed.
//
// https://github.com/apollographql/apollo-client/issues/11034
it.failing(
'incrementally rerenders data returned by a `fetchMore` for a deferred query',
async () => {
const query = gql`
query ($offset: Int) {
greetings(offset: $offset) {
message
... @defer {
recipient {
name
}
}
}
}
`;

const cache = new InMemoryCache({
typePolicies: {
Query: {
fields: {
greetings: offsetLimitPagination(),
},
},
},
});
const link = new MockSubscriptionLink();
const client = new ApolloClient({ link, cache });

const { result, renders } = renderSuspenseHook(
() => useSuspenseQuery(query, { variables: { offset: 0 } }),
{ client }
);

link.simulateResult({
result: {
data: {
greetings: [{ __typename: 'Greeting', message: 'Hello world' }],
},
hasNext: true,
},
});

await waitFor(() => {
expect(result.current).toMatchObject({
data: {
greetings: [
{
__typename: 'Greeting',
message: 'Hello world',
},
],
},
networkStatus: NetworkStatus.ready,
error: undefined,
});
});

link.simulateResult(
{
result: {
incremental: [
{
data: {
recipient: { name: 'Alice', __typename: 'Person' },
},
path: ['greetings', 0],
},
],
hasNext: false,
},
},
true
);

await waitFor(() => {
expect(result.current).toMatchObject({
data: {
greetings: [
{
__typename: 'Greeting',
message: 'Hello world',
recipient: {
__typename: 'Person',
name: 'Alice',
},
},
],
},
networkStatus: NetworkStatus.ready,
error: undefined,
});
});

let fetchMorePromise: Promise<ApolloQueryResult<unknown>>;
act(() => {
fetchMorePromise = result.current.fetchMore({
variables: { offset: 1 },
});
});

link.simulateResult({
result: {
data: {
greetings: [
{
__typename: 'Greeting',
message: 'Goodbye',
},
],
},
hasNext: true,
},
});

await waitFor(() => {
expect(result.current).toMatchObject({
data: {
greetings: [
{
__typename: 'Greeting',
message: 'Hello world',
recipient: {
__typename: 'Person',
name: 'Alice',
},
},
{
__typename: 'Greeting',
message: 'Goodbye',
},
],
},
networkStatus: NetworkStatus.ready,
error: undefined,
});
});

link.simulateResult(
{
result: {
incremental: [
{
data: {
recipient: { name: 'Bob', __typename: 'Person' },
},
path: ['greetings', 0],
},
],
hasNext: false,
},
},
true
);

await waitFor(() => {
expect(result.current).toMatchObject({
data: {
greetings: [
{
__typename: 'Greeting',
message: 'Hello world',
recipient: {
__typename: 'Person',
name: 'Alice',
},
},
{
__typename: 'Greeting',
message: 'Goodbye',
recipient: {
__typename: 'Person',
name: 'Bob',
},
},
],
},
networkStatus: NetworkStatus.ready,
error: undefined,
});
});

await expect(fetchMorePromise!).resolves.toEqual({
data: {
greetings: [
{
__typename: 'Greeting',
message: 'Goodbye',
recipient: {
__typename: 'Person',
name: 'Bob',
},
},
],
},
loading: false,
networkStatus: NetworkStatus.ready,
error: undefined,
});

expect(renders.count).toBe(5);
expect(renders.suspenseCount).toBe(2);
expect(renders.frames).toMatchObject([
{
data: {
greetings: [
{
__typename: 'Greeting',
message: 'Hello world',
},
],
},
networkStatus: NetworkStatus.ready,
error: undefined,
},
{
data: {
greetings: [
{
__typename: 'Greeting',
message: 'Hello world',
recipient: {
__typename: 'Person',
name: 'Alice',
},
},
],
},
networkStatus: NetworkStatus.ready,
error: undefined,
},
{
data: {
greetings: [
{
__typename: 'Greeting',
message: 'Hello world',
recipient: {
__typename: 'Person',
name: 'Alice',
},
},
{
__typename: 'Greeting',
message: 'Goodbye',
},
],
},
networkStatus: NetworkStatus.ready,
error: undefined,
},
{
data: {
greetings: [
{
__typename: 'Greeting',
message: 'Hello world',
recipient: {
__typename: 'Person',
name: 'Alice',
},
},
{
__typename: 'Greeting',
message: 'Goodbye',
recipient: {
__typename: 'Person',
name: 'Bob',
},
},
],
},
networkStatus: NetworkStatus.ready,
error: undefined,
},
]);
}
);

it('throws network errors returned by deferred queries', async () => {
const consoleSpy = jest.spyOn(console, 'error').mockImplementation();

Expand Down

0 comments on commit 34a2dd7

Please sign in to comment.