Skip to content

Commit c4e4e93

Browse files
author
Kellyarias02
committed
Refactor: to add test about as variants of polos and stickers
1 parent 59d6a55 commit c4e4e93

File tree

4 files changed

+75
-5
lines changed

4 files changed

+75
-5
lines changed

src/lib/utils.tests.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ export const createTestProduct = (overrides?: Partial<Product>): Product => ({
6363
categoryId: 1,
6464
isOnSale: false,
6565
features: ["Feature 1", "Feature 2"],
66+
variants: [],
67+
stickersVariants: [],
6668
createdAt: new Date(),
6769
updatedAt: new Date(),
6870
...overrides,

src/routes/product/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ export default function Product({ loaderData }: Route.ComponentProps) {
121121
onSelect={setSelectedMeasure}
122122
/>
123123
)}
124-
{/* Botón de agregar al carrito */}
124+
125125
<Button
126126
size="xl"
127127
className="w-full md:w-80"

src/routes/product/product.test.tsx

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ vi.mock("react-router", () => ({
2727
Form: vi.fn(({ children }) => <form>{children}</form>),
2828
useNavigation: vi.fn(() => createTestNavigation()),
2929
Link: vi.fn(({ children, ...props }) => <a {...props}>{children}</a>),
30+
useSearchParams: vi.fn(() => [new URLSearchParams(), vi.fn()]),
3031
}));
3132

3233
const createTestProps = (
@@ -61,6 +62,23 @@ describe("Product Component", () => {
6162
expect(screen.queryByText("S/150.99")).toBeInTheDocument();
6263
});
6364

65+
it("should render product price with correct currency for stickers variants", () => {
66+
// Step 1: Setup - Create test props with stickers variants
67+
const props = createTestProps({
68+
price: 100,
69+
stickersVariants: [
70+
{ id: 1, measure: "3*3", price: 80 },
71+
{ id: 2, measure: "5*5", price: 120 },
72+
{ id: 3, measure: "10*10", price: 150 },
73+
],
74+
});
75+
// Step 2: Mock - Component mocks already set up above
76+
// Step 3: Call - Render component
77+
render(<Product {...props} />);
78+
// Step 4: Verify - Check price is rendered correctly (should use first variant price)
79+
expect(screen.queryByText("S/80")).toBeInTheDocument();
80+
});
81+
6482
it("should render product description", () => {
6583
// Step 1: Setup - Create test props
6684
const props = createTestProps({
@@ -101,6 +119,44 @@ describe("Product Component", () => {
101119
});
102120
});
103121

122+
it("should render size selector when product has variants", () => {
123+
// Step 1: Setup - Create test props with variants
124+
const props = createTestProps({
125+
variants: [
126+
{ id: 1, size: "small" },
127+
{ id: 2, size: "medium" },
128+
{ id: 3, size: "large" },
129+
],
130+
});
131+
// Step 2: Mock - Component mocks already set up above
132+
// Step 3: Call - Render component
133+
render(<Product {...props} />);
134+
// Step 4: Verify - Check size selector is rendered
135+
expect(screen.queryByText("Talla")).toBeInTheDocument();
136+
expect(screen.queryByText("Small")).toBeInTheDocument();
137+
expect(screen.queryByText("Medium")).toBeInTheDocument();
138+
expect(screen.queryByText("Large")).toBeInTheDocument();
139+
});
140+
141+
it("should render measure selector when product has stickers variants", () => {
142+
// Step 1: Setup - Create test props with stickers variants
143+
const props = createTestProps({
144+
stickersVariants: [
145+
{ id: 1, measure: "3*3", price: 80 },
146+
{ id: 2, measure: "5*5", price: 120 },
147+
{ id: 3, measure: "10*10", price: 150 },
148+
],
149+
});
150+
// Step 2: Mock - Component mocks already set up above
151+
// Step 3: Call - Render component
152+
render(<Product {...props} />);
153+
// Step 4: Verify - Check measure selector is rendered
154+
expect(screen.queryByText("Medida")).toBeInTheDocument();
155+
expect(screen.queryByText("3*3")).toBeInTheDocument();
156+
expect(screen.queryByText("5*5")).toBeInTheDocument();
157+
expect(screen.queryByText("10*10")).toBeInTheDocument();
158+
});
159+
104160
it('should render "Agregar al Carrito" button', () => {
105161
// Step 1: Setup - Create test props
106162
const props = createTestProps();
@@ -148,7 +204,7 @@ describe("Product Component", () => {
148204
it("should render NotFound component when product is not provided", () => {
149205
// Step 1: Setup - Create props without product
150206
const props = createTestProps();
151-
props.loaderData.product = undefined;
207+
props.loaderData = { product: undefined };
152208

153209
// Step 2: Mock - Mock NotFound component
154210
// vi.mock("../not-found", () => ({

src/services/product.service.test.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ describe("Product Service", () => {
5151
expect(getCategoryBySlug).toHaveBeenCalledWith(testCategory.slug);
5252
expect(mockPrisma.product.findMany).toHaveBeenCalledWith({
5353
where: { categoryId: testCategory.id },
54+
include: {
55+
stickersVariants: true,
56+
variants: true
57+
},
5458
});
5559
expect(products).toEqual(
5660
mockedProducts.map((product) => ({
@@ -93,27 +97,35 @@ describe("Product Service", () => {
9397
// Step 4: Verify expected behavior
9498
expect(mockPrisma.product.findUnique).toHaveBeenCalledWith({
9599
where: { id: testProduct.id },
100+
include: {
101+
stickersVariants: true,
102+
variants: true
103+
},
96104
});
97105
expect(result).toEqual({
98106
...testProduct,
99107
price: testProduct.price.toNumber(),
100108
});
101109
});
102110

103-
it("should throw error when product does not exist", async () => {
111+
it("should return null when product does not exist", async () => {
104112
// Step 1: Setup - Configure ID for non-existent product
105113
const nonExistentId = 999;
106114

107115
// Step 2: Mock - Configure null response from Prisma
108116
vi.mocked(mockPrisma.product.findUnique).mockResolvedValue(null);
109117

110118
// Step 3: Call service function
111-
const productPromise = getProductById(nonExistentId);
119+
const result = await getProductById(nonExistentId);
112120

113121
// Step 4: Verify expected behavior
114-
await expect(productPromise).rejects.toThrow("Product not found");
122+
expect(result).toBeNull();
115123
expect(mockPrisma.product.findUnique).toHaveBeenCalledWith({
116124
where: { id: nonExistentId },
125+
include: {
126+
stickersVariants: true,
127+
variants: true
128+
},
117129
});
118130
});
119131
});

0 commit comments

Comments
 (0)