From 110e615d5187fa115b4d7bf16ccb908a1e32dfcc Mon Sep 17 00:00:00 2001 From: hazre Date: Sun, 19 Apr 2026 15:55:25 +0200 Subject: [PATCH] test: restore mocks after oxlint migration --- .../text-viewer/TextViewer.test.tsx | 13 ++++- .../features/settings/SettingsRoute.test.tsx | 51 ++++++++++--------- src/app/plugins/arborium/runtime.test.ts | 2 +- .../plugins/react-custom-html-parser.test.tsx | 13 ++++- .../useVoiceRecorder.test.tsx | 8 ++- src/app/utils/settingsSync.test.ts | 20 +++++--- 6 files changed, 70 insertions(+), 37 deletions(-) diff --git a/src/app/components/text-viewer/TextViewer.test.tsx b/src/app/components/text-viewer/TextViewer.test.tsx index 7c9d8f236..8308ec100 100644 --- a/src/app/components/text-viewer/TextViewer.test.tsx +++ b/src/app/components/text-viewer/TextViewer.test.tsx @@ -6,8 +6,17 @@ import * as css from './TextViewer.css'; const { copyToClipboard, CodeHighlightRenderer } = vi.hoisted(() => ({ copyToClipboard: vi.fn<() => Promise>(), - CodeHighlightRenderer: - vi.fn<(props: { code: string; language?: string; allowDetect?: boolean }) => JSX.Element>(), + CodeHighlightRenderer: vi.fn< + (props: { code: string; language?: string; allowDetect?: boolean }) => JSX.Element + >(({ code, language, allowDetect }) => ( + + {code} + + )), })); vi.mock('$utils/dom', () => ({ diff --git a/src/app/features/settings/SettingsRoute.test.tsx b/src/app/features/settings/SettingsRoute.test.tsx index 161c80c22..ea4e56e50 100644 --- a/src/app/features/settings/SettingsRoute.test.tsx +++ b/src/app/features/settings/SettingsRoute.test.tsx @@ -39,31 +39,34 @@ type RouterInitialEntry = key?: string; }; -const createMockSection = (title: string) => - function MockSection({ - requestBack, - requestClose, - }: { - requestBack?: () => void; - requestClose: () => void; - }) { - return ( -
-

{title}

- {requestBack && ( - - )} - -
- ); - }; - const { mockMatrixClient, mockProfile, mockUseSetting, createSectionMock } = vi.hoisted(() => { - const mockSettingsHook = vi.fn<() => readonly [boolean, (value: boolean) => void]>(); + const mockSettingsHook = vi.fn<() => readonly [boolean, (value: boolean) => void]>( + () => [true, vi.fn<(value: boolean) => void>()] as const + ); + // oxlint-disable-next-line unicorn/consistent-function-scoping -- vi.hoisted helpers cannot reference outer-scope factories + const createMockSection = (title: string) => + function MockSection({ + requestBack, + requestClose, + }: { + requestBack?: () => void; + requestClose: () => void; + }) { + return ( +
+

{title}

+ {requestBack && ( + + )} + +
+ ); + }; + return { mockMatrixClient: { getUserId: () => '@alice:server' }, mockProfile: { displayName: 'Alice', avatarUrl: undefined }, diff --git a/src/app/plugins/arborium/runtime.test.ts b/src/app/plugins/arborium/runtime.test.ts index 5bfe7f1bc..7fb482e27 100644 --- a/src/app/plugins/arborium/runtime.test.ts +++ b/src/app/plugins/arborium/runtime.test.ts @@ -201,7 +201,7 @@ describe('highlightCode', () => { const normalizeLanguage = vi.fn<(language: string) => string>((language: string) => language === 'js' ? 'javascript' : language ); - const detectLanguage = vi.fn<() => string>(); + const detectLanguage = vi.fn<() => string>(() => 'js'); const highlight = vi.fn<(language: string, code: string) => Promise>( async (language: string, code: string) => `
${code}
` ); diff --git a/src/app/plugins/react-custom-html-parser.test.tsx b/src/app/plugins/react-custom-html-parser.test.tsx index 2c331b147..e0f268348 100644 --- a/src/app/plugins/react-custom-html-parser.test.tsx +++ b/src/app/plugins/react-custom-html-parser.test.tsx @@ -15,8 +15,17 @@ import { const settingsLinkBaseUrl = 'https://app.example'; const { CodeHighlightRenderer } = vi.hoisted(() => ({ - CodeHighlightRenderer: - vi.fn<(props: { code: string; language?: string; allowDetect?: boolean }) => JSX.Element>(), + CodeHighlightRenderer: vi.fn< + (props: { code: string; language?: string; allowDetect?: boolean }) => JSX.Element + >(({ code, language, allowDetect }) => ( + + {code} + + )), })); vi.mock('$components/code-highlight', () => ({ diff --git a/src/app/plugins/voice-recorder-kit/useVoiceRecorder.test.tsx b/src/app/plugins/voice-recorder-kit/useVoiceRecorder.test.tsx index a4457e355..b7724764d 100644 --- a/src/app/plugins/voice-recorder-kit/useVoiceRecorder.test.tsx +++ b/src/app/plugins/voice-recorder-kit/useVoiceRecorder.test.tsx @@ -36,6 +36,7 @@ const nativeCancelAnimationFrame = globalThis.cancelAnimationFrame; const nativeMediaDevices = navigator.mediaDevices; let inputTrack: MockTrack; +let inputStream: MockStream; let destinationTrack: MockTrack; let createdAudioContexts: MockAudioContextInstance[]; @@ -63,7 +64,7 @@ function createMockAnalyserNode(): MockAnalyserNode { } class MockMediaRecorder { - public static isTypeSupported = vi.fn<() => boolean>(); + public static isTypeSupported = vi.fn<() => boolean>(() => true); public state: RecordingState = 'inactive'; @@ -123,13 +124,16 @@ function MockAudioContext(): MockAudioContextInstance { beforeEach(() => { inputTrack = createMockTrack(); + inputStream = { + getTracks: () => [inputTrack], + } as unknown as MockStream; destinationTrack = createMockTrack(); createdAudioContexts = []; Object.defineProperty(navigator, 'mediaDevices', { configurable: true, value: { - getUserMedia: vi.fn<() => Promise>(), + getUserMedia: vi.fn<() => Promise>(async () => inputStream), }, }); diff --git a/src/app/utils/settingsSync.test.ts b/src/app/utils/settingsSync.test.ts index a363533bd..2dcacb154 100644 --- a/src/app/utils/settingsSync.test.ts +++ b/src/app/utils/settingsSync.test.ts @@ -241,20 +241,28 @@ describe('exportSettingsAsJson', () => { // importSettingsFromJson describe('importSettingsFromJson', () => { + let changeListener: ((ev: Event) => void) | null; let mockInput: { type: string; accept: string; files: FileList | null; - onchange: ((ev: Event) => void) | null; + addEventListener: (type: string, listener: (ev: Event) => void) => void; click: () => void; }; beforeEach(() => { + changeListener = null; mockInput = { type: '', accept: '', files: null, - onchange: null, + addEventListener: vi.fn<(type: string, listener: (ev: Event) => void) => void>( + (type: string, listener: (ev: Event) => void) => { + if (type === 'change') { + changeListener = listener; + } + } + ), click: vi.fn<() => void>(), }; @@ -272,7 +280,7 @@ describe('importSettingsFromJson', () => { it('resolves null when no file is selected (empty files list)', async () => { // Start the promise, then immediately trigger onchange with no file. const promise = importSettingsFromJson(base); - mockInput.onchange?.(new Event('change')); + changeListener?.(new Event('change')); await expect(promise).resolves.toBeNull(); }); @@ -288,7 +296,7 @@ describe('importSettingsFromJson', () => { const promise = importSettingsFromJson({ ...base, isMarkdown: true }); // Trigger the change event; the file reader will asynchronously call onload. - mockInput.onchange?.(new Event('change')); + changeListener?.(new Event('change')); const result = await promise; expect(result).not.toBeNull(); @@ -301,7 +309,7 @@ describe('importSettingsFromJson', () => { mockInput.files = fakeFileList; const promise = importSettingsFromJson(base); - mockInput.onchange?.(new Event('change')); + changeListener?.(new Event('change')); await expect(promise).resolves.toBeNull(); }); @@ -315,7 +323,7 @@ describe('importSettingsFromJson', () => { mockInput.files = fakeFileList; const promise = importSettingsFromJson(base); - mockInput.onchange?.(new Event('change')); + changeListener?.(new Event('change')); await expect(promise).resolves.toBeNull(); });