Skip to content

Commit

Permalink
Merge branch '2447-preserve-cart-order' into staging
Browse files Browse the repository at this point in the history
  • Loading branch information
canac committed Dec 13, 2023
2 parents 83718a5 + 8ef70ce commit 137cd96
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/app/cart/cart.component.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,12 @@ class CartController {
} else {
this.loading = true
}
// Remember the order of the existing items in the cart
const orderByCode = this.cartData?.items?.map(item => item.code) ?? []
this.cartService.get()
.subscribe(data => {
// Sort the incoming cart to match the order of the previous cart, with new items at the top
data.items?.sort((item1, item2) => orderByCode.indexOf(item1.code) - orderByCode.indexOf(item2.code))
this.cartData = data
this.setLoadCartVars(reload)
},
Expand Down Expand Up @@ -97,7 +101,6 @@ class CartController {
.configureProduct(item.code, item.config, true, item.uri)
modal && modal.result
.then(() => {
pull(this.cartData.items, item)
this.loadCart(true)
}, angular.noop)
}
Expand Down
18 changes: 17 additions & 1 deletion src/app/cart/cart.component.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,23 @@ describe('cart', () => {
expect(self.controller.updating).toEqual(false)
})

it('should preserve the order', () => {
self.controller.cartService.get.mockReturnValue(Observable.of({ items: [{ code: '1' }, { code: '2' }, { code: '3' }] }))
self.controller.loadCart(true)

self.controller.cartService.get.mockReturnValue(Observable.of({ items: [{ code: '3' }, { code: '2' }, { code: '1' }, { code: '4' }] }))
self.controller.loadCart(true)

expect(self.controller.cartData).toEqual({ items: [{ code: '4' }, { code: '1' }, { code: '2' }, { code: '3' }] })
})

it('should handle empty data', () => {
self.controller.cartService.get.mockReturnValue(Observable.of({}))
self.controller.loadCart(true)

expect(self.controller.cartData).toEqual({})
})

it('should handle an error loading cart data', () => {
self.controller.cartData = 'previous data'
self.controller.cartService.get.mockReturnValue(Observable.throw('error'))
Expand Down Expand Up @@ -178,7 +195,6 @@ describe('cart', () => {

expect(self.controller.productModalService.configureProduct).toHaveBeenCalledWith('0123456', 'some config', true, 'uri1')
expect(self.controller.loadCart).toHaveBeenCalledWith(true)
expect(self.controller.cartData.items).toEqual([{ uri: 'uri2' }])
})
})

Expand Down

0 comments on commit 137cd96

Please sign in to comment.