(null);
const defaultProps = {
schema: {
type: "object",
properties: {
nested: { oneOf: [{ type: "string" }, { type: "integer" }] },
},
} as unknown as JsonSchemaType,
value: { nested: "test value" },
onChange: jest.fn(),
ref: formRef,
};
return (
);
};
return render();
};
describe("validateJson method", () => {
it("should return valid for form mode", () => {
const simpleSchema = {
type: "string" as const,
description: "Test string field",
};
const TestComponent = () => {
const formRef = useRef(null);
return (
);
};
render();
const validateButton = screen.getByTestId("validate-button");
fireEvent.click(validateButton);
expect(validateButton.getAttribute('data-validation-valid')).toBe('true');
expect(validateButton.getAttribute('data-validation-error')).toBe('');
});
it("should return valid for valid JSON in JSON mode", () => {
renderFormWithRef();
const validateButton = screen.getByTestId("validate-button");
fireEvent.click(validateButton);
expect(validateButton.getAttribute('data-validation-valid')).toBe('true');
expect(validateButton.getAttribute('data-validation-error')).toBe('');
});
it("should return invalid for malformed JSON in JSON mode", async () => {
renderFormWithRef();
// Enter invalid JSON
const textarea = screen.getByRole("textbox");
fireEvent.change(textarea, { target: { value: '{ "invalid": json }' } });
// Wait a bit for any debounced updates
await waitFor(() => {
const validateButton = screen.getByTestId("validate-button");
fireEvent.click(validateButton);
expect(validateButton.getAttribute('data-validation-valid')).toBe('false');
expect(validateButton.getAttribute('data-validation-error')).toContain('JSON');
});
});
it("should return valid for empty JSON in JSON mode", () => {
renderFormWithRef();
// Clear the textarea
const textarea = screen.getByRole("textbox");
fireEvent.change(textarea, { target: { value: '' } });
const validateButton = screen.getByTestId("validate-button");
fireEvent.click(validateButton);
expect(validateButton.getAttribute('data-validation-valid')).toBe('true');
expect(validateButton.getAttribute('data-validation-error')).toBe('');
});
it("should set error state when validation fails", async () => {
renderFormWithRef();
// Enter invalid JSON
const textarea = screen.getByRole("textbox");
fireEvent.change(textarea, { target: { value: '{ "trailing": "comma", }' } });
// Trigger validation
const validateButton = screen.getByTestId("validate-button");
fireEvent.click(validateButton);
// Check that validation result shows error
expect(validateButton.getAttribute('data-validation-valid')).toBe('false');
expect(validateButton.getAttribute('data-validation-error')).toContain('JSON');
});
});
describe("forwardRef functionality", () => {
it("should expose validateJson method through ref", () => {
const TestComponent = () => {
const formRef = useRef(null);
return (
);
};
render();
const testButton = screen.getByTestId("ref-test-button");
fireEvent.click(testButton);
expect(testButton.getAttribute('data-has-validate-method')).toBe('true');
});
});
});