-
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cart): add
daffCartGetItemTotalDiscount
(#2797)
- Loading branch information
Showing
6 changed files
with
119 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { TestBed } from '@angular/core/testing'; | ||
|
||
import { DaffCartItem } from '@daffodil/cart'; | ||
import { DaffCartItemFactory } from '@daffodil/cart/testing'; | ||
|
||
import { daffCartGetAffectedItems } from './get-affected-item'; | ||
|
||
describe('@daffodil/cart | daffCartGetAffectedItems', () => { | ||
let cartItemFactory: DaffCartItemFactory; | ||
let newItems: Array<DaffCartItem>; | ||
let oldItems: Array<DaffCartItem>; | ||
let result: Array<DaffCartItem['id']>; | ||
|
||
beforeEach(() => { | ||
cartItemFactory = TestBed.inject(DaffCartItemFactory); | ||
oldItems = cartItemFactory.createMany(2); | ||
newItems = [ | ||
{ | ||
...oldItems[0], | ||
qty: oldItems[0].qty + 1, | ||
}, | ||
oldItems[1], | ||
cartItemFactory.create({ | ||
id: `${oldItems[0].id}${oldItems[1].id}`, | ||
}), | ||
]; | ||
result = daffCartGetAffectedItems(oldItems, newItems); | ||
}); | ||
|
||
it('should return updated cart items', () => { | ||
expect(result).toContain(newItems[0].id); | ||
}); | ||
|
||
it('should return added cart items', () => { | ||
expect(result).toContain(newItems[2].id); | ||
}); | ||
|
||
it('should not return unchanged cart items', () => { | ||
expect(result).not.toContain(newItems[1].id); | ||
}); | ||
}); |
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,31 @@ | ||
import { TestBed } from '@angular/core/testing'; | ||
|
||
import { DaffCartItem } from '@daffodil/cart'; | ||
import { DaffCartItemFactory } from '@daffodil/cart/testing'; | ||
|
||
import { daffCartGetItemTotalDiscount } from './get-item-total-discount'; | ||
|
||
describe('@daffodil/cart | daffCartGetItemTotalDiscount', () => { | ||
let cartItemFactory: DaffCartItemFactory; | ||
let cartItem: DaffCartItem; | ||
|
||
beforeEach(() => { | ||
cartItemFactory = TestBed.inject(DaffCartItemFactory); | ||
cartItem = cartItemFactory.create(); | ||
}); | ||
|
||
it('should return the summed value of all the item\'s discounts', () => { | ||
cartItem.discounts = [ | ||
{ | ||
amount: 1, | ||
label: 'discount1', | ||
}, | ||
{ | ||
amount: 2, | ||
label: 'discount2', | ||
}, | ||
]; | ||
|
||
expect(daffCartGetItemTotalDiscount(cartItem)).toEqual(3); | ||
}); | ||
}); |
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,10 @@ | ||
import { daffAdd } from '@daffodil/core'; | ||
|
||
import { DaffCartItem } from '../models/public_api'; | ||
|
||
/** | ||
* Sums the discount amounts of the cart item. | ||
*/ | ||
export function daffCartGetItemTotalDiscount(item: DaffCartItem): number { | ||
return item?.discounts?.reduce((acc, { amount }) => daffAdd(acc, amount), 0) || 0; | ||
} |
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,33 @@ | ||
import { | ||
DaffCartItem, | ||
DaffCartItemInput, | ||
DaffCartItemInputType, | ||
} from '@daffodil/cart'; | ||
|
||
import { daffCartItemInputToItemTransform } from './input-to-item-transform'; | ||
|
||
describe('@daffodil/cart | daffCartItemInputToItemTransform', () => { | ||
let cartItemInput: DaffCartItemInput; | ||
let result: DaffCartItem; | ||
|
||
beforeEach(() => { | ||
cartItemInput = { | ||
type: DaffCartItemInputType.Simple, | ||
qty: 2, | ||
productId: 'productId', | ||
}; | ||
result = daffCartItemInputToItemTransform(cartItemInput); | ||
}); | ||
|
||
it('should set type', () => { | ||
expect(result.type).toEqual(cartItemInput.type); | ||
}); | ||
|
||
it('should set product ID', () => { | ||
expect(result.product_id).toEqual(cartItemInput.productId); | ||
}); | ||
|
||
it('should set qty', () => { | ||
expect(result.qty).toEqual(cartItemInput.qty); | ||
}); | ||
}); |
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
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './get-affected-item'; | ||
export * from './input-to-item-transform'; | ||
export * from './get-item-total-discount'; |