Browse Source

test(e2e): activate copy & paste block ref test

Junyi Du 3 years ago
parent
commit
ae29310cff
3 changed files with 55 additions and 49 deletions
  1. 47 42
      e2e-tests/editor.spec.ts
  2. 2 6
      e2e-tests/logseq_url.spec.ts
  3. 6 1
      e2e-tests/utils.ts

+ 47 - 42
e2e-tests/editor.spec.ts

@@ -1,6 +1,6 @@
 import { expect } from '@playwright/test'
 import { test } from './fixtures'
-import { createRandomPage, enterNextBlock, systemModifier } from './utils'
+import { createRandomPage, enterNextBlock, systemModifier, IsMac, getIsWebAPIClipboardSupported, captureConsoleWithPrefix } from './utils'
 import { dispatch_kb_events } from './util/keyboard-events'
 import * as kb_events from './util/keyboard-events'
 
@@ -142,44 +142,49 @@ test(
     }
   })
 
-// FIXME: ClipboardItem is not defined when running with this test
-// test('copy & paste block ref and replace its content', async ({ page }) => {
-//   await createRandomPage(page)
-
-//   await page.type('textarea >> nth=0', 'Some random text')
-//   if (IsMac) {
-//     await page.keyboard.press('Meta+c')
-//   } else {
-//     await page.keyboard.press('Control+c')
-//   }
-
-//   await page.pause()
-
-//   await page.press('textarea >> nth=0', 'Enter')
-//   if (IsMac) {
-//     await page.keyboard.press('Meta+v')
-//   } else {
-//     await page.keyboard.press('Control+v')
-//   }
-//   await page.keyboard.press('Escape')
-
-//   const blockRef$ = page.locator('.block-ref >> text="Some random text"');
-
-//   // Check if the newly created block-ref has the same referenced content
-//   await expect(blockRef$).toHaveCount(1);
-
-//   // Edit the last block
-//   await blockRef$.press('Enter')
-
-//   // Move cursor into the block ref
-//   for (let i = 0; i < 4; i++) {
-//     await page.press('textarea >> nth=0', 'ArrowLeft')
-//   }
-
-//   // Trigger replace-block-reference-with-content-at-point
-//   if (IsMac) {
-//     await page.keyboard.press('Meta+Shift+r')
-//   } else {
-//     await page.keyboard.press('Control+Shift+v')
-//   }
-// })
+test('copy & paste block ref and replace its content', async ({ page, block }) => {
+    await createRandomPage(page)
+    let IsWebAPIClipboardSupported = await getIsWebAPIClipboardSupported(page)
+    let promise_capture: Promise<string> = null
+
+    await block.mustFill('Some random text')
+    // FIXME: copy instantly will make content disappear
+    await page.waitForTimeout(1000)
+    if (!IsWebAPIClipboardSupported){
+        promise_capture = captureConsoleWithPrefix(page, "Copy without `clipboard-write` permission:")
+    }
+    if (IsMac) {
+        await page.keyboard.press('Meta+c')
+    } else {
+        await page.keyboard.press('Control+c')
+    }
+
+    await page.press('textarea >> nth=0', 'Enter')
+    if (IsWebAPIClipboardSupported){
+        if (IsMac) {
+            await page.keyboard.press('Meta+v')
+        } else {
+            await page.keyboard.press('Control+v')
+        }
+    } else {
+        await block.mustFill(await promise_capture)
+    }
+    await page.keyboard.press('Enter')
+
+    const blockRef$ = page.locator('.block-ref >> text="Some random text"');
+
+    // Check if the newly created block-ref has the same referenced content
+    await expect(blockRef$).toHaveCount(1);
+
+    // Move cursor into the block ref
+    for (let i = 0; i < 4; i++) {
+        await page.press('textarea >> nth=0', 'ArrowLeft')
+}
+
+    // Trigger replace-block-reference-with-content-at-point
+    if (IsMac) {
+        await page.keyboard.press('Meta+Shift+r')
+    } else {
+        await page.keyboard.press('Control+Shift+v')
+    }
+})

+ 2 - 6
e2e-tests/logseq_url.spec.ts

@@ -1,16 +1,12 @@
 import { expect } from '@playwright/test'
 import { test } from './fixtures'
-import { createRandomPage, lastBlock, captureConsoleWithPrefix, IsMac, IsLinux, queryPermission, doesClipboardItemExists } from './utils'
+import { createRandomPage, lastBlock, captureConsoleWithPrefix, IsMac, IsLinux, getIsWebAPIClipboardSupported } from './utils'
 
 test(
   "Logseq URLs (same graph)",
   async ({ page, block }) => {
     let paste_key = IsMac ? 'Meta+v' : 'Control+v'
-    let IsWebAPIClipboardSupported = (
-        // @ts-ignore "clipboard-write" is not included in TS's type definition for permissionName
-        await queryPermission(page, "clipboard-write") && 
-        await doesClipboardItemExists(page)
-    )
+    let IsWebAPIClipboardSupported = await getIsWebAPIClipboardSupported(page)
     // create a page with identify block
     let identify_text = "URL redirect target"
     let page_title = await createRandomPage(page)

+ 6 - 1
e2e-tests/utils.ts

@@ -268,4 +268,9 @@ export async function doesClipboardItemExists(page: Page): Promise<boolean> {
   return await page.evaluate((): boolean => {
     return typeof ClipboardItem !== "undefined"
   })
-}
+}
+
+export async function getIsWebAPIClipboardSupported(page: Page): Promise<boolean> {
+  // @ts-ignore "clipboard-write" is not included in TS's type definition for permissionName
+  return await queryPermission(page, "clipboard-write") && await doesClipboardItemExists(page)
+}