Files
sustenance/e2e/tests/websocket.spec.ts
T
2026-08-08 11:36:28 -04:00

45 lines
1.8 KiB
TypeScript

import { expect } from "@playwright/test";
import { test } from "../fixtures";
import { registerAndLogin, createList, addItem } from "../helpers";
test("a list updates live for another user via websocket", async ({ page, browser, server }) => {
// User A registers and creates a list.
await registerAndLogin(page, "alice@example.com");
await createList(page, "Weekly shop");
const listUrl = page.url();
// User B registers in a separate context (separate session).
const contextB = await browser.newContext({ baseURL: server.baseURL });
const pageB = await contextB.newPage();
await registerAndLogin(pageB, "bob@example.com");
// Both users open the same list.
await page.goto(listUrl);
await pageB.goto(listUrl);
// Give the websocket connections a moment to establish.
await page.waitForTimeout(500);
// Both start with an empty list.
await expect(pageB.locator("#list-meta")).toHaveText("0 items left out of 0");
// User A adds an item.
await addItem(page, "Apple", "2");
// It should appear on User B's page without any reload.
await expect(pageB.locator(".item-row").filter({ hasText: "Apple" })).toBeVisible();
await expect(pageB.locator(".item-row").filter({ hasText: "Apple" }).locator(".item-qty")).toHaveText("(2)");
// The left/total count should also update live for User B.
await expect(pageB.locator("#list-meta")).toHaveText("1 items left out of 1");
// User A removes the item.
await page.locator(".item-actions-button").first().click();
await page.locator("[id^='item-edit-delete']").click();
// The item and the count should update live on User B's page.
await expect(pageB.locator(".item-row").filter({ hasText: "Apple" })).toHaveCount(0);
await expect(pageB.locator("#list-meta")).toHaveText("0 items left out of 0");
await contextB.close();
});