mirror of
https://github.com/typicode/json-server.git
synced 2025-07-29 05:03:04 +08:00

* Remove automatic type conversion * Remove body-parser from default middlewares * Ignore lib * ES2015 * Use shortid * Add babel-register * Update paths to ./lib * Add .npmignore * Update bin * temp fix * Fix bin * Add message when creating default db * Use fs.watch * Fix operator existence test * Fix 0.12 tests * Update dependencies * Update * Increase timeout * Fix 0.12 support * 0.9.0-beta.1 * Fix missing example.json issue * 0.9.0-beta.2 * Update message * Update CHANGELOG.md * Update lowdb dependency * Add error message * Update README.md * Add database validation * Update * Update * Fix tests * Update
77 lines
1.6 KiB
JavaScript
77 lines
1.6 KiB
JavaScript
const assert = require('assert')
|
|
const utils = require('../../src/server/utils')
|
|
|
|
describe('utils', function () {
|
|
describe('getPage', function () {
|
|
const array = [1, 2, 3, 4, 5]
|
|
const perPage = 2
|
|
|
|
it('should return first page', function () {
|
|
assert.deepEqual(
|
|
utils.getPage(array, 1, perPage),
|
|
{
|
|
items: [1, 2],
|
|
current: 1,
|
|
first: 1,
|
|
next: 2,
|
|
last: 3
|
|
}
|
|
)
|
|
})
|
|
|
|
it('should return second page', function () {
|
|
assert.deepEqual(
|
|
utils.getPage(array, 2, perPage),
|
|
{
|
|
items: [3, 4],
|
|
current: 2,
|
|
first: 1,
|
|
prev: 1,
|
|
next: 3,
|
|
last: 3
|
|
}
|
|
)
|
|
})
|
|
|
|
it('should return third page (last)', function () {
|
|
assert.deepEqual(
|
|
utils.getPage(array, 3, perPage),
|
|
{
|
|
items: [5],
|
|
current: 3,
|
|
first: 1,
|
|
prev: 2,
|
|
last: 3
|
|
}
|
|
)
|
|
})
|
|
|
|
it('should return an empty array if page is greater than the last page', function () {
|
|
assert.deepEqual(
|
|
utils.getPage(array, 99, perPage),
|
|
{
|
|
items: []
|
|
}
|
|
)
|
|
})
|
|
|
|
it('should return the array if perPage is greater than the array size', function () {
|
|
assert.deepEqual(
|
|
utils.getPage(array, 1, 99),
|
|
{
|
|
items: array
|
|
}
|
|
)
|
|
})
|
|
|
|
it('should return an empty array if the array is empty', function () {
|
|
assert.deepEqual(
|
|
utils.getPage([], 1, 1),
|
|
{
|
|
items: []
|
|
}
|
|
)
|
|
})
|
|
})
|
|
})
|