Skip to content

Commit

Permalink
Add new tests and get all passing
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanFarmer committed Mar 8, 2024
1 parent 6e9a725 commit 54a9226
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 235 deletions.
13 changes: 8 additions & 5 deletions src/antd/InputNumber.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,17 @@ export const InputNumber = (props: InputNumberProps): InputNumber => {
(typeof numberType === "string" && numberType === "integer")
|| (Array.isArray(numberType) && numberType.includes("integer"))
) {
props.handleChange(props.path, value !== null ? coerceToInteger(value) : value)
props.handleChange(props.path, coerceToInteger(value))
} else {
props.handleChange(props.path, value !== null ? coerceToNumber(value) : value)
props.handleChange(props.path, coerceToNumber(value))
}
} else {
props.handleChange(props.path, value)
}
}

const min = schema.properties?.minimum as number | undefined
const max = schema.properties?.maximum as number | undefined
const min = schema.minimum
const max = schema.maximum

const defaultValue = typeof schema?.default === "number" ? schema.default : undefined

Expand All @@ -38,7 +40,8 @@ export const InputNumber = (props: InputNumberProps): InputNumber => {
const addonBefore = props.uischema.options?.addonBefore as string | undefined
const isPercentage = addonAfter?.trim() === "%"

const style = { marginLeft: 0, width: "100%" }
const marginLeft = min !== undefined && max !== undefined ? 16 : 0
const style = { marginLeft: marginLeft, width: "100%" }
const formatter = ((value?: number) => {
if (typeof value !== "undefined") {
if (isPercentage) {
Expand Down
21 changes: 21 additions & 0 deletions src/controls/NumericControls/NumericControl.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import {
numericWeightSchema,
numericUISchema,
numericUISchemaWithRule,
numericPriceSchema,
numericUSDUISchema,
} from "../../testSchemas/numericSchema/numericSchema"


Expand Down Expand Up @@ -113,4 +115,23 @@ describe("NumericControl", () => {
expect(data).toEqual({ numericValue: 123 })
})
})

it ("shows error message onBlur when field is required and empty", async () => {
render({
schema: numericTheNumberSchema,
uischema: numericUISchema,
})
const input = screen.getByRole("spinbutton")
await userEvent.clear(input)
await userEvent.tab()
expect(await screen.findByText("The Number is required")).not.toBeNull()
})

it ("shows units next to text input if set in UI schema", async () => {
render({
schema: numericPriceSchema,
uischema: numericUSDUISchema,
})
expect(await screen.findByText("$")).not.toBeNull()
})
})
2 changes: 1 addition & 1 deletion src/controls/NumericControls/NumericControl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const NumericControl = (props: ControlProps & RendererProps) => {
const initialValue = typeof props.schema.default === "number" ? props.schema.default : undefined

const rules: Rule[] = [
{ message: props.required ? `${props.label} is required` : "" },
{ required: true, message: `${props.label} is required` },
]

return (
Expand Down
15 changes: 14 additions & 1 deletion src/controls/NumericControls/NumericSliderControl.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import { render } from "../../common/test-render"
import { JSONSchema } from "json-schema-to-ts"
import {
numericSliderBasisPointsSchema,
numericSliderFinalGradeSchema,
numericSliderUISchema,
numericSliderFinalGradeSchema,
numericSliderPercentageUISchema,
numericSliderUISchemaWithRule,
} from "../../testSchemas/numericSchema/numericSliderSchema"

Expand Down Expand Up @@ -82,4 +83,16 @@ describe("NumericSliderControl", () => {
expect(data).toEqual({ numericRangeValue: 42.00 })
})
})

it ("shows units in tooltip if set in UI schema", async () => {
render({
schema: numericSliderFinalGradeSchema,
uischema: numericSliderPercentageUISchema,
})
const slider = screen.getByRole("slider")
expect(screen.queryByText("50%")).toBeNull()
await userEvent.hover(slider)

expect(screen.queryByText("50%")).not.toBeNull()
})
})
2 changes: 1 addition & 1 deletion src/controls/NumericControls/NumericSliderControl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export const NumericSliderControl = (props: ControlProps & RendererProps) => {
const initialValue = typeof props.schema.default === "number" ? props.schema.default : undefined

const rules: Rule[] = [
{ message: props.required ? `${props.label} is required` : "" },
{ required: true, message: `${props.label} is required` },
]

return (
Expand Down
218 changes: 0 additions & 218 deletions src/testSchemas/numberSchema.ts

This file was deleted.

4 changes: 3 additions & 1 deletion src/testSchemas/numericSchema/numericSliderSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export const numericSliderTemperatureSchema = {
minimum: -50,
maximum: 150,
multipleOf: 1,
default: 70,
},
},
required: ["numericRangeValue"],
Expand Down Expand Up @@ -67,6 +68,7 @@ export const numericSliderFinalGradeSchema = {
type: "number",
minimum: 0.0,
maximum: 1.0,
multipleOf: 0.01,
default: 0.5,
},
},
Expand All @@ -82,8 +84,8 @@ export const numericSliderDonateNowSchema = {
title: "Donate Now",
minimum: 5.00,
maximum: 1000.00,
default: 20.00,
multipleOf: 5.00,
default: 20.00,
}
},
required: ["numericRangeValue"],
Expand Down
4 changes: 3 additions & 1 deletion tsconfig.cjs.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
"compilerOptions": {
"lib": [
"ES6",
"DOM"
"ES2020",
"DOM",
"DOM.Iterable",
],
"target": "ES6",
"module": "CommonJS",
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.esm.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"compilerOptions": {
"lib": [
"ES2022",
"DOM"
"DOM",
"DOM.Iterable",
],
"target": "ES2022",
"module": "NodeNext",
Expand Down
6 changes: 0 additions & 6 deletions tsconfig.production.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,7 @@
"declarationMap": true,
"allowSyntheticDefaultImports": true,

"target": "ES2020",
"useDefineForClassFields": true,
"lib": [
"ES2020",
"DOM",
"DOM.Iterable"
],
"module": "ESNext",
"skipLibCheck": true,
"esModuleInterop": true,
Expand Down

0 comments on commit 54a9226

Please sign in to comment.