File size: 2,122 Bytes
eb1202c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | import { describe, expect, test } from "bun:test"
import {
disposeIfDisposable,
getHoveredLinkText,
getSpeechRecognitionCtor,
hasSetOption,
isDisposable,
setOptionIfSupported,
} from "./runtime-adapters"
describe("runtime adapters", () => {
test("detects and disposes disposable values", () => {
let count = 0
const value = {
dispose: () => {
count += 1
},
}
expect(isDisposable(value)).toBe(true)
disposeIfDisposable(value)
expect(count).toBe(1)
})
test("ignores non-disposable values", () => {
expect(isDisposable({ dispose: "nope" })).toBe(false)
expect(() => disposeIfDisposable({ dispose: "nope" })).not.toThrow()
})
test("sets options only when setter exists", () => {
const calls: Array<[string, unknown]> = []
const value = {
setOption: (key: string, next: unknown) => {
calls.push([key, next])
},
}
expect(hasSetOption(value)).toBe(true)
setOptionIfSupported(value, "fontFamily", "Berkeley Mono")
expect(calls).toEqual([["fontFamily", "Berkeley Mono"]])
expect(() => setOptionIfSupported({}, "fontFamily", "Berkeley Mono")).not.toThrow()
})
test("reads hovered link text safely", () => {
expect(getHoveredLinkText({ currentHoveredLink: { text: "https://example.com" } })).toBe("https://example.com")
expect(getHoveredLinkText({ currentHoveredLink: { text: 1 } })).toBeUndefined()
expect(getHoveredLinkText(null)).toBeUndefined()
})
test("resolves speech recognition constructor with webkit precedence", () => {
// oxlint-disable-next-line no-extraneous-class
class SpeechCtor {}
// oxlint-disable-next-line no-extraneous-class
class WebkitCtor {}
const ctor = getSpeechRecognitionCtor({
SpeechRecognition: SpeechCtor,
webkitSpeechRecognition: WebkitCtor,
})
expect(ctor).toBe(WebkitCtor)
})
test("returns undefined when no valid speech constructor exists", () => {
expect(getSpeechRecognitionCtor({ SpeechRecognition: "nope" })).toBeUndefined()
expect(getSpeechRecognitionCtor(undefined)).toBeUndefined()
})
})
|