|
| 1 | +/* eslint-env mocha */ |
| 2 | +import should from 'should' |
| 3 | +import mongoose from 'mongoose' |
| 4 | +import paginationPlugin from '../index' |
| 5 | + |
| 6 | +describe('mongoose-cursor-pagination', function () { |
| 7 | + before(function () { |
| 8 | + mongoose.Promise = Promise |
| 9 | + return mongoose.connect('mongodb://127.0.0.1/test', { |
| 10 | + useMongoClient: true |
| 11 | + }) |
| 12 | + }) |
| 13 | + |
| 14 | + before(function () { |
| 15 | + const UserSchema = new mongoose.Schema({ |
| 16 | + name: { type: String }, |
| 17 | + value: { type: Number, unique: true, index: true } |
| 18 | + }) |
| 19 | + UserSchema.plugin(paginationPlugin, { |
| 20 | + limit: 5 |
| 21 | + }) |
| 22 | + mongoose.model('User', UserSchema) |
| 23 | + }) |
| 24 | + |
| 25 | + before(function () { |
| 26 | + /* return mongoose.model('User').insertMany(Array(1000) |
| 27 | + .fill() |
| 28 | + .map((_, i) => ({ |
| 29 | + name: `User ${i}`, |
| 30 | + value: i |
| 31 | + }))) */ |
| 32 | + }) |
| 33 | + |
| 34 | + after(function () { |
| 35 | + // return mongoose.model('User').remove() |
| 36 | + }) |
| 37 | + |
| 38 | + it('pagination with no options', function () { |
| 39 | + return mongoose.model('User').paginate({}, { |
| 40 | + key: 'value' |
| 41 | + }) |
| 42 | + .then(results => { |
| 43 | + should.equal(results.items.length, 5) |
| 44 | + }) |
| 45 | + }) |
| 46 | + |
| 47 | + it('pagination by key and ascendingly', function () { |
| 48 | + return mongoose.model('User').paginate({}, { |
| 49 | + key: 'value', |
| 50 | + sort: { value: 1 } |
| 51 | + }) |
| 52 | + .then(results => { |
| 53 | + should.equal(results.items.length, 5) |
| 54 | + should.equal(results.hasMore, true) |
| 55 | + should.equal(results.items[0].value, 0) |
| 56 | + should.equal(results.items[4].value, 4) |
| 57 | + }) |
| 58 | + }) |
| 59 | + |
| 60 | + it('pagination by key, ascendingly and startingAfter', function () { |
| 61 | + return mongoose.model('User').paginate({}, { |
| 62 | + key: 'value', |
| 63 | + sort: { value: 1 }, |
| 64 | + startingAfter: 4 |
| 65 | + }) |
| 66 | + .then(results => { |
| 67 | + should.equal(results.items.length, 5) |
| 68 | + should.equal(results.hasMore, true) |
| 69 | + should.equal(results.items[0].value, 5) |
| 70 | + should.equal(results.items[4].value, 9) |
| 71 | + }) |
| 72 | + }) |
| 73 | + |
| 74 | + it('pagination when it reaches the end', function () { |
| 75 | + return mongoose.model('User').paginate({}, { |
| 76 | + key: 'value', |
| 77 | + sort: { value: 1 }, |
| 78 | + startingAfter: 998 |
| 79 | + }) |
| 80 | + .then(results => { |
| 81 | + should.equal(results.items.length, 1) |
| 82 | + should.equal(results.hasMore, false) |
| 83 | + should.equal(results.items[0].value, 999) |
| 84 | + }) |
| 85 | + }) |
| 86 | + |
| 87 | + it('pagination by key and descendingly', function () { |
| 88 | + return mongoose.model('User').paginate({}, { |
| 89 | + key: 'value', |
| 90 | + sort: { value: -1 } |
| 91 | + }) |
| 92 | + .then(results => { |
| 93 | + should.equal(results.items.length, 5) |
| 94 | + should.equal(results.hasMore, true) |
| 95 | + should.equal(results.items[0].value, 999) |
| 96 | + should.equal(results.items[4].value, 995) |
| 97 | + }) |
| 98 | + }) |
| 99 | + |
| 100 | + it('pagination by key, ascendingly and endingBefore', function () { |
| 101 | + return mongoose.model('User').paginate({}, { |
| 102 | + key: 'value', |
| 103 | + sort: { value: -1 }, |
| 104 | + endingBefore: 995 |
| 105 | + }) |
| 106 | + .then(results => { |
| 107 | + should.equal(results.items.length, 5) |
| 108 | + should.equal(results.hasMore, true) |
| 109 | + should.equal(results.items[0].value, 994) |
| 110 | + should.equal(results.items[4].value, 990) |
| 111 | + }) |
| 112 | + }) |
| 113 | +}) |
0 commit comments