|
| 1 | +import { render, screen, fireEvent } from '@testing-library/react'; |
| 2 | +import { describe, it, expect, jest } from '@jest/globals'; |
| 3 | +import DynamicJsonForm from '../DynamicJsonForm'; |
| 4 | +import type { JsonSchemaType } from '../DynamicJsonForm'; |
| 5 | + |
| 6 | +describe('DynamicJsonForm Integer Fields', () => { |
| 7 | + const renderForm = (props = {}) => { |
| 8 | + const defaultProps = { |
| 9 | + schema: { |
| 10 | + type: "integer" as const, |
| 11 | + description: "Test integer field" |
| 12 | + } satisfies JsonSchemaType, |
| 13 | + value: undefined, |
| 14 | + onChange: jest.fn() |
| 15 | + }; |
| 16 | + return render(<DynamicJsonForm {...defaultProps} {...props} />); |
| 17 | + }; |
| 18 | + |
| 19 | + describe('Basic Operations', () => { |
| 20 | + it('should render number input with step=1', () => { |
| 21 | + renderForm(); |
| 22 | + const input = screen.getByRole('spinbutton'); |
| 23 | + expect(input).toHaveProperty('type', 'number'); |
| 24 | + expect(input).toHaveProperty('step', '1'); |
| 25 | + }); |
| 26 | + |
| 27 | + it('should pass integer values to onChange', () => { |
| 28 | + const onChange = jest.fn(); |
| 29 | + renderForm({ onChange }); |
| 30 | + |
| 31 | + const input = screen.getByRole('spinbutton'); |
| 32 | + fireEvent.change(input, { target: { value: '42' } }); |
| 33 | + |
| 34 | + expect(onChange).toHaveBeenCalledWith(42); |
| 35 | + // Verify the value is a number, not a string |
| 36 | + expect(typeof onChange.mock.calls[0][0]).toBe('number'); |
| 37 | + }); |
| 38 | + |
| 39 | + it('should not pass string values to onChange', () => { |
| 40 | + const onChange = jest.fn(); |
| 41 | + renderForm({ onChange }); |
| 42 | + |
| 43 | + const input = screen.getByRole('spinbutton'); |
| 44 | + fireEvent.change(input, { target: { value: 'abc' } }); |
| 45 | + |
| 46 | + expect(onChange).not.toHaveBeenCalled(); |
| 47 | + }); |
| 48 | + }); |
| 49 | + |
| 50 | + describe('Edge Cases', () => { |
| 51 | + it('should handle non-numeric input by not calling onChange', () => { |
| 52 | + const onChange = jest.fn(); |
| 53 | + renderForm({ onChange }); |
| 54 | + |
| 55 | + const input = screen.getByRole('spinbutton'); |
| 56 | + fireEvent.change(input, { target: { value: 'abc' } }); |
| 57 | + |
| 58 | + expect(onChange).not.toHaveBeenCalled(); |
| 59 | + }); |
| 60 | + }); |
| 61 | +}); |
0 commit comments