Fix lint issues in unit test helpers
This commit is contained in:
@@ -3,11 +3,18 @@ import { describe, expect, test, vi, beforeEach, afterEach } from 'vitest';
|
||||
|
||||
vi.mock('@zxing/browser', () => {
|
||||
class MockReader {
|
||||
decodeFromVideoDevice = vi.fn(async (_device: any, _video: any, callback: any) => {
|
||||
callback({ getText: () => 'decoded-fallback' } as any);
|
||||
return { stop: vi.fn() } as any;
|
||||
});
|
||||
decodeFromVideoDevice = vi.fn(
|
||||
async (
|
||||
_device: string | undefined,
|
||||
_video: HTMLVideoElement,
|
||||
callback: (result: { getText: () => string }) => void,
|
||||
) => {
|
||||
callback({ getText: () => 'decoded-fallback' });
|
||||
return { stop: vi.fn() };
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
return { BrowserMultiFormatReader: MockReader };
|
||||
});
|
||||
|
||||
@@ -15,11 +22,15 @@ describe('useBarcodeScanner', () => {
|
||||
beforeEach(() => {
|
||||
vi.resetModules();
|
||||
vi.clearAllMocks();
|
||||
(globalThis as any).BarcodeDetector = undefined;
|
||||
const globalWithBarcode = globalThis as typeof globalThis & {
|
||||
BarcodeDetector?: new () => { detect: (source: ImageBitmap | HTMLCanvasElement) => Promise<Array<{ rawValue: string }>> };
|
||||
};
|
||||
globalWithBarcode.BarcodeDetector = undefined;
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
delete (globalThis as any).BarcodeDetector;
|
||||
const globalWithBarcode = globalThis as typeof globalThis & { BarcodeDetector?: unknown };
|
||||
delete globalWithBarcode.BarcodeDetector;
|
||||
});
|
||||
|
||||
test('falls back to ZXing reader when BarcodeDetector is unavailable', async () => {
|
||||
@@ -35,16 +46,22 @@ describe('useBarcodeScanner', () => {
|
||||
class MockBarcodeDetector {
|
||||
detect = detect;
|
||||
}
|
||||
(globalThis as any).BarcodeDetector = MockBarcodeDetector as any;
|
||||
const globalWithBarcode = globalThis as typeof globalThis & { BarcodeDetector?: typeof MockBarcodeDetector };
|
||||
globalWithBarcode.BarcodeDetector = MockBarcodeDetector;
|
||||
|
||||
const play = vi.fn().mockResolvedValue(undefined);
|
||||
const getVideoTracks = vi.fn().mockReturnValue([{ stop: vi.fn() }]);
|
||||
const getTracks = vi.fn().mockReturnValue([{ stop: vi.fn() }]);
|
||||
const stream = { getVideoTracks, getTracks } as any;
|
||||
navigator.mediaDevices = {
|
||||
getUserMedia: vi.fn().mockResolvedValue(stream),
|
||||
} as any;
|
||||
(globalThis as any).createImageBitmap = vi.fn().mockResolvedValue({});
|
||||
const stream = { getVideoTracks, getTracks };
|
||||
Object.defineProperty(navigator, 'mediaDevices', {
|
||||
value: {
|
||||
getUserMedia: vi.fn().mockResolvedValue(stream),
|
||||
},
|
||||
configurable: true,
|
||||
});
|
||||
const createImageBitmap = vi.fn<[], Promise<ImageBitmap>>().mockResolvedValue({} as ImageBitmap);
|
||||
const globalWithBitmap = globalThis as typeof globalThis & { createImageBitmap?: typeof createImageBitmap };
|
||||
globalWithBitmap.createImageBitmap = createImageBitmap;
|
||||
|
||||
const { useBarcodeScanner } = await import('../../src/hooks/useBarcodeScanner');
|
||||
const { result } = renderHook(() => useBarcodeScanner());
|
||||
|
||||
Reference in New Issue
Block a user