♻️ Refactor tests and pages

This commit is contained in:
AzazelN28 2024-05-07 13:12:58 +02:00
parent 572c6f02e2
commit 0091ac0f5f
24 changed files with 201 additions and 254 deletions

View file

@ -0,0 +1,38 @@
export class BasePage {
static async mockRPC(page, path, jsonFilename, options) {
if (!page) {
throw new TypeError("Invalid page argument. Must be a Playwright page.");
}
if (typeof path !== "string" && !(path instanceof RegExp)) {
throw new TypeError("Invalid path argument. Must be a string or a RegExp.");
}
const url = typeof path === "string" ? `**/api/rpc/command/${path}` : path;
const interceptConfig = {
status: 200,
contentType: "application/transit+json",
...options,
};
return page.route(url, (route) =>
route.fulfill({
...interceptConfig,
path: `playwright/data/${jsonFilename}`,
}),
);
}
#page = null;
constructor(page) {
this.#page = page;
}
get page() {
return this.#page;
}
async mockRPC(path, jsonFilename, options) {
return BasePage.mockRPC(this.page, path, jsonFilename, options);
}
}
export default BasePage;

View file

@ -0,0 +1,32 @@
import { MockWebSocketHelper } from "../../helpers/MockWebSocketHelper";
import BasePage from "./BasePage";
export class BaseWebSocketPage extends BasePage {
/**
* This should be called on `test.beforeEach`.
*
* @param {Page} page
* @returns
*/
static setupWebSockets(page) {
return MockWebSocketHelper.init(page);
}
/**
* Returns a promise that resolves when a WebSocket with the given URL is created.
*
* @param {string} url
* @returns {Promise<MockWebSocketHelper>}
*/
async waitForWebSocket(url) {
return MockWebSocketHelper.waitForURL(url);
}
/**
*
* @returns {Promise<MockWebSocketHelper>}
*/
async waitForNotificationsWebSocket() {
return this.waitForWebSocket("ws://0.0.0.0:3500/ws/notifications");
}
}

View file

@ -0,0 +1,54 @@
import { BasePage } from "./BasePage";
export class LoginPage extends BasePage {
static setupLoggedOutUser(page) {
return this.mockRPC(page, "get-profile", "get-profile-anonymous.json");
}
constructor(page) {
super(page);
this.loginButton = page.getByRole("button", { name: "Login" });
this.password = page.getByLabel("Password");
this.userName = page.getByLabel("Email");
this.message = page.getByText("Email or password is incorrect");
this.badLoginMsg = page.getByText("Enter a valid email please");
this.initialHeading = page.getByRole("heading", { name: "Log into my account" });
}
async fillEmailAndPasswordInputs(email, password) {
await this.userName.fill(email);
await this.password.fill(password);
}
async clickLoginButton() {
await this.loginButton.click();
}
async setupAllowedUser() {
await this.mockRPC("get-profile", "logged-in-user/get-profile-logged-in.json");
await this.mockRPC("get-teams", "logged-in-user/get-teams-default.json");
await this.mockRPC("get-font-variants?team-id=*", "logged-in-user/get-font-variants-empty.json");
await this.mockRPC("get-projects?team-id=*", "logged-in-user/get-projects-default.json");
await this.mockRPC("get-team-members?team-id=*", "logged-in-user/get-team-members-your-penpot.json");
await this.mockRPC("get-team-users?team-id=*", "logged-in-user/get-team-users-single-user.json");
await this.mockRPC(
"get-unread-comment-threads?team-id=*",
"logged-in-user/get-team-users-single-user.json",
);
await this.mockRPC("get-team-recent-files?team-id=*", "logged-in-user/get-team-recent-files-empty.json");
await this.mockRPC(
"get-profiles-for-file-comments",
"logged-in-user/get-profiles-for-file-comments-empty.json",
);
}
async setupLoginSuccess() {
await this.mockRPC("login-with-password", "logged-in-user/login-with-password-success.json");
}
async setupLoginError() {
await this.mockRPC("login-with-password", "login-with-password-error.json", { status: 400 });
}
}
export default LoginPage;

View file

@ -1,76 +0,0 @@
import { interceptRPC } from "../../helpers/index";
class LoginPage {
constructor(page) {
this.page = page;
this.loginButton = page.getByRole("button", { name: "Login" });
this.password = page.getByLabel("Password");
this.userName = page.getByLabel("Email");
this.message = page.getByText("Email or password is incorrect");
this.badLoginMsg = page.getByText("Enter a valid email please");
this.initialHeading = page.getByRole("heading", { name: "Log into my account" });
}
url() {
return this.page.url();
}
context() {
return this.page.context();
}
async fillEmailAndPasswordInputs(email, password) {
await this.userName.fill(email);
await this.password.fill(password);
}
async clickLoginButton() {
await this.loginButton.click();
}
async setupAllowedUser() {
await interceptRPC(this.page, "get-profile", "logged-in-user/get-profile-logged-in.json");
await interceptRPC(this.page, "get-teams", "logged-in-user/get-teams-default.json");
await interceptRPC(
this.page,
"get-font-variants?team-id=*",
"logged-in-user/get-font-variants-empty.json",
);
await interceptRPC(this.page, "get-projects?team-id=*", "logged-in-user/get-projects-default.json");
await interceptRPC(
this.page,
"get-team-members?team-id=*",
"logged-in-user/get-team-members-your-penpot.json",
);
await interceptRPC(
this.page,
"get-team-users?team-id=*",
"logged-in-user/get-team-users-single-user.json",
);
await interceptRPC(
this.page,
"get-unread-comment-threads?team-id=*",
"logged-in-user/get-team-users-single-user.json",
);
await interceptRPC(
this.page,
"get-team-recent-files?team-id=*",
"logged-in-user/get-team-recent-files-empty.json",
);
await interceptRPC(
this.page,
"get-profiles-for-file-comments",
"logged-in-user/get-profiles-for-file-comments-empty.json",
);
}
async setupLoginSuccess() {
await interceptRPC(this.page, "login-with-password", "logged-in-user/login-with-password-success.json");
}
async setupLoginError() {
await interceptRPC(this.page, "login-with-password", "login-with-password-error.json", { status: 400 });
}
}
export default LoginPage;