Skip to content

Commit 7ad63eb

Browse files
bors[bot]bidoubiwa
andauthored
Merge #387
387: Add pagination tests + fix pagination r=bidoubiwa a=bidoubiwa Co-authored-by: Charlotte Vermandel <[email protected]> Co-authored-by: cvermand <[email protected]>
2 parents c255931 + 1ef4110 commit 7ad63eb

File tree

6 files changed

+194
-9
lines changed

6 files changed

+194
-9
lines changed

jest.config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@ module.exports = {
1212
// jest-environment-jsdom 25 uses jsdom 15 which still supports node 10
1313
testEnvironment: 'jest-environment-jsdom',
1414
testPathIgnorePatterns: ['<rootDir>/tests/env', '<rootDir>/playgrounds'],
15+
testMatch: ['**/*.tests.ts'],
1516
},
1617
{
1718
preset: 'ts-jest',
1819
displayName: 'node',
1920
testEnvironment: 'node',
2021
testPathIgnorePatterns: ['<rootDir>/tests/env', '<rootDir>/playgrounds'],
22+
testMatch: ['**/*.tests.ts'],
2123
},
2224
],
2325
}

src/client/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export function instantMeiliSearch(
2929
paginationTotalHits: paginationTotalHits || 200,
3030
primaryKey: primaryKey || undefined,
3131
placeholderSearch: placeholderSearch !== false, // true by default
32-
hitsPerPage: hitsPerPage || 20, // 20 is the MeiliSearch's default limit value. `hitsPerPage` can be changed with `InsantSearch.configure`.
32+
hitsPerPage: hitsPerPage === undefined ? 20 : hitsPerPage, // 20 is the MeiliSearch's default limit value. `hitsPerPage` can be changed with `InsantSearch.configure`.
3333
page: page || 0, // default page is 0 if none is provided
3434
}
3535

