haiku-atelier-2024/tests/playwright/shop.spec.ts
2026-05-05 11:18:28 +02:00

86 lines
3.7 KiB
TypeScript

/** @effect-diagnostics asyncFunction:skip-file */
import type { APIRequestContext, Locator, Page, Response } from "@playwright/test";
import { expect, test } from "@playwright/test";
import { Effect, ManagedRuntime } from "effect";
import { ATTRIBUT_PAGE } from "../../web/app/themes/haiku-atelier-2024/src/scripts/constantes/dom.ts";
import type { WCV3Products } from "../../web/app/themes/haiku-atelier-2024/src/scripts/lib/types/api/v3/products";
import ShopPageRuntime from "../../web/app/themes/haiku-atelier-2024/src/scripts/page-boutique/runtime.ts";
import ShopPageElements from "../../web/app/themes/haiku-atelier-2024/src/scripts/page-boutique/service-elements.ts";
import type { BackendHeaders } from "./utils.ts";
import { getBackendHeadersFromHtml } from "./utils.ts";
import { APIClient } from "../../web/app/themes/haiku-atelier-2024/src/scripts-effect/lib/api.ts";
test.describe.configure({ mode: "parallel", timeout: 60_000 });
/** _Runtime_ pour effectuer les appels API. */
const TestRuntime = ManagedRuntime.make(APIClient.Live);
test("can do stuff", async ({ page }) => {
await page.goto("https://haikuatelier.gcch.local/shop/");
const doStuff = Effect.fn("doStuff")(function*() {
const Elements = yield* ShopPageElements;
expect(Elements.ProductsGrid.getAttribute(ATTRIBUT_PAGE)).toBe(1);
});
ShopPageRuntime.runSync(doStuff());
});
test.skip("can scroll to the end of the grid", async ({ page }): Promise<void> => {
await page.goto("https://haikuatelier.gcch.local/shop/");
let hasMoreProducts = true;
let currentPageNumber = "1";
const productsGrid: Locator = page.locator(".grille-produits");
await expect(productsGrid, "The Product's grid is visible").toBeVisible();
expect(await productsGrid.getAttribute("data-page"), "The initial page number attribute is correct").toBe(
currentPageNumber,
);
const showMoreButton: Locator = page.getByRole("button", { name: "Show more" });
await expect(showMoreButton, "The 'Show more' button is visible").toBeVisible();
while (hasMoreProducts) {
const newProductsResponse: Promise<Response> = page.waitForResponse(new RegExp(".*wp-json/wc/v3/products.*"));
await showMoreButton.click();
await newProductsResponse;
const newPageNumber = String(Number(currentPageNumber) + 1);
// Créé un nouveau Locator que l'on attend pour s'assurer que l'attribut soit bien mis à jour.
const gridWithNewPageNumber = page.locator(`.grille-produits[data-page="${newPageNumber}"]`);
await gridWithNewPageNumber.waitFor();
// Redondance pour expliciter la raison de l'assertion.
expect(await productsGrid.getAttribute("data-page"), "The page number attribute is incremented").toBe(
newPageNumber,
);
currentPageNumber = newPageNumber;
// La fin de la grille est atteint.
if (await showMoreButton.isHidden()) {
hasMoreProducts = false;
}
}
});
test.skip("can access all Products' pages", async ({ page, request }): Promise<void> => {
await page.goto("https://haikuatelier.gcch.local/shop/");
const links = await getAllProductsLinks(page, request);
for (const link of links) {
// Vérifie que le lien de la page retourne OK.
const req = await request.get(link);
await expect(req, "The Product's page is accessible").toBeOK();
}
});
const getAllProductsLinks = async (page: Page, request: APIRequestContext): Promise<Array<string>> => {
const backendHeaders: BackendHeaders = await getBackendHeadersFromHtml(page);
const response = await request.get("/wp-json/wc/v3/products?page=1&per_page=100&status=publish", {
headers: { Authorization: `Basic ${backendHeaders.authString}`, Nonce: backendHeaders.nonce },
});
const json = (await response.json()) as WCV3Products;
return json.map(p => p.permalink);
};