@helium/http
Version:
HTTP library for interacting with the Helium blockchain API
118 lines • 6.18 kB
JavaScript
"use strict";
var __asyncValues = (this && this.__asyncValues) || function (o) {
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
var m = o[Symbol.asyncIterator], i;
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const nock_1 = __importDefault(require("nock"));
const ResourceList_1 = __importDefault(require("../ResourceList"));
const Client_1 = __importDefault(require("../Client"));
const DataModel_1 = require("../models/DataModel");
describe('auto pagination', () => {
it('auto paginates with asyncIterator', async () => {
var _a, e_1, _b, _c;
const data = [{ name: 'dog' }, { name: 'cat' }, { name: 'bird' }].map((d) => new DataModel_1.GenericDataModel(d));
const list = new ResourceList_1.default(data);
const fetchedData = [];
try {
// eslint-disable-next-line no-restricted-syntax
for (var _d = true, list_1 = __asyncValues(list), list_1_1; list_1_1 = await list_1.next(), _a = list_1_1.done, !_a; _d = true) {
_c = list_1_1.value;
_d = false;
const item = _c;
fetchedData.push(item);
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (!_d && !_a && (_b = list_1.return)) await _b.call(list_1);
}
finally { if (e_1) throw e_1.error; }
}
expect(fetchedData).toEqual(data);
});
it('fetches additional pages asynchronously', async () => {
var _a, e_2, _b, _c;
const firstPageData = [{ name: 'cat' }, { name: 'dog' }].map((d) => new DataModel_1.GenericDataModel(d));
const secondPageData = [{ name: 'snake' }, { name: 'bird' }].map((d) => new DataModel_1.GenericDataModel(d));
const thirdPageData = [{ name: 'bat' }, { name: 'squirrel' }].map((d) => new DataModel_1.GenericDataModel(d));
(0, nock_1.default)('https://api.helium.io').get('/v1/animals').query({ cursor: 'cursor-1' }).reply(200, {
data: secondPageData,
cursor: 'cursor-2',
});
(0, nock_1.default)('https://api.helium.io').get('/v1/animals').query({ cursor: 'cursor-2' }).reply(200, {
data: thirdPageData,
});
const fetchMore = async ({ cursor } = {}) => {
const client = new Client_1.default();
const { data: { data, cursor: nextCursor }, } = await client.get('/animals', { cursor });
return new ResourceList_1.default(data, fetchMore, nextCursor);
};
const list = new ResourceList_1.default(firstPageData, fetchMore, 'cursor-1');
const fetchedData = [];
try {
// eslint-disable-next-line no-restricted-syntax
for (var _d = true, list_2 = __asyncValues(list), list_2_1; list_2_1 = await list_2.next(), _a = list_2_1.done, !_a; _d = true) {
_c = list_2_1.value;
_d = false;
const item = _c;
fetchedData.push(item);
}
}
catch (e_2_1) { e_2 = { error: e_2_1 }; }
finally {
try {
if (!_d && !_a && (_b = list_2.return)) await _b.call(list_2);
}
finally { if (e_2) throw e_2.error; }
}
expect(fetchedData).toEqual([...firstPageData, ...secondPageData, ...thirdPageData]);
});
});
describe('take', () => {
it('allows auto pagination in chunks', async () => {
const data = [{ name: 'dog' }, { name: 'cat' }, { name: 'bird' }, { name: 'snake' }].map((d) => new DataModel_1.GenericDataModel(d));
const list = new ResourceList_1.default(data);
const firstSet = await list.take(2);
const secondSet = await list.take(3);
expect([...firstSet, ...secondSet]).toEqual(data);
});
it('can be reset', async () => {
const data = [{ name: 'dog' }, { name: 'cat' }, { name: 'bird' }, { name: 'snake' }].map((d) => new DataModel_1.GenericDataModel(d));
const list = new ResourceList_1.default(data);
const firstSet = await list.take(2);
list.takeReset();
const secondSet = await list.take(2);
expect(firstSet).toEqual(secondSet);
});
});
describe('manual pagination', () => {
it('exposes the current page data', () => {
const data = [{ id: 1 }, { id: 2 }, { id: 3 }].map((d) => new DataModel_1.GenericDataModel(d));
const list = new ResourceList_1.default(data);
expect(list.data).toEqual(data);
});
it('fetches the next page of data', async () => {
const firstPageData = [{ name: 'cat' }, { name: 'dog' }].map((d) => new DataModel_1.GenericDataModel(d));
const secondPageData = [{ name: 'snake' }, { name: 'bird' }].map((d) => new DataModel_1.GenericDataModel(d));
(0, nock_1.default)('https://api.helium.io').get('/v1/animals').query({ cursor: 'cursor-1' }).reply(200, {
data: secondPageData,
});
const fetchMore = async ({ cursor } = {}) => {
const client = new Client_1.default();
const { data: { data }, } = await client.get('/animals', { cursor });
return new ResourceList_1.default(data, fetchMore);
};
const list = new ResourceList_1.default(firstPageData, fetchMore, 'cursor-1');
const nextList = await list.nextPage();
expect(nextList.data).toEqual(secondPageData);
});
});
//# sourceMappingURL=ResourceList.spec.js.map