diff --git a/libs/cart/src/helpers/get-affected-item.spec.ts b/libs/cart/src/helpers/get-affected-item.spec.ts new file mode 100644 index 0000000000..e3ec985307 --- /dev/null +++ b/libs/cart/src/helpers/get-affected-item.spec.ts @@ -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; + let oldItems: Array; + let result: Array; + + 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); + }); +}); diff --git a/libs/cart/src/helpers/get-item-total-discount.spec.ts b/libs/cart/src/helpers/get-item-total-discount.spec.ts new file mode 100644 index 0000000000..e7bc783a93 --- /dev/null +++ b/libs/cart/src/helpers/get-item-total-discount.spec.ts @@ -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); + }); +}); diff --git a/libs/cart/src/helpers/get-item-total-discount.ts b/libs/cart/src/helpers/get-item-total-discount.ts new file mode 100644 index 0000000000..b0c2a990ef --- /dev/null +++ b/libs/cart/src/helpers/get-item-total-discount.ts @@ -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; +} diff --git a/libs/cart/src/helpers/input-to-item-transform.spec.ts b/libs/cart/src/helpers/input-to-item-transform.spec.ts new file mode 100644 index 0000000000..8594274019 --- /dev/null +++ b/libs/cart/src/helpers/input-to-item-transform.spec.ts @@ -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); + }); +}); diff --git a/libs/cart/src/helpers/input-to-item-transform.ts b/libs/cart/src/helpers/input-to-item-transform.ts index 4bf898b54a..c58f89b1ad 100644 --- a/libs/cart/src/helpers/input-to-item-transform.ts +++ b/libs/cart/src/helpers/input-to-item-transform.ts @@ -3,6 +3,9 @@ import { DaffCartItemInput, } from '../models/public_api'; +/** + * Creates a stub cart item from the cart item input. + */ export const daffCartItemInputToItemTransform = (input: DaffCartItemInput): DaffCartItem => ({ type: input.type, product_id: input.productId, diff --git a/libs/cart/src/helpers/public_api.ts b/libs/cart/src/helpers/public_api.ts index 57e8d6e682..b94d5164b6 100644 --- a/libs/cart/src/helpers/public_api.ts +++ b/libs/cart/src/helpers/public_api.ts @@ -1,2 +1,3 @@ export * from './get-affected-item'; export * from './input-to-item-transform'; +export * from './get-item-total-discount';