Skip to content
This repository has been archived by the owner on Apr 13, 2023. It is now read-only.

Commit

Permalink
Add test to verify error is persisted on re-render, when appropriate
Browse files Browse the repository at this point in the history
Issue #3295 demonstrates a problem where errors aren't maintained
on re-renders, when they should still be available. This issue
was fixed by PR #3339, but this commit will add an extra test to
verify that the issue is indeed fixed.

Fixes #3295.
  • Loading branch information
hwillson committed Aug 13, 2019
1 parent da470c6 commit 691b80d
Showing 1 changed file with 55 additions and 1 deletion.
56 changes: 55 additions & 1 deletion packages/hooks/src/__tests__/useQuery.test.tsx
@@ -1,4 +1,4 @@
import React, { useState } from 'react';
import React, { useState, useReducer } from 'react';
import { DocumentNode, GraphQLError } from 'graphql';
import gql from 'graphql-tag';
import { MockedProvider, MockLink } from '@apollo/react-testing';
Expand Down Expand Up @@ -319,5 +319,59 @@ describe('useQuery Hook', () => {
</ApolloProvider>
);
});

it('should persist errors on re-render if they are still valid', done => {
const query = gql`
query SomeQuery {
stuff {
thing
}
}
`;

const mocks = [
{
request: { query },
result: {
errors: [new GraphQLError('forced error')]
}
}
];

let renderCount = 0;
function App() {
const [_, forceUpdate] = useReducer(x => x + 1, 0);
const { loading, error } = useQuery(query);

switch (renderCount) {
case 0:
expect(loading).toBeTruthy();
expect(error).toBeUndefined();
break;
case 1:
expect(error).toBeDefined();
expect(error!.message).toEqual('GraphQL error: forced error');
setTimeout(() => {
forceUpdate(0);
});
break;
case 2:
expect(error).toBeDefined();
expect(error!.message).toEqual('GraphQL error: forced error');
done();
break;
default: // Do nothing
}

renderCount += 1;
return null;
}

render(
<MockedProvider mocks={mocks}>
<App />
</MockedProvider>
);
});
});
});

0 comments on commit 691b80d

Please sign in to comment.