فهرست منبع

refactor: e2e tests modKey

Konstantinos Kaloutas 2 سال پیش
والد
کامیت
cd7ca56d3f
4فایلهای تغییر یافته به همراه32 افزوده شده و 131 حذف شده
  1. 25 104
      e2e-tests/editor.spec.ts
  2. 4 12
      e2e-tests/hotkey.spec.ts
  3. 3 7
      e2e-tests/page-search.spec.ts
  4. 0 8
      e2e-tests/utils.ts

+ 25 - 104
e2e-tests/editor.spec.ts

@@ -1,6 +1,6 @@
 import { expect } from '@playwright/test'
 import { test } from './fixtures'
-import { createRandomPage, enterNextBlock, systemModifier, IsMac } from './utils'
+import { createRandomPage, enterNextBlock, modKey } from './utils'
 import { dispatch_kb_events } from './util/keyboard-events'
 import * as kb_events from './util/keyboard-events'
 
@@ -78,7 +78,7 @@ test('create new page from bracketing text #4971', async ({ page, block }) => {
 
   await block.mustType(`[[${title}]]`)
 
-  await page.keyboard.press(systemModifier('Control+o'))
+  await page.keyboard.press(modKey + '+o')
 
   // Check page title equals to `title`
   await page.waitForTimeout(100)
@@ -171,20 +171,12 @@ test('copy & paste block ref and replace its content', async ({ page, block }) =
   // FIXME: https://github.com/logseq/logseq/issues/7541
   await page.waitForTimeout(1000)
 
-  if (IsMac) {
-    await page.keyboard.press('Meta+c')
-  } else {
-    await page.keyboard.press('Control+c')
-  }
+  await page.keyboard.press(modKey + '+c')
 
   await page.press('textarea >> nth=0', 'Enter')
   await block.waitForBlocks(2)
 
-  if (IsMac) {
-    await page.keyboard.press('Meta+v')
-  } else {
-    await page.keyboard.press('Control+v')
-  }
+  await page.keyboard.press(modKey + '+v')
   await page.keyboard.press('Enter')
 
   // Check if the newly created block-ref has the same referenced content
@@ -198,11 +190,8 @@ test('copy & paste block ref and replace its content', async ({ page, block }) =
   await expect(page.locator('textarea >> nth=0')).not.toHaveValue('Some random text')
 
   // Trigger replace-block-reference-with-content-at-point
-  if (IsMac) {
-    await page.keyboard.press('Meta+Shift+r')
-  } else {
-    await page.keyboard.press('Control+Shift+r')
-  }
+  await page.keyboard.press(modKey + '+Shift+r')
+
   await expect(page.locator('textarea >> nth=0')).toHaveValue('Some random text')
   await block.escapeEditing()
 
@@ -218,11 +207,7 @@ test('copy and paste block after editing new block #5962', async ({ page, block
   await page.keyboard.press('Escape')
   await expect(page.locator('.ls-block.selected')).toHaveCount(1)
 
-  if (IsMac) {
-    await page.keyboard.press('Meta+c', { delay: 10 })
-  } else {
-    await page.keyboard.press('Control+c', { delay: 10 })
-  }
+  await page.keyboard.press(modKey + '+c', { delay: 10 })
 
   await page.keyboard.press('Enter')
   await expect(page.locator('.ls-block.selected')).toHaveCount(0)
@@ -232,11 +217,7 @@ test('copy and paste block after editing new block #5962', async ({ page, block
 
   await block.mustType('Typed block')
 
-  if (IsMac) {
-    await page.keyboard.press('Meta+v')
-  } else {
-    await page.keyboard.press('Control+v')
-  }
+  await page.keyboard.press(modKey + '+v')
   await expect(page.locator('text="Typed block"')).toHaveCount(1)
   await block.waitForBlocks(3)
 })
@@ -253,11 +234,7 @@ test('undo and redo after starting an action should not destroy text #6267', asy
   await page.keyboard.type('[[', { delay: 50 })
 
   await expect(page.locator(`[data-modal-name="page-search"]`)).toBeVisible()
-  if (IsMac) {
-    await page.keyboard.press('Meta+z')
-  } else {
-    await page.keyboard.press('Control+z')
-  }
+  await page.keyboard.press(modKey + '+z')
   await page.waitForTimeout(100)
 
   // Should close the action menu when we undo the action prompt
@@ -267,11 +244,7 @@ test('undo and redo after starting an action should not destroy text #6267', asy
   await expect(page.locator('text="text1"')).toHaveCount(1)
 
   // And it should keep what was undone as a redo action
-  if (IsMac) {
-    await page.keyboard.press('Meta+Shift+z')
-  } else {
-    await page.keyboard.press('Control+Shift+z')
-  }
+  await page.keyboard.press(modKey + '+Shift+z')
   await expect(page.locator('text="text2"')).toHaveCount(1)
 })
 
@@ -288,11 +261,7 @@ test('undo after starting an action should close the action menu #6269', async (
     await expect(page.locator(`[data-modal-name="${modalName}"]`)).toBeVisible()
 
     // Undo, removing "/today", and closing the action modal
-    if (IsMac) {
-      await page.keyboard.press('Meta+z')
-    } else {
-      await page.keyboard.press('Control+z')
-    }
+    await page.keyboard.press(modKey + '+z')
     await page.waitForTimeout(100)
     await expect(page.locator('text="/today"')).toHaveCount(0)
     await expect(page.locator(`[data-modal-name="${modalName}"]`)).not.toBeVisible()
@@ -567,31 +536,19 @@ test('should not erase typed text when expanding block quickly after typing #389
   await page.waitForTimeout(500)
   await page.type('textarea >> nth=0', ' then expand', { delay: 10 })
   // A quick cmd-down must not destroy the typed text
-  if (IsMac) {
-    await page.keyboard.press('Meta+ArrowDown')
-  } else {
-    await page.keyboard.press('Control+ArrowDown')
-  }
+  await page.keyboard.press(modKey + '+ArrowDown')
   await page.waitForTimeout(500)
   expect(await page.inputValue('textarea >> nth=0')).toBe(
     'initial text, then expand'
   )
 
   // First undo should delete the last typed information, not undo a no-op expand action
-  if (IsMac) {
-    await page.keyboard.press('Meta+z')
-  } else {
-    await page.keyboard.press('Control+z')
-  }
+  await page.keyboard.press(modKey + '+z')
   expect(await page.inputValue('textarea >> nth=0')).toBe(
     'initial text,'
   )
 
-  if (IsMac) {
-    await page.keyboard.press('Meta+z')
-  } else {
-    await page.keyboard.press('Control+z')
-  }
+  await page.keyboard.press(modKey + '+z')
   expect(await page.inputValue('textarea >> nth=0')).toBe(
     ''
   )
@@ -601,40 +558,28 @@ test('should keep correct undo and redo seq after indenting or outdenting the bl
   await createRandomPage(page)
 
   await block.mustFill("foo")
-  
+
   await page.keyboard.press("Enter")
   await expect(page.locator('textarea >> nth=0')).toHaveText("")
   await block.indent()
   await block.mustFill("bar")
   await expect(page.locator('textarea >> nth=0')).toHaveText("bar")
 
-  if (IsMac) {
-    await page.keyboard.press('Meta+z')
-  } else {
-    await page.keyboard.press('Control+z')
-  }
+  await page.keyboard.press(modKey + '+z')
   // should undo "bar" input
   await expect(page.locator('textarea >> nth=0')).toHaveText("")
-  if (IsMac) {
-    await page.keyboard.press('Shift+Meta+z')
-  } else {
-    await page.keyboard.press('Shift+Control+z')
-  }
+  await page.keyboard.press(modKey + '+Shift+z')
   // should redo "bar" input
   await expect(page.locator('textarea >> nth=0')).toHaveText("bar")
   await page.keyboard.press("Shift+Tab")
-  
+
   await page.keyboard.press("Enter")
   await expect(page.locator('textarea >> nth=0')).toHaveText("")
   // swap input seq
   await block.mustFill("baz")
   await block.indent()
 
-  if (IsMac) {
-    await page.keyboard.press('Meta+z')
-  } else {
-    await page.keyboard.press('Control+z')
-  }
+  await page.keyboard.press(modKey + '+z')
   // should undo indention
   await expect(page.locator('textarea >> nth=0')).toHaveText("baz")
   await page.keyboard.press("Shift+Tab")
@@ -646,40 +591,16 @@ test('should keep correct undo and redo seq after indenting or outdenting the bl
   await block.indent()
   await page.keyboard.type(" bbb")
   await expect(page.locator('textarea >> nth=0')).toHaveText("aaa bbb")
-  if (IsMac) {
-    await page.keyboard.press('Meta+z')
-  } else {
-    await page.keyboard.press('Control+z')
-  }
+  await page.keyboard.press(modKey + '+z')
   await expect(page.locator('textarea >> nth=0')).toHaveText("aaa")
-  if (IsMac) {
-    await page.keyboard.press('Meta+z')
-  } else {
-    await page.keyboard.press('Control+z')
-  }
+  await page.keyboard.press(modKey + '+z')
   await expect(page.locator('textarea >> nth=0')).toHaveText("aaa")
-  if (IsMac) {
-    await page.keyboard.press('Meta+z')
-  } else {
-    await page.keyboard.press('Control+z')
-  }
+  await page.keyboard.press(modKey + '+z')
   await expect(page.locator('textarea >> nth=0')).toHaveText("")
-  if (IsMac) {
-    await page.keyboard.press('Shift+Meta+z')
-  } else {
-    await page.keyboard.press('Shift+Control+z')
-  }
+  await page.keyboard.press(modKey + '+Shift+z')
   await expect(page.locator('textarea >> nth=0')).toHaveText("aaa")
-  if (IsMac) {
-    await page.keyboard.press('Shift+Meta+z')
-  } else {
-    await page.keyboard.press('Shift+Control+z')
-  }
+  await page.keyboard.press(modKey + '+Shift+z')
   await expect(page.locator('textarea >> nth=0')).toHaveText("aaa")
-  if (IsMac) {
-    await page.keyboard.press('Shift+Meta+z')
-  } else {
-    await page.keyboard.press('Shift+Control+z')
-  }
+  await page.keyboard.press(modKey + '+Shift+z')
   await expect(page.locator('textarea >> nth=0')).toHaveText("aaa bbb")
 })

+ 4 - 12
e2e-tests/hotkey.spec.ts

@@ -1,13 +1,9 @@
 import { expect } from '@playwright/test'
 import { test } from './fixtures'
-import { createRandomPage, newBlock, lastBlock, IsMac, IsLinux } from './utils'
+import { createRandomPage, newBlock, lastBlock, modKey, IsLinux } from './utils'
 
 test('open search dialog', async ({ page }) => {
-  if (IsMac) {
-    await page.keyboard.press('Meta+k')
-  } else {
-    await page.keyboard.press('Control+k')
-  }
+  await page.keyboard.press(modKey + '+k')
 
   await page.waitForSelector('[placeholder="Search or create page"]')
   await page.keyboard.press('Escape')
@@ -17,12 +13,8 @@ test('open search dialog', async ({ page }) => {
 test('insert link #3278', async ({ page }) => {
   await createRandomPage(page)
 
-  let hotKey = 'Control+l'
-  let selectAll = 'Control+a'
-  if (IsMac) {
-    hotKey = 'Meta+l'
-    selectAll = 'Meta+a'
-  }
+  let hotKey = modKey + '+l'
+  let selectAll = modKey + '+a'
 
   // Case 1: empty link
   await lastBlock(page)

+ 3 - 7
e2e-tests/page-search.spec.ts

@@ -1,7 +1,7 @@
 import { expect, Page } from '@playwright/test'
 import { test } from './fixtures'
 import { Block } from './types'
-import { IsMac, createRandomPage, newBlock, newInnerBlock, randomString, lastBlock, enterNextBlock } from './utils'
+import { modKey, createRandomPage, newBlock, newInnerBlock, randomString, lastBlock, enterNextBlock } from './utils'
 
 /***
  * Test alias features
@@ -9,12 +9,8 @@ import { IsMac, createRandomPage, newBlock, newInnerBlock, randomString, lastBlo
  * Consider diacritics
  ***/
 
-let hotkeyOpenLink = 'Control+o'
-let hotkeyBack = 'Control+['
-if (IsMac) {
-  hotkeyOpenLink = 'Meta+o'
-  hotkeyBack = 'Meta+['
-}
+let hotkeyOpenLink = modKey + '+o'
+let hotkeyBack = modKey + '+['
 
 test('Search page and blocks (diacritics)', async ({ page, block }) => {
   const rand = randomString(20)

+ 0 - 8
e2e-tests/utils.ts

@@ -242,14 +242,6 @@ export function randomBoolean(): boolean {
   return Math.random() < 0.5;
 }
 
-export function systemModifier(shortcut: string): string {
-  if (IsMac) {
-    return shortcut.replace('Control', 'Meta')
-  } else {
-    return shortcut
-  }
-}
-
 export async function captureConsoleWithPrefix(page: Page, prefix: string, timeout: number = 3000): Promise<string> {
   return new Promise((resolve, reject) => {
     let console_handler = (msg: ConsoleMessage) => {