fd055a3a2a
* test(frontend): add vitest test infrastructure - Add vitest, testing-library, jsdom dependencies - Configure vitest with jsdom environment - Add test setup with mocks for fetch, localStorage, matchMedia - Set 75% coverage threshold * Add frontend tests (#61) * Initial plan * Add frontend tests - utilities, hooks, and components (21% coverage) Co-authored-by: DanielVolz <3275994+DanielVolz@users.noreply.github.com> * Add more component tests (Auth, AboutModal, ExportModal) and useRefill hook tests - 30% coverage Co-authored-by: DanielVolz <3275994+DanielVolz@users.noreply.github.com> * Add useMedicationForm utility function tests - 30% coverage Co-authored-by: DanielVolz <3275994+DanielVolz@users.noreply.github.com> * Add AppHeader tests and more schedule.ts tests - 32% coverage Co-authored-by: DanielVolz <3275994+DanielVolz@users.noreply.github.com> * Changes before error encountered Co-authored-by: DanielVolz <3275994+DanielVolz@users.noreply.github.com> * Fix page tests and add more tests - 326 tests passing, 34% coverage Co-authored-by: DanielVolz <3275994+DanielVolz@users.noreply.github.com> * Add tests for ProfileModal, UserFilterModal, MedDetailModal - 361 tests, 36% coverage Co-authored-by: DanielVolz <3275994+DanielVolz@users.noreply.github.com> * Add SharedSchedule tests - 366 tests, 39% coverage Co-authored-by: DanielVolz <3275994+DanielVolz@users.noreply.github.com> * Expand page tests - 383 tests, 39% coverage Co-authored-by: DanielVolz <3275994+DanielVolz@users.noreply.github.com> * Add MobileEditModal tests - 409 tests, 40% coverage Co-authored-by: DanielVolz <3275994+DanielVolz@users.noreply.github.com> * Expand Dashboard and Schedule page tests - 427 tests, 40% coverage Co-authored-by: DanielVolz <3275994+DanielVolz@users.noreply.github.com> * Fix code review issues - remove invalid remindEnabled property Co-authored-by: DanielVolz <3275994+DanielVolz@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: DanielVolz <3275994+DanielVolz@users.noreply.github.com> --------- Co-authored-by: Copilot <198982749+Copilot@users.noreply.github.com> Co-authored-by: DanielVolz <3275994+DanielVolz@users.noreply.github.com>
86 lines
2.2 KiB
TypeScript
86 lines
2.2 KiB
TypeScript
import '@testing-library/jest-dom';
|
|
import { vi } from 'vitest';
|
|
|
|
// Mock fetch globally
|
|
global.fetch = vi.fn();
|
|
|
|
// Mock localStorage
|
|
const localStorageMock = {
|
|
getItem: vi.fn(),
|
|
setItem: vi.fn(),
|
|
removeItem: vi.fn(),
|
|
clear: vi.fn(),
|
|
};
|
|
Object.defineProperty(window, 'localStorage', { value: localStorageMock });
|
|
|
|
// Mock matchMedia
|
|
Object.defineProperty(window, 'matchMedia', {
|
|
writable: true,
|
|
value: vi.fn().mockImplementation((query) => ({
|
|
matches: false,
|
|
media: query,
|
|
onchange: null,
|
|
addListener: vi.fn(),
|
|
removeListener: vi.fn(),
|
|
addEventListener: vi.fn(),
|
|
removeEventListener: vi.fn(),
|
|
dispatchEvent: vi.fn(),
|
|
})),
|
|
});
|
|
|
|
// Mock navigator.clipboard
|
|
Object.defineProperty(navigator, 'clipboard', {
|
|
value: {
|
|
writeText: vi.fn().mockResolvedValue(undefined),
|
|
readText: vi.fn().mockResolvedValue(''),
|
|
},
|
|
writable: true,
|
|
});
|
|
|
|
// Mock URL.createObjectURL and URL.revokeObjectURL
|
|
global.URL.createObjectURL = vi.fn().mockReturnValue('blob:test-url');
|
|
global.URL.revokeObjectURL = vi.fn();
|
|
|
|
// Mock window.history
|
|
const mockHistoryPushState = vi.fn();
|
|
const mockHistoryBack = vi.fn();
|
|
Object.defineProperty(window, 'history', {
|
|
value: {
|
|
pushState: mockHistoryPushState,
|
|
back: mockHistoryBack,
|
|
replaceState: vi.fn(),
|
|
state: null,
|
|
length: 1,
|
|
scrollRestoration: 'auto',
|
|
go: vi.fn(),
|
|
forward: vi.fn(),
|
|
},
|
|
writable: true,
|
|
});
|
|
|
|
// Mock react-i18next globally
|
|
vi.mock('react-i18next', () => ({
|
|
useTranslation: () => ({
|
|
t: (key: string, options?: Record<string, unknown>) => {
|
|
if (options?.count !== undefined) return `${key}_${options.count}`;
|
|
if (options?.max !== undefined) return `Max ${options.max} chars`;
|
|
if (options?.days !== undefined) return `${key} (${options.days} days)`;
|
|
return key;
|
|
},
|
|
i18n: {
|
|
language: 'en',
|
|
changeLanguage: vi.fn(),
|
|
},
|
|
}),
|
|
I18nextProvider: ({ children }: { children: React.ReactNode }) => children,
|
|
initReactI18next: { type: '3rdParty', init: vi.fn() },
|
|
}));
|
|
|
|
// Reset mocks before each test
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
localStorageMock.getItem.mockReturnValue(null);
|
|
mockHistoryPushState.mockClear();
|
|
mockHistoryBack.mockClear();
|
|
});
|