feat: add order to pagination

This commit is contained in:
Chen, Tai-Lin committed 2023-12-21 17:28:48 +08:00
1 parent 68db16419d
commit fa36b63fbe
8 files changed
+85 -50

No files matched your search

+21 -11
View File
@@ -1,32 +1,42 @@
import mongoose from 'mongoose'
import { Desc } from '../src/models/pagination.js'
import Pagination, { ASC, DESC } from '../src/models/pagination.js'
describe('Pagination', () => {
it('should return correct limit', async () => {
const pagination = new Desc(10)
it('should failed when not provide order', () => {
expect(() => new Pagination()).toThrow('order must be asc or desc')
})
it('should return correct limit', () => {
const pagination = new Pagination(DESC, 10)
expect(pagination.limit()).toMatchObject({ $limit: 10 })
})
it('should return correct order', async () => {
const pagination = new Desc(10)
it('desc should return correct order', async () => {
const pagination = new Pagination(DESC, 10)
expect(pagination.order('_id')).toMatchObject({ $sort: { _id: -1 } })
expect(pagination.order('time')).toMatchObject({ $sort: { time: -1 } })
expect(pagination.sort('_id')).toMatchObject({ $sort: { _id: -1 } })
expect(pagination.sort('time')).toMatchObject({ $sort: { time: -1 } })
})
it('asc should return correct order', async () => {
const pagination = new Pagination(ASC, 10)
expect(pagination.sort('_id')).toMatchObject({ $sort: { _id: 1 } })
expect(pagination.sort('time')).toMatchObject({ $sort: { time: 1 } })
})
it('should return correct match stage for empty nextKey', async () => {
const pagination = new Desc(10)
const pagination = new Pagination(DESC, 10)
expect(pagination.matchStage('_id')).toMatchObject({ $match: {} })
expect(pagination.match('_id')).toMatchObject({ $match: {} })
})
it('should return correct match stage for empty nextKey', async () => {
const nextKeyStr = '656c7fea28a4c087bfcefe12'
const pagination = new Desc(10, nextKeyStr)
const pagination = new Pagination(DESC, 10, nextKeyStr)
const nextKey = new mongoose.Types.ObjectId(nextKeyStr)
expect(pagination.matchStage('_id')).toMatchObject({
expect(pagination.match('_id')).toMatchObject({
$match: {
_id: {
$lt: nextKey,