Skip to content

Commit 05e8c9e

Browse files
committed
Added basic tests.
1 parent ed55335 commit 05e8c9e

File tree

3 files changed

+130
-3
lines changed

3 files changed

+130
-3
lines changed

README.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,14 @@
11
# mongoose-cursor-pagination
2-
Mongoose cursor-based pagination
2+
[![Build Status](https://travis-ci.org/enkidevs/mongoose-cursor-pagination.svg?branch=master)](https://travis-ci.org/enkidevs/mongoose-cursor-pagination.svg?branch=master)
3+
[![Dependency Status](https://david-dm.org/enkidevs/mongoose-cursor-pagination.svg)](https://david-dm.org/enkidevs/mongoose-cursor-pagination)
4+
[![devDependency Status](https://david-dm.org/enkidevs/mongoose-cursor-pagination/dev-status.svg)](https://david-dm.org/enkidevs/mongoose-cursor-pagination#info=devDependencies)
5+
[![contributions welcome](https://img.shields.io/badge/contributions-welcome-brightgreen.svg?style=flat)](https://github.com/enkidevs/mongoose-cursor-pagination/issues)
6+
[![HitCount](http://hits.dwyl.io/enkidevs/mongoose-cursor-pagination.svg)](http://hits.dwyl.io/enkidevs/mongoose-cursor-pagination)
7+
8+
> Mongoose cursor-based pagination
9+
10+
## Installation
11+
12+
```bash
13+
npm install mongoose-cursor-pagination --save
14+
```

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
"lib"
88
],
99
"scripts": {
10-
"build": "babel src --out-dir lib",
11-
"test": "standard"
10+
"prepare": "babel src --out-dir lib",
11+
"test": "standard && mocha 'lib/**/__tests__/*'"
1212
},
1313
"repository": {
1414
"type": "git",
@@ -32,7 +32,9 @@
3232
"babel-cli": "^6.26.0",
3333
"babel-preset-es2015": "^6.24.1",
3434
"babel-preset-stage-0": "^6.24.1",
35+
"mocha": "^4.0.1",
3536
"mongoose": "^4.12.3",
37+
"should": "^13.1.2",
3638
"standard": "*"
3739
}
3840
}

src/__tests__/index.js

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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

Comments
 (0)