Skip to content

Commit f82ffa8

Browse files
authored
add e2e tests (#22)
1 parent 694e1ec commit f82ffa8

File tree

10 files changed

+95
-6
lines changed

10 files changed

+95
-6
lines changed

.github/workflows/ci.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,12 @@ jobs:
104104
fcitx5-js.tgz
105105
fcitx5-js-dev.tar.bz2
106106
107+
- name: Test
108+
run: |
109+
npx playwright install
110+
npx playwright install-deps
111+
pnpm run test
112+
107113
- name: Setup tmate session
108114
if: ${{ failure() }}
109115
uses: mxschmitt/action-tmate@v3

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ node_modules
88
pnpm-lock.yaml
99
preview/index.js
1010
preview/index.js.map
11+
test-results

.vscode/tasks.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
{
4343
"type": "shell",
4444
"label": "Preview",
45-
"command": "npx serve -l 9000 -S preview",
45+
"command": "pnpm run preview",
4646
"group": {
4747
"kind": "build"
4848
}
@@ -70,6 +70,14 @@
7070
"group": {
7171
"kind": "build"
7272
}
73+
},
74+
{
75+
"type": "shell",
76+
"label": "Test",
77+
"command": "pnpm run test",
78+
"group": {
79+
"kind": "build"
80+
}
7381
}
7482
]
7583
}

package.json

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,23 @@
99
"dist"
1010
],
1111
"scripts": {
12-
"lint": "eslint page",
13-
"lint:fix": "eslint page --fix",
12+
"lint": "eslint page tests",
13+
"lint:fix": "eslint page tests --fix",
1414
"check": "tsc --noEmit",
1515
"build": "node scripts/build.mjs",
1616
"prepack": "bash scripts/prepack.sh",
17-
"postpack": "bash scripts/postpack.sh"
17+
"postpack": "bash scripts/postpack.sh",
18+
"preview": "serve -l 9000 -S preview",
19+
"test": "playwright test --browser all",
20+
"test:chromium": "playwright test --browser chromium",
21+
"test:firefox": "playwright test --browser firefox",
22+
"test:webkit": "playwright test --browser webkit"
1823
},
1924
"license": "AGPL-3.0-or-later",
2025
"devDependencies": {
2126
"@antfu/eslint-config": "^3.12.1",
27+
"@playwright/test": "^1.50.1",
28+
"@types/node": "^22.13.5",
2229
"@types/textarea-caret": "^3.0.3",
2330
"@types/uzip": "^0.20201231.2",
2431
"error-stack-parser": "^2.1.4",

playwright.config.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { defineConfig } from '@playwright/test';
2+
3+
export default defineConfig({
4+
testDir: 'tests',
5+
fullyParallel: true,
6+
webServer: {
7+
command: 'pnpm run preview',
8+
port: 9000,
9+
reuseExistingServer: true
10+
}
11+
})

preview/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
<button>click me</button>
77
<script type="module">
88
import { fcitxReady } from "./Fcitx5.js"
9+
window.fcitxReady = fcitxReady
910
await fcitxReady
1011
window.fcitx.enable()
11-
window.fcitx.setInputMethods(['pinyin'])
1212
</script>
1313
</body>
1414
</html>

tests/global.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
declare global {
2+
interface Window {
3+
fcitxReady: Promise<void>
4+
}
5+
}
6+
7+
export {}

tests/test-generic.spec.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { expect, test } from '@playwright/test'
2+
import { init } from './util'
3+
4+
test('keyboard-us', async ({ page }) => {
5+
const addons: string[] = []
6+
page.on('console', (msg) => {
7+
const match = msg.text().match(/Loaded addon (\S+)/)
8+
if (match) {
9+
addons.push(match[1])
10+
}
11+
})
12+
13+
await init(page)
14+
expect(addons.sort()).toEqual(['clipboard', 'imselector', 'keyboard', 'quickphrase', 'unicode', 'wasmfrontend', 'webpanel'])
15+
16+
const textarea = page.locator('textarea')
17+
await textarea.click()
18+
await page.keyboard.press('q')
19+
await expect(textarea).toHaveValue('q')
20+
21+
const input = page.locator('input')
22+
await input.click()
23+
await page.keyboard.press('w')
24+
await expect(input).toHaveValue('w')
25+
})
26+
27+
test('keyboard-th', async ({ page }) => {
28+
await init(page)
29+
30+
await page.evaluate(async () => {
31+
window.fcitx.setInputMethods(['keyboard-th'])
32+
})
33+
const textarea = page.locator('textarea')
34+
await textarea.click()
35+
for (const key of 'l;ylfu') {
36+
await page.keyboard.press(key)
37+
}
38+
await expect(textarea).toHaveValue('สวัสดี')
39+
})

tests/util.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import type {
2+
Page,
3+
} from '@playwright/test'
4+
5+
export async function init(page: Page) {
6+
await page.goto('http://localhost:9000')
7+
return page.evaluate(() => {
8+
return window.fcitxReady
9+
})
10+
}

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@
99
"esModuleInterop": true,
1010
"isolatedModules": true
1111
},
12-
"include": ["page/*.ts"]
12+
"include": ["page/*.ts", "tests/*.ts"]
1313
}

0 commit comments

Comments
 (0)