-
-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added 'getDefaultProduct' convenience method to product repo
refs https://github.com/TryGhost/Team/issues/1869 - There are multiple places in the codebase fetching "default product". The code is slightly divergent in each one of them and has been a source of bugs (like the one referenced). Having the logic captured in one place will allow reducing the code duplication, making code less bug prone, and making testing the modules dependent on the "setDefaultProduct" method easier
- Loading branch information
Showing
2 changed files
with
43 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
ghost/members-api/test/unit/lib/repositories/product.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
const assert = require('assert'); | ||
const sinon = require('sinon'); | ||
const ProductRepository = require('../../../../lib/repositories/product'); | ||
|
||
describe('MemberRepository', function () { | ||
describe('getDefaultProduct', function () { | ||
it('calls list method with specific parameters', async function () { | ||
const productRepository = new ProductRepository({}); | ||
const listStub = sinon.stub(productRepository, 'list').resolves({ | ||
data: [{ | ||
id: 'default_product_id' | ||
}] | ||
}); | ||
|
||
const defaultProduct = await productRepository.getDefaultProduct({ | ||
withRelated: ['stripePrices'] | ||
}); | ||
|
||
assert.ok(listStub.called); | ||
|
||
assert.equal(listStub.args[0][0].filter, 'type:paid+active:true', 'should only take into account paid and active records'); | ||
assert.equal(listStub.args[0][0].limit, 1, 'should only fetch a single record'); | ||
assert.deepEqual(listStub.args[0][0].withRelated, ['stripePrices'], 'should extend passed in options'); | ||
|
||
assert.equal(defaultProduct.id, 'default_product_id', 'returns a single product object'); | ||
}); | ||
}); | ||
}); |