mirror of
https://github.com/streetwriters/notesnook.git
synced 2025-12-24 07:29:30 +01:00
**Why?** The old test suite was a confusing mess, hard to maintain, update, and add more tests to. It lacked a much needed layer of expressivity & the reusable functions were all over the place. Since it used a global `page` (by mistake), it couldn't run in parallel. Moreover, the global `page` approach caused random flakiness. All the above reasons led to this OM (Object Model) based approach to tests. The tests are now much more expressive, reslient, resuable & easier to maintain. During the rewriting process I also added a couple more tests (about 10) so this is a net improvement. Previously, running the tests were also quite slow (15-25s avg). This has now been improved to (5-8s avg) by running the tests in production. This means the app now requires to be built before running the tests: ```sh npm run build:test:web # this is only required once npm run test:web ```
91 lines
2.7 KiB
TypeScript
91 lines
2.7 KiB
TypeScript
/*
|
|
This file is part of the Notesnook project (https://notesnook.com/)
|
|
|
|
Copyright (C) 2022 Streetwriters (Private) Limited
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU General Public License as published by
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
(at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
import { Locator, Page } from "@playwright/test";
|
|
import { getTestId } from "../utils";
|
|
import { Item, Notebook } from "./types";
|
|
|
|
export async function* iterateList(list: Locator) {
|
|
const count = await list.count();
|
|
for (let i = 0; i < count; ++i) {
|
|
yield list.nth(i);
|
|
}
|
|
return null;
|
|
}
|
|
|
|
export async function fillNotebookDialog(
|
|
page: Page,
|
|
notebook: Notebook,
|
|
editing = false
|
|
) {
|
|
const titleInput = page.locator(getTestId("title-input"));
|
|
const descriptionInput = page.locator(getTestId("description-input"));
|
|
const topicInput = page.locator(getTestId(`edit-topic-input`));
|
|
const topicInputAction = page.locator(getTestId(`edit-topic-action`));
|
|
|
|
await titleInput.waitFor({ state: "visible" });
|
|
|
|
await titleInput.fill(notebook.title);
|
|
if (notebook.description) await descriptionInput.fill(notebook.description);
|
|
|
|
const topicItems = page.locator(getTestId("topic-item"));
|
|
for (let i = 0; i < notebook.topics.length; ++i) {
|
|
if (editing) {
|
|
const topicItem = topicItems.nth(i);
|
|
await topicItem.click();
|
|
}
|
|
await topicInput.fill(notebook.topics[i]);
|
|
await topicInputAction.click();
|
|
}
|
|
|
|
await confirmDialog(page);
|
|
}
|
|
|
|
export async function fillItemDialog(page: Page, item: Item) {
|
|
const titleInput = page.locator(getTestId("title-input"));
|
|
await titleInput.waitFor({ state: "visible" });
|
|
|
|
await titleInput.fill(item.title);
|
|
|
|
await confirmDialog(page);
|
|
}
|
|
|
|
export async function fillPasswordDialog(page: Page, password: string) {
|
|
await page.locator(getTestId("dialog-password")).fill(password);
|
|
await confirmDialog(page);
|
|
}
|
|
|
|
export async function confirmDialog(page: Page) {
|
|
const dialogConfirm = page.locator(getTestId("dialog-yes"));
|
|
await dialogConfirm.click();
|
|
// await dialogConfirm.waitFor({ state: "detached" });
|
|
}
|
|
|
|
export async function waitToHaveText(page: Page, id: string) {
|
|
await page.waitForFunction(
|
|
({ id }) => {
|
|
return (
|
|
(document.querySelector(`[data-test-id="${id}"]`)?.textContent
|
|
?.length || 0) > 0
|
|
);
|
|
},
|
|
{ id }
|
|
);
|
|
}
|