Skip to content

Commit

Permalink
Merge pull request #832 from tchapgouv/e2e-move-tchap-functions-out-o…
Browse files Browse the repository at this point in the history
…f-element-files

E2E : move tchap functions out of element files
  • Loading branch information
estellecomment authored Nov 15, 2023
2 parents c8bf524 + 9589a59 commit 9f53623
Show file tree
Hide file tree
Showing 12 changed files with 151 additions and 138 deletions.
20 changes: 2 additions & 18 deletions cypress/e2e/content-scanner.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/// <reference types="cypress" />
/// <reference types="@testing-library/cypress" />

import RoomUtils from "../utils/room-utils";
import RandomUtils from "../utils/random-utils";

describe("Content Scanner", () => {
Expand All @@ -16,29 +15,14 @@ describe("Content Scanner", () => {

const roomName = "test/" + today + "/content_scanner_" + RandomUtils.generateRandom(4);

RoomUtils.createPrivateRoom(roomName).then(() => {
cy.createPrivateRoom(roomName).then(() => {
//open room
cy.get('[aria-label="' + roomName + '"]').click();
});
});

afterEach(() => {
// We find the roomId to clean up from the current URL.
// Note : This is simple and works, so good enough for now. But if we want to store the roomId at the end of the test instead, we could use “as”
// for passing the value around : https://docs.cypress.io/guides/core-concepts/variables-and-aliases#Sharing-Context
// Do NOT use a describe-level variable (like "let roomIdToCleanup") like we do in unit tests, cypress does not work like that.
cy.url().then((urlString) => {
console.log("roomId url string", urlString);
console.log("roomId url string split", urlString.split("/#/room/"));
console.log("roomIdToCleanup", urlString.split("/#/room/")[1]);
const roomId = urlString.split("/#/room/")[1];
if (roomId) {
cy.leaveRoom(roomId);
// todo also forgetRoom to save resources.
} else {
console.error("Did not find roomId in url. Not cleaning up.");
}
});
cy.leaveCurrentRoom();
});

const uploadFile = (file: string) => {
Expand Down
17 changes: 1 addition & 16 deletions cypress/e2e/create-room.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,7 @@ describe("Create Room", () => {
});

afterEach(() => {
// We find the roomId to clean up from the current URL.
// Note : This is simple and works, so good enough for now. But if we want to store the roomId at the end of the test instead, we could use “as”
// for passing the value around : https://docs.cypress.io/guides/core-concepts/variables-and-aliases#Sharing-Context
// Do NOT use a describe-level variable (like "let roomIdToCleanup") like we do in unit tests, cypress does not work like that.
cy.url().then((urlString) => {
console.log("roomId url string", urlString);
console.log("roomId url string split", urlString.split("/#/room/"));
console.log("roomIdToCleanup", urlString.split("/#/room/")[1]);
const roomId = urlString.split("/#/room/")[1];
if (roomId) {
cy.leaveRoomWithSilentFail(roomId);
// todo also forgetRoom to save resources.
} else {
console.error("Did not find roomId in url. Not cleaning up.");
}
});
cy.leaveCurrentRoomWithSilentFail();
});

it("should allow us to create a private room with name", () => {
Expand Down
4 changes: 2 additions & 2 deletions cypress/e2e/export-room-members.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe("Export room members feature", () => {

it("should display the tooltip on button hover", () => {
const roomName = "test/" + today + "/export_room_members/" + RandomUtils.generateRandom(4);
RoomUtils.createPublicRoom(roomName).then((roomId) => {
cy.createPublicRoom(roomName).then((roomId) => {
RoomUtils.openPeopleMenu(roomName);
cy.get('[data-testid="tc_exportRoomMembersButton"]')
.trigger("mouseover")
Expand All @@ -43,7 +43,7 @@ describe("Export room members feature", () => {
"$";
const userIdRegex = new RegExp(userIdRegexString);

RoomUtils.createPublicRoom(roomName).then((roomId) => {
cy.createPublicRoom(roomName).then((roomId) => {
RoomUtils.openPeopleMenu(roomName);
cy.get('[data-testid="tc_exportRoomMembersButton"]')
.click()
Expand Down
3 changes: 1 addition & 2 deletions cypress/e2e/location.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/// <reference types="cypress" />

import RoomUtils from "../utils/room-utils";
import RandomUtils from "../utils/random-utils";

describe("Location", () => {
Expand All @@ -16,7 +15,7 @@ describe("Location", () => {
it("does not show Location button in message composer", () => {
// Create a room
const roomName = "test/" + today + "/no_location_button/" + RandomUtils.generateRandom(4);
RoomUtils.createPublicRoom(roomName).then((roomId) => {
cy.createPublicRoom(roomName).then((roomId) => {
//open room
cy.get('[aria-label="' + roomName + '"]').click();
// Open menu in message composer
Expand Down
8 changes: 4 additions & 4 deletions cypress/e2e/room-access-settings.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe("Check room access settings", () => {
it("creates a public room and check access settings", () => {
const roomName = "test/" + today + "/public_room_check_access_settings" + RandomUtils.generateRandom(4);

RoomUtils.createPublicRoom(roomName).then((roomId) => {
cy.createPublicRoom(roomName).then((roomId) => {
RoomUtils.openRoomAccessSettings(roomName);

//assert
Expand All @@ -45,7 +45,7 @@ describe("Check room access settings", () => {
it("creates a private room and check access settings", () => {
const roomName = "test/" + today + "/private_room_check_access_settings" + RandomUtils.generateRandom(4);

RoomUtils.createPrivateRoom(roomName).then((roomId) => {
cy.createPrivateRoom(roomName).then((roomId) => {
RoomUtils.openRoomAccessSettings(roomName);

//assert
Expand All @@ -72,7 +72,7 @@ describe("Check room access settings", () => {
it("creates a private room with external and check access settings", () => {
const roomName = "test/" + today + "/external_room_check_access_settings" + RandomUtils.generateRandom(4);

RoomUtils.createPrivateWithExternalRoom(roomName).then((roomId) => {
cy.createPrivateWithExternalRoom(roomName).then((roomId) => {
RoomUtils.openRoomAccessSettings(roomName);

//assert
Expand Down Expand Up @@ -100,7 +100,7 @@ describe("Check room access settings", () => {
const roomName =
"test/" + today + "/private_room_change_external_access_settings" + RandomUtils.generateRandom(4);

RoomUtils.createPrivateRoom(roomName).then((roomId) => {
cy.createPrivateRoom(roomName).then((roomId) => {
RoomUtils.openRoomAccessSettings(roomName);

// click on 'Allow the externals to join' this room
Expand Down
2 changes: 1 addition & 1 deletion cypress/e2e/tchap-external-room-style.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe("Style of external rooms", () => {
it("displays special header in external private room", () => {
const roomName = "test/" + today + "/external_room_header_" + RandomUtils.generateRandom(4);

RoomUtils.createPrivateWithExternalRoom(roomName).then((roomId) => {
cy.createPrivateWithExternalRoom(roomName).then((roomId) => {
//open room
cy.get('[aria-label="' + roomName + '"]').click();

Expand Down
56 changes: 0 additions & 56 deletions cypress/support/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,6 @@ declare global {
* Returns the MatrixClient from the MatrixClientPeg
*/
getClient(): Chainable<MatrixClient | undefined>;
/**
* Create a room with given options.
* @param options the options to apply when creating the room
* @return the ID of the newly created room
*/
createRoom(options: ICreateRoomOpts): Chainable<string>;
/**
* Create a space with given options.
* @param options the options to apply when creating the space
Expand Down Expand Up @@ -129,18 +123,6 @@ declare global {
* @param roomIdOrAlias the id or alias of the room to join
*/
joinRoom(roomIdOrAlias: string): Chainable<Room>;
/**
* :TCHAP: added this function
* Leave a room.
* @param roomId the id of the room to invite to
*/
leaveRoom(roomId: string): Chainable<{}>;
/**
* :TCHAP: added this function
* Leave a room. If the leaving fails, log and carry on without crashing the test.
* @param roomId the id of the room to invite to
*/
leaveRoomWithSilentFail(roomId: string): Chainable<{}>;
}
}
}
Expand All @@ -156,30 +138,6 @@ Cypress.Commands.add("getDmRooms", (userId: string): Chainable<string[]> => {
.then((dmRoomMap) => dmRoomMap[userId] ?? []);
});

//:tchap: added this createRoom
Cypress.Commands.add("createRoom", (options: ICreateRoomOpts): Chainable<string> => {
return cy.window({ log: false }).then(async (win) => {
const cli = win.mxMatrixClientPeg.matrixClient;
const resp = await cli.createRoom(options);
const roomId = resp.room_id;

if (!cli.getRoom(roomId)) {
await new Promise<void>((resolve) => {
const onRoom = (room: Room) => {
if (room.roomId === roomId) {
cli.off(win.matrixcs.ClientEvent.Room, onRoom);
resolve();
}
};
cli.on(win.matrixcs.ClientEvent.Room, onRoom);
});
}

return roomId;
});
});
//:tchap: end

Cypress.Commands.add("createSpace", (options: ICreateRoomOpts): Chainable<string> => {
return cy.createRoom({
...options,
Expand Down Expand Up @@ -241,17 +199,3 @@ Cypress.Commands.add("bootstrapCrossSigning", () => {
Cypress.Commands.add("joinRoom", (roomIdOrAlias: string): Chainable<Room> => {
return cy.getClient().then((cli) => cli.joinRoom(roomIdOrAlias));
});

Cypress.Commands.add("leaveRoom", (roomId: string): Chainable<{}> => {
return cy.getClient().then((cli) => cli.leave(roomId));
});

// Needed until this is fixed : https://github.com/tchapgouv/synapse-manage-last-admin/issues/11
Cypress.Commands.add("leaveRoomWithSilentFail", (roomId: string): Chainable<{}> => {
return cy.getClient().then((cli) => {
return cli.leave(roomId).catch((err) => {
cy.log("COULD NOT LEAVE ROOM ! Continuing silently.", err);
return {};
});
});
});
15 changes: 2 additions & 13 deletions cypress/support/e2e.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,5 @@
/*
Copyright 2022 The Matrix.org Foundation C.I.C.
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.
Copyright DINUM 2023
*/

/// <reference types="cypress" />
Expand All @@ -20,3 +8,4 @@ import "cypress-real-events";

import "./loginByEmail";
import "./client";
import "./rooms";
14 changes: 1 addition & 13 deletions cypress/support/loginByEmail.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,5 @@
/*
Copyright 2022 The Matrix.org Foundation C.I.C.
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.
Copyright DINUM 2023
*/

/// <reference types="cypress" />
Expand Down
132 changes: 132 additions & 0 deletions cypress/support/rooms.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/**
* Copyright DINUM 2023
*/

/// <reference types="cypress" />
/// <reference types="@testing-library/cypress" />

import Chainable = Cypress.Chainable;
import type { ICreateRoomOpts } from "matrix-js-sdk/src/@types/requests";
import type { Room } from "matrix-js-sdk/src/models/room";

import TchapCreateRoom from "../../src/tchap/lib/createTchapRoom";
import { TchapRoomType } from "../../src/tchap/@types/tchap";

declare global {
// eslint-disable-next-line @typescript-eslint/no-namespace
namespace Cypress {
interface Chainable {
/**
* Leave a room.
* @param roomId the id of the room to invite to
*/
leaveRoom(roomId: string): Chainable<{}>;

/**
* Leave a room. If the leaving fails, log and carry on without crashing the test.
* Needed until this is fixed : https://github.com/tchapgouv/synapse-manage-last-admin/issues/11
* @param roomId the id of the room to invite to
*/
leaveRoomWithSilentFail(roomId: string): Chainable<{}>;

/**
* Leave the room that we are currently in.
*/
leaveCurrentRoom(): Chainable<{}>;

/**
* Leave the room that we are currently in. If the leaving fails, log and carry on without crashing the test.
*/
leaveCurrentRoomWithSilentFail(): Chainable<{}>;

/**
* Create a room with given options.
* @param options the options to apply when creating the room
* @return the ID of the newly created room
*/
createRoom(options: ICreateRoomOpts): Chainable<string>;

createPublicRoom(roomName: string): Chainable<string>;

createPrivateRoom(roomName: string): Chainable<string>;

createPrivateWithExternalRoom(roomName: string): Chainable<string>;
}
}
}

Cypress.Commands.add("leaveRoom", (roomId: string): Chainable<{}> => {
return cy.getClient().then((cli) => cli.leave(roomId));
});

Cypress.Commands.add("leaveRoomWithSilentFail", (roomId: string): Chainable<{}> => {
return cy.getClient().then((cli) => {
return cli.leave(roomId).catch((err) => {
cy.log("COULD NOT LEAVE ROOM ! Continuing silently.", err);
return {};
});
});
});

Cypress.Commands.add("leaveCurrentRoom", (): Chainable<{}> => {
// We find the roomId to clean up from the current URL.
return cy.url().then((urlString) => {
const roomId = urlString.split("/#/room/")[1];
if (roomId) {
return cy.leaveRoom(roomId);
} else {
console.error("Did not find roomId in url. Not cleaning up.");
return {};
}
});
});

Cypress.Commands.add("leaveCurrentRoomWithSilentFail", (): Chainable<{}> => {
// We find the roomId to clean up from the current URL.
return cy.url().then((urlString) => {
const roomId = urlString.split("/#/room/")[1];
if (roomId) {
return cy.leaveRoomWithSilentFail(roomId);
} else {
console.error("Did not find roomId in url. Not cleaning up.");
return {};
}
});
});

Cypress.Commands.add("createRoom", (options: ICreateRoomOpts): Chainable<string> => {
return cy.window({ log: false }).then(async (win) => {
const cli = win.mxMatrixClientPeg.matrixClient;
const resp = await cli.createRoom(options);
const roomId = resp.room_id;

if (!cli.getRoom(roomId)) {
await new Promise<void>((resolve) => {
const onRoom = (room: Room) => {
if (room.roomId === roomId) {
cli.off(win.matrixcs.ClientEvent.Room, onRoom);
resolve();
}
};
cli.on(win.matrixcs.ClientEvent.Room, onRoom);
});
}

return roomId;
});
});

Cypress.Commands.add("createPublicRoom", (roomName: string): Chainable<string> => {
return cy.createRoom(TchapCreateRoom.roomCreateOptions(roomName, TchapRoomType.Forum, false).createOpts);
});

Cypress.Commands.add("createPrivateRoom", (roomName: string): Chainable<string> => {
return cy.createRoom(TchapCreateRoom.roomCreateOptions(roomName, TchapRoomType.Private, false).createOpts);
});

Cypress.Commands.add("createPrivateWithExternalRoom", (roomName: string): Chainable<string> => {
return cy.createRoom(TchapCreateRoom.roomCreateOptions(roomName, TchapRoomType.External, false).createOpts);
});

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

0 comments on commit 9f53623

Please sign in to comment.