Skip to content

Commit cebcfc0

Browse files
bors[bot]bidoubiwa
andauthored
Merge #575
575: Fix pagination not showing the right amount of pages r=bidoubiwa a=bidoubiwa fixes: #574 Bug was due to splice changing hits on place. Changing order of function call fixes this issue as `searchresponse.hits` was not altered yet. Co-authored-by: Charlotte Vermandel <[email protected]>
2 parents 23c1097 + 0490f8d commit cebcfc0

File tree

3 files changed

+21
-20
lines changed

3 files changed

+21
-20
lines changed

src/adapter/search-response-adapter/__tests__/pagination-adapter.tests.ts

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,6 @@ const numberPagesTestParameters = [
2929
},
3030
// Not an Algolia behavior. Algolia returns an error:
3131
// "Value too small for \"hitsPerPage\" parameter, expected integer between 0 and 9223372036854775807",
32-
{
33-
hitsPerPage: -1,
34-
hitsLength: 20,
35-
numberPages: 0,
36-
},
37-
// Not an Algolia behavior. Algolia returns an error:
38-
// "Value too small for \"hitsPerPage\" parameter, expected integer between 0 and 9223372036854775807",
3932
{
4033
hitsPerPage: 1.5,
4134
hitsLength: 20,
@@ -129,15 +122,6 @@ const paginateHitsTestsParameters = [
129122
hitsPerPage: 0,
130123
returnedHits: [],
131124
},
132-
// Wrong types
133-
// Not an Algolia behavior. Algolia returns an error:
134-
// "Value too small for \"hitsPerPage\" parameter, expected integer between 0 and 9223372036854775807",
135-
{
136-
hits: [{ id: 1 }, { id: 2 }, { id: 3 }],
137-
page: 0,
138-
hitsPerPage: -1,
139-
returnedHits: [],
140-
},
141125
]
142126

143127
describe.each(numberPagesTestParameters)(
@@ -163,3 +147,17 @@ describe.each(paginateHitsTestsParameters)(
163147
})
164148
}
165149
)
150+
151+
it('Should throw when hitsPerPage is negative', () => {
152+
try {
153+
const hits: string[] = []
154+
const hitsPerPage = -1
155+
const page = 0
156+
adaptPagination(hits, page, hitsPerPage)
157+
} catch (e: any) {
158+
expect(e.message).toBe(
159+
'Value too small for "hitsPerPage" parameter, expected integer between 0 and 9223372036854775807'
160+
)
161+
expect(e.name).toBe('TypeError')
162+
}
163+
})

src/adapter/search-response-adapter/pagination-adapter.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ export function adaptPagination(
1111
page: number,
1212
hitsPerPage: number
1313
): Array<Record<string, any>> {
14+
if (hitsPerPage < 0) {
15+
throw new TypeError(
16+
'Value too small for "hitsPerPage" parameter, expected integer between 0 and 9223372036854775807'
17+
)
18+
}
1419
const start = page * hitsPerPage
15-
return hits.splice(start, hitsPerPage)
20+
return hits.slice(start, start + hitsPerPage)
1621
}

src/adapter/search-response-adapter/search-response-adapter.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,11 @@ export function adaptSearchResponse<T>(
2929
if (exhaustiveFacetsCount) {
3030
searchResponseOptionals.exhaustiveFacetsCount = exhaustiveFacetsCount
3131
}
32-
33-
const hits = adaptHits(searchResponse.hits, searchContext, paginationContext)
34-
3532
const nbPages = ceiledDivision(
3633
searchResponse.hits.length,
3734
paginationContext.hitsPerPage
3835
)
36+
const hits = adaptHits(searchResponse.hits, searchContext, paginationContext)
3937

4038
const exhaustiveNbHits = searchResponse.exhaustiveNbHits
4139
const nbHits = searchResponse.nbHits

0 commit comments

Comments
 (0)