Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

only increment lastRequestId when using links to get results #7956

Merged
merged 4 commits into from
Apr 7, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
write test to ensure lastRequestId only increments when we expect it to
  • Loading branch information
Daniel authored and benjamn committed Apr 7, 2021
commit 4718ea0755616aa8f53d6e472c34931a60bcedbe
47 changes: 22 additions & 25 deletions src/core/__tests__/ObservableQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,28 @@ import mockQueryManager from '../../utilities/testing/mocking/mockQueryManager';
import mockWatchQuery from '../../utilities/testing/mocking/mockWatchQuery';
import wrap from '../../utilities/testing/wrap';

export const mockFetchQuery = (queryManager: QueryManager<any>) => {
const fetchQueryObservable = queryManager.fetchQueryObservable;
const fetchQueryByPolicy: QueryManager<any>["fetchQueryByPolicy"] =
(queryManager as any).fetchQueryByPolicy;

const mock = <T extends
| typeof fetchQueryObservable
| typeof fetchQueryByPolicy
>(original: T) => jest.fn<ReturnType<T>, Parameters<T>>(function () {
return original.apply(queryManager, arguments);
});

const mocks = {
fetchQueryObservable: mock(fetchQueryObservable),
fetchQueryByPolicy: mock(fetchQueryByPolicy),
};

Object.assign(queryManager, mocks);

return mocks;
};

describe('ObservableQuery', () => {
// Standard data for all these tests
const query = gql`
Expand Down Expand Up @@ -929,31 +951,6 @@ describe('ObservableQuery', () => {
});

describe('refetch', () => {
function mockFetchQuery(queryManager: QueryManager<any>) {
const fetchQueryObservable = queryManager.fetchQueryObservable;
const fetchQueryByPolicy: QueryManager<any>["fetchQueryByPolicy"] =
(queryManager as any).fetchQueryByPolicy;

const mock = <T extends
| typeof fetchQueryObservable
| typeof fetchQueryByPolicy
>(original: T) => jest.fn<
ReturnType<T>,
Parameters<T>
>(function () {
return original.apply(queryManager, arguments);
});

const mocks = {
fetchQueryObservable: mock(fetchQueryObservable),
fetchQueryByPolicy: mock(fetchQueryByPolicy),
};

Object.assign(queryManager, mocks);

return mocks;
}

itAsync('calls fetchRequest with fetchPolicy `network-only` when using a non-networked fetch policy', (resolve, reject) => {
const mockedResponses = [
{
Expand Down
59 changes: 59 additions & 0 deletions src/core/__tests__/QueryManager/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import subscribeAndCount from '../../../utilities/testing/subscribeAndCount';
import { stripSymbols } from '../../../utilities/testing/stripSymbols';
import { itAsync } from '../../../utilities/testing/itAsync';
import { ApolloClient } from '../../../core'
import { mockFetchQuery } from '../ObservableQuery';

interface MockedMutation {
reject: (reason: any) => any;
Expand Down Expand Up @@ -2722,6 +2723,64 @@ describe('QueryManager', () => {
]).then(resolve, reject);
});


itAsync('only increments "queryInfo.lastRequestId" when fetching data from network', (resolve, reject) => {
const query = gql`
query query($id: ID!) {
people_one(id: $id) {
name
}
}
`;
const variables = { id: 1 };
const dataOne = {
people_one: {
name: 'Luke Skywalker',
},
};
const mockedResponses = [
{
request: { query, variables },
result: { data: dataOne },
},
];

const queryManager = mockQueryManager(reject, ...mockedResponses);
const queryOptions: WatchQueryOptions<any> = {
query,
variables,
fetchPolicy: 'cache-and-network',
};
const observable = queryManager.watchQuery(queryOptions);

const mocks = mockFetchQuery(queryManager);
const queryId = '1';
const getQuery: QueryManager<any>["getQuery"] =
(queryManager as any).getQuery.bind(queryManager);

subscribeAndCount(reject, observable, async (handleCount) => {
const query = getQuery(queryId);
const fqbpCalls = mocks.fetchQueryByPolicy.mock.calls;
expect(query.lastRequestId).toEqual(1);
expect(fqbpCalls.length).toBe(1);

// Simulate updating the options of the query, which will trigger
// fetchQueryByPolicy, but it should just read from cache and not
// update "queryInfo.lastRequestId". For more information, see
// https://github.com/apollographql/apollo-client/pull/7956#issue-610298427
await observable.setOptions({
...queryOptions,
fetchPolicy: 'cache-first',
});

// "fetchQueryByPolicy" was called, but "lastRequestId" does not update
// since it was able to read from cache.
expect(query.lastRequestId).toEqual(1);
expect(fqbpCalls.length).toBe(2);
resolve();
});
})

describe('polling queries', () => {
itAsync('allows you to poll queries', (resolve, reject) => {
const query = gql`
Expand Down