test: restore mocks after oxlint migration

This commit is contained in:
hazre committed 2026-04-19 15:56:51 +02:00
1 parent f2bd03f427
commit 110e615d51
6 files changed
+70 -37

No files matched your search

@@ -6,8 +6,17 @@ import * as css from './TextViewer.css';
const { copyToClipboard, CodeHighlightRenderer } = vi.hoisted(() => ({
copyToClipboard: vi.fn<() => Promise<void>>(),
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
data-testid="highlight"
data-language={language}
data-allow-detect={String(Boolean(allowDetect))}
>
{code}
</code>
)),
}));
vi.mock('$utils/dom', () => ({
@@ -39,31 +39,34 @@ type RouterInitialEntry =
key?: string;
};
const createMockSection = (title: string) =>
function MockSection({
requestBack,
requestClose,
}: {
requestBack?: () => void;
requestClose: () => void;
}) {
return (
<div>
<h1>{title}</h1>
{requestBack && (
<button type="button" onClick={requestBack}>
Back
</button>
)}
<button type="button" onClick={requestClose}>
Close
</button>
</div>
);
};
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 (
<div>
<h1>{title}</h1>
{requestBack && (
<button type="button" onClick={requestBack}>
Back
</button>
)}
<button type="button" onClick={requestClose}>
Close
</button>
</div>
);
};
return {
mockMatrixClient: { getUserId: () => '@alice:server' },
mockProfile: { displayName: 'Alice', avatarUrl: undefined },
+1 -1
View File
@@ -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<string>>(
async (language: string, code: string) => `<pre data-language="${language}">${code}</pre>`
);
@@ -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
data-testid="arborium-code"
data-language={language}
data-allow-detect={String(Boolean(allowDetect))}
>
{code}
</code>
)),
}));
vi.mock('$components/code-highlight', () => ({
@@ -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<MockStream>>(),
getUserMedia: vi.fn<() => Promise<MockStream>>(async () => inputStream),
},
});
+14 -6
View File
@@ -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();
});