Files

38 lines
1.2 KiB
TypeScript
Raw Permalink Normal View History

2026-10-05 18:10:18 -04:00
/** @vitest-environment jsdom */
import { cleanup, fireEvent, render, screen } from '@testing-library/react';
import { afterEach, describe, expect, it, vi } from 'vitest';
import { NumberInput } from './controls';
afterEach(cleanup);
describe('NumberInput', () => {
it('renders on its own and reports the entered number', () => {
const onValueChange = vi.fn();
2026-10-08 16:53:20 -04:00
render(<NumberInput aria-label='Limit' value={10} onValueChange={onValueChange} />);
2026-10-05 18:10:18 -04:00
const input = screen.getByRole('textbox', { name: 'Limit' });
2026-10-08 16:53:20 -04:00
2026-10-05 18:10:18 -04:00
expect(input).toHaveValue('10');
fireEvent.change(input, { target: { value: '25' } });
fireEvent.blur(input);
expect(onValueChange).toHaveBeenLastCalledWith(25, expect.anything());
});
it('reports an emptied field as null', () => {
const onValueChange = vi.fn();
2026-10-08 16:53:20 -04:00
render(<NumberInput aria-label='Limit' value={10} onValueChange={onValueChange} />);
2026-10-05 18:10:18 -04:00
const input = screen.getByRole('textbox', { name: 'Limit' });
2026-10-08 16:53:20 -04:00
2026-10-05 18:10:18 -04:00
fireEvent.change(input, { target: { value: '' } });
fireEvent.blur(input);
expect(onValueChange).toHaveBeenLastCalledWith(null, expect.anything());
});
});