Files
stockfill/src/platform/web.test.ts
T
beatzaplenty 0f46e7b331 Fix unit tests
new file:   src/components/PickItemRow.narrow.test.tsx
	new file:   src/components/ProductRow.additional.test.tsx
	new file:   src/platform/web.test.ts
	modified:   src/screens/ManageProductsScreen.test.tsx
	new file:   src/services/importExportService.additional.test.ts
2025-12-01 17:40:17 +10:00

68 lines
2.1 KiB
TypeScript

// src/platform/web.test.ts
import { describe, it, expect, vi, afterEach } from 'vitest';
import { isOnline, triggerDownload } from './web';
describe('platform web utilities', () => {
afterEach(() => {
vi.unstubAllGlobals();
});
it('isOnline uses navigator.onLine when available', () => {
const originalNavigator = (globalThis as any).navigator;
try {
vi.stubGlobal('navigator', { onLine: false } as any);
expect(isOnline()).toBe(false);
vi.stubGlobal('navigator', { onLine: true } as any);
expect(isOnline()).toBe(true);
} finally {
if (originalNavigator !== undefined) {
vi.stubGlobal('navigator', originalNavigator);
} else {
vi.unstubAllGlobals();
}
}
});
it('isOnline returns true when navigator missing', () => {
const originalNavigator = (globalThis as any).navigator;
try {
vi.stubGlobal('navigator', undefined as any);
expect(isOnline()).toBe(true);
} finally {
if (originalNavigator !== undefined) {
vi.stubGlobal('navigator', originalNavigator);
} else {
vi.unstubAllGlobals();
}
}
});
it('triggerDownload calls createObjectURL, click and revokeObjectURL', () => {
const createObjectURL = vi.fn().mockReturnValue('blob:fake');
const revokeObjectURL = vi.fn();
vi.stubGlobal('URL', { createObjectURL, revokeObjectURL } as any);
const clickMock = vi.fn();
const anchor: any = { href: '', download: '', click: clickMock };
const origCreateElement = document.createElement.bind(document);
(document as any).createElement = (tag: string) => {
if (tag === 'a') return anchor;
return origCreateElement(tag);
};
try {
const blob = new Blob(['hello'], { type: 'text/plain' });
triggerDownload(blob, 'file.txt');
expect(createObjectURL).toHaveBeenCalled();
expect(clickMock).toHaveBeenCalled();
expect(revokeObjectURL).toHaveBeenCalled();
} finally {
(document as any).createElement = origCreateElement;
vi.unstubAllGlobals();
}
});
});