-
Notifications
You must be signed in to change notification settings - Fork 633
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
12 changed files
with
121 additions
and
76 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
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,31 @@ | ||
import { expect, test } from "@playwright/test"; | ||
import { | ||
addCurrentProductToCart, | ||
clickOnRandomProductElement, | ||
getCurrentProductPrice, | ||
openCart, | ||
selectRandomAvailableVariant, | ||
} from "./utils"; | ||
|
||
test("STF_01", async ({ page }) => { | ||
await page.goto("/"); | ||
|
||
const product = await clickOnRandomProductElement({ page }); | ||
await selectRandomAvailableVariant({ page }); | ||
|
||
const price = await getCurrentProductPrice({ page }); | ||
|
||
await expect(page.getByTestId("CartNavItem")).toContainText("0 items"); | ||
await addCurrentProductToCart({ page }); | ||
await expect(page.getByTestId("CartNavItem")).toContainText("1 item"); | ||
await addCurrentProductToCart({ page }); | ||
await expect(page.getByTestId("CartNavItem")).toContainText("2 items"); | ||
|
||
await openCart({ page }); | ||
|
||
const totalPrice = (price * 2).toFixed(2); | ||
await expect(page.getByTestId("CartProductList").getByRole("listitem")).toHaveCount(1); | ||
await expect(page.getByTestId("CartProductList").getByRole("listitem")).toContainText(product.name); | ||
await expect(page.getByTestId("CartProductList").getByRole("listitem")).toContainText(`Qty: 2`); | ||
await expect(page.getByTestId("CartProductList").getByRole("listitem")).toContainText(totalPrice); | ||
}); |
This file was deleted.
Oops, something went wrong.
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,48 @@ | ||
import { expect, type Page } from "@playwright/test"; | ||
|
||
export async function clickOnRandomProductElement({ page }: { page: Page }) { | ||
const productLinks = page.getByTestId("ProductElement"); | ||
await productLinks.first().waitFor(); | ||
const count = await productLinks.count(); | ||
const randomProductLink = productLinks.nth(Math.floor(Math.random() * count)); | ||
|
||
const name = await randomProductLink.getByRole("heading").textContent(); | ||
const priceRange = await randomProductLink.getByTestId("ProductElement_PriceRange").textContent(); | ||
const category = await randomProductLink.getByTestId("ProductElement_Category").textContent(); | ||
|
||
await randomProductLink.click(); | ||
|
||
await page.waitForURL("**/products/*"); | ||
|
||
expect(name, "Missing product name").toBeTruthy(); | ||
expect(priceRange, "Missing product priceRange").toBeTruthy(); | ||
expect(category, "Missing product category").toBeTruthy(); | ||
return { name: name!, priceRange: priceRange!, category: category! }; | ||
} | ||
|
||
export async function getCurrentProductPrice({ page }: { page: Page }) { | ||
const price = await page.getByTestId("ProductElement_Price").textContent(); | ||
expect(price, "Missing product price").toBeTruthy(); | ||
return Number.parseFloat(price!.replace(/[^0-9\.]/g, "")); | ||
} | ||
|
||
export async function selectRandomAvailableVariant({ page }: { page: Page }) { | ||
const variant = page.getByTestId("VariantSelector").getByRole("radio", { disabled: false }); | ||
const count = await variant.count(); | ||
// some products only have a single variant | ||
if (count > 0) { | ||
await variant.nth(Math.floor(Math.random() * count)).click(); | ||
} | ||
await page.waitForURL(/\?variant=.+/); | ||
} | ||
|
||
export async function addCurrentProductToCart({ page }: { page: Page }) { | ||
expect(page.url()).toContain("/products/"); | ||
expect(page.url()).toContain("?variant="); | ||
await page.getByRole("button", { name: "Add to cart" }).click(); | ||
} | ||
|
||
export async function openCart({ page }: { page: Page }) { | ||
await page.getByTestId("CartNavItem").click(); | ||
await page.getByText("Your Shopping Cart").waitFor(); | ||
} |
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
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
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
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
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
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
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
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 |
---|---|---|
@@ -1,38 +1,29 @@ | ||
import { useMemo, useEffect, useState, useCallback } from "react"; | ||
import { useEffect, useState, useCallback } from "react"; | ||
import { usePathname, useSearchParams } from "next/navigation"; | ||
|
||
type Args = { | ||
breakpoint: number; | ||
}; | ||
|
||
export const useMobileMenu = ({ breakpoint }: Args) => { | ||
export const useMobileMenu = () => { | ||
const [isOpen, setIsOpen] = useState(false); | ||
const pathname = usePathname(); | ||
const searchParams = useSearchParams(); | ||
useSearchParams(); | ||
|
||
useEffect(() => { | ||
const handleResize = () => { | ||
if (window.innerWidth > breakpoint) { | ||
setIsOpen(false); | ||
}, [pathname]); | ||
|
||
useEffect(() => { | ||
const handleResize = (ev: MediaQueryListEvent) => { | ||
if (ev.matches) { | ||
setIsOpen(false); | ||
} | ||
}; | ||
window.addEventListener("resize", handleResize); | ||
return () => window.removeEventListener("resize", handleResize); | ||
}, [breakpoint, isOpen]); | ||
|
||
useEffect(() => { | ||
setIsOpen(false); | ||
}, [pathname, searchParams]); | ||
const matchMedia = window.matchMedia(`(min-width: 768px)`); | ||
matchMedia.addEventListener("change", handleResize, { passive: true }); | ||
return () => matchMedia.removeEventListener("change", handleResize); | ||
}, []); | ||
|
||
const closeMenu = useCallback(() => setIsOpen(false), []); | ||
const openMenu = useCallback(() => setIsOpen(true), []); | ||
|
||
return useMemo( | ||
() => ({ | ||
isOpen, | ||
closeMenu, | ||
openMenu, | ||
}), | ||
[closeMenu, isOpen, openMenu], | ||
); | ||
return { isOpen, closeMenu, openMenu }; | ||
}; |