Skip to content

Commit

Permalink
Fix an invalid pagination test
Browse files Browse the repository at this point in the history
  • Loading branch information
szmarczak committed May 10, 2020
1 parent 8862270 commit 47c1afe
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -724,7 +724,7 @@ The function takes three arguments:
- `allItems` - An array of the emitted items.
- `currentItems` - Items from the current response.

It should return an object representing Got options pointing to the next page. If there are no more pages, `false` should be returned.
It should return an object representing Got options pointing to the next page. The options are merged automatically with the previous request, therefore the options returned `pagination.paginate(...)` must reflect changes only. If there are no more pages, `false` should be returned.

For example, if you want to stop when the response contains less items than expected, you can use something like this:

Expand Down
15 changes: 9 additions & 6 deletions test/pagination.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {URL} from 'url';
import test from 'ava';
import getStream = require('get-stream');
import got, {Response} from '../source';
import withServer, {withBodyParsingServer} from './helpers/with-server';
import {ExtendedTestServer} from './helpers/types';
Expand Down Expand Up @@ -333,7 +334,7 @@ test('allowGetBody sends json payload with .paginate()', withBodyParsingServer,
t.deepEqual(results, [1, 2, 3]);
});

test('`hooks` are not duplicated', withBodyParsingServer, async (t, server, got) => {
test('`hooks` are not duplicated', withServer, async (t, server, got) => {
let page = 1;
server.get('/', (_request, response) => {
response.end(JSON.stringify([page++]));
Expand Down Expand Up @@ -370,11 +371,11 @@ test('`hooks` are not duplicated', withBodyParsingServer, async (t, server, got)
t.deepEqual(result, [1, 2, 3]);
});

test.failing('allowGetBody sends correct json payload with .paginate()', withBodyParsingServer, async (t, server, got) => {
test('allowGetBody sends correct json payload with .paginate()', withServer, async (t, server, got) => {
let page = 1;
server.get('/', (request, response) => {
server.get('/', async (request, response) => {
try {
JSON.parse(request.body);
JSON.parse(await getStream(request));
} catch {
response.statusCode = 422;
}
Expand All @@ -392,9 +393,11 @@ test.failing('allowGetBody sends correct json payload with .paginate()', withBod
return false; // Stop after page 3
}

const {json, ...otherOptions}: any = response.request.options;
const {json} = response.request.options;

return {json: {...json, page}, ...otherOptions};
return {
json: {...json, page}
};
}
}
});
Expand Down

0 comments on commit 47c1afe

Please sign in to comment.