From 6a13860a808ea9006926c4c1d3c62e6113df6ba1 Mon Sep 17 00:00:00 2001 From: Felix Schulze Date: Sat, 6 Dec 2025 20:46:01 +0100 Subject: [PATCH] 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. --- .../__tests__/FireCalculatorForm.test.tsx | 7 ++++--- vitest.setup.ts | 20 ++++++++++++++++++- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/src/app/components/__tests__/FireCalculatorForm.test.tsx b/src/app/components/__tests__/FireCalculatorForm.test.tsx index 14addae..428c8bb 100644 --- a/src/app/components/__tests__/FireCalculatorForm.test.tsx +++ b/src/app/components/__tests__/FireCalculatorForm.test.tsx @@ -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(); 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 () => { diff --git a/vitest.setup.ts b/vitest.setup.ts index bff97bb..ab8856e 100644 --- a/vitest.setup.ts +++ b/vitest.setup.ts @@ -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(), + })), + }); +}