Skip to content

Commit

Permalink
update partialRefetch tests
Browse files Browse the repository at this point in the history
  • Loading branch information
brainkim committed Aug 10, 2021
1 parent 482a98d commit c2fa63f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 51 deletions.
24 changes: 13 additions & 11 deletions src/react/components/__tests__/client/Query.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1785,16 +1785,20 @@ describe('Query component', () => {
});

describe('Partial refetching', () => {
const origConsoleWarn = console.warn;
let errorSpy!: ReturnType<typeof jest.spyOn>;

beforeAll(() => {
console.warn = () => null;
beforeEach(() => {
errorSpy = jest.spyOn(console, 'error')
.mockImplementation(() => {});
});

afterAll(() => {
console.warn = origConsoleWarn;
errorSpy.mockRestore();
});

// TODO(brian): This is a terrible legacy test which is causing console
// error calls no matter what I try and I do not want to care about it
// anymore :)
itAsync(
'should attempt a refetch when the query result was marked as being ' +
'partial, the returned data was reset to an empty Object by the ' +
Expand All @@ -1811,11 +1815,10 @@ describe('Query component', () => {
}
`;

let done = false;
const allPeopleData = {
allPeople: { people: [{ name: 'Luke Skywalker' }] },
};
const errorSpy = jest.spyOn(console, 'error')
.mockImplementation(() => {});
const query = allPeopleQuery;
const link = mockSingleLink(
{ request: { query }, result: { data: {} } },
Expand All @@ -1834,6 +1837,9 @@ describe('Query component', () => {
try {
if (!loading) {
expect(data).toEqual(allPeopleData);
expect(errorSpy).toHaveBeenCalledTimes(1);
expect(errorSpy.mock.calls[0][0]).toMatch('Missing field');
done = true;
}
} catch (err) {
reject(err);
Expand All @@ -1849,11 +1855,7 @@ describe('Query component', () => {
</ApolloProvider>
);

return wait().then(() => {
expect(errorSpy).toHaveBeenCalledTimes(1);
expect(errorSpy.mock.calls[0][0]).toMatch('Missing field');
errorSpy.mockRestore();
}).then(resolve, reject);
wait(() => done).then(resolve, reject);
}
);

Expand Down
55 changes: 15 additions & 40 deletions src/react/hooks/__tests__/useQuery.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2241,6 +2241,7 @@ describe('useQuery Hook', () => {
{
request: { query },
result: { data: {} },
delay: 20,
},
{
request: { query },
Expand Down Expand Up @@ -2270,34 +2271,25 @@ describe('useQuery Hook', () => {

expect(result.current.loading).toBe(true);
expect(result.current.data).toBe(undefined);
expect(result.current.error).toBe(undefined);
expect(result.current.networkStatus).toBe(NetworkStatus.loading);

const updates = result.all.length;
await waitForNextUpdate();
expect(result.all.length - updates).toBe(2);
// waitForUpdate seems to miss the erroring render
const previous = result.all[result.all.length - 2];
if (previous instanceof Error) {
throw previous;
}

expect(previous.loading).toBe(true);
expect(previous.error).toBe(undefined);
expect(previous.data).toBe(undefined);

expect(result.current.loading).toBe(true);
expect(result.current.error).toBe(undefined);
expect(result.current.data).toBe(undefined);
expect(result.current.error).toBe(undefined);
expect(result.current.networkStatus).toBe(NetworkStatus.refetch);

expect(errorSpy).toHaveBeenCalledTimes(1);
expect(errorSpy.mock.calls[0][0]).toMatch('Missing field');
errorSpy.mockRestore();

await waitForNextUpdate();

expect(result.current.loading).toBe(false);
expect(result.current.data).toEqual({ hello: 'world' });
expect(result.current.error).toBe(undefined);
expect(result.current.networkStatus).toBe(NetworkStatus.ready);
errorSpy.mockRestore();
});

it('should attempt a refetch when data is missing and partialRefetch is true 2', async () => {
Expand All @@ -2318,7 +2310,7 @@ describe('useQuery Hook', () => {
const errorSpy = jest.spyOn(console, 'error')
.mockImplementation(() => {});
const link = mockSingleLink(
{ request: { query }, result: { data: {} } },
{ request: { query }, result: { data: {} }, delay: 20 },
{ request: { query }, result: { data }, delay: 20 }
);

Expand All @@ -2343,24 +2335,13 @@ describe('useQuery Hook', () => {

expect(result.current.loading).toBe(true);
expect(result.current.data).toBe(undefined);
expect(result.current.error).toBe(undefined);
expect(result.current.networkStatus).toBe(NetworkStatus.loading);

const updates = result.all.length;
await waitForNextUpdate();
expect(result.all.length - updates).toBe(2);
// waitForUpdate seems to miss the erroring render
const previous = result.all[result.all.length - 2];
if (previous instanceof Error) {
throw previous;
}

expect(previous.loading).toBe(true);
expect(previous.error).toBe(undefined);
expect(previous.data).toBe(undefined);

expect(result.current.loading).toBe(true);
expect(result.current.error).toBe(undefined);
expect(result.current.data).toBe(undefined);
expect(result.current.error).toBe(undefined);
expect(result.current.networkStatus).toBe(NetworkStatus.refetch);

expect(errorSpy).toHaveBeenCalledTimes(1);
Expand All @@ -2370,6 +2351,7 @@ describe('useQuery Hook', () => {
await waitForNextUpdate();
expect(result.current.loading).toBe(false);
expect(result.current.data).toEqual(data);
expect(result.current.error).toBe(undefined);
expect(result.current.networkStatus).toBe(NetworkStatus.ready);
});

Expand All @@ -2382,6 +2364,7 @@ describe('useQuery Hook', () => {
{
request: { query },
result: { data: {} },
delay: 20,
},
{
request: { query },
Expand Down Expand Up @@ -2412,22 +2395,12 @@ describe('useQuery Hook', () => {

expect(result.current.loading).toBe(true);
expect(result.current.data).toBe(undefined);
expect(result.current.error).toBe(undefined);
expect(result.current.networkStatus).toBe(NetworkStatus.loading);

const updates = result.all.length;
await waitForNextUpdate();
expect(result.all.length - updates).toBe(2);
// waitForUpdate seems to miss the erroring render
const previous = result.all[result.all.length - 2];
if (previous instanceof Error) {
throw previous;
}

expect(previous.loading).toBe(true);
expect(previous.error).toBe(undefined);
expect(previous.data).toBe(undefined);

expect(result.current.loading).toBe(true);
expect(result.current.error).toBe(undefined);
expect(result.current.data).toBe(undefined);
expect(result.current.networkStatus).toBe(NetworkStatus.refetch);

Expand All @@ -2436,8 +2409,10 @@ describe('useQuery Hook', () => {
errorSpy.mockRestore();

await waitForNextUpdate();

expect(result.current.loading).toBe(false);
expect(result.current.data).toEqual({ hello: 'world' });
expect(result.current.error).toBe(undefined);
expect(result.current.networkStatus).toBe(NetworkStatus.ready);
});
});
Expand Down

0 comments on commit c2fa63f

Please sign in to comment.