Skip to content

Commit

Permalink
test: 유틸 함수 예시 테스트 코드 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
creco-hanghae committed Jan 17, 2025
1 parent ea7d72d commit 2243209
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 5 deletions.
32 changes: 31 additions & 1 deletion src/advanced/__tests__/advanced.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { act, fireEvent, render, screen, within } from '@testing-library/react';
import { CartPage } from '../../refactoring/components/CartPage';
import { AdminPage } from "../../refactoring/components/AdminPage/AdminPage";
import { Coupon, Product } from '../../types';
import { addDiscountToProduct } from "../../refactoring/models/product";

const mockProducts: Product[] = [
{
Expand Down Expand Up @@ -233,7 +234,36 @@ describe('advanced > ', () => {

describe('자유롭게 작성해보세요.', () => {
test('새로운 유틸 함수를 만든 후에 테스트 코드를 작성해서 실행해보세요', () => {
expect(true).toBe(false);
expect(addDiscountToProduct({
id: '1',
name: '내 상품',
price: 1000,
stock: 10,
discounts: [{
quantity: 1,
rate: 0.05
}],
}, {
quantity: 5,
rate: 0.1,
})).toMatchObject(
{
"discounts": [
{
"quantity": 1,
"rate": 0.05,
},
{
"quantity": 5,
"rate": 0.1,
},
],
"id": "1",
"name": "내 상품",
"price": 1000,
"stock": 10,
}
)
})

test('새로운 hook 함수르 만든 후에 테스트 코드를 작성해서 실행해보세요', () => {
Expand Down
7 changes: 3 additions & 4 deletions src/refactoring/components/AdminPage/AdminPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Container, Title } from '../Styled.tsx';
import { Section } from '../Section.tsx';
import { CouponManagement } from './CouponManagement.tsx';
import { ProductManagement } from './ProductManagement.tsx';
import { addDiscountToProduct } from '../../models/product.ts';

interface Props {
products: Product[];
Expand Down Expand Up @@ -84,10 +85,8 @@ export const AdminPage = ({ products, coupons, onProductUpdate, onProductAdd, on
const handleAddDiscount = (productId: string) => {
const updatedProduct = products.find(p => p.id === productId);
if (updatedProduct && editingProduct) {
const newProduct = {
...updatedProduct,
discounts: [...updatedProduct.discounts, newDiscount]
};
const newProduct = addDiscountToProduct(updatedProduct, newDiscount);

onProductUpdate(newProduct);
setEditingProduct(newProduct);
setNewDiscount({ quantity: 0, rate: 0 });
Expand Down
11 changes: 11 additions & 0 deletions src/refactoring/models/product.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Product, Discount } from "../../types";

export function addDiscountToProduct(
product: Product,
discount: Discount
): Product {
return {
...product,
discounts: [...product.discounts, discount],
};
}

0 comments on commit 2243209

Please sign in to comment.