|
/* eslint-env jest */ |
|
|
|
import paginatorFactory from './paginator' |
|
import { User, users } from './__fixtures__' |
|
|
|
describe('Factory', () => { |
|
let u: User[] |
|
let page: number |
|
|
|
beforeEach(() => { |
|
/* tslint:disable-next-line:no-shadowed-variable */ |
|
u = users.map(u => ({ ...u })) |
|
}) |
|
|
|
test('Typical use', () => { |
|
page = 1 |
|
const max: number = 5 |
|
const paginator = paginatorFactory<User>(u, max) |
|
|
|
const currentPage = paginator(page) |
|
expect(currentPage).toMatchSnapshot() |
|
expect(currentPage).toHaveProperty('page', page) |
|
expect(currentPage.items.map(e => e.id)).toMatchObject([0, 1, 2, 3, 4]) |
|
|
|
const total = u.length |
|
const pages = total / max |
|
expect(currentPage).toHaveProperty('items') |
|
expect(currentPage).toHaveProperty('pages', pages) |
|
expect(currentPage).toHaveProperty('total', total) |
|
|
|
const pageTwo = paginator(2) |
|
expect(pageTwo).toMatchSnapshot() |
|
expect(pageTwo).toHaveProperty('page', 2) |
|
expect(pageTwo).toHaveProperty('pages', pages) |
|
expect(pageTwo).toHaveProperty('total', total) |
|
expect(pageTwo.items.map(e => e.id)).toMatchObject([5, 6, 7, 8, 9]) |
|
|
|
const [sixth] = pageTwo.items |
|
expect(sixth).toMatchObject(u[5]) |
|
expect(sixth).toMatchObject({ id: 5 }) |
|
}) |
|
|
|
// should return the first page (of 3) |
|
// page increment starts at 1 |
|
// users fixture has 15 elements |
|
test('max=5_page=1', () => { |
|
page = 1 |
|
const max: number = 5 |
|
const paginator = paginatorFactory<User>(u, max) |
|
const currentPage = paginator(page) |
|
|
|
expect(currentPage.items.length).toEqual(5) |
|
expect(currentPage.items[0].id).toEqual(0) |
|
expect(currentPage.items[4].id).toEqual(4) |
|
|
|
expect(currentPage.page).toEqual(1) |
|
expect(currentPage.pages).toEqual(3) |
|
expect(currentPage.total).toEqual(15) |
|
expect(currentPage.error).toBeFalsy() |
|
}) |
|
|
|
// should return the second page (of 3) |
|
test('max=5_page=2(middle)', () => { |
|
page = 2 |
|
const max: number = 5 |
|
const paginator = paginatorFactory<User>(u, max) |
|
const currentPage = paginator(page) |
|
|
|
expect(currentPage.items.length).toEqual(5) |
|
expect(currentPage.items[0].id).toEqual(5) |
|
expect(currentPage.items[4].id).toEqual(9) |
|
|
|
expect(currentPage.page).toEqual(2) |
|
expect(currentPage.pages).toEqual(3) |
|
expect(currentPage.total).toEqual(15) |
|
expect(currentPage.error).toBeUndefined() |
|
}) |
|
|
|
// should return the third page (of 3) |
|
test('max=5_page=3(upper limit)', () => { |
|
page = 3 |
|
const max: number = 5 |
|
const paginator = paginatorFactory<User>(u, max) |
|
const currentPage = paginator(page) |
|
|
|
expect(currentPage.items.length).toEqual(5) |
|
expect(currentPage.items[0].id).toEqual(10) |
|
expect(currentPage.items[4].id).toEqual(14) |
|
|
|
expect(currentPage.page).toEqual(3) |
|
expect(currentPage.pages).toEqual(3) |
|
expect(currentPage.total).toEqual(15) |
|
expect(currentPage.error).toBeUndefined() |
|
}) |
|
|
|
// invalid page should return an error |
|
test('max=5_page=4(oob)', () => { |
|
page = 4 |
|
const max: number = 5 |
|
const paginator = paginatorFactory<User>(u, max) |
|
const currentPage = paginator(page) |
|
|
|
expect(currentPage.items).toMatchObject([]) |
|
expect(currentPage.error).toBe( |
|
'The page number provided (4) is larger than the maximum number of pages (3)', |
|
) |
|
}) |
|
|
|
// invalid page should return an error |
|
test('max=5_page=0(oob)', () => { |
|
page = 0 |
|
const max: number = 5 |
|
const paginator = paginatorFactory<User>(u, max) |
|
const currentPage = paginator(page) |
|
|
|
expect(currentPage.items).toMatchObject([]) |
|
expect(currentPage.error).toBe( |
|
'The page number provided (0) cannot be under 1', |
|
) |
|
}) |
|
|
|
// invalid page should return an error |
|
test('max=5_page=-1(oob)', () => { |
|
page = -1 |
|
const max: number = 5 |
|
const paginator = paginatorFactory<User>(u, max) |
|
const currentPage = paginator(page) |
|
expect(currentPage.error).toBe( |
|
'The page number provided (-1) cannot be under 1', |
|
) |
|
}) |
|
|
|
// should default to page = 1 on lack of input |
|
test('max=5_noPageInput', () => { |
|
const max: number = 5 |
|
const paginator = paginatorFactory<User>(u, max) |
|
// @ts-ignore |
|
const currentPage = paginator() |
|
|
|
expect(currentPage.items.length).toEqual(5) |
|
expect(currentPage.items[0].id).toEqual(0) |
|
expect(currentPage.items[4].id).toEqual(4) |
|
|
|
expect(currentPage.page).toEqual(1) |
|
expect(currentPage.pages).toEqual(3) |
|
expect(currentPage.total).toEqual(15) |
|
expect(currentPage.error).toBeUndefined() |
|
}) |
|
|
|
// when max=users.length, page 1 should contain the entire items |
|
test('max=15(maximum)_page=1', () => { |
|
page = 1 |
|
const max: number = 15 |
|
const paginator = paginatorFactory<User>(u, max) |
|
const currentPage = paginator(page) |
|
|
|
expect(currentPage.items.length).toEqual(u.length) |
|
expect(currentPage.items[0].id).toEqual(0) |
|
expect(currentPage.items[14].id).toEqual(14) |
|
|
|
expect(currentPage.page).toEqual(1) |
|
expect(currentPage.pages).toEqual(1) |
|
expect(currentPage.total).toEqual(15) |
|
expect(currentPage.error).toBeUndefined() |
|
}) |
|
|
|
// when max does not divide evenly into total, the last page (2) should |
|
// contain only the remaining elements. |
|
test('max=12(split page)_page=1', () => { |
|
page = 2 |
|
const max: number = 12 |
|
const paginator = paginatorFactory<User>(u, max) |
|
const currentPage = paginator(page) |
|
|
|
expect(currentPage.items.length).toEqual(3) |
|
expect(currentPage.items[0].id).toEqual(12) |
|
expect(currentPage.items[2].id).toEqual(14) |
|
|
|
expect(currentPage.page).toEqual(2) |
|
expect(currentPage.pages).toEqual(2) |
|
expect(currentPage.total).toEqual(15) |
|
expect(currentPage.error).toBeUndefined() |
|
}) |
|
|
|
// max = 1 should return 15 single-element pages |
|
test('max=1(small page)_page=1', () => { |
|
page = 1 |
|
const max: number = 1 |
|
const paginator = paginatorFactory<User>(u, max) |
|
const currentPage = paginator(page) |
|
|
|
expect(currentPage).toMatchSnapshot() |
|
expect(currentPage.items.length).toEqual(1) |
|
expect(currentPage.items[0].id).toEqual(0) |
|
expect(currentPage.error).toBeUndefined() |
|
}) |
|
|
|
// should return pages of one element each |
|
test('max=1(small page)_page=7', () => { |
|
page = 7 |
|
const max: number = 1 |
|
const paginator = paginatorFactory<User>(u, max) |
|
const currentPage = paginator(page) |
|
|
|
expect(currentPage.items.length).toEqual(1) |
|
expect(currentPage.items[0].id).toEqual(6) |
|
|
|
expect(currentPage.page).toEqual(7) |
|
expect(currentPage.pages).toEqual(15) |
|
expect(currentPage.total).toEqual(15) |
|
expect(currentPage.error).toBeUndefined() |
|
}) |
|
|
|
// should return pages of one element each |
|
test('max=1(small page)_page=15', () => { |
|
page = 15 |
|
const max: number = 1 |
|
const paginator = paginatorFactory<User>(u, max) |
|
const currentPage = paginator(page) |
|
|
|
expect(currentPage.items.length).toEqual(1) |
|
expect(currentPage.items[0].id).toEqual(14) |
|
|
|
expect(currentPage.page).toEqual(15) |
|
expect(currentPage.pages).toEqual(15) |
|
expect(currentPage.total).toEqual(15) |
|
expect(currentPage.error).toBeUndefined() |
|
}) |
|
|
|
// should return pages of two elements each |
|
test('max=2(small page)_page=1', () => { |
|
page = 1 |
|
const max: number = 2 |
|
const paginator = paginatorFactory<User>(u, max) |
|
const currentPage = paginator(page) |
|
|
|
expect(currentPage.items.length).toEqual(2) |
|
expect(currentPage.items[0].id).toEqual(0) |
|
expect(currentPage.items[1].id).toEqual(1) |
|
|
|
expect(currentPage.page).toEqual(1) |
|
expect(currentPage.pages).toEqual(8) |
|
expect(currentPage.total).toEqual(15) |
|
expect(currentPage.error).toBeUndefined() |
|
}) |
|
|
|
// max out of bounds should default to total |
|
test('max=16(oob)_page=1', () => { |
|
page = 1 |
|
const max: number = 16 |
|
const paginator = paginatorFactory<User>(u, max) |
|
const currentPage = paginator(page) |
|
|
|
expect(currentPage.items.length).toEqual(u.length) |
|
expect(currentPage.items[0].id).toEqual(0) |
|
expect(currentPage.items[14].id).toEqual(14) |
|
|
|
expect(currentPage.page).toEqual(1) |
|
expect(currentPage.pages).toEqual(1) |
|
expect(currentPage.total).toEqual(15) |
|
expect(currentPage.error).toBeUndefined() |
|
}) |
|
|
|
// Max below 1 should be ignored and act as if max was default=10 |
|
test('max=0(oob)_page=1', () => { |
|
page = 1 |
|
const max: number = 0 |
|
const paginator = paginatorFactory<User>(u, max) |
|
const currentPage = paginator(page) |
|
|
|
expect(currentPage.page).toEqual(1) |
|
expect(currentPage.pages).toEqual(2) |
|
expect(currentPage.error).toBeUndefined() |
|
}) |
|
test('max is optional', () => { |
|
page = 1 |
|
const paginator = paginatorFactory<User>(u) |
|
const currentPage = paginator(page) |
|
|
|
expect(currentPage.page).toEqual(1) |
|
expect(currentPage.pages).toEqual(2) |
|
expect(currentPage.total).toEqual(15) |
|
expect(currentPage.error).toBeUndefined() |
|
}) |
|
|
|
// max when -1 should be ignored and act as if max was default=10 |
|
test('max=-1(oob)_page=1', () => { |
|
page = 1 |
|
const max: number = -1 |
|
const paginator = paginatorFactory<User>(u, max) |
|
const currentPage = paginator(page) |
|
|
|
expect(currentPage.page).toEqual(1) |
|
expect(currentPage.pages).toEqual(2) |
|
expect(currentPage.total).toEqual(15) |
|
expect(currentPage.error).toBeUndefined() |
|
}) |
|
|
|
// max out of bounds should be ignored and act as if max was default=10 |
|
test('max=-10(oob)_page=1', () => { |
|
page = 1 |
|
const max: number = -10 |
|
const paginator = paginatorFactory<User>(u, max) |
|
const currentPage = paginator(page) |
|
|
|
expect(currentPage.page).toEqual(1) |
|
expect(currentPage.pages).toEqual(2) |
|
expect(currentPage.total).toEqual(15) |
|
expect(currentPage.error).toBeUndefined() |
|
}) |
|
|
|
// with no input for max, should return an error |
|
test('noMax_page=1', () => { |
|
page = 1 |
|
const paginator = paginatorFactory<User>(u) |
|
const currentPage = paginator(page) |
|
|
|
expect(currentPage.page).toEqual(1) |
|
expect(currentPage.pages).toEqual(2) |
|
expect(currentPage.total).toEqual(15) |
|
}) |
|
|
|
// with an empty array as input, should return an error |
|
test('emptyArrayInput1', () => { |
|
expect(() => { |
|
u = [] |
|
page = 1 |
|
const max: number = 5 |
|
paginatorFactory<User>(u, max) |
|
}).toThrowError('The provided array was empty') |
|
}) |
|
|
|
// with an empty array as input, should return an error |
|
test('emptyArrayInput2', () => { |
|
expect(() => { |
|
u = [] |
|
page = 0 |
|
const max: number = 5 |
|
paginatorFactory<User>(u, max) |
|
}).toThrowError('The provided array was empty') |
|
}) |
|
|
|
// with an empty array as input, should return an error |
|
test('emptyArrayInput3', () => { |
|
expect(() => { |
|
u = [] |
|
page = 1 |
|
const max: number = 0 |
|
paginatorFactory<User>(u, max) |
|
}).toThrowError('The provided array was empty') |
|
}) |
|
|
|
// with an empty array as input, should return an error |
|
test('emptyArrayInput4', () => { |
|
expect(() => { |
|
u = [] |
|
page = 10 |
|
const max: number = 5 |
|
const paginator = paginatorFactory<User>(u, max) |
|
paginator(page) |
|
}).toThrowError('The provided array was empty') |
|
}) |
|
|
|
// with an empty array as input, should return an error |
|
test('emptyArrayInput5', () => { |
|
expect(() => { |
|
u = [] |
|
page = -10 |
|
const max: number = -5 |
|
const paginator = paginatorFactory<User>(u, max) |
|
paginator(page) |
|
}).toThrowError('The provided array was empty') |
|
}) |
|
|
|
test('smallArrayInput', () => { |
|
u = [{ ...users[0] }] |
|
page = 1 |
|
const max: number = 1 |
|
const paginator = paginatorFactory<User>(u, max) |
|
const currentPage = paginator(page) |
|
|
|
expect(currentPage.items[0].id).toEqual(0) |
|
|
|
expect(currentPage.page).toEqual(1) |
|
expect(currentPage.pages).toEqual(1) |
|
expect(currentPage.total).toEqual(1) |
|
expect(currentPage.error).toBeUndefined() |
|
}) |
|
|
|
test('smallArrayInput2', () => { |
|
u = [{ ...users[0] }] |
|
page = 1 |
|
const max: number = 10 |
|
const paginator = paginatorFactory<User>(u, max) |
|
const currentPage = paginator(page) |
|
|
|
expect(currentPage.items[0].id).toEqual(0) |
|
|
|
expect(currentPage.page).toEqual(1) |
|
expect(currentPage.pages).toEqual(1) |
|
expect(currentPage.total).toEqual(1) |
|
expect(currentPage.error).toBeUndefined() |
|
}) |
|
|
|
//paginator should function as expected with multiple calls |
|
test('paginator used twice', () => { |
|
page = 1 |
|
const max: number = 5 |
|
const paginator = paginatorFactory<User>(u, max) |
|
const currentPage = paginator(page) |
|
|
|
expect(currentPage).toMatchSnapshot() |
|
expect(currentPage.items.length).toEqual(5) |
|
expect(currentPage.items[0].id).toEqual(0) |
|
|
|
expect(currentPage.page).toEqual(1) |
|
expect(currentPage.pages).toEqual(3) |
|
expect(currentPage.total).toEqual(15) |
|
expect(currentPage.error).toBeUndefined() |
|
|
|
const nextPage = paginator(page + 1) |
|
|
|
expect(nextPage).toMatchSnapshot() |
|
expect(nextPage.items).toMatchSnapshot() |
|
expect(nextPage.items.length).toEqual(5) |
|
expect(nextPage.items[0].id).toEqual(5) |
|
|
|
expect(nextPage.page).toEqual(2) |
|
expect(nextPage.pages).toEqual(3) |
|
expect(nextPage.total).toEqual(15) |
|
expect(currentPage.error).toBeUndefined() |
|
}) |
|
|
|
test('samePageTwice', () => { |
|
page = 1 |
|
const max: number = 5 |
|
const paginator = paginatorFactory<User>(u, max) |
|
const currentPage = paginator(page) |
|
|
|
expect(currentPage.items.length).toEqual(5) |
|
expect(currentPage.items[0].id).toEqual(0) |
|
expect(currentPage.items[4].id).toEqual(4) |
|
|
|
expect(currentPage.page).toEqual(1) |
|
expect(currentPage.pages).toEqual(3) |
|
expect(currentPage.total).toEqual(15) |
|
|
|
const samePage = paginator(page) |
|
|
|
expect(samePage).toEqual(currentPage) |
|
|
|
expect(samePage.items.length).toEqual(5) |
|
expect(samePage.items[0].id).toEqual(0) |
|
expect(samePage.items[4].id).toEqual(4) |
|
|
|
expect(samePage.page).toEqual(1) |
|
expect(samePage.pages).toEqual(3) |
|
expect(samePage.total).toEqual(15) |
|
}) |
|
|
|
// should return an error |
|
test('null1', () => { |
|
expect(() => { |
|
page = 1 |
|
const max: number = 5 |
|
// @ts-ignore |
|
paginatorFactory<User>(null, max) |
|
}).toThrowError('The provided array was invalid') |
|
}) |
|
|
|
// should return an error |
|
test('null2', () => { |
|
expect(() => { |
|
page = 1 |
|
// @ts-ignore |
|
const paginator = paginatorFactory<User>(u, null) |
|
paginator(page) |
|
}).toThrowError('Max pages argument MUST be an integer') |
|
}) |
|
|
|
// should return an error |
|
test('null3', () => { |
|
expect(() => { |
|
// @ts-ignore |
|
page = null |
|
const max: number = 5 |
|
const paginator = paginatorFactory<User>(u, max) |
|
paginator(page) |
|
}).toThrowError('page argument MUST be an integer') |
|
}) |
|
|
|
// should return an error |
|
test('floating_point_max', () => { |
|
expect(() => { |
|
page = 1 |
|
const max: number = 1.5 |
|
const paginator = paginatorFactory<User>(u, max) |
|
paginator(page) |
|
}).toThrowError('Max pages argument MUST be an integer') |
|
}) |
|
|
|
// should return an error |
|
test('floating_point_page', () => { |
|
expect(() => { |
|
page = 1.5 |
|
const max: number = 5 |
|
const paginator = paginatorFactory<User>(u, max) |
|
paginator(page) |
|
}).toThrowError('page argument MUST be an integer') |
|
}) |
|
}) |