|
| 1 | +/* |
| 2 | +Copyright 2025 New Vector Ltd. |
| 3 | +
|
| 4 | +SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial |
| 5 | +Please see LICENSE files in the repository root for full details. |
| 6 | +*/ |
| 7 | + |
| 8 | +import { type Page } from "@playwright/test"; |
| 9 | + |
| 10 | +import { test, expect } from "../../element-web-test"; |
| 11 | + |
| 12 | +const screenshotOptions = (page: Page) => ({ |
| 13 | + mask: [page.locator(".mx_MessageTimestamp")], |
| 14 | + // Hide the jump to bottom button in the timeline to avoid flakiness |
| 15 | + // Exclude timestamp and read marker from snapshot |
| 16 | + css: ` |
| 17 | + .mx_JumpToBottomButton { |
| 18 | + display: none !important; |
| 19 | + } |
| 20 | + .mx_TopUnreadMessagesBar, .mx_MessagePanel_myReadMarker { |
| 21 | + display: none !important; |
| 22 | + } |
| 23 | + `, |
| 24 | +}); |
| 25 | +test.describe("Custom Component API", () => { |
| 26 | + test.use({ |
| 27 | + displayName: "Manny", |
| 28 | + config: { |
| 29 | + modules: ["/modules/custom-component-module.js"], |
| 30 | + }, |
| 31 | + page: async ({ page }, use) => { |
| 32 | + await page.route("/modules/custom-component-module.js", async (route) => { |
| 33 | + await route.fulfill({ path: "playwright/sample-files/custom-component-module.js" }); |
| 34 | + }); |
| 35 | + await use(page); |
| 36 | + }, |
| 37 | + room: async ({ page, app, user, bot }, use) => { |
| 38 | + const roomId = await app.client.createRoom({ name: "TestRoom" }); |
| 39 | + await use({ roomId }); |
| 40 | + }, |
| 41 | + }); |
| 42 | + test.describe("basic functionality", () => { |
| 43 | + test( |
| 44 | + "should replace the render method of a textual event", |
| 45 | + { tag: "@screenshot" }, |
| 46 | + async ({ page, room, app }) => { |
| 47 | + await app.viewRoomById(room.roomId); |
| 48 | + await app.client.sendMessage(room.roomId, "Simple message"); |
| 49 | + await expect(await page.locator(".mx_EventTile_last")).toMatchScreenshot( |
| 50 | + "custom-component-tile.png", |
| 51 | + screenshotOptions(page), |
| 52 | + ); |
| 53 | + }, |
| 54 | + ); |
| 55 | + test( |
| 56 | + "should fall through if one module does not render a component", |
| 57 | + { tag: "@screenshot" }, |
| 58 | + async ({ page, room, app }) => { |
| 59 | + await app.viewRoomById(room.roomId); |
| 60 | + await app.client.sendMessage(room.roomId, "Fall through here"); |
| 61 | + await expect(await page.locator(".mx_EventTile_last")).toMatchScreenshot( |
| 62 | + "custom-component-tile-fall-through.png", |
| 63 | + screenshotOptions(page), |
| 64 | + ); |
| 65 | + }, |
| 66 | + ); |
| 67 | + test( |
| 68 | + "should render the original content of a textual event conditionally", |
| 69 | + { tag: "@screenshot" }, |
| 70 | + async ({ page, room, app }) => { |
| 71 | + await app.viewRoomById(room.roomId); |
| 72 | + await app.client.sendMessage(room.roomId, "Do not replace me"); |
| 73 | + await expect(await page.locator(".mx_EventTile_last")).toMatchScreenshot( |
| 74 | + "custom-component-tile-original.png", |
| 75 | + screenshotOptions(page), |
| 76 | + ); |
| 77 | + }, |
| 78 | + ); |
| 79 | + test("should disallow editing when the allowEditingEvent hint is set to false", async ({ page, room, app }) => { |
| 80 | + await app.viewRoomById(room.roomId); |
| 81 | + await app.client.sendMessage(room.roomId, "Do not show edits"); |
| 82 | + await page.getByText("Do not show edits").hover(); |
| 83 | + await expect( |
| 84 | + await page.getByRole("toolbar", { name: "Message Actions" }).getByRole("button", { name: "Edit" }), |
| 85 | + ).not.toBeVisible(); |
| 86 | + }); |
| 87 | + test( |
| 88 | + "should render the next registered component if the filter function throws", |
| 89 | + { tag: "@screenshot" }, |
| 90 | + async ({ page, room, app }) => { |
| 91 | + await app.viewRoomById(room.roomId); |
| 92 | + await app.client.sendMessage(room.roomId, "Crash the filter!"); |
| 93 | + await expect(await page.locator(".mx_EventTile_last")).toMatchScreenshot( |
| 94 | + "custom-component-crash-handle-filter.png", |
| 95 | + screenshotOptions(page), |
| 96 | + ); |
| 97 | + }, |
| 98 | + ); |
| 99 | + test( |
| 100 | + "should render original component if the render function throws", |
| 101 | + { tag: "@screenshot" }, |
| 102 | + async ({ page, room, app }) => { |
| 103 | + await app.viewRoomById(room.roomId); |
| 104 | + await app.client.sendMessage(room.roomId, "Crash the renderer!"); |
| 105 | + await expect(await page.locator(".mx_EventTile_last")).toMatchScreenshot( |
| 106 | + "custom-component-crash-handle-renderer.png", |
| 107 | + screenshotOptions(page), |
| 108 | + ); |
| 109 | + }, |
| 110 | + ); |
| 111 | + }); |
| 112 | +}); |
0 commit comments