Skip to content

Commit

Permalink
feat: add cart recalculate action (#136)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikedebock authored Jan 10, 2024
1 parent 0fcfd07 commit 7927c24
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/sixty-turtles-happen.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@labdigital/commercetools-mock': minor
---

add support for cart recalculate action
9 changes: 9 additions & 0 deletions src/repositories/cart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,15 @@ export class CartRepository extends AbstractResourceRepository<'cart'> {
// Update cart total price
resource.totalPrice.centAmount = calculateCartTotalPrice(resource)
},
recalculate: () => {
// Dummy action when triggering a recalculation of the cart
//
// From commercetools documentation:
// This update action does not set any Cart field in particular,
// but it triggers several Cart updates to bring prices and discounts to the latest state.
// Those can become stale over time when no Cart updates have been performed for a while
// and prices on related Products have changed in the meanwhile.
},
addItemShippingAddress: (
context: RepositoryContext,
resource: Writable<Cart>,
Expand Down
24 changes: 24 additions & 0 deletions src/services/cart.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,30 @@ describe('Cart Update Actions', () => {
expect(response.body.lineItems).toHaveLength(0)
})

test('recalculate', async () => {
const product = await supertest(ctMock.app)

Check warning on line 307 in src/services/cart.test.ts

View workflow job for this annotation

GitHub Actions / Lint codebase

'product' is assigned a value but never used
.post(`/dummy/products`)
.send(productDraft)
.then((x) => x.body)

assert(cart, 'cart not created')

const response = await supertest(ctMock.app)
.post(`/dummy/carts/${cart.id}`)
.send({
version: 1,
actions: [
{
action: 'recalculate',
updateProductData: true,
},
],
})

expect(response.status).toBe(200)
expect(response.body.version).toBe(1)
})

test('removeLineItem', async () => {
const product = await supertest(ctMock.app)
.post(`/dummy/products`)
Expand Down

0 comments on commit 7927c24

Please sign in to comment.