reproduce pagination failure in test

This commit is contained in:
Kiara Grouwstra 2021-10-04 23:00:02 +02:00
parent 8978cda8e1
commit ced2be41ad
3 changed files with 45 additions and 42 deletions

View File

@ -1,7 +1,13 @@
"use strict";
import { mergeMap } from "rxjs/operators";
import { of, from, concat, Observable } from "rxjs";
import { List, MollieClient, MollieOptions, Payment } from "@mollie/api-client";
import { of, from, concat, Observable, firstValueFrom } from "rxjs";
import { toArray, mergeMap } from "rxjs/operators";
import { List } from "@mollie/api-client";
import { log } from "./utils";
function printId<T>(x: T): T {
log(x);
return x;
}
export const mollieKeys = {
leden: process.env.MOLLIE_API_KEY_LEDEN || '',
@ -11,8 +17,11 @@ export const mollieKeys = {
};
export function unrollResult<T>(result: List<T>): Observable<T> {
log('unrollResult');
// log(result);
// const { count, nextPage, links } = result;
const { count, nextPage, links } = result;
log(count);
// log(nextPage);
return concat(
of(...result),
of(... result.nextPage ? listAll(result.nextPage()) : []),
@ -20,7 +29,15 @@ export function unrollResult<T>(result: List<T>): Observable<T> {
}
export function listAll<T>(page$: Promise<List<T>>): Observable<T> {
log('listAll');
// log(page$);
// page$.then(log);
return from(page$).pipe(
printId,
mergeMap(unrollResult)
);
}
export function listPromise<T>(page$: Promise<List<T>>): Promise<T[]> {
return firstValueFrom(listAll(page$).pipe(toArray()));
}

1
src/utils.ts Normal file
View File

@ -0,0 +1 @@
export const log = console.log.bind(console);

View File

@ -1,41 +1,26 @@
import { createMollieClient } from '@mollie/api-client';
import { mollieKeys, listAll } from '../src/mollie';
const log = console.log.bind(console);
import { log } from "../src/utils";
import { mollieKeys, listPromise } from '../src/mollie';
test('airtable can print records', (done: () => void) => {
const mollieClient = createMollieClient({ apiKey: mollieKeys.leden });
// const entity = mollieClient.customers;
const entity = mollieClient.payments;
// const entity = mollieClient.refunds;
// const entity = mollieClient.chargebacks;
const obs = listAll(entity.list());
// log('just before subscribe');
obs.subscribe({
next(x: any) {
log('got value ' + x);
},
error(err: string) {
console.error('something wrong occurred: ' + err);
},
complete() {
log('done');
done();
}
});
// log('just after subscribe');
describe('airtable', () => {
// entity
// .list()
// .then(result => {
// console.log(result);
// // const { count, nextPage, links } = result;
// // result.forEach(item => {
// // console.log(item);
// // })
// });
// // same for payments, refunds, chargebacks
// expect(true).toBe(true);
});
const mollie = createMollieClient({ apiKey: mollieKeys.leden });
it('can paginate customers', async () => {
const list = await listPromise(mollie.customers.list());
// log(list);
expect(list.length).toBeGreaterThan(50);
});
xit('can fetch a page of payments', async () => {
const list = await mollie.payments.list();
expect(list.length).toEqual(50);
});
xit('can paginate payments', async () => {
const list = await listPromise(mollie.payments.list());
// log(list);
log(list.length);
});
})