Merge pull request #25418 from Julien-Hery/feat/limitVisitsApi

feat(visitsApi): make use of `limit` query param in `.list()` function
This commit is contained in:
Fredrik Adelöw
2024-10-07 21:16:48 +02:00
committed by GitHub
3 changed files with 46 additions and 11 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-home': minor
---
**BREAKING** Implement usage of unused `limit` query parameter in visits API `.list()` function
+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);
}
/**