Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Commit

Permalink
Add e2e tests for the module system
Browse files Browse the repository at this point in the history
Signed-off-by: Dominik Henneke <[email protected]>
  • Loading branch information
dhenneke committed Aug 10, 2023
1 parent c55400d commit d7bb05d
Show file tree
Hide file tree
Showing 8 changed files with 226 additions and 0 deletions.
68 changes: 68 additions & 0 deletions cypress/e2e/module-system/roomViewLifecycle.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
Copyright 2023 Nordeck IT + Consulting GmbH.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

/// <reference types="cypress" />

import { HomeserverInstance } from "../../plugins/utils/homeserver";

describe("RoomViewLifecycle", () => {
let homeserver: HomeserverInstance;

beforeEach(() => {
cy.window().then((win) => {
win.localStorage.setItem("mx_local_settings", '{"language":"en"}'); // Ensure the language is set to a consistent value
});
cy.startHomeserver("default").then((data) => {
homeserver = data;

cy.intercept(
{ method: "GET", pathname: "/config.json" },
{ body: { default_server_config: { "m.homeserver": { base_url: data.baseUrl } } } },
);
});
});

afterEach(() => {
cy.stopHomeserver(homeserver);
});

it("should show login without the PreviewRoomNotLoggedIn lifecycle", () => {
cy.visit(`/#/room/!example:localhost`);

cy.contains("Join the conversation with an account");
cy.contains("Sign Up");
cy.contains("Sign In");
});

it("should show login with the PreviewRoomNotLoggedIn lifecycle", () => {
// we must reload the page first, otherwise, the module system settings get lost...
cy.visit("/");
cy.contains("Welcome");

// we need to set the data to local storage again because of the reload
cy.window().then((win) => {
win.localStorage.setItem("mx_local_settings", '{"language":"en"}'); // Ensure the language is set to a consistent value
});
cy.enableModuleSystem();
cy.moduleSystemPreviewRoom("!example:localhost");

cy.visit(`/#/room/!example:localhost`);
cy.reload();

cy.contains("Join the room to participate");
cy.contains("Join");
});
});
44 changes: 44 additions & 0 deletions cypress/e2e/module-system/translations.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
Copyright 2023 Nordeck IT + Consulting GmbH.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

/// <reference types="cypress" />

import { HomeserverInstance } from "../../plugins/utils/homeserver";

describe("Custom translations module", () => {
let homeserver: HomeserverInstance;

beforeEach(() => {
cy.enableModuleSystem();

cy.window().then((win) => {
win.localStorage.setItem("mx_lhs_size", "0"); // Collapse left panel for these tests
});
cy.startHomeserver("default").then((data) => {
homeserver = data;

cy.initTestUser(homeserver, "Tom");
});
});

afterEach(() => {
cy.stopHomeserver(homeserver);
});

it("should override the 'Welcome' translation", () => {
cy.get("Howdy Tom (Changed by the Module System)");
});
});
48 changes: 48 additions & 0 deletions cypress/example_module/ExampleModule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright 2023 Nordeck IT + Consulting GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { ModuleApi } from "@matrix-org/react-sdk-module-api/lib/ModuleApi";
import { RuntimeModule } from "@matrix-org/react-sdk-module-api/lib/RuntimeModule";
import {
RoomPreviewListener,
RoomViewLifecycle,
} from "@matrix-org/react-sdk-module-api/lib/lifecycles/RoomViewLifecycle";

export default class ExampleModule extends RuntimeModule {
public constructor(moduleApi: ModuleApi) {
super(moduleApi);

// Only apply the module when it is activated so we don't interfere with
// other tests that should not be affected by this module.
if (window.localStorage.getItem("cypress_module_system_enable") !== "true") {
return;
}

this.moduleApi.registerTranslations({
"Welcome %(name)s": {
en: "Howdy %(name)s (Changed by the Module System)",
},
});

this.on(RoomViewLifecycle.PreviewRoomNotLoggedIn, this.onRoomPreview);
}

protected onRoomPreview: RoomPreviewListener = (opts, roomId) => {
if (window.localStorage.getItem("cypress_module_system_preview_room_id") === roomId) {
opts.canJoin = true;
}
};
}
5 changes: 5 additions & 0 deletions cypress/example_module/build_config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# This file is included by the `yarn start:e2e` task in element-web. It will
# register the module in this folder as a module in the dev mode so that the
# Cypress tests can be executed correctly.
modules:
- "file:node_modules/matrix-react-sdk/cypress/example_module"
8 changes: 8 additions & 0 deletions cypress/example_module/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "example_module",
"version": "1.0.0",
"main": "ExampleModule.ts",
"dependencies": {
"@matrix-org/react-sdk-module-api": "*"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,5 @@ suppress_key_server_warning: true

ui_auth:
session_timeout: "300s"

allow_guest_access: true
1 change: 1 addition & 0 deletions cypress/support/e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import "./network";
import "./composer";
import "./proxy";
import "./axe";
import "./moduleSystem";

installLogsCollector({
// specify the types of logs to collect (and report to the node console at the end of the test)
Expand Down
50 changes: 50 additions & 0 deletions cypress/support/moduleSystem.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
Copyright 2023 Nordeck IT + Consulting GmbH.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

/// <reference types="cypress" />

declare global {
// eslint-disable-next-line @typescript-eslint/no-namespace
namespace Cypress {
interface Chainable {
// Enables the module system for the current session.
enableModuleSystem(): void;
moduleSystemPreviewRoom(roomId: string): void;
}
}
}

beforeEach(() => {
cy.window().then((win) => {
win.localStorage.removeItem("cypress_module_system_enable");
win.localStorage.removeItem("cypress_module_system_preview_room_id");
});
});

Cypress.Commands.add("enableModuleSystem", (): void => {
cy.window().then((win) => {
win.localStorage.setItem("cypress_module_system_enable", "true");
});
});

Cypress.Commands.add("moduleSystemPreviewRoom", (roomId: string): void => {
cy.window().then((win) => {
win.localStorage.setItem("cypress_module_system_preview_room_id", roomId);
});
});

// Needed to make this file a module
export {};

0 comments on commit d7bb05d

Please sign in to comment.