Skip to content

Commit

Permalink
Add new test for integer rounding
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanFarmer committed Mar 13, 2024
1 parent 8ed9805 commit ac3dcd3
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/antd/InputNumber.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const InputNumber = (props: InputNumberProps): InputNumber => {
const value = props.data === undefined || isDataEmptyObj ? defaultValue : props.data as number

const numberType = schema.type
const isInteger = typeof numberType === "string" && numberType === "integer"
const isInteger = (typeof numberType === "string" && numberType === "integer") || (Array.isArray(numberType) && numberType.length === 1 && numberType.includes("integer"))
const handleChange = (value: number | null) => {
if (typeof value === "number") {
if (isInteger) {
Expand Down
20 changes: 18 additions & 2 deletions src/controls/NumericControls/NumericControl.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {
numericMagnitudeSchema,
numericTheNumberSchema,
numericWeightSchema,
numericSheepSchema,
numericBeansSchema,
numericUISchema,
numericUISchemaWithRule,
numericPriceSchema,
Expand Down Expand Up @@ -93,9 +95,9 @@ describe("NumericControl", () => {
expect(screen.getByRole("spinbutton")).toHaveValue(`${dataVal}`)
})

it.each([[numericTheNumberSchema]])("renders default value when no data is provided", (schema: JSONSchema) => {
it("renders default value when no data is provided", () => {
render({
schema: schema,
schema: numericTheNumberSchema,
uischema: numericUISchema,
})
expect(screen.getByRole("spinbutton")).toHaveValue("42.42")
Expand Down Expand Up @@ -137,4 +139,18 @@ describe("NumericControl", () => {
})
await screen.findByText("$")
})

it.each([
numericSheepSchema,
numericBeansSchema,
])("is treated as an integer if the schema type is integer or the type is an array with only integer", async (schema: JSONSchema) => {
render({
schema: schema,
uischema: numericUISchema,
})
const input = screen.getByRole("spinbutton")
await userEvent.type(input, "123.45") // try to input a float
await userEvent.tab()
expect(input).toHaveValue("123") // it should be rounded to an integer
})
})
11 changes: 11 additions & 0 deletions src/testSchemas/numericSchema/numericSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,17 @@ export const numericSheepSchema = {
required: ["numericValue"],
} satisfies JSONSchema

export const numericBeansSchema = {
type: "object",
properties: {
numericValue: {
title: "Beans",
type: ["integer"],
},
},
required: ["numericValue"],
} satisfies JSONSchema

export const numericROISchema = {
type: "object",
properties: {
Expand Down

0 comments on commit ac3dcd3

Please sign in to comment.