Skip to content

Commit

Permalink
Test paginate init hooks and require transform to return an array
Browse files Browse the repository at this point in the history
Fixes #1574
  • Loading branch information
szmarczak committed Apr 15, 2021
1 parent 56ce43e commit 2e95440
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
4 changes: 3 additions & 1 deletion source/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,11 @@ const create = (defaults: InstanceDefaults): Got => {
const response = (await got(undefined, undefined, normalizedOptions)) as Response;

// eslint-disable-next-line no-await-in-loop
const parsed = await pagination.transform(response);
const parsed: unknown[] = await pagination.transform(response);
const currentItems: T[] = [];

assert.array(parsed);

for (const item of parsed) {
if (pagination.filter({item, currentItems, allItems})) {
if (!pagination.shouldContinue({item, currentItems, allItems})) {
Expand Down
34 changes: 34 additions & 0 deletions test/pagination.ts
Original file line number Diff line number Diff line change
Expand Up @@ -689,3 +689,37 @@ test('pagination using extended searchParams', withServer, async (t, server, got
t.true(all[i] === `/?page=${i}&limit=10` || all[i] === `/?limit=10&page=${i}`);
}
});

test('calls init hooks on pagination', withServer, async (t, server) => {
server.get('/', (request, response) => {
response.end(JSON.stringify([request.url]));
});

const instance = got.extend({
hooks: {
init: [
options => {
options.searchParams = 'foobar';
}
]
}
});

const received = await instance.paginate.all<string>(server.url, {
searchParams: 'unicorn'
});

t.deepEqual(received, [
'/?foobar='
]);
});

test('throws when transform does not return an array', withServer, async (t, server) => {
server.get('/', (_request, response) => {
response.end(JSON.stringify(""));
});

await t.throwsAsync(got.paginate.all<string>(server.url));

// TODO: Add assert message above.
});

0 comments on commit 2e95440

Please sign in to comment.