test(frontend): sample component tests for button/dialog/theme-toggle (B2)

This commit is contained in:
2026-09-15 11:54:56 +08:00
parent 38957b4c2c
commit a3755bcfab
4 changed files with 105 additions and 0 deletions
+24
View File
@@ -19,3 +19,27 @@ Element.prototype.scrollIntoView ??= function scrollIntoView() {};
Element.prototype.hasPointerCapture ??= (() => false) as never;
Element.prototype.setPointerCapture ??= (() => {}) as never;
Element.prototype.releasePointerCapture ??= (() => {}) as never;
// jsdom 未实现 matchMedia(theme.tsx 按 prefers-color-scheme 求值):EventTarget 基类覆盖
// addEventListener/removeEventListener,addListener/removeListener 是旧版 legacy API 兼容面。
class MediaQueryListStub extends EventTarget {
media: string;
matches: boolean;
onchange: ((this: MediaQueryList, ev: MediaQueryListEvent) => unknown) | null = null;
constructor(media: string) {
super();
this.media = media;
this.matches = false;
}
addListener(listener: (this: MediaQueryList, ev: MediaQueryListEvent) => unknown) {
this.addEventListener("change", listener as EventListener);
}
removeListener(listener: (this: MediaQueryList, ev: MediaQueryListEvent) => unknown) {
this.removeEventListener("change", listener as EventListener);
}
}
globalThis.matchMedia ??= ((media: string) => new MediaQueryListStub(media)) as unknown as typeof matchMedia;