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

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

Merged
merged 2 commits into from
Aug 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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>
);
});
});
});