Skip to content

Commit

Permalink
added more tests for sorting and excluding hasMore
Browse files Browse the repository at this point in the history
added more badges to readme
  • Loading branch information
caffeinated-tech committed Mar 13, 2023
1 parent 3affa52 commit b5408fe
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 9 deletions.
@@ -1,4 +1,4 @@
name: Node.js CI
name: Tests & Coverage

on:
push:
Expand Down
10 changes: 10 additions & 0 deletions README.md
Expand Up @@ -7,7 +7,17 @@
<p align="center">
<a href="https://github.com/caffeinated-tech/mongoose-pagination/graphs/contributors" alt="Contributors">
<img src="https://img.shields.io/github/contributors/caffeinated-tech/mongoose-pagination" />
</a>

<a href='https://www.npmjs.com/package/mongoose-fast-pagination'>
<img src='https://img.shields.io/npm/dw/mongoose-fast-pagination' alt='Downloads' />
</a>


<a href='https://github.com/caffeinated-tech/mongoose-pagination/actions/workflows/tests.yaml'>
<img src='https://coveralls.io/repos/github/caffeinated-tech/mongoose-pagination/badge.svg?branch=master' alt='Automated Tests' />
</a>

<a href='https://coveralls.io/github/caffeinated-tech/mongoose-pagination?branch=master'>
<img src='https://coveralls.io/repos/github/caffeinated-tech/mongoose-pagination/badge.svg?branch=master' alt='Coverage Status' />
</a>
Expand Down
83 changes: 75 additions & 8 deletions test/pagination.ts
Expand Up @@ -34,6 +34,16 @@ describe('Item', async function () {
assert.ok(!totalCount, 'totalCount is returned');
});

it('doesnt return hsaMore if ovverode in options', async function () {
let { hasMore } = await Item.paginate({
where: {},
options: {
includeHasMore: false
}
})
assert.ok(!hasMore, 'hasMore is returned');
});

it('can project fields to be returned', async function () {
let { documents } = await Item.paginate({
projection: {
Expand All @@ -60,6 +70,7 @@ describe('Item', async function () {
assert.equal(documents.length, 0);
});


it('hasMore is false if no more documents', async function () {
let { documents, hasMore } = await Item.paginate({
where: {},
Expand Down Expand Up @@ -178,6 +189,30 @@ describe('Item', async function () {
assert.equal(secondPage.documents.length, 0);
});

it('Can return an empty second page when the lastId document is deleted', async function () {
let { documents, hasMore } = await Item.paginate({
where: {},
sort: {
createdAt: 1
},
perPage: 1000
})
const lastDocument = documents[documents.length - 1]
const lastId = lastDocument.id
await lastDocument.delete()

let secondPage = await Item.paginate({
where: {},
sort: {
createdAt: 1
},
perPage: 1000,
lastId
})
assert.equal(secondPage.hasMore, false);
assert.equal(secondPage.documents.length, 0);
});

it('can filter by nested field', async function () {
let { documents, hasMore } = await Item.paginate({
where: {
Expand All @@ -190,25 +225,57 @@ describe('Item', async function () {
assert.equal(documents.length, 1);
})

it('can sort by nested field', async function () {
it('can sort by in ascending order', async function () {
let { documents, hasMore } = await Item.paginate({
where: {},
sort: {
'nested.dateField': -1
createdAt: -1
},
perPage: 100
})

assert.equal(hasMore, true);
assert.equal(documents.length, 100);
const lastId = documents[documents.length - 1].id
let secondPage = await Item.paginate({
let previousDocument = null
for (let document of documents) {
if (previousDocument) {
assert.ok(document.createdAt < previousDocument.createdAt, 'each document should have a smaller createdAt than the previous one')
}
previousDocument = document
}
})

it('can sort by in descending order', async function () {
let { documents, hasMore } = await Item.paginate({
where: {},
perPage: 100,
lastId
sort: {
createdAt: 1
},
perPage: 100
})
assert.equal(secondPage.hasMore, true);
assert.equal(secondPage.documents.length, 100);

assert.equal(hasMore, true);
assert.equal(documents.length, 100);
let previousDocument = null
for (let document of documents) {
if (previousDocument) {
assert.ok(document.createdAt > previousDocument.createdAt, 'each document should have a smaller createdAt than the previous one')
}
previousDocument = document
}
})

it('can sort by nested field', async function () {
let { documents, hasMore } = await Item.paginate({
where: {},
sort: {
'nested.dateField': -1
},
perPage: 100
})

assert.equal(hasMore, true);
assert.equal(documents.length, 100);
})
});
});

0 comments on commit b5408fe

Please sign in to comment.