Skip to content

Commit

Permalink
feat(cart): add daffCartGetItemTotalDiscount (#2797)
Browse files Browse the repository at this point in the history
  • Loading branch information
griest024 committed May 14, 2024
1 parent bbc3904 commit 0c1edcc
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 0 deletions.
41 changes: 41 additions & 0 deletions libs/cart/src/helpers/get-affected-item.spec.ts
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);
});
});
31 changes: 31 additions & 0 deletions libs/cart/src/helpers/get-item-total-discount.spec.ts
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);
});
});
10 changes: 10 additions & 0 deletions libs/cart/src/helpers/get-item-total-discount.ts
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;
}
33 changes: 33 additions & 0 deletions libs/cart/src/helpers/input-to-item-transform.spec.ts
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);
});
});
3 changes: 3 additions & 0 deletions libs/cart/src/helpers/input-to-item-transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions libs/cart/src/helpers/public_api.ts
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';

0 comments on commit 0c1edcc

Please sign in to comment.