-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
108 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import React from "react"; | ||
import { screen, render, waitFor } from "@testing-library/react"; | ||
import CartProvider from "../provider/CartProvider"; | ||
import Cart from "./Cart"; | ||
|
||
describe("testing cart in integration", () => { | ||
beforeEach(() => { | ||
jest.spyOn(global, "fetch").mockImplementation(() => | ||
Promise.resolve({ | ||
json: jest.fn().mockResolvedValue(mockResponse), | ||
}) | ||
); | ||
}); | ||
afterEach(() => jest.clearAllMocks()); | ||
|
||
const setup = () => { | ||
render( | ||
<CartProvider> | ||
<Cart /> | ||
</CartProvider> | ||
); | ||
}; | ||
|
||
const mockResponse = { | ||
products: [], | ||
}; | ||
test("renders cart", async () => { | ||
jest.spyOn(global, "fetch").mockImplementation(() => | ||
Promise.resolve({ | ||
json: jest.fn().mockResolvedValue(mockResponse), | ||
}) | ||
); | ||
|
||
await waitFor(() => { | ||
setup(); | ||
}); | ||
expect(screen.getByText("Cart")).toBeInTheDocument(); | ||
expect(screen.getByText("Cart is empty")).toBeInTheDocument(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import React from "react"; | ||
import { screen, render } from "@testing-library/react"; | ||
import CartItem from "./CartItem"; | ||
import userEvent from "@testing-library/user-event"; | ||
|
||
describe("cart item", () => { | ||
const setup = () => { | ||
const increaseQuantity = jest.fn(); | ||
const decreaseQuantity = jest.fn(); | ||
const data = { | ||
id: "12345", | ||
title: "iPhone 14 pro", | ||
price: "140000", | ||
quantity: 1, | ||
thumbnail: "", | ||
}; | ||
render( | ||
<CartItem | ||
data={data} | ||
decreaseQuantity={decreaseQuantity} | ||
increaseQuantity={increaseQuantity} | ||
/> | ||
); | ||
const incBtn = screen.getByTestId("increase-quantity-button"); | ||
const decBtn = screen.getByTestId("decrease-quantity-button"); | ||
return { | ||
incBtn, | ||
decBtn, | ||
increaseQuantity, | ||
decreaseQuantity, | ||
}; | ||
}; | ||
test("renders single cart item", () => { | ||
setup(); | ||
expect(screen.getByTestId("product-count")).toHaveTextContent("1"); | ||
expect(screen.getByTestId("product-quantity")).toHaveTextContent("Qty: 1"); | ||
}); | ||
|
||
test("increase and decrease buttons function properly", async () => { | ||
const { incBtn, decBtn, decreaseQuantity, increaseQuantity } = setup(); | ||
|
||
// increase quantity | ||
await userEvent.click(incBtn); | ||
expect(increaseQuantity).toHaveBeenCalled(); | ||
expect(increaseQuantity).toHaveBeenCalledWith("12345"); | ||
|
||
// decrease quantity | ||
await userEvent.click(decBtn); | ||
expect(decreaseQuantity).toHaveBeenCalled(); | ||
expect(decreaseQuantity).toHaveBeenCalledWith("12345", 1); | ||
}); | ||
}); |