Improves input test reliability and restores setup mocks

Switches input change test to use async wait for reliable value assertion.
Restores and enhances test setup with matchMedia mock to support media query-dependent components in jsdom.
This commit is contained in:
2025-12-06 20:46:01 +01:00
parent 0a5d691d04
commit 6a13860a80
2 changed files with 23 additions and 4 deletions

View File

@@ -70,15 +70,16 @@ describe('FireCalculatorForm', () => {
});
});
it('allows changing inputs', () => {
// using fireEvent for reliability with number inputs in jsdom
it('allows changing inputs', async () => {
render(<FireCalculatorForm />);
const savingsInput = screen.getByRole('spinbutton', { name: /Monthly Savings/i });
fireEvent.change(savingsInput, { target: { value: '2000' } });
expect(savingsInput).toHaveValue(2000);
await waitFor(() => {
expect(savingsInput).toHaveValue(2000);
});
});
it('validates inputs', async () => {

View File

@@ -1,2 +1,20 @@
import "@testing-library/jest-dom";
import '@testing-library/jest-dom';
import { vi } from 'vitest';
// Provide a basic matchMedia mock for jsdom so components using media queries
// (e.g. pointer detection in Tooltip) do not throw during tests.
if (!window.matchMedia) {
Object.defineProperty(window, 'matchMedia', {
writable: true,
value: vi.fn().mockImplementation((query: string) => ({
matches: false,
media: query,
onchange: null,
addListener: vi.fn(), // deprecated but still used in some libs
removeListener: vi.fn(),
addEventListener: vi.fn(),
removeEventListener: vi.fn(),
dispatchEvent: vi.fn(),
})),
});
}