Move unit tests into e2e folder
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
import { describe, expect, test, vi, beforeEach } from 'vitest';
|
import { describe, expect, test, vi, beforeEach } from 'vitest';
|
||||||
|
|
||||||
const makeDexieMock = () => {
|
const setupDexieMock = () => {
|
||||||
const version = vi.fn().mockReturnThis();
|
const version = vi.fn().mockReturnThis();
|
||||||
const stores = vi.fn().mockReturnThis();
|
const stores = vi.fn().mockReturnThis();
|
||||||
const upgrade = vi.fn().mockReturnThis();
|
const upgrade = vi.fn().mockReturnThis();
|
||||||
@@ -18,6 +18,8 @@ const makeDexieMock = () => {
|
|||||||
constructor() {}
|
constructor() {}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
vi.doMock('dexie', () => ({ default: MockDexie, Dexie: MockDexie, Table: class {} }));
|
||||||
|
|
||||||
return { MockDexie, version };
|
return { MockDexie, version };
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -28,36 +30,33 @@ describe('db/index initializeDatabase', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
test('initializeDatabase calls applyMigrations with db', async () => {
|
test('initializeDatabase calls applyMigrations with db', async () => {
|
||||||
const { MockDexie } = makeDexieMock();
|
setupDexieMock();
|
||||||
const applyMigrations = vi.fn().mockResolvedValue(undefined);
|
const applyMigrations = vi.fn().mockResolvedValue(undefined);
|
||||||
|
|
||||||
vi.mock('dexie', () => ({ default: MockDexie, Dexie: MockDexie, Table: class {} }));
|
vi.doMock('../../src/db/migrations', () => ({ applyMigrations }));
|
||||||
vi.mock('../src/db/migrations', () => ({ applyMigrations }));
|
|
||||||
|
|
||||||
const { initializeDatabase, db } = await import('../src/db');
|
const { initializeDatabase, db } = await import('../../src/db');
|
||||||
await initializeDatabase();
|
await initializeDatabase();
|
||||||
|
|
||||||
expect(applyMigrations).toHaveBeenCalledWith(db);
|
expect(applyMigrations).toHaveBeenCalledWith(db);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('initializeDatabase rejects when applyMigrations fails', async () => {
|
test('initializeDatabase rejects when applyMigrations fails', async () => {
|
||||||
const { MockDexie } = makeDexieMock();
|
setupDexieMock();
|
||||||
const applyMigrations = vi.fn().mockRejectedValue(new Error('fail'));
|
const applyMigrations = vi.fn().mockRejectedValue(new Error('fail'));
|
||||||
|
|
||||||
vi.mock('dexie', () => ({ default: MockDexie, Dexie: MockDexie, Table: class {} }));
|
vi.doMock('../../src/db/migrations', () => ({ applyMigrations }));
|
||||||
vi.mock('../src/db/migrations', () => ({ applyMigrations }));
|
|
||||||
|
|
||||||
const { initializeDatabase } = await import('../src/db');
|
const { initializeDatabase } = await import('../../src/db');
|
||||||
|
|
||||||
await expect(initializeDatabase()).rejects.toThrow('fail');
|
await expect(initializeDatabase()).rejects.toThrow('fail');
|
||||||
});
|
});
|
||||||
|
|
||||||
test('StockFillDB sets up versions on construction', async () => {
|
test('StockFillDB sets up versions on construction', async () => {
|
||||||
const { MockDexie, version } = makeDexieMock();
|
const { version } = setupDexieMock();
|
||||||
vi.mock('dexie', () => ({ default: MockDexie, Dexie: MockDexie, Table: class {} }));
|
vi.doMock('../../src/db/migrations', () => ({ applyMigrations: vi.fn() }));
|
||||||
vi.mock('../src/db/migrations', () => ({ applyMigrations: vi.fn() }));
|
|
||||||
|
|
||||||
const { db } = await import('../src/db');
|
const { db } = await import('../../src/db');
|
||||||
|
|
||||||
expect(version).toHaveBeenCalled();
|
expect(version).toHaveBeenCalled();
|
||||||
expect((version as any).mock.calls.length).toBeGreaterThanOrEqual(1);
|
expect((version as any).mock.calls.length).toBeGreaterThanOrEqual(1);
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
import { describe, expect, test, vi, beforeEach } from 'vitest';
|
import { describe, expect, test, vi, beforeEach } from 'vitest';
|
||||||
import { makeMockDb } from './utils/mockDb';
|
import { makeMockDb } from '../utils/mockDb';
|
||||||
|
|
||||||
vi.mock('uuid', () => ({ v4: vi.fn(() => 'mock-uuid') }));
|
vi.mock('uuid', () => ({ v4: vi.fn(() => 'mock-uuid') }));
|
||||||
|
|
||||||
@@ -18,7 +18,7 @@ describe('applyMigrations', () => {
|
|||||||
{ id: 'p1', category: 'Beverages', name: 'Cola' },
|
{ id: 'p1', category: 'Beverages', name: 'Cola' },
|
||||||
]);
|
]);
|
||||||
|
|
||||||
const { applyMigrations } = await import('../src/db/migrations');
|
const { applyMigrations } = await import('../../src/db/migrations');
|
||||||
await applyMigrations(db as any);
|
await applyMigrations(db as any);
|
||||||
|
|
||||||
expect(db.categories.add).toHaveBeenCalledWith(
|
expect(db.categories.add).toHaveBeenCalledWith(
|
||||||
@@ -36,7 +36,7 @@ describe('applyMigrations', () => {
|
|||||||
{ id: 'p2', category: 'cat-1', name: 'Chips' },
|
{ id: 'p2', category: 'cat-1', name: 'Chips' },
|
||||||
]);
|
]);
|
||||||
|
|
||||||
const { applyMigrations } = await import('../src/db/migrations');
|
const { applyMigrations } = await import('../../src/db/migrations');
|
||||||
await applyMigrations(db as any);
|
await applyMigrations(db as any);
|
||||||
|
|
||||||
expect(db.categories.add).not.toHaveBeenCalled();
|
expect(db.categories.add).not.toHaveBeenCalled();
|
||||||
@@ -53,7 +53,7 @@ describe('applyMigrations', () => {
|
|||||||
{ id: 'pl-1', categories: ['Fruit'] },
|
{ id: 'pl-1', categories: ['Fruit'] },
|
||||||
]);
|
]);
|
||||||
|
|
||||||
const { applyMigrations } = await import('../src/db/migrations');
|
const { applyMigrations } = await import('../../src/db/migrations');
|
||||||
await applyMigrations(db as any);
|
await applyMigrations(db as any);
|
||||||
|
|
||||||
expect(db.pickLists.update).toHaveBeenCalledWith('pl-1', { categories: ['cat-fruit'] });
|
expect(db.pickLists.update).toHaveBeenCalledWith('pl-1', { categories: ['cat-fruit'] });
|
||||||
@@ -9,17 +9,23 @@ describe('main entry point', () => {
|
|||||||
|
|
||||||
test('renders app when root element exists', async () => {
|
test('renders app when root element exists', async () => {
|
||||||
const render = vi.fn();
|
const render = vi.fn();
|
||||||
vi.mock('react-dom/client', () => ({ createRoot: () => ({ render }) }));
|
vi.doMock('react-dom/client', () => ({
|
||||||
|
default: { createRoot: () => ({ render }) },
|
||||||
|
createRoot: () => ({ render }),
|
||||||
|
}));
|
||||||
|
|
||||||
await import('../src/main');
|
await import('../../src/main');
|
||||||
|
|
||||||
expect(render).toHaveBeenCalled();
|
expect(render).toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
|
|
||||||
test('throws when root element is missing', async () => {
|
test('throws when root element is missing', async () => {
|
||||||
document.body.innerHTML = '';
|
document.body.innerHTML = '';
|
||||||
vi.mock('react-dom/client', () => ({ createRoot: () => ({ render: vi.fn() }) }));
|
vi.doMock('react-dom/client', () => ({
|
||||||
|
default: { createRoot: () => ({ render: vi.fn() }) },
|
||||||
|
createRoot: () => ({ render: vi.fn() }),
|
||||||
|
}));
|
||||||
|
|
||||||
await expect(import('../src/main')).rejects.toThrow('Root element not found');
|
await expect(import('../../src/main')).rejects.toThrow('Root element not found');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -23,7 +23,7 @@ describe('useBarcodeScanner', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
test('falls back to ZXing reader when BarcodeDetector is unavailable', async () => {
|
test('falls back to ZXing reader when BarcodeDetector is unavailable', async () => {
|
||||||
const { useBarcodeScanner } = await import('../src/hooks/useBarcodeScanner');
|
const { useBarcodeScanner } = await import('../../src/hooks/useBarcodeScanner');
|
||||||
const { result } = renderHook(() => useBarcodeScanner());
|
const { result } = renderHook(() => useBarcodeScanner());
|
||||||
result.current.videoRef.current = document.createElement('video');
|
result.current.videoRef.current = document.createElement('video');
|
||||||
|
|
||||||
@@ -39,13 +39,14 @@ describe('useBarcodeScanner', () => {
|
|||||||
|
|
||||||
const play = vi.fn().mockResolvedValue(undefined);
|
const play = vi.fn().mockResolvedValue(undefined);
|
||||||
const getVideoTracks = vi.fn().mockReturnValue([{ stop: vi.fn() }]);
|
const getVideoTracks = vi.fn().mockReturnValue([{ stop: vi.fn() }]);
|
||||||
const stream = { getVideoTracks } as any;
|
const getTracks = vi.fn().mockReturnValue([{ stop: vi.fn() }]);
|
||||||
|
const stream = { getVideoTracks, getTracks } as any;
|
||||||
navigator.mediaDevices = {
|
navigator.mediaDevices = {
|
||||||
getUserMedia: vi.fn().mockResolvedValue(stream),
|
getUserMedia: vi.fn().mockResolvedValue(stream),
|
||||||
} as any;
|
} as any;
|
||||||
(globalThis as any).createImageBitmap = vi.fn().mockResolvedValue({});
|
(globalThis as any).createImageBitmap = vi.fn().mockResolvedValue({});
|
||||||
|
|
||||||
const { useBarcodeScanner } = await import('../src/hooks/useBarcodeScanner');
|
const { useBarcodeScanner } = await import('../../src/hooks/useBarcodeScanner');
|
||||||
const { result } = renderHook(() => useBarcodeScanner());
|
const { result } = renderHook(() => useBarcodeScanner());
|
||||||
const video = document.createElement('video');
|
const video = document.createElement('video');
|
||||||
Object.defineProperty(video, 'play', { value: play });
|
Object.defineProperty(video, 'play', { value: play });
|
||||||
@@ -2,7 +2,7 @@ import { renderHook, waitFor } from '@testing-library/react';
|
|||||||
import { describe, expect, test, vi, beforeEach } from 'vitest';
|
import { describe, expect, test, vi, beforeEach } from 'vitest';
|
||||||
|
|
||||||
const setupHook = async () => {
|
const setupHook = async () => {
|
||||||
const module = await import('../src/hooks/useServiceWorker');
|
const module = await import('../../src/hooks/useServiceWorker');
|
||||||
return module.useServiceWorker;
|
return module.useServiceWorker;
|
||||||
};
|
};
|
||||||
|
|
||||||
+2
-2
@@ -80,10 +80,10 @@ export default defineConfig({
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
test: {
|
test: {
|
||||||
include: ['src/**/*.{test,spec}.{ts,tsx}'],
|
include: ['src/**/*.{test,spec}.{ts,tsx}', 'e2e/unit/**/*.{test,spec}.{ts,tsx}'],
|
||||||
environment: 'jsdom',
|
environment: 'jsdom',
|
||||||
setupFiles: './src/test/setup.ts',
|
setupFiles: './src/test/setup.ts',
|
||||||
exclude: ['e2e/**/*'],
|
exclude: ['e2e/*.spec.ts', 'e2e/fixtures.ts', 'e2e/test-helpers.ts'],
|
||||||
coverage: {
|
coverage: {
|
||||||
provider: 'v8',
|
provider: 'v8',
|
||||||
reporter: ['text', 'lcov'],
|
reporter: ['text', 'lcov'],
|
||||||
|
|||||||
Reference in New Issue
Block a user