Skip to content

Commit

Permalink
✅ test: validateUtil 테스트 코드 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
dosilv committed Jan 16, 2025
1 parent 0ff6451 commit f5fbc90
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 3 deletions.
File renamed without changes.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
},
"dependencies": {
"react": "^18.3.1",
"react-dom": "^18.3.1"
"react-dom": "^18.3.1",
"zustand": "^5.0.3"
},
"devDependencies": {
"@testing-library/jest-dom": "^6.6.2",
Expand Down
26 changes: 26 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 45 additions & 2 deletions src/advanced/__tests__/advanced.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ import { CartPage } from "../../refactoring/components";
import { AdminPage } from "../../refactoring/components";
import { Coupon, Product } from "../../types";
import { useCart, useCoupons, useProducts } from "../../refactoring/hooks";
import { beforeEach } from "node:test";
import { localStorageUtil } from "../../refactoring/utils";
import { localStorageUtil, validateData } from "../../refactoring/utils";

const mockProducts: Product[] = [
{
Expand Down Expand Up @@ -404,3 +403,47 @@ describe("advanced > ", () => {
});
});
});

describe("validateUtil 테스트", () => {
test("유효한 데이터 검사 시 true를 반환한다.", () => {
const validProduct = {
id: "p1",
name: "유효한 상품 🎁",
price: 10000,
stock: 20,
discounts: [{ quantity: 10, rate: 0.1 }],
};

expect(validateData(validProduct)).toBe(true);
});

test("유효하지 않은 데이터 검사 시 false를 반환하고, alert이 뜬다.", () => {
const invalidProduct = {
id: "",
name: "이상한 상품 🤔",
price: 10000,
stock: -20,
discounts: [],
};

const alertMock = vi.spyOn(window, "alert").mockImplementation(() => {});

expect(validateData(invalidProduct)).toBe(false);
expect(alertMock).toHaveBeenCalledWith(
"유효한 값을 입력해주세요. (id, stock, discounts)",
);
});

test("검사를 생략할 key를 추가하면 해당 value에 대한 검사를 생략한다.", () => {
const product = {
id: "p1",
name: "할인 없는 상품 😓",
price: 10000,
stock: 20,
discounts: [],
};

expect(validateData(product)).toBe(false);
expect(validateData(product, ["discounts"])).toBe(true);
});
});

0 comments on commit f5fbc90

Please sign in to comment.