diff --git a/plugins/home/src/api/VisitsStorageApi.test.ts b/plugins/home/src/api/VisitsStorageApi.test.ts index e361c624b9..5e8bc2dbfc 100644 --- a/plugins/home/src/api/VisitsStorageApi.test.ts +++ b/plugins/home/src/api/VisitsStorageApi.test.ts @@ -134,11 +134,25 @@ describe('VisitsStorageApi.create', () => { let visitsToSave: Array>; 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); + }); }); }); diff --git a/plugins/home/src/api/VisitsStorageApi.ts b/plugins/home/src/api/VisitsStorageApi.ts index a7cd063455..ca19dcbdf1 100644 --- a/plugins/home/src/api/VisitsStorageApi.ts +++ b/plugins/home/src/api/VisitsStorageApi.ts @@ -30,6 +30,8 @@ export type VisitsStorageApiOptions = { type ArrayElement = 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); } /**