Skip to content

Commit

Permalink
More subscribeAndCount improvements in fetchMore tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamn committed Mar 9, 2022
1 parent a2dd960 commit c47e347
Showing 1 changed file with 115 additions and 82 deletions.
197 changes: 115 additions & 82 deletions src/__tests__/fetchMore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ describe('updateQuery on a query with required and optional variables', () => {

return new Promise(resolve => setTimeout(resolve, 5))
.then(() => obsHandle)
.then((watchedQuery: ObservableQuery<any>) => {
.then((watchedQuery: ObservableQuery<any, any>) => {
expect(latestResult.data.entry.value).toBe(1);
watchedQuery.updateQuery((prevResult: any) => {
const res = cloneDeep(prevResult);
Expand Down Expand Up @@ -280,36 +280,44 @@ describe('fetchMore on an observable query', () => {
result: resultMore,
});

let latestResult: any;
observable.subscribe({
next(result: any) {
latestResult = result;
},
});
subscribeAndCount(reject, observable, (count, result) => {
if (count === 1) {
expect(result.loading).toBe(false);
expect(result.data.entry.comments).toHaveLength(10);

return observable.fetchMore({
// Rely on the fact that the original variables had limit: 10
variables: { start: 10 },
updateQuery: (prev, options) => {
expect(options.variables).toEqual(variablesMore);
return observable.fetchMore({
// Rely on the fact that the original variables had limit: 10
variables: { start: 10 },
updateQuery: (prev, options) => {
expect(options.variables).toEqual(variablesMore);

const state = cloneDeep(prev) as any;
state.entry.comments = [
...state.entry.comments,
...options.fetchMoreResult.entry.comments,
];
return state;
},
}).then(fetchMoreResult => {
// This is the server result
expect(fetchMoreResult.loading).toBe(false);
expect(fetchMoreResult.data.entry.comments).toHaveLength(10);
});

const state = cloneDeep(prev) as any;
state.entry.comments = [
...state.entry.comments,
...(options.fetchMoreResult as any).entry.comments,
];
return state;
},
}).then(data => {
// This is the server result
expect(data.data.entry.comments).toHaveLength(10);
expect(data.loading).toBe(false);
const comments = latestResult.data.entry.comments;
expect(comments).toHaveLength(20);
for (let i = 1; i <= 20; i++) {
expect(comments[i - 1].text).toEqual(`comment ${i}`);
} else if (count === 2) {
const combinedComments = result.data.entry.comments;
expect(combinedComments).toHaveLength(20);
for (let i = 1; i <= 20; i++) {
expect(combinedComments[i - 1].text).toEqual(`comment ${i}`);
}

setTimeout(resolve, 10);
} else {
reject(`Too many results (${
JSON.stringify({ count, result })
})`);
}
}).then(resolve, reject);
});
});

itAsync('field policy', (resolve, reject) => {
Expand Down Expand Up @@ -368,32 +376,47 @@ describe('fetchMore on an observable query', () => {
result: resultMore,
});

let latestResult: any;
observable.subscribe({
next(result: any) {
latestResult = result;
},
});
subscribeAndCount(reject, observable, (count, result) => {
if (count === 1) {
expect(result.loading).toBe(false);
expect(result.data.entry.comments).toHaveLength(10);

return observable.fetchMore({
variables: { start: 10 }, // rely on the fact that the original variables had limit: 10
updateQuery: (prev, options) => {
const state = cloneDeep(prev) as any;
state.entry.comments = [
...state.entry.comments,
...(options.fetchMoreResult as any).entry.comments,
];
return state;
},
}).then(data => {
expect(data.data.entry.comments).toHaveLength(10); // this is the server result
expect(data.loading).toBe(false);
const comments = latestResult.data.entry.comments;
expect(comments).toHaveLength(20);
for (let i = 1; i <= 20; i++) {
expect(comments[i - 1].text).toEqual(`comment ${i}`);
return observable.fetchMore({
variables: { start: 10 }, // rely on the fact that the original variables had limit: 10
updateQuery: (prev, options) => {
expect(options.variables).toEqual(variablesMore);
const state = cloneDeep(prev) as any;
state.entry.comments = [
...state.entry.comments,
...options.fetchMoreResult.entry.comments,
];
return state;
},
}).then(fetchMoreResult => {
expect(fetchMoreResult.loading).toBe(false);
const fetchMoreComments = fetchMoreResult.data.entry.comments;
expect(fetchMoreComments).toHaveLength(10);
fetchMoreComments.forEach((comment, i) => {
expect(comment.text).toEqual(`comment ${i + 11}`)
});
});

} else if (count === 2) {
expect(result.loading).toBe(false);
const combinedComments = result.data.entry.comments;
expect(combinedComments).toHaveLength(20);

combinedComments.forEach((comment, i) => {
expect(comment.text).toEqual(`comment ${i + 1}`);
});

setTimeout(resolve, 10);
} else {
reject(`Too many results (${
JSON.stringify({ count, result })
})`);
}
}).then(resolve, reject);
});
});

itAsync('field policy', (resolve, reject) => {
Expand All @@ -413,9 +436,7 @@ describe('fetchMore on an observable query', () => {
result: resultMore,
});

// let latestResult: ApolloQueryResult<any>;
subscribeAndCount(reject, observable, (count, result) => {
// latestResult = result;
if (count === 1) {
expect(result.loading).toBe(false);
expect(result.data.entry.comments).toHaveLength(10);
Expand Down Expand Up @@ -1163,34 +1184,46 @@ describe('fetchMore on an observable query', () => {
result: result2,
});

let latestResult: any;
observable.subscribe({
next(result: any) {
latestResult = result;
},
});
subscribeAndCount(reject, observable, (count, result) => {
if (count === 1) {
expect(result.loading).toBe(false);
expect(result.data.entry.comments).toHaveLength(10);

return observable.fetchMore({
query: query2,
variables: variables2,
updateQuery: (prev, options) => {
const state = cloneDeep(prev) as any;
state.entry.comments = [
...state.entry.comments,
...options.fetchMoreResult.comments,
];
return state;
},
}).then(fetchMoreResult => {
expect(fetchMoreResult.loading).toBe(false);
expect(fetchMoreResult.data.comments).toHaveLength(10);
});

return observable.fetchMore({
query: query2,
variables: variables2,
updateQuery: (prev, options) => {
const state = cloneDeep(prev) as any;
state.entry.comments = [
...state.entry.comments,
...(options.fetchMoreResult as any).comments,
];
return state;
},
}).then(() => {
const comments = latestResult.data.entry.comments;
expect(comments).toHaveLength(20);
for (let i = 1; i <= 10; i++) {
expect(comments[i - 1].text).toEqual(`comment ${i}`);
}
for (let i = 11; i <= 20; i++) {
expect(comments[i - 1].text).toEqual(`new comment ${i}`);
} else if (count === 2) {
expect(result.loading).toBe(false);
const combinedComments = result.data.entry.comments;
expect(combinedComments).toHaveLength(20);

for (let i = 1; i <= 10; i++) {
expect(combinedComments[i - 1].text).toEqual(`comment ${i}`);
}
for (let i = 11; i <= 20; i++) {
expect(combinedComments[i - 1].text).toEqual(`new comment ${i}`);
}

setTimeout(resolve, 10);
} else {
reject(`Too many results (${
JSON.stringify({ count, result })
})`);
}
}).then(resolve, reject);
});
});

describe('will not get an error from `fetchMore` if thrown', () => {
Expand Down Expand Up @@ -1473,7 +1506,7 @@ describe('fetchMore on an observable query with connection', () => {
const state = cloneDeep(prev) as any;
state.entry.comments = [
...state.entry.comments,
...(options.fetchMoreResult as any).entry.comments,
...options.fetchMoreResult.entry.comments,
];
return state;
},
Expand Down Expand Up @@ -1562,7 +1595,7 @@ describe('fetchMore on an observable query with connection', () => {
const state = cloneDeep(prev) as any;
state.entry.comments = [
...state.entry.comments,
...(options.fetchMoreResult as any).entry.comments,
...options.fetchMoreResult.entry.comments,
];
return state;
},
Expand Down

0 comments on commit c47e347

Please sign in to comment.