Add paginate test

This commit is contained in:
Sam Carlton 2022-05-18 17:06:04 -05:00
parent c0581c633b
commit fef9002c8f

View file

@ -9,6 +9,9 @@ import {
fuzzyMatchesWholeWord,
eitherMatches
} from '~/helpers/matching.js'
import {
PaginatedList
} from '~/helpers/api/pagination.js'
require('dotenv').config()
@ -178,3 +181,45 @@ test('Can match names with pluses', (t) => {
})
test('Can paginate', async (t) => {
const cases = [
{
from: {
list: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
perPage: 2,
},
expect: {
pageCount: 5,
pages: [
{
number: 1,
items: [1, 2],
json: '[1,2]'
},
{
number: 2,
items: [3, 4],
json: '[3,4]'
}
]
}
}
]
for ( const { from, expect } of cases ) {
const paginatedList = new PaginatedList( from )
// Assert that page count is correct
t.is( paginatedList.pageCount, expect.pageCount, 'pageCount is incorrect' )
// Assert that the pages we're expecting are there
for ( const expectedPage of expect.pages ) {
// Get respective output page
const outputPage = paginatedList.pages[ expectedPage.number - 1 ]
t.deepEqual( outputPage, expectedPage, `Page ${ expectedPage.number } is an unexpected structure` )
}
}
} )