refactor: add Pagination class

This commit is contained in:
Chen, Tai-Lin committed 2023-12-21 17:09:20 +08:00
1 parent 19cd10ab7b
commit 68db16419d
9 files changed
+99 -89

No files matched your search

@@ -0,0 +1,37 @@
import mongoose from 'mongoose'
import { Desc } from '../src/models/pagination.js'
describe('Pagination', () => {
it('should return correct limit', async () => {
const pagination = new Desc(10)
expect(pagination.limit()).toMatchObject({ $limit: 10 })
})
it('should return correct order', async () => {
const pagination = new Desc(10)
expect(pagination.order('_id')).toMatchObject({ $sort: { _id: -1 } })
expect(pagination.order('time')).toMatchObject({ $sort: { time: -1 } })
})
it('should return correct match stage for empty nextKey', async () => {
const pagination = new Desc(10)
expect(pagination.matchStage('_id')).toMatchObject({ $match: {} })
})
it('should return correct match stage for empty nextKey', async () => {
const nextKeyStr = '656c7fea28a4c087bfcefe12'
const pagination = new Desc(10, nextKeyStr)
const nextKey = new mongoose.Types.ObjectId(nextKeyStr)
expect(pagination.matchStage('_id')).toMatchObject({
$match: {
_id: {
$lt: nextKey,
},
},
})
})
})