Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions mock-api/msw/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1144,10 +1144,10 @@ export const handlers = makeHandlers({
return utilizationForSilo(silo)
},
siloUtilizationList({ query }) {
const { items: silos, nextPage } = paginated(query, db.silos)
const { items: silos, next_page } = paginated(query, db.silos)
return {
items: silos.map(utilizationForSilo),
nextPage,
next_page,
}
},
vpcList({ query }) {
Expand Down
12 changes: 6 additions & 6 deletions mock-api/msw/util.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@ describe('paginated', () => {
const items = [{ id: 'a' }, { id: 'b' }, { id: 'c' }]
const page = paginated({}, items)
expect(page.items).toEqual([{ id: 'a' }, { id: 'b' }, { id: 'c' }])
expect(page.nextPage).toBeNull()
expect(page.next_page).toBeNull()
})

it('should return the first 100 items with no limit passed', () => {
const items = Array.from({ length: 200 }).map((_, i) => ({ id: 'i' + i }))
const page = paginated({}, items)
expect(page.items.length).toBe(100)
expect(page.items).toEqual(items.slice(0, 100))
expect(page.nextPage).toBe('i100')
expect(page.next_page).toBe('i100')
})

it('should return page with null `nextPage` if items equal page', () => {
it('should return page with null `next_page` if items equal page', () => {
const items = [
{ id: 'a' },
{ id: 'b' },
Expand All @@ -44,7 +44,7 @@ describe('paginated', () => {
const page = paginated({}, items)
expect(page.items.length).toBe(10)
expect(page.items).toEqual(items.slice(0, 10))
expect(page.nextPage).toBeNull()
expect(page.next_page).toBeNull()
})

it('should return 5 items with a limit of 5', () => {
Expand All @@ -59,15 +59,15 @@ describe('paginated', () => {
const page = paginated({ limit: 5 }, items)
expect(page.items.length).toBe(5)
expect(page.items).toEqual(items.slice(0, 5))
expect(page.nextPage).toBe('f')
expect(page.next_page).toBe('f')
})

it('should return the second page when given a `page_token`', () => {
const items = [{ id: 'a' }, { id: 'b' }, { id: 'c' }, { id: 'd' }]
const page = paginated({ pageToken: 'b' }, items)
expect(page.items.length).toBe(3)
expect(page.items).toEqual([{ id: 'b' }, { id: 'c' }, { id: 'd' }])
expect(page.nextPage).toBeNull()
expect(page.next_page).toBeNull()
})
})

Expand Down
8 changes: 4 additions & 4 deletions mock-api/msw/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ interface PaginateOptions {
}
export interface ResultsPage<I extends { id: string }> {
items: I[]
nextPage: string | null
next_page: string | null
}

export const paginated = <P extends PaginateOptions, I extends { id: string }>(
Expand All @@ -59,20 +59,20 @@ export const paginated = <P extends PaginateOptions, I extends { id: string }>(
if (startIndex > items.length) {
return {
items: [],
nextPage: null,
next_page: null,
}
}

if (limit + startIndex >= items.length) {
return {
items: items.slice(startIndex),
nextPage: null,
next_page: null,
}
}

return {
items: items.slice(startIndex, startIndex + limit),
nextPage: `${items[startIndex + limit].id}`,
next_page: `${items[startIndex + limit].id}`,
}
}

Expand Down
Loading