Skip to content

Commit

Permalink
🎨 컴포넌트 Cart 와 Admin 폴더로 구조 변경
Browse files Browse the repository at this point in the history
- Cart 관련 컴포넌트 components/Cart 로 이동
- Cart와 Admin 으로 폴더 구조 변경 관리
  • Loading branch information
pitangland committed Jan 15, 2025
1 parent 81a3549 commit a7daebd
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 31 deletions.
46 changes: 23 additions & 23 deletions src/basic/__tests__/basic.test.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { useState } from 'react';
import { describe, expect, test } from 'vitest';
import { act, fireEvent, render, renderHook, screen, within } from '@testing-library/react';
import { CartPage } from '../../refactoring/components/CartPage';
import { AdminPage } from '../../refactoring/components/AdminPage';
import { CartPage } from '../../refactoring/pages/CartPage';
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';
Expand Down Expand Up @@ -46,27 +46,27 @@ const mockCoupons: Coupon[] = [
];

const TestAdminPage = () => {
const [products, setProducts] = useState<Product[]>(mockProducts);
const [coupons, setCoupons] = useState<Coupon[]>(mockCoupons);
const [productList, setProductList] = useState<Product[]>(mockProducts);
const [couponList, setCouponList] = useState<Coupon[]>(mockCoupons);

const handleProductUpdate = (updatedProduct: Product) => {
setProducts((prevProducts) =>
prevProducts.map((p) => (p.id === updatedProduct.id ? updatedProduct : p)),
setProductList((prevProductList) =>
prevProductList.map((p) => (p.id === updatedProduct.id ? updatedProduct : p)),
);
};

const handleProductAdd = (newProduct: Product) => {
setProducts((prevProducts) => [...prevProducts, newProduct]);
setProductList((prevProductList) => [...prevProductList, newProduct]);
};

const handleCouponAdd = (newCoupon: Coupon) => {
setCoupons((prevCoupons) => [...prevCoupons, newCoupon]);
setCouponList((prevCouponList) => [...prevCouponList, newCoupon]);
};

return (
<AdminPage
products={products}
coupons={coupons}
productList={productList}
couponList={couponList}
onProductUpdate={handleProductUpdate}
onProductAdd={handleProductAdd}
onCouponAdd={handleCouponAdd}
Expand All @@ -77,7 +77,7 @@ const TestAdminPage = () => {
describe('basic > ', () => {
describe('시나리오 테스트 > ', () => {
test('장바구니 페이지 테스트 > ', async () => {
render(<CartPage products={mockProducts} coupons={mockCoupons} />);
render(<CartPage productList={mockProducts} couponList={mockCoupons} />);
const product1 = screen.getByTestId('product-p1');
const product2 = screen.getByTestId('product-p2');
const product3 = screen.getByTestId('product-p3');
Expand Down Expand Up @@ -251,24 +251,24 @@ describe('basic > ', () => {
});

describe('useProducts > ', () => {
const initialProducts: Product[] = [
const initialProductList: Product[] = [
{ id: '1', name: 'Product 1', price: 100, stock: 10, discounts: [] },
];

test('특정 제품으로 초기화할 수 있다.', () => {
const { result } = renderHook(() => useProducts(initialProducts));
expect(result.current.products).toEqual(initialProducts);
const { result } = renderHook(() => useProducts(initialProductList));
expect(result.current.productList).toEqual(initialProductList);
});

test('제품을 업데이트할 수 있다.', () => {
const { result } = renderHook(() => useProducts(initialProducts));
const updatedProduct = { ...initialProducts[0], name: 'Updated Product' };
const { result } = renderHook(() => useProducts(initialProductList));
const updatedProduct = { ...initialProductList[0], name: 'Updated Product' };

act(() => {
result.current.updateProduct(updatedProduct);
});

expect(result.current.products[0]).toEqual({
expect(result.current.productList[0]).toEqual({
discounts: [],
id: '1',
name: 'Updated Product',
Expand All @@ -278,7 +278,7 @@ describe('basic > ', () => {
});

test('새로운 제품을 추가할 수 있다.', () => {
const { result } = renderHook(() => useProducts(initialProducts));
const { result } = renderHook(() => useProducts(initialProductList));
const newProduct: Product = {
id: '2',
name: 'New Product',
Expand All @@ -291,15 +291,15 @@ describe('basic > ', () => {
result.current.addProduct(newProduct);
});

expect(result.current.products).toHaveLength(2);
expect(result.current.products[1]).toEqual(newProduct);
expect(result.current.productList).toHaveLength(2);
expect(result.current.productList[1]).toEqual(newProduct);
});
});

describe('useCoupons > ', () => {
test('쿠폰을 초기화할 수 있다.', () => {
const { result } = renderHook(() => useCoupons(mockCoupons));
expect(result.current.coupons).toEqual(mockCoupons);
expect(result.current.couponList).toEqual(mockCoupons);
});

test('쿠폰을 추가할 수 있다', () => {
Expand All @@ -315,8 +315,8 @@ describe('basic > ', () => {
result.current.addCoupon(newCoupon);
});

expect(result.current.coupons).toHaveLength(3);
expect(result.current.coupons[2]).toEqual(newCoupon);
expect(result.current.couponList).toHaveLength(3);
expect(result.current.couponList[2]).toEqual(newCoupon);
});
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CartItem } from 'src/types';
import { getAppliedDiscount } from '../models/cart';
import { getAppliedDiscount } from '../../models/cart';

interface CartListProps {
cart: CartItem[];
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Product, CartItem } from '../../types';
import { calculateRemainingStock } from '../models/cart';
import { calculateMaxDiscount } from '../models/discount';
import { Product, CartItem } from '../../../types';
import { calculateRemainingStock } from '../../models/cart';
import { calculateMaxDiscount } from '../../models/discount';

interface ProductListProps {
productList: Product[];
Expand Down
8 changes: 4 additions & 4 deletions src/refactoring/pages/CartPage.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Coupon, Product } from '../../types';
import { useCart } from '../hooks';
import { ProductList } from '../components/ProductList';
import { CartList } from '../components/CartList';
import { CouponSelector } from '../components/CouponSeletor';
import { OrderSummary } from '../components/OrderSummary';
import { ProductList } from '../components/Cart/ProductList';
import { CartList } from '../components/Cart/CartList';
import { CouponSelector } from '../components/Cart/CouponSeletor';
import { OrderSummary } from '../components/Cart/OrderSummary';

interface Props {
productList: Product[];
Expand Down

0 comments on commit a7daebd

Please sign in to comment.