Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pagination tests + fix pagination #387

Merged
merged 11 commits into from
May 3, 2021
2 changes: 2 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ module.exports = {
// jest-environment-jsdom 25 uses jsdom 15 which still supports node 10
testEnvironment: 'jest-environment-jsdom',
testPathIgnorePatterns: ['<rootDir>/tests/env', '<rootDir>/playgrounds'],
testMatch: ['**/*.tests.ts'],
},
{
preset: 'ts-jest',
displayName: 'node',
testEnvironment: 'node',
testPathIgnorePatterns: ['<rootDir>/tests/env', '<rootDir>/playgrounds'],
testMatch: ['**/*.tests.ts'],
},
],
}
2 changes: 1 addition & 1 deletion src/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export function instantMeiliSearch(
paginationTotalHits: paginationTotalHits || 200,
primaryKey: primaryKey || undefined,
placeholderSearch: placeholderSearch !== false, // true by default
hitsPerPage: hitsPerPage || 20, // 20 is the MeiliSearch's default limit value. `hitsPerPage` can be changed with `InsantSearch.configure`.
hitsPerPage: hitsPerPage === undefined ? 20 : hitsPerPage, // 20 is the MeiliSearch's default limit value. `hitsPerPage` can be changed with `InsantSearch.configure`.
page: page || 0, // default page is 0 if none is provided
}

Expand Down
177 changes: 172 additions & 5 deletions src/transformers/__tests__/pagination.tests.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,172 @@
describe('Working test', () => {
it('equal', () => {
expect(3).toBe(3)
})
})
import { getNumberPages, paginateHits } from '../'
import { defaultContext } from './utils'

const numberPagesTestParameters = [
{
hitsPerPage: 0,
hitsLength: 100,
numberPages: 0,
},
{
hitsPerPage: 1,
hitsLength: 100,
numberPages: 100,
},
{
hitsPerPage: 20,
hitsLength: 24,
numberPages: 2,
},
{
hitsPerPage: 20,
hitsLength: 0,
numberPages: 0,
},
{
hitsPerPage: 0,
hitsLength: 0,
numberPages: 0,
},
// Not an Algolia behavior. Algolia returns an error:
// "Value too small for \"hitsPerPage\" parameter, expected integer between 0 and 9223372036854775807",
{
hitsPerPage: -1,
hitsLength: 20,
numberPages: 0,
},
// Not an Algolia behavior. Algolia returns an error:
// "Value too small for \"hitsPerPage\" parameter, expected integer between 0 and 9223372036854775807",
{
hitsPerPage: 1.5,
hitsLength: 20,
numberPages: 14,
},
]

const paginateHitsTestsParameters = [
// Empty hits
{
hits: [],
page: 0,
hitsPerPage: 20,
returnedHits: [],
},
{
hits: [],
page: 100,
hitsPerPage: 0,
returnedHits: [],
},
{
hits: [],
page: 100,
hitsPerPage: 20,
returnedHits: [],
},

// Page 0
{
hits: [{ id: 1 }, { id: 2 }, { id: 3 }],
page: 0,
hitsPerPage: 20,
returnedHits: [{ id: 1 }, { id: 2 }, { id: 3 }],
},
{
hits: [{ id: 1 }, { id: 2 }, { id: 3 }],
page: 0,
hitsPerPage: 0,
returnedHits: [],
},
{
hits: [{ id: 1 }, { id: 2 }, { id: 3 }],
page: 0,
hitsPerPage: 20,
returnedHits: [{ id: 1 }, { id: 2 }, { id: 3 }],
},
{
hits: [{ id: 1 }, { id: 2 }, { id: 3 }],
page: 0,
hitsPerPage: 2,
returnedHits: [{ id: 1 }, { id: 2 }],
},

// Page 1
{
hits: [{ id: 1 }, { id: 2 }, { id: 3 }],
page: 1,
hitsPerPage: 2,
returnedHits: [{ id: 3 }],
},
{
hits: [{ id: 1 }, { id: 2 }, { id: 3 }],
page: 1,
hitsPerPage: 20,
returnedHits: [],
},
{
hits: [{ id: 1 }, { id: 2 }, { id: 3 }],
page: 1,
hitsPerPage: 0,
returnedHits: [],
},

// Page 2
{
hits: [{ id: 1 }, { id: 2 }, { id: 3 }],
page: 2,
hitsPerPage: 20,
returnedHits: [],
},
{
hits: [{ id: 1 }, { id: 2 }, { id: 3 }],
page: 2,
hitsPerPage: 20,
returnedHits: [],
},
{
hits: [{ id: 1 }, { id: 2 }, { id: 3 }],
page: 2,
hitsPerPage: 0,
returnedHits: [],
},
// Wrong types
// Not an Algolia behavior. Algolia returns an error:
// "Value too small for \"hitsPerPage\" parameter, expected integer between 0 and 9223372036854775807",
{
hits: [{ id: 1 }, { id: 2 }, { id: 3 }],
page: 0,
hitsPerPage: -1,
returnedHits: [],
},
]

describe.each(numberPagesTestParameters)(
'Get Number Pages tests',
({ hitsPerPage, hitsLength, numberPages }) => {
it(`Should return ${numberPages} pages when hitsPerPage is ${hitsPerPage} and hits length is ${hitsLength}`, () => {
const response = getNumberPages(hitsLength, {
...defaultContext,
hitsPerPage: hitsPerPage,
})
expect(response).toBe(numberPages)
})
}
)

describe.each(paginateHitsTestsParameters)(
'Paginate hits tests',
({ hits, page, hitsPerPage, returnedHits }) => {
it(`Should return ${JSON.stringify(
returnedHits
)} when hitsPerPage is ${hitsPerPage}, number of page is ${page} and when hits is ${JSON.stringify(
hits
)}`, () => {
const response = paginateHits(hits, {
...defaultContext,
page,
hitsPerPage,
})
expect(response).toEqual(returnedHits)
})
}
)
13 changes: 13 additions & 0 deletions src/transformers/__tests__/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { MeiliSearch } from 'meilisearch'
const HOST = 'http://localhost:7700'

const defaultContext = {
client: new MeiliSearch({ host: HOST }),
paginationTotalHits: 200,
primaryKey: undefined,
placeholderSearch: true,
hitsPerPage: 20,
page: 0,
}

export { defaultContext }
9 changes: 6 additions & 3 deletions src/transformers/pagination.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@ export const getNumberPages: GetNumberPages = function (
hitsLength,
{ hitsPerPage }
) {
const adjust = hitsLength % hitsPerPage! === 0 ? 0 : 1
return Math.floor(hitsLength / hitsPerPage!) + adjust // total number of pages
if (hitsPerPage > 0) {
const NumberPages = Math.ceil(hitsLength / hitsPerPage) // total number of pages rounded up to the next largest integer.
return NumberPages
}
return 0
}

export const paginateHits: PaginateHits = function (
hits,
{ page, hitsPerPage }
) {
const start = page * hitsPerPage!
const start = page * hitsPerPage
return hits.splice(start, hitsPerPage)
}
File renamed without changes.