From 4c6229124ad22986140aa7179505578aca46dc2d Mon Sep 17 00:00:00 2001 From: Jason Claxton <30830544+Jozzey@users.noreply.github.com> Date: Mon, 21 Oct 2024 15:26:34 +0100 Subject: [PATCH] Update some unit tests to use Model stubbing (#1426) We have found a pattern we can use to stub Objection Models in our unit tests. In this PR, we are going to use the model stubbing in some unit tests where it makes sense so that we have some examples of the pattern being used in our code for future reference. --- .../bill-runs/fetch-bill-runs.service.test.js | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/test/services/bill-runs/fetch-bill-runs.service.test.js b/test/services/bill-runs/fetch-bill-runs.service.test.js index 670fd25427..bae158e04e 100644 --- a/test/services/bill-runs/fetch-bill-runs.service.test.js +++ b/test/services/bill-runs/fetch-bill-runs.service.test.js @@ -10,6 +10,7 @@ const { expect } = Code // Test helpers const BillRunHelper = require('../../support/helpers/bill-run.helper.js') +const BillRunModel = require('../../../app/models/bill-run.model.js') const DatabaseConfig = require('../../../config/database.config.js') const RegionHelper = require('../../support/helpers/region.helper.js') @@ -103,6 +104,27 @@ describe('Fetch Bill Runs service', () => { }) }) }) + + describe('when there are no bill runs', () => { + beforeEach(async () => { + // There will usually be bill runs in the database from other tests so we stub the query to simulate no bill runs + const queryStub = Sinon.stub(BillRunModel, 'query') + + queryStub.returns({ + select: Sinon.stub().returnsThis(), + innerJoinRelated: Sinon.stub().returnsThis(), + orderBy: Sinon.stub().returnsThis(), + page: Sinon.stub().resolves({ results: [], total: 0 }) + }) + }) + + it('returns a result with no "results" and 0 for "total"', async () => { + const result = await FetchBillRunsService.go() + + expect(result.results).to.be.empty() + expect(result.total).to.equal(0) + }) + }) }) function _addBillRun (billRunNumber, createdAt, netTotal, creditNoteCount, invoiceCount, regionId) {