test(page-controller): add happy-dom env and basic tests

This commit is contained in:
Simon
2026-06-11 21:10:05 +08:00
parent e65c7c9601
commit 56c09a9ae9
4 changed files with 129 additions and 0 deletions

View 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('❌')
})
})
})