Skip to content

Commit

Permalink
✏️ 변수명 수정
Browse files Browse the repository at this point in the history
✏️ 변수명 가독성을 위한 수정
- getMaxApplicableDiscount => calculateItemMax
- 그에 맞게 basic.test 도 수정
  • Loading branch information
pitangland committed Jan 16, 2025
1 parent a7daebd commit 9cc919c
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
9 changes: 5 additions & 4 deletions src/basic/__tests__/basic.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { AdminPage } from '../../refactoring/pages/AdminPage';
import { CartItem, Coupon, Product } from '../../types';
import { useCart, useCoupons, useProducts } from '../../refactoring/hooks';
import * as cartUtils from '../../refactoring/models/cart';
import * as discountUtils from '../../refactoring/models/discount';

const mockProducts: Product[] = [
{
Expand Down Expand Up @@ -335,24 +336,24 @@ describe('basic > ', () => {
describe('calculateItemTotal', () => {
test('할인 없이 총액을 계산해야 합니다.', () => {
const item: CartItem = { product: testProduct, quantity: 1 };
expect(cartUtils.calculateItemTotal(item)).toBe(100);
expect(discountUtils.calculateItemTotal(item)).toBe(100);
});

test('수량에 따라 올바른 할인을 적용해야 합니다.', () => {
const item: CartItem = { product: testProduct, quantity: 5 };
expect(cartUtils.calculateItemTotal(item)).toBe(400); // 500 * 0.8
expect(discountUtils.calculateItemTotal(item)).toBe(400); // 500 * 0.8
});
});

describe('getMaxApplicableDiscount', () => {
test('할인이 적용되지 않으면 0을 반환해야 합니다.', () => {
const item: CartItem = { product: testProduct, quantity: 1 };
expect(cartUtils.getMaxApplicableDiscount(item)).toBe(0);
expect(discountUtils.calculateItemMaxDiscount(item)).toBe(0);
});

test('적용 가능한 가장 높은 할인율을 반환해야 합니다.', () => {
const item: CartItem = { product: testProduct, quantity: 5 };
expect(cartUtils.getMaxApplicableDiscount(item)).toBe(0.2);
expect(discountUtils.calculateItemMaxDiscount(item)).toBe(0.2);
});
});

Expand Down
4 changes: 2 additions & 2 deletions src/refactoring/models/discount.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { CartItem } from 'src/types';

// 개별 항목에서 적용 가능한 최대 할인율 계산
export const getMaxApplicableDiscount = (item: CartItem) => {
export const calculateItemMaxDiscount = (item: CartItem) => {
const discount = item.product.discounts.reduce((maxDiscount, d) => {
return item.quantity >= d.quantity && d.rate > maxDiscount ? d.rate : maxDiscount;
}, 0);
Expand All @@ -11,7 +11,7 @@ export const getMaxApplicableDiscount = (item: CartItem) => {
// 개별 항목의 할인율 적용한 총 금액 계산
export const calculateItemTotal = (item: CartItem) => {
const { product, quantity } = item;
const discount = getMaxApplicableDiscount(item);
const discount = calculateItemMaxDiscount(item);
return product.price * quantity * (1 - discount);
};

Expand Down

0 comments on commit 9cc919c

Please sign in to comment.