From 7be09fd7bf0942bd47dd68027f3fc1247990a9aa Mon Sep 17 00:00:00 2001 From: Dominic Charley-Roy Date: Fri, 4 Mar 2022 17:37:30 -0500 Subject: [PATCH 1/3] Update search pagination param to be page. --- lib/autoPagination.js | 2 +- test/autoPagination.spec.js | 19 +++++++++---------- types/lib.d.ts | 4 ++-- 3 files changed, 12 insertions(+), 13 deletions(-) diff --git a/lib/autoPagination.js b/lib/autoPagination.js index 7304a0bc5d..fe6f1f15b6 100644 --- a/lib/autoPagination.js +++ b/lib/autoPagination.js @@ -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 { diff --git a/test/autoPagination.spec.js b/test/autoPagination.spec.js index 8d76b4b344..553ea1f3b7 100644 --- a/test/autoPagination.spec.js +++ b/test/autoPagination.spec.js @@ -708,12 +708,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]}-encoded` + : nul, + }; return {...props, ...nextPageProperties}; }; @@ -775,14 +774,14 @@ describe('auto pagination', function() { ], limit: 5, expectedIds: [1, 2, 3, 4], - expectedParamsLog: ['?next_page=2-encoded'], + expectedParamsLog: ['?page=2-encoded'], }); testCase({ pages: [[1, 2], [3, 4], [5]], 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({ @@ -793,7 +792,7 @@ describe('auto pagination', function() { ], 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({ @@ -804,7 +803,7 @@ describe('auto pagination', function() { ], limit: 6, expectedIds: [1, 2, 3, 4, 5, 6], - expectedParamsLog: ['?next_page=2-encoded', '?next_page=4-encoded'], + expectedParamsLog: ['?page=2-encoded', '?page=4-encoded'], }); }); }); diff --git a/types/lib.d.ts b/types/lib.d.ts index 2c2a41b6ec..1117452766 100644 --- a/types/lib.d.ts +++ b/types/lib.d.ts @@ -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 extends Promise>>, From b6f127c33df0dbafaa579a2f46f2a6aa48889b97 Mon Sep 17 00:00:00 2001 From: Dominic Charley-Roy <78050200+dcr-stripe@users.noreply.github.com> Date: Fri, 4 Mar 2022 17:40:08 -0500 Subject: [PATCH 2/3] Fix typo. Co-authored-by: Richard Marmorstein <52928443+richardm-stripe@users.noreply.github.com> --- test/autoPagination.spec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/autoPagination.spec.js b/test/autoPagination.spec.js index 553ea1f3b7..e695272f92 100644 --- a/test/autoPagination.spec.js +++ b/test/autoPagination.spec.js @@ -711,7 +711,7 @@ describe('auto pagination', function() { const nextPageProperties = { next_page: props.has_more ? `${props.data[props.data.length - 1]}-encoded` - : nul, + : null, }; return {...props, ...nextPageProperties}; }; From d3443040bdd29ce3e0ec0a5e58d15b17ee69e0f3 Mon Sep 17 00:00:00 2001 From: Dominic Charley-Roy Date: Fri, 4 Mar 2022 18:18:01 -0500 Subject: [PATCH 3/3] Fix async tests. --- test/autoPagination.spec.js | 225 +++++++++++++++++++----------------- 1 file changed, 122 insertions(+), 103 deletions(-) diff --git a/test/autoPagination.spec.js b/test/autoPagination.spec.js index e695272f92..c43bf93a2c 100644 --- a/test/autoPagination.spec.js +++ b/test/autoPagination.spec.js @@ -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; @@ -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'}], + }); }); }); }); @@ -710,7 +735,7 @@ describe('auto pagination', function() { const addNextPage = (props) => { const nextPageProperties = { next_page: props.has_more - ? `${props.data[props.data.length - 1]}-encoded` + ? `${props.data[props.data.length - 1].id}-encoded` : null, }; return {...props, ...nextPageProperties}; @@ -747,44 +772,29 @@ 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: ['?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: ['?page=2-encoded', '?page=4-encoded'], }); + }); - testCase({ + it('respects limit even when paginating', () => { + return testCase(mockPagination, { pages: [ [1, 2], [3, 4], @@ -794,16 +804,25 @@ describe('auto pagination', function() { expectedIds: [1, 2, 3, 4, 5], 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: ['?page=2-encoded', '?page=4-encoded'], }); }); });