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

Update search pagination to use page param instead of next_page. #1364

Merged
merged 3 commits into from
Mar 11, 2022
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: 1 addition & 1 deletion lib/autoPagination.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function makeAutoPaginationMethods(self, requestArgs, spec, firstPagePromise) {
);
}
return makeRequest(self, requestArgs, spec, {
next_page: pageResult.next_page,
page: pageResult.next_page,
});
};
} else {
Expand Down
240 changes: 129 additions & 111 deletions test/autoPagination.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,25 @@ describe('auto pagination', function() {
});
});

const testCase = (
mockPaginationFn,
{pages, limit, expectedIds, expectedParamsLog, initialArgs}
) => {
const {paginator, paramsLog} = mockPaginationFn(pages, initialArgs);

return expect(
paginator.autoPagingToArray({limit}).then((result) => {
return {
ids: result.map((x) => x.id),
paramsLog,
};
})
).to.eventually.deep.equal({
ids: expectedIds,
paramsLog: expectedParamsLog,
});
};

describe('pagination logic using a mock paginator', () => {
const mockPagination = (pages, initialArgs) => {
let i = 1;
Expand Down Expand Up @@ -608,92 +627,98 @@ describe('auto pagination', function() {
return {paginator, paramsLog};
};

const testCase = ({
pages,
limit,
expectedIds,
expectedParamsLog,
initialArgs,
}) => {
const {paginator, paramsLog} = mockPagination(pages, initialArgs);
expect(
paginator.autoPagingToArray({limit}).then((result) => ({
ids: result.map((x) => x.id),
paramsLog,
}))
).to.eventually.deep.equal({
ids: expectedIds,
paramsLog: expectedParamsLog,
});
};

it('paginates forwards as expected', () => {
testCase({
pages: [
[1, 2],
[3, 4],
],
limit: 5,
expectedIds: [1, 2, 3, 4],
expectedParamsLog: ['?starting_after=2'],
describe('foward pagination', () => {
it('paginates forwards through a page', () => {
return testCase(mockPagination, {
pages: [
[1, 2],
[3, 4],
],
limit: 10,
expectedIds: [1, 2, 3, 4],
expectedParamsLog: ['?starting_after=2'],
});
});

testCase({
pages: [[1, 2], [3, 4], [5]],
limit: 5,
expectedIds: [1, 2, 3, 4, 5],
expectedParamsLog: ['?starting_after=2', '?starting_after=4'],
it('paginates forwards through un-even sized pages', () => {
return testCase(mockPagination, {
pages: [[1, 2], [3, 4], [5]],
limit: 10,
expectedIds: [1, 2, 3, 4, 5],
expectedParamsLog: ['?starting_after=2', '?starting_after=4'],
});
});

testCase({
pages: [
[1, 2],
[3, 4],
[5, 6],
],
limit: 5,
expectedIds: [1, 2, 3, 4, 5],
expectedParamsLog: ['?starting_after=2', '?starting_after=4'],
it('respects limit even when paginating', () => {
return testCase(mockPagination, {
pages: [
[1, 2],
[3, 4],
[5, 6],
],
limit: 5,
expectedIds: [1, 2, 3, 4, 5],
expectedParamsLog: ['?starting_after=2', '?starting_after=4'],
});
});

testCase({
pages: [
[1, 2],
[3, 4],
[5, 6],
],
limit: 6,
expectedIds: [1, 2, 3, 4, 5, 6],
expectedParamsLog: ['?starting_after=2', '?starting_after=4'],
it('paginates through multiple full pages', () => {
return testCase(mockPagination, {
pages: [
[1, 2],
[3, 4],
[5, 6],
[7, 8],
[9, 10],
],
limit: 10,
expectedIds: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
expectedParamsLog: [
'?starting_after=2',
'?starting_after=4',
'?starting_after=6',
'?starting_after=8',
],
});
});
});

it('paginates backwards as expected', () => {
testCase({
pages: [
[-2, -1],
[-4, -3],
],
limit: 5,
expectedIds: [-1, -2, -3, -4],
expectedParamsLog: ['?ending_before=-2'],
initialArgs: [{ending_before: '0'}],
describe('backwards pagination', () => {
it('paginates forwards through a page', () => {
return testCase(mockPagination, {
pages: [
[-2, -1],
[-4, -3],
],
limit: 5,
expectedIds: [-1, -2, -3, -4],
expectedParamsLog: ['?ending_before=-2'],
initialArgs: [{ending_before: '0'}],
});
});

testCase({
pages: [[-2, -1], [-4, -3], [-5]],
limit: 5,
expectedIds: [-1, -2, -3, -4, -5],
expectedParamsLog: ['?ending_before=-2', '?ending_before=-4'],
initialArgs: [{ending_before: '0'}],
it('paginates backwards through un-even sized pages ', () => {
return testCase(mockPagination, {
pages: [[-2, -1], [-4, -3], [-5]],
limit: 5,
expectedIds: [-1, -2, -3, -4, -5],
expectedParamsLog: ['?ending_before=-2', '?ending_before=-4'],
initialArgs: [{ending_before: '0'}],
});
});

testCase({
pages: [[-2, -1], [-4, -3], [-5]],
limit: 4,
expectedIds: [-1, -2, -3, -4],
expectedParamsLog: ['?ending_before=-2'],
initialArgs: [{ending_before: '0'}],
it('respects limit', () => {
return testCase(mockPagination, {
pages: [
[-2, -1],
[-4, -3],
[-6, -5],
],
limit: 5,
expectedIds: [-1, -2, -3, -4, -5],
expectedParamsLog: ['?ending_before=-2', '?ending_before=-4'],
initialArgs: [{ending_before: '0'}],
});
});
});
});
Expand All @@ -708,12 +733,11 @@ describe('auto pagination', function() {
};

const addNextPage = (props) => {
let nextPageProperties = {};
if (props.has_more) {
nextPageProperties = {
next_page: `${props.data[props.data.length - 1]}-encoded`,
};
}
const nextPageProperties = {
next_page: props.has_more
? `${props.data[props.data.length - 1].id}-encoded`
: null,
};
return {...props, ...nextPageProperties};
};

Expand Down Expand Up @@ -748,63 +772,57 @@ describe('auto pagination', function() {
return {paginator, paramsLog};
};

const testCase = ({
pages,
limit,
expectedIds,
expectedParamsLog,
initialArgs,
}) => {
const {paginator, paramsLog} = mockPagination(pages, initialArgs);
expect(
paginator.autoPagingToArray({limit}).then((result) => ({
ids: result.map((x) => x.id),
paramsLog,
}))
).to.eventually.deep.equal({
ids: expectedIds,
paramsLog: expectedParamsLog,
});
};

it('paginates forwards as expected', () => {
testCase({
it('paginates forwards through a page', () => {
return testCase(mockPagination, {
pages: [
[1, 2],
[3, 4],
],
limit: 5,
limit: 10,
expectedIds: [1, 2, 3, 4],
expectedParamsLog: ['?next_page=2-encoded'],
expectedParamsLog: ['?page=2-encoded'],
});
});

testCase({
it('paginates forwards through uneven-sized pages', () => {
return testCase(mockPagination, {
pages: [[1, 2], [3, 4], [5]],
limit: 5,
limit: 10,
expectedIds: [1, 2, 3, 4, 5],
expectedParamsLog: ['?next_page=2-encoded', '?next_page=4-encoded'],
expectedParamsLog: ['?page=2-encoded', '?page=4-encoded'],
});
});

testCase({
it('respects limit even when paginating', () => {
return testCase(mockPagination, {
pages: [
[1, 2],
[3, 4],
[5, 6],
],
limit: 5,
expectedIds: [1, 2, 3, 4, 5],
expectedParamsLog: ['?next_page=2-encoded', '?next_page=4-encoded'],
expectedParamsLog: ['?page=2-encoded', '?page=4-encoded'],
});
});

testCase({
it('paginates through multiple full pages', () => {
return testCase(mockPagination, {
pages: [
[1, 2],
[3, 4],
[5, 6],
[7, 8],
[9, 10],
],
limit: 10,
expectedIds: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
expectedParamsLog: [
'?page=2-encoded',
'?page=4-encoded',
'?page=6-encoded',
'?page=8-encoded',
],
limit: 6,
expectedIds: [1, 2, 3, 4, 5, 6],
expectedParamsLog: ['?next_page=2-encoded', '?next_page=4-encoded'],
});
});
});
Expand Down
4 changes: 2 additions & 2 deletions types/lib.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,9 +249,9 @@ declare module 'stripe' {

/**
* The page token to use to get the next page of results. If `has_more` is
* true, this will be set.
* true, this will be set to a concrete string value.
*/
next_page?: string;
next_page: string | null;
}
export interface ApiSearchResultPromise<T>
extends Promise<Response<ApiSearchResult<T>>>,
Expand Down