feat(visitsApi): make use of limit query param

Signed-off-by: Julien <julien.hery@ext.adeo.com>
This commit is contained in:
Julien
2024-06-26 17:11:16 +02:00
parent 6728270cdd
commit 91767ddc54
2 changed files with 41 additions and 11 deletions
+38 -10
View File
@@ -134,11 +134,25 @@ describe('VisitsStorageApi.create', () => {
let visitsToSave: Array<Omit<Visit, 'id' | 'hits' | 'timestamp'>>;
let baseDate: number;
const fillVisitsApi = () => {
baseDate = Date.now();
// Chaining items to ensure the right setSystemTime
return visitsToSave.reduce(
(acc, visit, index) =>
acc.then(() => {
jest.setSystemTime(baseDate + 360_000 * index);
return api.save({ visit });
}),
Promise.resolve({}),
);
};
beforeEach(() => {
api = VisitsStorageApi.create({
storageApi: MockStorageApi.create(),
identityApi: mockIdentityApi,
});
visitsToSave = [
{
pathname: '/catalog/default/component/playback-order-1',
@@ -156,16 +170,8 @@ describe('VisitsStorageApi.create', () => {
name: 'Playback Order Odd',
},
];
baseDate = Date.now();
// Chaining items to ensure the right setSystemTime
return visitsToSave.reduce(
(acc, visit, index) =>
acc.then(() => {
jest.setSystemTime(baseDate + 360_000 * index);
return api.save({ visit });
}),
Promise.resolve({}),
);
return fillVisitsApi();
});
it('retrieves visits', async () => {
@@ -335,5 +341,27 @@ describe('VisitsStorageApi.create', () => {
expect(visits).toHaveLength(1);
expect(visits).toEqual([expect.objectContaining(visitsToSave[0])]);
});
it('retrieves a limited set of visits according to given limit', async () => {
const visits = await api.list({
limit: 2,
});
expect(visitsToSave.length).toBeGreaterThan(2);
expect(visits.length).toEqual(2);
});
it('retrieves a default set of 8 visits', async () => {
visitsToSave = Array.from({ length: 9 }, (_, index) => ({
pathname: `/catalog/default/component/playback-order-${index}`,
entityRef: `component:default/playback-order-${index}`,
name: `Playback Order ${index}`,
}));
await fillVisitsApi();
const visits = await api.list();
expect(visitsToSave.length).toBeGreaterThan(8);
expect(visits.length).toEqual(8);
});
});
});
+3 -1
View File
@@ -30,6 +30,8 @@ export type VisitsStorageApiOptions = {
type ArrayElement<A> = A extends readonly (infer T)[] ? T : never;
const DEFAULT_LIST_LIMIT = 8;
/**
* @public
* This is an implementation of VisitsApi that relies on a StorageApi.
@@ -83,7 +85,7 @@ export class VisitsStorageApi implements VisitsApi {
});
});
return visits;
return visits.slice(0, queryParams?.limit ?? DEFAULT_LIST_LIMIT);
}
/**