Lines changed: 172 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,172 @@
1-
describe('Working test', () => {
2-
it('equal', () => {
3-
expect(3).toBe(3)
4-
})
5-
})
1+
import { getNumberPages, paginateHits } from '../'
2+
import { defaultContext } from './utils'
3+
4+
const numberPagesTestParameters = [
5+
{
6+
hitsPerPage: 0,
7+
hitsLength: 100,
8+
numberPages: 0,
9+
},
10+
{
11+
hitsPerPage: 1,
12+
hitsLength: 100,
13+
numberPages: 100,
14+
},
15+
{
16+
hitsPerPage: 20,
17+
hitsLength: 24,
18+
numberPages: 2,
19+
},
20+
{
21+
hitsPerPage: 20,
22+
hitsLength: 0,
23+
numberPages: 0,
24+
},
25+
{
26+
hitsPerPage: 0,
27+
hitsLength: 0,
28+
numberPages: 0,
29+
},
30+
// Not an Algolia behavior. Algolia returns an error:
31+
// "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",
39+
{
40+
hitsPerPage: 1.5,
41+
hitsLength: 20,
42+
numberPages: 14,
43+
},
44+
]
45+
46+
const paginateHitsTestsParameters = [
47+
// Empty hits
48+
{
49+
hits: [],
50+
page: 0,
51+
hitsPerPage: 20,
52+
returnedHits: [],
53+
},
54+
{
55+
hits: [],
56+
page: 100,
57+
hitsPerPage: 0,
58+
returnedHits: [],
59+
},
60+
{
61+
hits: [],
62+
page: 100,
63+
hitsPerPage: 20,
64+
returnedHits: [],
65+
},
66+
67+
// Page 0
68+
{
69+
hits: [{ id: 1 }, { id: 2 }, { id: 3 }],
70+
page: 0,
71+
hitsPerPage: 20,
72+
returnedHits: [{ id: 1 }, { id: 2 }, { id: 3 }],
73+
},
74+
{
75+
hits: [{ id: 1 }, { id: 2 }, { id: 3 }],
76+
page: 0,
77+
hitsPerPage: 0,
78+
returnedHits: [],
79+
},
80+
{
81+
hits: [{ id: 1 }, { id: 2 }, { id: 3 }],
82+
page: 0,
83+
hitsPerPage: 20,
84+
returnedHits: [{ id: 1 }, { id: 2 }, { id: 3 }],
85+
},
86+
{
87+
hits: [{ id: 1 }, { id: 2 }, { id: 3 }],
88+
page: 0,
89+
hitsPerPage: 2,
90+
returnedHits: [{ id: 1 }, { id: 2 }],
91+
},
92+
93+
// Page 1
94+
{
95+
hits: [{ id: 1 }, { id: 2 }, { id: 3 }],
96+
page: 1,
97+
hitsPerPage: 2,
98+
returnedHits: [{ id: 3 }],
99+
},
100+
{
101+
hits: [{ id: 1 }, { id: 2 }, { id: 3 }],
102+
page: 1,
103+
hitsPerPage: 20,
104+
returnedHits: [],
105+
},
106+
{
107+
hits: [{ id: 1 }, { id: 2 }, { id: 3 }],
108+
page: 1,
109+
hitsPerPage: 0,
110+
returnedHits: [],
111+
},
112+
113+
// Page 2
114+
{
115+
hits: [{ id: 1 }, { id: 2 }, { id: 3 }],
116+
page: 2,
117+
hitsPerPage: 20,
118+
returnedHits: [],
119+
},
120+
{
121+
hits: [{ id: 1 }, { id: 2 }, { id: 3 }],
122+
page: 2,
123+
hitsPerPage: 20,
124+
returnedHits: [],
125+
},
126+
{
127+
hits: [{ id: 1 }, { id: 2 }, { id: 3 }],
128+
page: 2,
129+
hitsPerPage: 0,
130+
returnedHits: [],
131+
},
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+
},
141+
]
142+
143+
describe.each(numberPagesTestParameters)(
144+
'Get Number Pages tests',
145+
({ hitsPerPage, hitsLength, numberPages }) => {
146+
it(`Should return ${numberPages} pages when hitsPerPage is ${hitsPerPage} and hits length is ${hitsLength}`, () => {
147+
const response = getNumberPages(hitsLength, {
148+
...defaultContext,
149+
hitsPerPage: hitsPerPage,
150+
})
151+
expect(response).toBe(numberPages)
152+
})
153+
}
154+
)
155+
156+
describe.each(paginateHitsTestsParameters)(
157+
'Paginate hits tests',
158+
({ hits, page, hitsPerPage, returnedHits }) => {
159+
it(`Should return ${JSON.stringify(
160+
returnedHits
161+
)} when hitsPerPage is ${hitsPerPage}, number of page is ${page} and when hits is ${JSON.stringify(
162+
hits
163+
)}`, () => {
164+
const response = paginateHits(hits, {
165+
...defaultContext,
166+
page,
167+
hitsPerPage,
168+
})
169+
expect(response).toEqual(returnedHits)
170+
})
171+
}
172+
)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { MeiliSearch } from 'meilisearch'
2+
const HOST = 'http://localhost:7700'
3+
4+
const defaultContext = {
5+
client: new MeiliSearch({ host: HOST }),
6+
paginationTotalHits: 200,
7+
primaryKey: undefined,
8+
placeholderSearch: true,
9+
hitsPerPage: 20,
10+
page: 0,
11+
}
12+
13+
export { defaultContext }

src/transformers/pagination.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,17 @@ export const getNumberPages: GetNumberPages = function (
44
hitsLength,
55
{ hitsPerPage }
66
) {
7-
const adjust = hitsLength % hitsPerPage! === 0 ? 0 : 1
8-
return Math.floor(hitsLength / hitsPerPage!) + adjust // total number of pages
7+
if (hitsPerPage > 0) {
8+
const NumberPages = Math.ceil(hitsLength / hitsPerPage) // total number of pages rounded up to the next largest integer.
9+
return NumberPages
10+
}
11+
return 0
912
}
1013

1114
export const paginateHits: PaginateHits = function (
1215
hits,
1316
{ page, hitsPerPage }
1417
) {
15-
const start = page * hitsPerPage!
18+
const start = page * hitsPerPage
1619
return hits.splice(start, hitsPerPage)
1720
}
File renamed without changes.

0 commit comments

Comments
 (0)