Quellcode durchsuchen

test(e2e): add test cases for hotkey

Andelf vor 3 Jahren
Ursprung
Commit
ee38781656
2 geänderte Dateien mit 98 neuen und 3 gelöschten Zeilen
  1. 83 0
      e2e-tests/hotkey.spec.ts
  2. 15 3
      e2e-tests/utils.ts

+ 83 - 0
e2e-tests/hotkey.spec.ts

@@ -0,0 +1,83 @@
+import { test, expect } from '@playwright/test'
+import { ElectronApplication, Page, BrowserContext, _electron as electron } from 'playwright'
+import { createRandomPage, newBlock, lastBlock, appFirstLoaded, IsMac, IsLinux } from './utils'
+
+let electronApp: ElectronApplication
+let context: BrowserContext
+let page: Page
+
+test.beforeAll(async () => {
+    electronApp = await electron.launch({
+        cwd: "./static",
+        args: ["electron.js"],
+    })
+
+    context = electronApp.context()
+    await context.tracing.start({ screenshots: true, snapshots: true });
+})
+
+test.beforeEach(async () => {
+    // discard any dialog by ESC
+    if (page) {
+        await page.keyboard.press('Escape')
+        await page.keyboard.press('Escape')
+    } else {
+        page = await electronApp.firstWindow()
+    }
+})
+
+test.afterAll(async () => {
+    await electronApp.close()
+})
+
+test('open search dialog', async () => {
+    await appFirstLoaded(page)
+
+    if (IsMac) {
+        await page.keyboard.press('Meta+k')
+    } else if (IsLinux) {
+        await page.keyboard.press('Control+k')
+    } else {
+        // TODO: test on Windows and other platforms
+        expect(false)
+    }
+
+    await page.waitForSelector('[placeholder="Search or create page"]')
+})
+
+// See-also: https://github.com/logseq/logseq/issues/3278
+test('insert link', async () => {
+    await createRandomPage(page)
+
+    let hotKey = 'Control+l'
+    let selectAll = 'Control+a'
+    if (IsMac) {
+        hotKey = 'Meta+l'
+        selectAll = 'Meta+a'
+    }
+
+    // Case 1: empty link
+    await lastBlock(page)
+    await page.press(':nth-match(textarea, 1)', hotKey)
+    expect(await page.inputValue(':nth-match(textarea, 1)')).toBe('[]()')
+    await page.type(':nth-match(textarea, 1)', 'Logseq Website')
+    expect(await page.inputValue(':nth-match(textarea, 1)')).toBe('[Logseq Website]()')
+
+    // Case 2: link with label
+    await newBlock(page)
+    await page.type(':nth-match(textarea, 1)', 'Logseq')
+    await page.press(':nth-match(textarea, 1)', selectAll)
+    await page.press(':nth-match(textarea, 1)', hotKey)
+    expect(await page.inputValue(':nth-match(textarea, 1)')).toBe('[Logseq]()')
+    await page.type(':nth-match(textarea, 1)', 'https://logseq.com/')
+    expect(await page.inputValue(':nth-match(textarea, 1)')).toBe('[Logseq](https://logseq.com/)')
+
+    // Case 3: link with URL
+    await newBlock(page)
+    await page.type(':nth-match(textarea, 1)', 'https://logseq.com/')
+    await page.press(':nth-match(textarea, 1)', selectAll)
+    await page.press(':nth-match(textarea, 1)', hotKey)
+    expect(await page.inputValue(':nth-match(textarea, 1)')).toBe('[](https://logseq.com/)')
+    await page.type(':nth-match(textarea, 1)', 'Logseq')
+    expect(await page.inputValue(':nth-match(textarea, 1)')).toBe('[Logseq](https://logseq.com/)')
+})

+ 15 - 3
e2e-tests/utils.ts

@@ -1,6 +1,10 @@
-import { Page } from 'playwright'
+import { Page, Locator } from 'playwright'
 import { expect } from '@playwright/test'
+import process from 'process'
 
+export const IsMac = process.platform === 'darwin'
+export const IsLinux = process.platform === 'linux'
+export const IsWindows = process.platform === 'win32'
 
 export function randomString(length: number) {
     const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
@@ -14,6 +18,10 @@ export function randomString(length: number) {
     return result;
 }
 
+export async function appFirstLoaded(page: Page) {
+    await page.waitForSelector('text=This is a demo graph, changes will not be saved until you open a local folder')
+}
+
 export async function openSidebar(page: Page) {
     let sidebarVisible = await page.isVisible('#sidebar-nav-wrapper .left-sidebar-inner')
     if (!sidebarVisible) {
@@ -35,16 +43,20 @@ export async function createRandomPage(page: Page) {
     await page.waitForSelector(':nth-match(textarea, 1)', { state: 'visible' })
 }
 
-export async function lastBlock(page: Page) {
+export async function lastBlock(page: Page): Promise<Locator> {
     // discard any popups
     await page.keyboard.press('Escape')
     // click last block
     await page.click('.ls-block >> nth=-1')
     // wait for textarea
     await page.waitForSelector(':nth-match(textarea, 1)', { state: 'visible' })
+
+    return page.locator(':nth-match(textarea, 1)')
 }
 
-export async function newBlock(page: Page) {
+export async function newBlock(page: Page): Promise<Locator> {
     await lastBlock(page)
     await page.press(':nth-match(textarea, 1)', 'Enter')
+
+    return page.locator(':nth-match(textarea, 1)')
 }