2024-07-23 14:01:10 +02:00
|
|
|
import NumberRangeInput from '$lib/components/shared-components/number-range-input.svelte';
|
2024-11-02 16:49:07 +01:00
|
|
|
import { render, type RenderResult } from '@testing-library/svelte';
|
2024-07-23 14:01:10 +02:00
|
|
|
import userEvent from '@testing-library/user-event';
|
2025-08-30 13:39:25 -05:00
|
|
|
import type { Mock } from 'vitest';
|
2024-07-23 14:01:10 +02:00
|
|
|
|
|
|
|
|
describe('NumberRangeInput component', () => {
|
|
|
|
|
const user = userEvent.setup();
|
|
|
|
|
let sut: RenderResult<NumberRangeInput>;
|
|
|
|
|
let input: HTMLInputElement;
|
2025-08-30 13:39:25 -05:00
|
|
|
let onInput: Mock;
|
|
|
|
|
let onKeyDown: Mock;
|
2024-07-23 14:01:10 +02:00
|
|
|
|
|
|
|
|
beforeEach(() => {
|
2025-08-30 13:39:25 -05:00
|
|
|
onInput = vi.fn();
|
|
|
|
|
onKeyDown = vi.fn();
|
2024-11-02 16:49:07 +01:00
|
|
|
sut = render(NumberRangeInput, {
|
|
|
|
|
id: '',
|
|
|
|
|
min: -90,
|
|
|
|
|
max: 90,
|
2025-08-30 13:39:25 -05:00
|
|
|
onInput,
|
|
|
|
|
onKeyDown,
|
2024-11-02 16:49:07 +01:00
|
|
|
});
|
2024-07-23 14:01:10 +02:00
|
|
|
input = sut.getByRole('spinbutton') as HTMLInputElement;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('updates value', async () => {
|
|
|
|
|
expect(input.value).toBe('');
|
2024-11-02 16:49:07 +01:00
|
|
|
await sut.rerender({ value: 10 });
|
2024-07-23 14:01:10 +02:00
|
|
|
expect(input.value).toBe('10');
|
2025-08-30 13:39:25 -05:00
|
|
|
expect(onInput).not.toHaveBeenCalled();
|
|
|
|
|
expect(onKeyDown).not.toHaveBeenCalled();
|
2024-07-23 14:01:10 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('restricts minimum value', async () => {
|
|
|
|
|
await user.type(input, '-91');
|
|
|
|
|
expect(input.value).toBe('-90');
|
2025-08-30 13:39:25 -05:00
|
|
|
expect(onInput).toHaveBeenCalled();
|
|
|
|
|
expect(onKeyDown).toHaveBeenCalled();
|
2024-07-23 14:01:10 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('restricts maximum value', async () => {
|
|
|
|
|
await user.type(input, '09990');
|
|
|
|
|
expect(input.value).toBe('90');
|
2025-08-30 13:39:25 -05:00
|
|
|
expect(onInput).toHaveBeenCalled();
|
|
|
|
|
expect(onKeyDown).toHaveBeenCalled();
|
2024-07-23 14:01:10 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('allows entering negative numbers', async () => {
|
|
|
|
|
await user.type(input, '-10');
|
|
|
|
|
expect(input.value).toBe('-10');
|
2025-08-30 13:39:25 -05:00
|
|
|
expect(onInput).toHaveBeenCalled();
|
|
|
|
|
expect(onKeyDown).toHaveBeenCalled();
|
2024-07-23 14:01:10 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('allows entering zero', async () => {
|
|
|
|
|
await user.type(input, '0');
|
|
|
|
|
expect(input.value).toBe('0');
|
2025-08-30 13:39:25 -05:00
|
|
|
expect(onInput).toHaveBeenCalled();
|
|
|
|
|
expect(onKeyDown).toHaveBeenCalled();
|
2024-07-23 14:01:10 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('allows entering decimal numbers', async () => {
|
|
|
|
|
await user.type(input, '-0.09001');
|
|
|
|
|
expect(input.value).toBe('-0.09001');
|
2025-08-30 13:39:25 -05:00
|
|
|
expect(onInput).toHaveBeenCalled();
|
|
|
|
|
expect(onKeyDown).toHaveBeenCalled();
|
2024-07-23 14:01:10 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('ignores text input', async () => {
|
|
|
|
|
await user.type(input, 'test');
|
|
|
|
|
expect(input.value).toBe('');
|
2025-08-30 13:39:25 -05:00
|
|
|
expect(onInput).toHaveBeenCalled();
|
|
|
|
|
expect(onKeyDown).toHaveBeenCalled();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('test', async () => {
|
|
|
|
|
await user.type(input, 'd');
|
|
|
|
|
expect(onInput).not.toHaveBeenCalled();
|
|
|
|
|
expect(onKeyDown).toHaveBeenCalled();
|
2024-07-23 14:01:10 +02:00
|
|
|
});
|
|
|
|
|
});
|