test(page-controller): add happy-dom env and basic tests
This commit is contained in:
@@ -42,10 +42,14 @@
|
||||
"homepage": "https://alibaba.github.io/page-agent/",
|
||||
"scripts": {
|
||||
"build": "vite build",
|
||||
"test": "vitest run",
|
||||
"prepublishOnly": "node ../../scripts/pre-publish.js",
|
||||
"postpublish": "node ../../scripts/post-publish.js"
|
||||
},
|
||||
"dependencies": {
|
||||
"ai-motion": "^0.4.8"
|
||||
},
|
||||
"devDependencies": {
|
||||
"happy-dom": "^20.10.2"
|
||||
}
|
||||
}
|
||||
|
||||
40
packages/page-controller/src/PageController.test.ts
Normal file
40
packages/page-controller/src/PageController.test.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
|
||||
import { PageController } from './PageController'
|
||||
|
||||
describe('PageController', () => {
|
||||
it('constructs and exposes the current url', async () => {
|
||||
const controller = new PageController()
|
||||
expect(controller).toBeInstanceOf(PageController)
|
||||
expect(await controller.getCurrentUrl()).toBe(window.location.href)
|
||||
})
|
||||
|
||||
describe('executeJavascript', () => {
|
||||
it('runs a script and returns its result', async () => {
|
||||
const controller = new PageController()
|
||||
const result = await controller.executeJavascript('return 1 + 2')
|
||||
expect(result).toMatchObject({ success: true })
|
||||
expect(result.message).toContain('3')
|
||||
})
|
||||
|
||||
it('exposes the abort signal to the script scope', async () => {
|
||||
const controller = new PageController()
|
||||
const controllerSignal = new AbortController()
|
||||
controllerSignal.abort()
|
||||
|
||||
const result = await controller.executeJavascript(
|
||||
'return signal.aborted',
|
||||
controllerSignal.signal
|
||||
)
|
||||
expect(result).toMatchObject({ success: true })
|
||||
expect(result.message).toContain('true')
|
||||
})
|
||||
|
||||
it('reports a syntax error as a failed result', async () => {
|
||||
const controller = new PageController()
|
||||
const result = await controller.executeJavascript('return (')
|
||||
expect(result.success).toBe(false)
|
||||
expect(result.message).toContain('❌')
|
||||
})
|
||||
})
|
||||
})
|
||||
10
packages/page-controller/vitest.config.js
Normal file
10
packages/page-controller/vitest.config.js
Normal file
@@ -0,0 +1,10 @@
|
||||
import { defineConfig } from 'vitest/config'
|
||||
|
||||
export default defineConfig({
|
||||
test: {
|
||||
name: 'page-controller',
|
||||
environment: 'happy-dom',
|
||||
include: ['src/**/*.test.ts'],
|
||||
silent: 'passed-only',
|
||||
},
|
||||
})
|
||||
Reference in New Issue
Block a user