From 224320994fce57299dc6f2438571d6364ed8b0a2 Mon Sep 17 00:00:00 2001 From: creco-hanghae Date: Fri, 17 Jan 2025 09:58:51 +0900 Subject: [PATCH] =?UTF-8?q?test:=20=EC=9C=A0=ED=8B=B8=20=ED=95=A8=EC=88=98?= =?UTF-8?q?=20=EC=98=88=EC=8B=9C=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=BD=94?= =?UTF-8?q?=EB=93=9C=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/advanced/__tests__/advanced.test.tsx | 32 ++++++++++++++++++- .../components/AdminPage/AdminPage.tsx | 7 ++-- src/refactoring/models/product.ts | 11 +++++++ 3 files changed, 45 insertions(+), 5 deletions(-) create mode 100644 src/refactoring/models/product.ts diff --git a/src/advanced/__tests__/advanced.test.tsx b/src/advanced/__tests__/advanced.test.tsx index d365f1a0..f079e20c 100644 --- a/src/advanced/__tests__/advanced.test.tsx +++ b/src/advanced/__tests__/advanced.test.tsx @@ -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[] = [ { @@ -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 함수르 만든 후에 테스트 코드를 작성해서 실행해보세요', () => { diff --git a/src/refactoring/components/AdminPage/AdminPage.tsx b/src/refactoring/components/AdminPage/AdminPage.tsx index 8b74caae..6ecfff7d 100644 --- a/src/refactoring/components/AdminPage/AdminPage.tsx +++ b/src/refactoring/components/AdminPage/AdminPage.tsx @@ -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[]; @@ -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 }); diff --git a/src/refactoring/models/product.ts b/src/refactoring/models/product.ts new file mode 100644 index 00000000..c90eb5d6 --- /dev/null +++ b/src/refactoring/models/product.ts @@ -0,0 +1,11 @@ +import { Product, Discount } from "../../types"; + +export function addDiscountToProduct( + product: Product, + discount: Discount +): Product { + return { + ...product, + discounts: [...product.discounts, discount], + }; +} \ No newline at end of file