Skip to content

Commit

Permalink
Update some unit tests to use Model stubbing (#1426)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Jozzey authored Oct 21, 2024
1 parent ebab844 commit 4c62291
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions test/services/bill-runs/fetch-bill-runs.service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 4c62291

Please sign in to comment.