|
| 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 String Fields", () => { |
| 7 | + const renderForm = (props = {}) => { |
| 8 | + const defaultProps = { |
| 9 | + schema: { |
| 10 | + type: "string" as const, |
| 11 | + description: "Test string field", |
| 12 | + } satisfies JsonSchemaType, |
| 13 | + value: undefined, |
| 14 | + onChange: jest.fn(), |
| 15 | + }; |
| 16 | + return render(<DynamicJsonForm {...defaultProps} {...props} />); |
| 17 | + }; |
| 18 | + |
| 19 | + describe("Type Validation", () => { |
| 20 | + it("should handle numeric input as string type", () => { |
| 21 | + const onChange = jest.fn(); |
| 22 | + renderForm({ onChange }); |
| 23 | + |
| 24 | + const input = screen.getByRole("textbox"); |
| 25 | + fireEvent.change(input, { target: { value: "123321" } }); |
| 26 | + |
| 27 | + expect(onChange).toHaveBeenCalledWith("123321"); |
| 28 | + // Verify the value is a string, not a number |
| 29 | + expect(typeof onChange.mock.calls[0][0]).toBe("string"); |
| 30 | + }); |
| 31 | + |
| 32 | + it("should render as text input, not number input", () => { |
| 33 | + renderForm(); |
| 34 | + const input = screen.getByRole("textbox"); |
| 35 | + expect(input).toHaveProperty("type", "text"); |
| 36 | + }); |
| 37 | + }); |
| 38 | +}); |
| 39 | + |
| 40 | +describe("DynamicJsonForm Integer Fields", () => { |
| 41 | + const renderForm = (props = {}) => { |
| 42 | + const defaultProps = { |
| 43 | + schema: { |
| 44 | + type: "integer" as const, |
| 45 | + description: "Test integer field", |
| 46 | + } satisfies JsonSchemaType, |
| 47 | + value: undefined, |
| 48 | + onChange: jest.fn(), |
| 49 | + }; |
| 50 | + return render(<DynamicJsonForm {...defaultProps} {...props} />); |
| 51 | + }; |
| 52 | + |
| 53 | + describe("Basic Operations", () => { |
| 54 | + it("should render number input with step=1", () => { |
| 55 | + renderForm(); |
| 56 | + const input = screen.getByRole("spinbutton"); |
| 57 | + expect(input).toHaveProperty("type", "number"); |
| 58 | + expect(input).toHaveProperty("step", "1"); |
| 59 | + }); |
| 60 | + |
| 61 | + it("should pass integer values to onChange", () => { |
| 62 | + const onChange = jest.fn(); |
| 63 | + renderForm({ onChange }); |
| 64 | + |
| 65 | + const input = screen.getByRole("spinbutton"); |
| 66 | + fireEvent.change(input, { target: { value: "42" } }); |
| 67 | + |
| 68 | + expect(onChange).toHaveBeenCalledWith(42); |
| 69 | + // Verify the value is a number, not a string |
| 70 | + expect(typeof onChange.mock.calls[0][0]).toBe("number"); |
| 71 | + }); |
| 72 | + |
| 73 | + it("should not pass string values to onChange", () => { |
| 74 | + const onChange = jest.fn(); |
| 75 | + renderForm({ onChange }); |
| 76 | + |
| 77 | + const input = screen.getByRole("spinbutton"); |
| 78 | + fireEvent.change(input, { target: { value: "abc" } }); |
| 79 | + |
| 80 | + expect(onChange).not.toHaveBeenCalled(); |
| 81 | + }); |
| 82 | + }); |
| 83 | + |
| 84 | + describe("Edge Cases", () => { |
| 85 | + it("should handle non-numeric input by not calling onChange", () => { |
| 86 | + const onChange = jest.fn(); |
| 87 | + renderForm({ onChange }); |
| 88 | + |
| 89 | + const input = screen.getByRole("spinbutton"); |
| 90 | + fireEvent.change(input, { target: { value: "abc" } }); |
| 91 | + |
| 92 | + expect(onChange).not.toHaveBeenCalled(); |
| 93 | + }); |
| 94 | + }); |
| 95 | +}); |
0 commit comments