Fix lint issues in unit test helpers

This commit is contained in:
beatz174-bit
2025-12-02 18:41:17 +10:00
parent f807857bec
commit 1030476ed1
4 changed files with 95 additions and 51 deletions
+1 -1
View File
@@ -59,7 +59,7 @@ describe('db/index initializeDatabase', () => {
const { db } = await import('../../src/db');
expect(version).toHaveBeenCalled();
expect((version as any).mock.calls.length).toBeGreaterThanOrEqual(1);
expect(version.mock.calls.length).toBeGreaterThanOrEqual(1);
expect(db).toBeDefined();
});
});
+11 -16
View File
@@ -1,4 +1,5 @@
import { describe, expect, test, vi, beforeEach } from 'vitest';
import type { StockFillDB } from '../../src/db';
import { makeMockDb } from '../utils/mockDb';
vi.mock('uuid', () => ({ v4: vi.fn(() => 'mock-uuid') }));
@@ -11,15 +12,13 @@ describe('applyMigrations', () => {
test('creates category for product name and updates product to use new id', async () => {
const db = makeMockDb();
(db.categories.toArray as any)
db.categories.toArray
.mockResolvedValueOnce([])
.mockResolvedValueOnce([{ id: 'mock-uuid', name: 'Beverages', created_at: 1, updated_at: 1 }]);
(db.products.toArray as any).mockResolvedValueOnce([
{ id: 'p1', category: 'Beverages', name: 'Cola' },
]);
db.products.toArray.mockResolvedValueOnce([{ id: 'p1', category: 'Beverages', name: 'Cola' }]);
const { applyMigrations } = await import('../../src/db/migrations');
await applyMigrations(db as any);
await applyMigrations(db as unknown as StockFillDB);
expect(db.categories.add).toHaveBeenCalledWith(
expect.objectContaining({ id: 'mock-uuid', name: 'Beverages' }),
@@ -29,15 +28,13 @@ describe('applyMigrations', () => {
test('skips update when product category already matches existing id', async () => {
const db = makeMockDb();
(db.categories.toArray as any)
db.categories.toArray
.mockResolvedValueOnce([{ id: 'cat-1', name: 'Snacks' }])
.mockResolvedValueOnce([{ id: 'cat-1', name: 'Snacks' }]);
(db.products.toArray as any).mockResolvedValueOnce([
{ id: 'p2', category: 'cat-1', name: 'Chips' },
]);
db.products.toArray.mockResolvedValueOnce([{ id: 'p2', category: 'cat-1', name: 'Chips' }]);
const { applyMigrations } = await import('../../src/db/migrations');
await applyMigrations(db as any);
await applyMigrations(db as unknown as StockFillDB);
expect(db.categories.add).not.toHaveBeenCalled();
expect(db.products.update).not.toHaveBeenCalled();
@@ -45,16 +42,14 @@ describe('applyMigrations', () => {
test('normalizes pickList categories names to ids', async () => {
const db = makeMockDb();
(db.categories.toArray as any)
db.categories.toArray
.mockResolvedValueOnce([{ id: 'cat-fruit', name: 'Fruit' }])
.mockResolvedValueOnce([{ id: 'cat-fruit', name: 'Fruit' }]);
(db.products.toArray as any).mockResolvedValueOnce([]);
(db.pickLists.toArray as any).mockResolvedValueOnce([
{ id: 'pl-1', categories: ['Fruit'] },
]);
db.products.toArray.mockResolvedValueOnce([]);
db.pickLists.toArray.mockResolvedValueOnce([{ id: 'pl-1', categories: ['Fruit'] }]);
const { applyMigrations } = await import('../../src/db/migrations');
await applyMigrations(db as any);
await applyMigrations(db as unknown as StockFillDB);
expect(db.pickLists.update).toHaveBeenCalledWith('pl-1', { categories: ['cat-fruit'] });
});
+29 -12
View File
@@ -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());