From 23052b3f26596dfc1bdc4ecc83106c8251093402 Mon Sep 17 00:00:00 2001 From: Raine Revere Date: Tue, 3 Dec 2024 12:32:52 +0000 Subject: [PATCH] Consolidate CommandPalette tests and refactor swipe helper. --- src/e2e/puppeteer/__tests__/commandPalette.ts | 34 +++++++++++++------ src/e2e/puppeteer/__tests__/gestures.ts | 24 ------------- .../helpers/{gesture.ts => swipe.ts} | 23 ++++++++----- 3 files changed, 39 insertions(+), 42 deletions(-) delete mode 100644 src/e2e/puppeteer/__tests__/gestures.ts rename src/e2e/puppeteer/helpers/{gesture.ts => swipe.ts} (58%) diff --git a/src/e2e/puppeteer/__tests__/commandPalette.ts b/src/e2e/puppeteer/__tests__/commandPalette.ts index 46f7ef7d3b..aec591197a 100644 --- a/src/e2e/puppeteer/__tests__/commandPalette.ts +++ b/src/e2e/puppeteer/__tests__/commandPalette.ts @@ -1,9 +1,13 @@ import path from 'path' +import { KnownDevices } from 'puppeteer' import { isMac } from '../../../browser' +import sleep from '../../../util/sleep' import configureSnapshots from '../configureSnapshots' +import paste from '../helpers/paste' import press from '../helpers/press' import screenshot from '../helpers/screenshot' import setTheme from '../helpers/setTheme' +import swipe from '../helpers/swipe' import { page } from '../setup' expect.extend({ @@ -12,23 +16,33 @@ expect.extend({ vi.setConfig({ testTimeout: 20000, hookTimeout: 20000 }) -/** Open sidebar and wait for it to slide all the way open. */ -const openCommandPalette = async () => { +it('open with keyboard', async () => { // Simulate pressing Ctrl + P or Command + P // process.platform === 'darwin' const modifierKey = isMac ? 'Meta' : 'Control' // Use Meta for macOS, Control for others - await page.keyboard.down(modifierKey) // Press the modifier key - await press('P') // Press the 'P' key - await page.keyboard.up(modifierKey) // Release the modifier key + await page.keyboard.down(modifierKey) + await press('P') + await page.keyboard.up(modifierKey) - await new Promise(resolve => setTimeout(resolve, 200)) -} - -it('command palette', async () => { - await openCommandPalette() + // TODO: Replace sleep with wait + await sleep(200) expect(await screenshot()).toMatchImageSnapshot({ customSnapshotIdentifier: 'commandPalette' }) await setTheme('Light') expect(await screenshot()).toMatchImageSnapshot({ customSnapshotIdentifier: 'commandPalette-light' }) }) + +it('open with gesture', async () => { + const importText = ` + - Hello` + + await page.emulate(KnownDevices['iPhone 15 Pro']) + await paste(importText) + + await swipe('r') + + // the command palette should open + const popupValue = await page.locator('[data-testid=popup-value]').wait() + expect(popupValue).toBeTruthy() +}) diff --git a/src/e2e/puppeteer/__tests__/gestures.ts b/src/e2e/puppeteer/__tests__/gestures.ts deleted file mode 100644 index e8bb8c8f3d..0000000000 --- a/src/e2e/puppeteer/__tests__/gestures.ts +++ /dev/null @@ -1,24 +0,0 @@ -import { KnownDevices } from 'puppeteer' -import { drawHorizontalLineGesture } from '../helpers/gesture' -import paste from '../helpers/paste' -import { page } from '../setup' - -vi.setConfig({ testTimeout: 20000, hookTimeout: 20000 }) -const iPhone = KnownDevices['iPhone 15 Pro'] - -describe('gestures', () => { - it('when starting a gesture, the command palette will open', async () => { - const cursor = 'Hello' - const importText = ` - - ${cursor}` - - await page.emulate(iPhone) - await paste(importText) - - await drawHorizontalLineGesture() - - // the command palette should open - const popupValue = await page.$('[data-testid=popup-value]') - expect(popupValue).toBeTruthy() - }) -}) diff --git a/src/e2e/puppeteer/helpers/gesture.ts b/src/e2e/puppeteer/helpers/swipe.ts similarity index 58% rename from src/e2e/puppeteer/helpers/gesture.ts rename to src/e2e/puppeteer/helpers/swipe.ts index c9c1d362c6..e991ff6af9 100644 --- a/src/e2e/puppeteer/helpers/gesture.ts +++ b/src/e2e/puppeteer/helpers/swipe.ts @@ -1,21 +1,25 @@ import { page } from '../setup' /** Draw a gesture on the screen. */ -const gesture = async (points: { x: number; y: number }[], complete: boolean = true) => { +const swipePoints = async (points: { x: number; y: number }[], complete: boolean = true) => { const start = points[0] await page.touchscreen.touchStart(start.x, start.y) - for (let i = 1; i < points.length; i++) await page.touchscreen.touchMove(points[i].x, points[i].y) + for (let i = 1; i < points.length; i++) { + await page.touchscreen.touchMove(points[i].x, points[i].y) + } - if (complete) await page.touchscreen.touchEnd() + if (complete) { + await page.touchscreen.touchEnd() + } } -export default gesture - -/** Draw a horizontal line gesture at a certain y coordinate on the touch screen. */ -export const drawHorizontalLineGesture = (y: number = 100) => - gesture( +/** Swipe right. */ +// TODO: Support other directions and multiple swipes. +const swipe = async (direction: 'r') => { + const y = 100 + await swipePoints( [ { x: 100, y }, { x: 110, y }, @@ -41,3 +45,6 @@ export const drawHorizontalLineGesture = (y: number = 100) => ], false, ) +} + +export default swipe