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
…3362)

* Add test to verify error is persisted on re-render, when appropriate

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.

* Changelog update
  • Loading branch information
hwillson committed Aug 13, 2019
1 parent da470c6 commit 3e0a436
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
2 changes: 2 additions & 0 deletions Changelog.md
Expand Up @@ -19,6 +19,8 @@
[@hwillson](https://github.com/hwillson) in [#3337](https://github.com/apollographql/react-apollo/pull/3337)
- Add `@types/react` as a peer dep, to address potential TS compilation errors when using `ApolloProvider`. <br/>
[@zkochan](https://github.com/zkochan) in [#3278](https://github.com/apollographql/react-apollo/pull/3278)
- Make sure `error`'s are maintained after re-renders, when they should be. <br/>
[@hwillson](https://github.com/hwillson) in [#3362](https://github.com/apollographql/react-apollo/pull/3362)

## 3.0.0 (2019-08-06)

Expand Down
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 3e0a436

Please sign in to comment.