From b5408fe077c90bcf26a7eff20835a5a2f185d1f3 Mon Sep 17 00:00:00 2001 From: liam Date: Mon, 13 Mar 2023 22:48:27 +0000 Subject: [PATCH] added more tests for sorting and excluding hasMore added more badges to readme --- .github/workflows/{demo.yaml => tests.yaml} | 2 +- README.md | 10 +++ test/pagination.ts | 83 +++++++++++++++++++-- 3 files changed, 86 insertions(+), 9 deletions(-) rename .github/workflows/{demo.yaml => tests.yaml} (91%) diff --git a/.github/workflows/demo.yaml b/.github/workflows/tests.yaml similarity index 91% rename from .github/workflows/demo.yaml rename to .github/workflows/tests.yaml index a87c0ee..dcb444c 100644 --- a/.github/workflows/demo.yaml +++ b/.github/workflows/tests.yaml @@ -1,4 +1,4 @@ -name: Node.js CI +name: Tests & Coverage on: push: diff --git a/README.md b/README.md index e251003..5ff0031 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,17 @@

+ + + + Downloads + + + + Automated Tests + + Coverage Status diff --git a/test/pagination.ts b/test/pagination.ts index bbbadfd..4a679c6 100644 --- a/test/pagination.ts +++ b/test/pagination.ts @@ -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: { @@ -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: {}, @@ -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: { @@ -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); }) }); });