Files
notesnook/apps/mobile/e2e/tests/utils.js

150 lines
3.7 KiB
JavaScript
Raw Permalink Normal View History

/*
This file is part of the Notesnook project (https://notesnook.com/)
2023-01-16 13:44:52 +05:00
Copyright (C) 2023 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/>.
*/
2022-08-30 16:13:11 +05:00
import { notesnook } from "../test.ids";
import { readFileSync } from "fs";
import { expect as jestExpect } from "@jest/globals";
import { toMatchImageSnapshot } from "jest-image-snapshot";
import { device as _device } from "detox";
2022-08-08 23:35:48 +05:00
jestExpect.extend({ toMatchImageSnapshot });
2022-04-01 00:53:37 +05:00
const sleep = (duration) =>
new Promise((resolve) =>
2022-04-01 00:53:37 +05:00
setTimeout(() => {
resolve();
}, duration)
);
2020-11-30 16:16:03 +05:00
2022-08-08 23:35:48 +05:00
async function LaunchApp() {
2022-08-09 16:15:34 +05:00
await expect(element(by.id(notesnook.ids.default.root))).toBeVisible();
2022-05-11 12:06:32 +05:00
await sleep(500);
2021-07-19 11:21:48 +05:00
}
2020-11-30 16:16:03 +05:00
2022-08-08 23:35:48 +05:00
function elementById(id) {
2021-12-06 15:11:53 +05:00
return element(by.id(id)).atIndex(0);
2021-12-06 12:52:55 +05:00
}
2022-08-08 23:35:48 +05:00
function elementByText(text) {
2022-01-10 10:59:29 +05:00
return element(by.text(text)).atIndex(0);
2021-12-06 12:52:55 +05:00
}
2022-08-08 23:35:48 +05:00
async function tapById(id) {
2021-12-06 12:52:55 +05:00
await elementById(id).tap();
}
2022-08-08 23:35:48 +05:00
async function tapByText(text) {
2021-12-06 12:52:55 +05:00
await elementByText(text).tap();
}
2022-08-08 23:35:48 +05:00
async function visibleByText(text) {
2021-12-06 12:52:55 +05:00
await expect(elementByText(text)).toBeVisible();
}
2022-08-08 23:35:48 +05:00
async function visibleById(id) {
2021-12-06 12:52:55 +05:00
await expect(elementById(id)).toBeVisible();
}
2022-08-08 23:35:48 +05:00
async function notVisibleById(id) {
2022-04-01 00:53:37 +05:00
await expect(elementById(id)).not.toBeVisible();
}
2022-08-08 23:35:48 +05:00
async function notVisibleByText(text) {
2022-04-03 03:35:01 +05:00
await expect(elementByText(text)).not.toBeVisible();
}
2022-08-09 16:15:34 +05:00
async function exitEditor() {
await _device.pressBack();
await _device.pressBack();
2022-08-09 16:15:34 +05:00
}
2022-08-08 23:35:48 +05:00
async function createNote(_title, _body) {
let title = _title || "Test note description that ";
let body =
_body ||
"Test note description that is very long and should not fit in text.";
2022-01-10 10:59:29 +05:00
await tapById(notesnook.buttons.add);
2022-08-09 16:15:34 +05:00
let webview = web(by.id(notesnook.editor.id));
await expect(webview.element(by.web.className("ProseMirror"))).toExist();
await webview.element(by.web.className("ProseMirror")).tap();
await webview.element(by.web.className("ProseMirror")).typeText(body, true);
2022-08-09 16:15:34 +05:00
await exitEditor();
2022-01-10 10:59:29 +05:00
await expect(element(by.text(body))).toBeVisible();
2022-01-10 22:54:21 +05:00
2022-04-01 00:53:37 +05:00
return { title, body };
}
2022-08-08 23:35:48 +05:00
async function openSideMenu() {
2022-04-01 00:53:37 +05:00
let menu = elementById(notesnook.ids.default.header.buttons.left);
await menu.tap();
await sleep(100);
2022-01-10 10:59:29 +05:00
}
2022-08-08 23:35:48 +05:00
async function navigate(screen) {
2022-04-01 00:53:37 +05:00
await sleep(500);
2021-12-06 12:52:55 +05:00
let menu = elementById(notesnook.ids.default.header.buttons.left);
await menu.tap();
2022-05-11 12:06:32 +05:00
await sleep(500);
2021-12-06 12:52:55 +05:00
await elementByText(screen).tap();
}
2022-01-10 10:59:29 +05:00
2022-01-10 22:54:21 +05:00
const testvars = {
2022-04-01 00:53:37 +05:00
isFirstTest: true
};
2022-01-10 22:54:21 +05:00
2022-08-08 23:35:48 +05:00
async function prepare() {
2022-01-10 22:54:21 +05:00
if (testvars.isFirstTest) {
testvars.isFirstTest = false;
return await LaunchApp();
}
2022-01-10 10:59:29 +05:00
await device.reverseTcpPort(8081);
await device.uninstallApp();
await device.installApp();
2022-04-01 00:53:37 +05:00
await device.launchApp({ newInstance: true });
2022-01-10 10:59:29 +05:00
await LaunchApp();
}
2022-04-01 00:53:37 +05:00
2022-08-08 23:35:48 +05:00
async function matchSnapshot(element, name) {
2022-04-01 00:53:37 +05:00
let path = await element.takeScreenshot(name);
const bitmapBuffer = readFileSync(path);
2023-01-14 11:42:24 +05:00
jestExpect(bitmapBuffer).toMatchImageSnapshot({
failureThreshold: 200,
failureThresholdType: "pixel"
});
2022-04-01 00:53:37 +05:00
}
2022-08-08 23:35:48 +05:00
export {
2022-08-08 23:35:48 +05:00
matchSnapshot,
prepare,
LaunchApp,
createNote,
navigate,
openSideMenu,
notVisibleById,
notVisibleByText,
visibleById,
visibleByText,
tapById,
tapByText,
elementByText,
elementById,
2022-08-09 16:15:34 +05:00
sleep,
exitEditor
2022-08-08 23:35:48 +05:00
};