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:
@@ -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 () => {
|
||||
|
||||
@@ -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(),
|
||||
})),
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user