0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-20 22:42:53 -05:00

Added unit test for TiersRepository

We were completely missing tests for this, and the new logic pushed us under
the coverage threshold.
This commit is contained in:
Fabien "egg" O'Carroll 2023-05-12 12:00:12 -04:00 committed by Fabien 'egg' O'Carroll
parent c0ca7b16f6
commit 08597b47ba

View file

@ -0,0 +1,54 @@
const assert = require('assert');
const sinon = require('sinon');
const {Product: ProductModel} = require('../../../../../core/server/models/product');
const TierRepository = require('../../../../../core/server/services/tiers/TierRepository');
const {Tier} = require('@tryghost/tiers');
describe('TierRepository', function () {
after(function () {
sinon.restore();
});
it('Can do basic functionality', async function () {
const DomainEvents = {
dispatch: sinon.stub()
};
sinon.stub(ProductModel, 'findAll').resolves([]);
sinon.stub(ProductModel, 'findOne').resolves([]);
sinon.stub(ProductModel, 'edit').resolves();
sinon.stub(ProductModel, 'add').resolves();
const repository = new TierRepository({
DomainEvents,
ProductModel
});
await repository.init();
const tier = await Tier.create({
name: 'Test',
slug: 'test-tier',
type: 'paid',
status: 'active',
currency: 'USD',
monthlyPrice: 5,
yearlyPrice: 50
});
await repository.save(tier);
assert(ProductModel.add.calledOnce);
const result = await repository.getById(tier.id);
assert(ProductModel.findOne.notCalled);
assert(result);
result.name = 'New Name';
await repository.save(tier);
assert(ProductModel.edit.calledOnce);
});
});