Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.
TypeScript boardcollaborationdocdocxexceloffce365officepdfpptpptxpresentationsdksheetslidesspreadsheetuniverwordwordprocessorworkspacexlsx
/**
* Copyright 2023-present DreamNum Co., Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import type { ComponentProps } from 'react';
import { cleanup, fireEvent, render } from '@testing-library/react';
import { useEffect, useRef, useState } from 'react';
import { afterEach, describe, expect, it, vi } from 'vitest';
import enUS from '../../../locale/en-US';
import { ConfigProvider } from '../../config-provider/ConfigProvider';
import { InputNumber } from '../InputNumber';
import '@testing-library/jest-dom/vitest';
function BlurOnControlledReset() {
const [value, setValue] = useState(24);
const inputRef = useRef<HTMLInputElement>(null);
useEffect(() => {
if (value === 11) {
inputRef.current?.blur();
}
}, [value]);
return (
<InputNumber
ref={inputRef}
value={value}
onKeyDown={(event) => {
if (event.key === 'Escape') {
setValue(11);
}
}}
/>
);
}
describe('InputNumber controlled reset', () => {
it('keeps the new controlled value when focus leaves during reset effects', () => {
const { container } = render(<BlurOnControlledReset />);
const input = container.querySelector('input')!;
input.focus();
fireEvent.keyDown(input, { key: 'Escape', code: 'Escape' });
expect(input.value).toBe('11');
expect(document.activeElement).not.toBe(input);
});
});
function TestComponent(props: { onChange: (value: number | null) => void } & Omit<ComponentProps<typeof InputNumber>, 'value' | 'onChange'>) {
const { onChange, ...restProps } = props;
const [value, setValue] = useState(0);
function handleChange(newValue: number | null) {
setValue(newValue ?? 0);
onChange(newValue);
}
return <InputNumber value={value} onChange={handleChange} {...restProps} />;
}
afterEach(() => {
cleanup();
vi.clearAllMocks();
});
describe('InputNumber', () => {
it('forwards the accessible name and native input attributes to the editable control', () => {
const { getByRole } = render(<InputNumber aria-label="Font size" name="fontSize" required defaultValue={12} />);
const input = getByRole('textbox', { name: 'Font size' });
expect(input).toHaveAttribute('name', 'fontSize');
expect(input).toBeRequired();
});
const onChange = vi.fn();
it('should render with default value', () => {
const { getByRole } = render(<InputNumber defaultValue={5} />);
const input = getByRole('textbox') as HTMLInputElement;
expect(input.value).toBe('5');
});
it('should render with value (controlled)', () => {
const { getByRole } = render(<InputNumber value={10} />);
const input = getByRole('textbox') as HTMLInputElement;
expect(input.value).toBe('10');
});
it('should call onChange when input changes', () => {
const { container } = render(<TestComponent onChange={onChange} />);
fireEvent.change(container.querySelector('input')!, { target: { value: '123' } });
expect(onChange).toHaveBeenCalledWith(123);
});
it('should handle empty input', () => {
const { container } = render(<TestComponent onChange={onChange} />);
fireEvent.change(container.querySelector('input')!, { target: { value: '' } });
expect(onChange).toHaveBeenCalledWith(null);
});
it('should handle non-numeric input', () => {
const { container } = render(<TestComponent onChange={onChange} />);
fireEvent.change(container.querySelector('input')!, { target: { value: 'abc' } });
expect(onChange).toHaveBeenCalledWith(0);
});
it('should handle negative numbers', () => {
const { container } = render(<TestComponent onChange={onChange} />);
fireEvent.change(container.querySelector('input')!, { target: { value: '-123' } });
expect(onChange).toHaveBeenCalledWith(-123);
});
it('should handle decimal numbers', () => {
const { container } = render(<TestComponent onChange={onChange} />);
fireEvent.change(container.querySelector('input')!, { target: { value: '123.45' } });
expect(onChange).toHaveBeenCalledWith(123.45);
});
it('should emit blur event with final value', () => {
const { container } = render(<InputNumber defaultValue={0} onChange={onChange} />);
const input = container.querySelector('input')!;
fireEvent.change(input, { target: { value: '456' } });
fireEvent.blur(input);
expect(onChange).toHaveBeenCalledWith(456);
fireEvent.change(input, { target: { value: Infinity } });
fireEvent.blur(input);
expect(onChange).toHaveBeenLastCalledWith(0);
});
it('should emit blur event with final value & empty input', () => {
const { container } = render(<TestComponent onChange={onChange} allowEmpty />);
const input = container.querySelector('input')!;
fireEvent.change(input, { target: { value: '' } });
fireEvent.blur(input);
expect(onChange).toHaveBeenCalledWith(null);
});
it('should handle keydown events', () => {
const { container } = render(<TestComponent onChange={onChange} />);
const input = container.querySelector('input')!;
// Simulate keydown for increment
fireEvent.keyDown(input, { key: 'ArrowUp' });
expect(onChange).toHaveBeenCalledWith(1);
// Simulate keydown for decrement
fireEvent.keyDown(input, { key: 'ArrowDown' });
expect(onChange).toHaveBeenCalledWith(0);
});
it('should respect min/max and disable control buttons at bounds', () => {
onChange.mockClear();
const { container } = render(<InputNumber value={1} min={1} max={2} onChange={onChange} />);
const [incBtn, decBtn] = Array.from(container.querySelectorAll('[role="button"]')) as HTMLElement[];
expect(decBtn).toHaveAttribute('aria-disabled', 'true');
expect(incBtn).toHaveAttribute('aria-disabled', 'false');
fireEvent.click(decBtn);
expect(onChange).not.toHaveBeenCalled();
fireEvent.click(incBtn);
expect(onChange).toHaveBeenCalledWith(2);
});
it('should support formatter/parser/precision and press enter callback', () => {
const onPressEnter = vi.fn();
const { container } = render(
<InputNumber
defaultValue={1.236}
precision={2}
parser={(v) => (v ?? '').replace('$', '')}
formatter={(v) => `$${v}`}
onPressEnter={onPressEnter}
/>
);
const input = container.querySelector('input') as HTMLInputElement;
expect(input.value).toBe('$1.24');
fireEvent.change(input, { target: { value: '$2.345' } });
fireEvent.blur(input);
expect(input.value).toBe('$2.35');
fireEvent.keyDown(input, { key: 'Enter' });
expect(onPressEnter).toHaveBeenCalled();
});
it('keeps numeric editing and bounds when a compact input hides its stepper controls', () => {
const onChange = vi.fn();
const { getByRole, queryByRole } = render(
<InputNumber size="small" controls={false} defaultValue={12} min={0} max={19} onChange={onChange} />
);
const input = getByRole('textbox');
expect(queryByRole('button')).toBeNull();
fireEvent.change(input, { target: { value: '18' } });
fireEvent.keyDown(input, { key: 'ArrowUp' });
expect(input).toHaveValue('19');
fireEvent.keyDown(input, { key: 'ArrowUp' });
expect(input).toHaveValue('19');
fireEvent.keyDown(input, { key: 'ArrowDown' });
expect(onChange).toHaveBeenLastCalledWith(18);
});
it('should hide controls when controls is false and keep input disabled', () => {
const { container } = render(<InputNumber defaultValue={3} controls={false} disabled />);
expect(container.querySelector('[role="button"]')).toBeNull();
const input = container.querySelector('input') as HTMLInputElement;
expect(input).toBeDisabled();
});
it('should clear an optional value while keeping increment and decrement controls', () => {
const onLocalChange = vi.fn();
const { container } = render(<InputNumber allowClear allowEmpty defaultValue={3} onChange={onLocalChange} />);
const clearButton = container.querySelector('button') as HTMLButtonElement;
expect(clearButton).not.toBeNull();
expect(container.querySelectorAll('[role="button"]')).toHaveLength(2);
fireEvent.click(clearButton);
expect(onLocalChange).toHaveBeenCalledWith(null);
});
it('should support ref callback and object ref', () => {
const callbackRef = vi.fn();
const objectRef = { current: null as HTMLInputElement | null };
const { unmount } = render(<InputNumber defaultValue={1} ref={callbackRef} />);
expect(callbackRef).toHaveBeenCalled();
unmount();
render(<InputNumber defaultValue={2} ref={objectRef} />);
expect(objectRef.current).not.toBeNull();
});
it('handles invalid custom parser output', () => {
const onLocalChange = vi.fn();
const { container } = render(
<InputNumber
defaultValue={1}
parser={() => null as unknown as string}
onChange={onLocalChange}
/>
);
const input = container.querySelector('input') as HTMLInputElement;
fireEvent.change(input, { target: { value: '123' } });
expect(onLocalChange).toHaveBeenCalledWith(null);
});
it('strips nonnumeric characters before parsing', () => {
const onLocalChange = vi.fn();
const { container } = render(<InputNumber defaultValue={1} onChange={onLocalChange} />);
const input = container.querySelector('input') as HTMLInputElement;
fireEvent.change(input, { target: { value: '1e3' } });
expect(onLocalChange).toHaveBeenCalledWith(13);
fireEvent.blur(input);
expect(input.value).toBe('13');
});
it('should step from null value using min baseline and keep boundary', () => {
const onLocalChange = vi.fn();
const { container } = render(<InputNumber allowEmpty min={2} max={3} value={null} onChange={onLocalChange} />);
const [incBtn] = Array.from(container.querySelectorAll('[role="button"]')) as HTMLElement[];
fireEvent.click(incBtn);
expect(onLocalChange).toHaveBeenCalledWith(3);
fireEvent.click(incBtn);
expect(onLocalChange).toHaveBeenCalledTimes(1);
});
it('should handle NaN and clamp by min/max while typing', () => {
const onLocalChange = vi.fn();
const { container } = render(<InputNumber defaultValue={5} min={0} max={10} onChange={onLocalChange} />);
const input = container.querySelector('input') as HTMLInputElement;
fireEvent.change(input, { target: { value: '-' } });
expect(onLocalChange).toHaveBeenCalledWith(null);
fireEvent.change(input, { target: { value: '9999' } });
expect(onLocalChange).toHaveBeenCalledWith(10);
expect(input.value).toBe('10');
fireEvent.change(input, { target: { value: '-9999' } });
expect(onLocalChange).toHaveBeenCalledWith(0);
expect(input.value).toBe('0');
});
it('should restore last valid value on blur when current value is invalid', () => {
const onLocalChange = vi.fn();
const { container } = render(<InputNumber defaultValue={6} onChange={onLocalChange} />);
const input = container.querySelector('input') as HTMLInputElement;
fireEvent.change(input, { target: { value: '-' } });
fireEvent.blur(input);
expect(onLocalChange).toHaveBeenCalledWith(6);
expect(input.value).toBe('6');
});
it('should clamp out-of-range value on blur', () => {
const onLocalChange = vi.fn();
const { container } = render(<InputNumber defaultValue={1} min={0} max={10} onChange={onLocalChange} />);
const input = container.querySelector('input') as HTMLInputElement;
fireEvent.change(input, { target: { value: '100' } });
fireEvent.blur(input);
expect(onLocalChange).toHaveBeenCalledWith(10);
expect(input.value).toBe('10');
fireEvent.change(input, { target: { value: '-100' } });
fireEvent.blur(input);
expect(onLocalChange).toHaveBeenCalledWith(0);
expect(input.value).toBe('0');
});
it('should use last valid value as baseline when stepping from empty and support decrement click', () => {
const onLocalChange = vi.fn();
const { container } = render(<InputNumber allowEmpty defaultValue={2} onChange={onLocalChange} min={0} />);
const input = container.querySelector('input') as HTMLInputElement;
const [incBtn, decBtn] = Array.from(container.querySelectorAll('[role="button"]')) as HTMLElement[];
fireEvent.change(input, { target: { value: '' } });
fireEvent.click(incBtn);
expect(onLocalChange).toHaveBeenCalledWith(3);
fireEvent.click(decBtn);
expect(onLocalChange).toHaveBeenCalledWith(2);
});
it('should prevent interactions when disabled and prevent default on control mousedown', () => {
const onLocalChange = vi.fn();
const onKeyDown = vi.fn();
const { container } = render(<InputNumber defaultValue={2} disabled onChange={onLocalChange} onKeyDown={onKeyDown} />);
const input = container.querySelector('input') as HTMLInputElement;
const [incBtn, decBtn] = Array.from(container.querySelectorAll('[role="button"]')) as HTMLElement[];
const incMouseDown = new MouseEvent('mousedown', { bubbles: true, cancelable: true });
const decMouseDown = new MouseEvent('mousedown', { bubbles: true, cancelable: true });
expect(incBtn.dispatchEvent(incMouseDown)).toBe(false);
expect(decBtn.dispatchEvent(decMouseDown)).toBe(false);
fireEvent.keyDown(input, { key: 'ArrowUp' });
fireEvent.click(incBtn);
fireEvent.click(decBtn);
expect(onKeyDown).not.toHaveBeenCalled();
expect(onLocalChange).not.toHaveBeenCalled();
});
it('keeps labeled increment and decrement controls usable in RTL', () => {
const onLocalChange = vi.fn();
const { container } = render(
<ConfigProvider locale={enUS.design} mountContainer={document.body}>
<div dir="rtl">
<InputNumber defaultValue={1} onChange={onLocalChange} />
</div>
</ConfigProvider>
);
const incrementButton = container.querySelector(`[aria-label="${enUS.design.Accessibility.increment}"]`) as HTMLElement;
const decrementButton = container.querySelector(`[aria-label="${enUS.design.Accessibility.decrement}"]`) as HTMLElement;
fireEvent.click(incrementButton);
expect(onLocalChange).toHaveBeenCalledWith(2);
fireEvent.click(decrementButton);
expect(onLocalChange).toHaveBeenCalledWith(1);
});
});