Skip to content

Commit

Permalink
Test that fetchMore passes new variables to merge functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamn committed Jun 22, 2020
1 parent d261c4f commit 3329497
Showing 1 changed file with 190 additions and 2 deletions.
192 changes: 190 additions & 2 deletions src/__tests__/fetchMore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { assign, cloneDeep } from 'lodash';
import gql from 'graphql-tag';

import { mockSingleLink } from '../utilities/testing/mocking/mockLink';
import subscribeAndCount from '../utilities/testing/subscribeAndCount';
import { InMemoryCache } from '../cache/inmemory/inMemoryCache';
import { ApolloClient, NetworkStatus, ObservableQuery } from '../';
import { itAsync } from '../utilities/testing/itAsync';
Expand Down Expand Up @@ -211,7 +212,19 @@ describe('fetchMore on an observable query', () => {

const client = new ApolloClient({
link,
cache: new InMemoryCache(),
cache: new InMemoryCache({
typePolicies: {
Query: {
fields: {
entry: {
merge(_, incoming) {
return incoming;
},
},
},
},
},
}),
});

return client.watchQuery<any>({
Expand Down Expand Up @@ -298,6 +311,169 @@ describe('fetchMore on an observable query', () => {
}).then(resolve, reject);
});

itAsync('fetchMore passes new args to field merge function', (resolve, reject) => {
const mergeArgsHistory: (Record<string, any> | null)[] = [];
const groceriesFieldPolicy = offsetLimitPagination();
const { merge } = groceriesFieldPolicy;
groceriesFieldPolicy.merge = function (existing, incoming, options) {
mergeArgsHistory.push(options.args);
return merge!.call(this, existing, incoming, options);
};

const cache = new InMemoryCache({
typePolicies: {
Query: {
fields: {
groceries: groceriesFieldPolicy,
},
},
},
});

const query = gql`
query GroceryList($offset: Int!, $limit: Int!) {
groceries(offset: $offset, limit: $limit) {
id
item
found
}
}
`;

const initialVars = {
offset: 0,
limit: 2,
};

const initialGroceries = [
{
__typename: "GroceryItem",
id: 1,
item: "organic whole milk",
found: false,
},
{
__typename: "GroceryItem",
id: 2,
item: "beer that we both like",
found: false,
},
];

const additionalVars = {
offset: 2,
limit: 3,
};

const additionalGroceries = [
{
__typename: "GroceryItem",
id: 3,
item: "gluten-free pasta",
found: false,
},
{
__typename: "GroceryItem",
id: 4,
item: "goat cheese",
found: false,
},
{
__typename: "GroceryItem",
id: 5,
item: "paper towels",
found: false,
},
];

const finalGroceries = [
...initialGroceries,
...additionalGroceries,
];

const client = new ApolloClient({
cache,
link: mockSingleLink({
request: {
query,
variables: initialVars,
},
result: {
data: {
groceries: initialGroceries,
},
},
}, {
request: {
query,
variables: additionalVars,
},
result: {
data: {
groceries: additionalGroceries,
},
},
}).setOnError(reject),
});

const observable = client.watchQuery({
query,
variables: initialVars,
});

subscribeAndCount(reject, observable, (count, result) => {
if (count === 1) {
expect(result).toEqual({
loading: false,
networkStatus: NetworkStatus.ready,
data: {
groceries: initialGroceries,
},
});

expect(mergeArgsHistory).toEqual([
{ offset: 0, limit: 2 },
]);

observable.fetchMore({
variables: {
offset: 2,
limit: 3,
},
}).then(result => {
expect(result).toEqual({
loading: false,
networkStatus: NetworkStatus.ready,
data: {
groceries: additionalGroceries,
},
});

expect(observable.options.fetchPolicy).toBeUndefined();
});

} else if (count === 2) {
// This result comes entirely from the cache, without updating the
// original variables for the ObservableQuery, because the
// offsetLimitPagination field policy has keyArgs:false.
expect(result).toEqual({
loading: false,
networkStatus: NetworkStatus.ready,
data: {
groceries: finalGroceries,
},
});

expect(mergeArgsHistory).toEqual([
{ offset: 0, limit: 2 },
{ offset: 2, limit: 3 },
]);

resolve();
}
});
});

itAsync('fetching more with a different query', (resolve, reject) => {
const observable = setup(reject, {
request: {
Expand Down Expand Up @@ -482,7 +658,19 @@ describe('fetchMore on an observable query with connection', () => {

const client = new ApolloClient({
link,
cache: new InMemoryCache(),
cache: new InMemoryCache({
typePolicies: {
Query: {
fields: {
entry: {
merge(_, incoming) {
return incoming;
},
},
},
},
},
}),
});

return client.watchQuery<any>({
Expand Down

0 comments on commit 3329497

Please sign in to comment.