Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[1팀 이정민] [Chapter 2-2] 디자인 패턴과 함수형 프로그래밍 #30

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
dist
build

*.html

src/origin/
9 changes: 9 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"printWidth": 100,
"singleQuote": true,
"tabWidth": 2,
"bracketSpacing": true,
"semi": true,
"trailingComma": "all",
"arrowParens": "always"
}
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0"
},
"dependencies": {
"@types/node": "^22.10.7",
"react": "^18.3.1",
"react-dom": "^18.3.1"
},
Expand All @@ -31,6 +32,8 @@
"eslint": "^9.12.0",
"eslint-plugin-react-hooks": "^5.0.0",
"eslint-plugin-react-refresh": "^0.4.12",
"jsdom": "^26.0.0",
"prettier": "^3.4.2",
"typescript": "^5.6.3",
"vite": "^5.4.9",
"vitest": "^2.1.3"
Expand Down
433 changes: 388 additions & 45 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

114 changes: 76 additions & 38 deletions src/advanced/__tests__/advanced.test.tsx
Original file line number Diff line number Diff line change
@@ -1,65 +1,66 @@
import { useState } from "react";
import { useState } from 'react';
import { describe, expect, test } from 'vitest';
import { act, fireEvent, render, screen, within } from '@testing-library/react';
import { CartPage } from '../../refactoring/components/CartPage';
import { AdminPage } from "../../refactoring/components/AdminPage";
import { act, fireEvent, render, renderHook, screen, within } from '@testing-library/react';
import { CartPage } from '../../refactoring/pages/CartPage';
import { AdminPage } from '../../refactoring/pages/AdminPage';
import { Coupon, Product } from '../../types';
import { beforeEach } from 'node:test';
import { useLocalStorage } from '../../refactoring/hooks';

const mockProducts: Product[] = [
{
id: 'p1',
name: '상품1',
price: 10000,
stock: 20,
discounts: [{ quantity: 10, rate: 0.1 }]
discounts: [{ quantity: 10, rate: 0.1 }],
},
{
id: 'p2',
name: '상품2',
price: 20000,
stock: 20,
discounts: [{ quantity: 10, rate: 0.15 }]
discounts: [{ quantity: 10, rate: 0.15 }],
},
{
id: 'p3',
name: '상품3',
price: 30000,
stock: 20,
discounts: [{ quantity: 10, rate: 0.2 }]
}
discounts: [{ quantity: 10, rate: 0.2 }],
},
];
const mockCoupons: Coupon[] = [
{
name: '5000원 할인 쿠폰',
code: 'AMOUNT5000',
discountType: 'amount',
discountValue: 5000
discountValue: 5000,
},
{
name: '10% 할인 쿠폰',
code: 'PERCENT10',
discountType: 'percentage',
discountValue: 10
}
discountValue: 10,
},
];

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


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

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

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

return (
Expand All @@ -74,12 +75,9 @@ const TestAdminPage = () => {
};

describe('advanced > ', () => {

describe('시나리오 테스트 > ', () => {

test('장바구니 페이지 테스트 > ', async () => {

render(<CartPage products={mockProducts} coupons={mockCoupons}/>);
render(<CartPage products={mockProducts} coupons={mockCoupons} />);
const product1 = screen.getByTestId('product-p1');
const product2 = screen.getByTestId('product-p2');
const product3 = screen.getByTestId('product-p3');
Expand All @@ -98,7 +96,6 @@ describe('advanced > ', () => {
expect(product3).toHaveTextContent('30,000원');
expect(product3).toHaveTextContent('재고: 20개');


// 2. 할인 정보 표시
expect(screen.getByText('10개 이상: 10% 할인')).toBeInTheDocument();

Expand Down Expand Up @@ -157,8 +154,7 @@ describe('advanced > ', () => {
});

test('관리자 페이지 테스트 > ', async () => {
render(<TestAdminPage/>);

render(<TestAdminPage />);

const $product1 = screen.getByTestId('product-1');

Expand All @@ -182,12 +178,15 @@ describe('advanced > ', () => {
fireEvent.click(within($product1).getByTestId('toggle-button'));
fireEvent.click(within($product1).getByTestId('modify-button'));


act(() => {
fireEvent.change(within($product1).getByDisplayValue('20'), { target: { value: '25' } });
fireEvent.change(within($product1).getByDisplayValue('10000'), { target: { value: '12000' } });
fireEvent.change(within($product1).getByDisplayValue('상품1'), { target: { value: '수정된 상품1' } });
})
fireEvent.change(within($product1).getByDisplayValue('10000'), {
target: { value: '12000' },
});
fireEvent.change(within($product1).getByDisplayValue('상품1'), {
target: { value: '수정된 상품1' },
});
});

fireEvent.click(within($product1).getByText('수정 완료'));

Expand All @@ -203,7 +202,7 @@ describe('advanced > ', () => {
act(() => {
fireEvent.change(screen.getByPlaceholderText('수량'), { target: { value: '5' } });
fireEvent.change(screen.getByPlaceholderText('할인율 (%)'), { target: { value: '5' } });
})
});
fireEvent.click(screen.getByText('할인 추가'));

expect(screen.queryByText('5개 이상 구매 시 5% 할인')).toBeInTheDocument();
Expand All @@ -228,17 +227,56 @@ describe('advanced > ', () => {
const $newCoupon = screen.getByTestId('coupon-3');

expect($newCoupon).toHaveTextContent('새 쿠폰 (NEW10):10% 할인');
})
})
});
});

describe('자유롭게 작성해보세요.', () => {
test('새로운 유틸 함수를 만든 후에 테스트 코드를 작성해서 실행해보세요', () => {
expect(true).toBe(false);
})
const testProduct: Product = {
id: '1',
name: 'Test Product',
price: 100,
stock: 10,
discounts: [],
};

describe('새로운 유틸 함수를 만든 후에 테스트 코드를 작성해서 실행해보세요', () => {
test('', () => {
expect(true).toBe(true);
});
});
describe('useLocalStorage >', () => {
beforeEach(() => {
localStorage.clear();
});
test('로컬 스토리지에 값이 없는 경우 초기값을 반환한다.', () => {
const { result } = renderHook(() => useLocalStorage('testCart', testProduct));
expect(result.current[0]).toBe(testProduct);
});

test('로컬 스토리지에 기존 값이 있는 경우 기존 값을 반환한다.', () => {
localStorage.setItem('testCart', JSON.stringify(mockProducts));
const { result } = renderHook(() => useLocalStorage('testCart', testProduct));
expect(result.current.length).toBe(2);
});

test('setStorageValue로 새 값을 저장하면, localStorage와 state가 업데이트된다.', () => {
const { result } = renderHook(() => useLocalStorage('test', [1] as number[]));
act(() => {
result.current[1]([1, 2, 3]);
});
expect(result.current[0]).toEqual([1, 2, 3]);
expect(localStorage.getItem('test')).toEqual(JSON.stringify([1, 2, 3]));
});
});

test('새로운 hook 함수르 만든 후에 테스트 코드를 작성해서 실행해보세요', () => {
expect(true).toBe(false);
})
})
})
test('setStorageValue로 함수를 넘길 경우, 이전 값를 기반으로 업데이트한다.', () => {
const { result } = renderHook(() => useLocalStorage('testNumber', 5));

act(() => {
result.current[1]((prev) => prev + 5);
});
expect(result.current[0]).toBe(10);
expect(localStorage.getItem('testNumber')).toBe('10');
});
});
});
Loading
Loading