|
| 1 | +/** |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +import { FormlyFieldConfig } from "@ngx-formly/core"; |
| 21 | +import { FormlyJsonschema } from "@ngx-formly/core/json-schema"; |
| 22 | +import { applySafeNumericParser, parseNumericInput } from "./numeric-input-parser.util"; |
| 23 | + |
| 24 | +describe("parseNumericInput", () => { |
| 25 | + it("returns the number for a numeric value", () => { |
| 26 | + expect(parseNumericInput(10)).toEqual(10); |
| 27 | + expect(parseNumericInput(0)).toEqual(0); |
| 28 | + expect(parseNumericInput(-5)).toEqual(-5); |
| 29 | + }); |
| 30 | + |
| 31 | + it("parses numeric strings", () => { |
| 32 | + expect(parseNumericInput("10")).toEqual(10); |
| 33 | + expect(parseNumericInput("-5")).toEqual(-5); |
| 34 | + expect(parseNumericInput("0")).toEqual(0); |
| 35 | + expect(parseNumericInput("3.5")).toEqual(3.5); |
| 36 | + }); |
| 37 | + |
| 38 | + it("treats empty / null / undefined as null", () => { |
| 39 | + expect(parseNumericInput("")).toBeNull(); |
| 40 | + expect(parseNumericInput(null)).toBeNull(); |
| 41 | + expect(parseNumericInput(undefined)).toBeNull(); |
| 42 | + }); |
| 43 | + |
| 44 | + it("returns null for non-numeric input instead of throwing", () => { |
| 45 | + expect(parseNumericInput("abc")).toBeNull(); |
| 46 | + expect(parseNumericInput("1a")).toBeNull(); |
| 47 | + expect(parseNumericInput({})).toBeNull(); |
| 48 | + }); |
| 49 | + |
| 50 | + it("preserves negative and zero values (does not clamp to 1)", () => { |
| 51 | + // Guards the reported bug: a negative limit must pass through unchanged here, |
| 52 | + // not be turned into 1 or dropped. |
| 53 | + expect(parseNumericInput(-100)).toEqual(-100); |
| 54 | + expect(parseNumericInput("-1")).toEqual(-1); |
| 55 | + }); |
| 56 | + |
| 57 | + it("parses negative decimals and whitespace-padded numbers", () => { |
| 58 | + expect(parseNumericInput("-3.5")).toEqual(-3.5); |
| 59 | + expect(parseNumericInput(" 10 ")).toEqual(10); |
| 60 | + }); |
| 61 | + |
| 62 | + it("treats whitespace-only strings as null", () => { |
| 63 | + expect(parseNumericInput(" ")).toBeNull(); |
| 64 | + expect(parseNumericInput(" ")).toBeNull(); |
| 65 | + expect(parseNumericInput("\t")).toBeNull(); |
| 66 | + }); |
| 67 | + |
| 68 | + it("rejects NaN and infinities as null", () => { |
| 69 | + expect(parseNumericInput(NaN)).toBeNull(); |
| 70 | + expect(parseNumericInput(Infinity)).toBeNull(); |
| 71 | + expect(parseNumericInput(-Infinity)).toBeNull(); |
| 72 | + expect(parseNumericInput("Infinity")).toBeNull(); |
| 73 | + }); |
| 74 | + |
| 75 | + it("rejects non-number/non-string values (boolean, array, object) as null", () => { |
| 76 | + expect(parseNumericInput(true)).toBeNull(); |
| 77 | + expect(parseNumericInput(false)).toBeNull(); |
| 78 | + expect(parseNumericInput([5])).toBeNull(); |
| 79 | + expect(parseNumericInput([1, 2])).toBeNull(); |
| 80 | + }); |
| 81 | + |
| 82 | + it("preserves large finite numbers", () => { |
| 83 | + expect(parseNumericInput(1_000_000_000)).toEqual(1_000_000_000); |
| 84 | + expect(parseNumericInput("2000000")).toEqual(2000000); |
| 85 | + }); |
| 86 | + |
| 87 | + it("never touches the DOM (safe to call without an element)", () => { |
| 88 | + // The whole point of the replacement: no document.querySelector / .validity access. |
| 89 | + expect(() => parseNumericInput("42")).not.toThrow(); |
| 90 | + expect(parseNumericInput("42")).toEqual(42); |
| 91 | + }); |
| 92 | +}); |
| 93 | + |
| 94 | +describe("applySafeNumericParser", () => { |
| 95 | + it("replaces a numeric field's existing (crash-prone) parser with the safe one", () => { |
| 96 | + // Mimics @ngx-formly's parser that throws on nz-input-number when the value is null. |
| 97 | + const crashingParser = () => { |
| 98 | + throw new TypeError("Cannot read properties of undefined (reading 'badInput')"); |
| 99 | + }; |
| 100 | + const field: FormlyFieldConfig = { type: "integer", parsers: [crashingParser] }; |
| 101 | + |
| 102 | + applySafeNumericParser(field); |
| 103 | + |
| 104 | + // the crash-prone parser is gone |
| 105 | + expect(field.parsers).toHaveLength(1); |
| 106 | + expect(field.parsers?.[0]).not.toBe(crashingParser); |
| 107 | + // the null intermediate state that used to crash is now handled safely |
| 108 | + expect(() => (field.parsers?.[0] as (v: unknown, f: FormlyFieldConfig) => unknown)(null, field)).not.toThrow(); |
| 109 | + expect((field.parsers?.[0] as (v: unknown, f: FormlyFieldConfig) => unknown)(null, field)).toBeNull(); |
| 110 | + // and it still converts real values |
| 111 | + expect((field.parsers?.[0] as (v: unknown, f: FormlyFieldConfig) => unknown)("10", field)).toEqual(10); |
| 112 | + expect((field.parsers?.[0] as (v: unknown, f: FormlyFieldConfig) => unknown)("-5", field)).toEqual(-5); |
| 113 | + }); |
| 114 | + |
| 115 | + it("replaces the parser for type 'number' too (not just 'integer')", () => { |
| 116 | + const field: FormlyFieldConfig = { |
| 117 | + type: "number", |
| 118 | + parsers: [ |
| 119 | + () => { |
| 120 | + throw new Error("original numeric parser should not run"); |
| 121 | + }, |
| 122 | + ], |
| 123 | + }; |
| 124 | + |
| 125 | + applySafeNumericParser(field); |
| 126 | + |
| 127 | + expect((field.parsers?.[0] as (v: unknown, f: FormlyFieldConfig) => unknown)("3.5", field)).toEqual(3.5); |
| 128 | + }); |
| 129 | + |
| 130 | + it("does NOT touch an enum (select) field's parser", () => { |
| 131 | + // Attribute selectors are 'enum' fields; they carry a (string) parser but render as a |
| 132 | + // select, not nz-input-number, so they must be left alone. |
| 133 | + const enumParser = (v: unknown) => v; |
| 134 | + const field: FormlyFieldConfig = { type: "enum", parsers: [enumParser] }; |
| 135 | + |
| 136 | + applySafeNumericParser(field); |
| 137 | + |
| 138 | + expect(field.parsers?.[0]).toBe(enumParser); |
| 139 | + }); |
| 140 | + |
| 141 | + it("does NOT touch a string field's parser (regression: text inputs must keep working)", () => { |
| 142 | + // FormlyJsonschema also sets a parser on string fields. Replacing it with the numeric |
| 143 | + // parser would turn typed text into null and break every text property across all |
| 144 | + // operators, so string fields must be left completely alone. |
| 145 | + const stringParser = (v: unknown) => v; |
| 146 | + const field: FormlyFieldConfig = { type: "string", parsers: [stringParser] }; |
| 147 | + |
| 148 | + applySafeNumericParser(field); |
| 149 | + |
| 150 | + expect(field.parsers).toHaveLength(1); |
| 151 | + expect(field.parsers?.[0]).toBe(stringParser); // exact same parser, untouched |
| 152 | + // typed text still passes through unchanged |
| 153 | + expect((field.parsers?.[0] as (v: unknown, f: FormlyFieldConfig) => unknown)("hello", field)).toEqual("hello"); |
| 154 | + }); |
| 155 | + |
| 156 | + it("leaves non-numeric fields (without parsers) untouched", () => { |
| 157 | + const field: FormlyFieldConfig = { type: "string" }; |
| 158 | + |
| 159 | + applySafeNumericParser(field); |
| 160 | + |
| 161 | + expect(field.parsers).toBeUndefined(); |
| 162 | + }); |
| 163 | + |
| 164 | + it("does not add parsers to a field that had none", () => { |
| 165 | + const field: FormlyFieldConfig = { key: "someBoolean", type: "boolean" }; |
| 166 | + |
| 167 | + applySafeNumericParser(field); |
| 168 | + |
| 169 | + expect(field.parsers).toBeUndefined(); |
| 170 | + }); |
| 171 | + |
| 172 | + it("the replacement parser treats the clear-field cases ('' and null) as null", () => { |
| 173 | + const field: FormlyFieldConfig = { |
| 174 | + type: "integer", |
| 175 | + parsers: [ |
| 176 | + () => { |
| 177 | + throw new Error("original parser should not run"); |
| 178 | + }, |
| 179 | + ], |
| 180 | + }; |
| 181 | + |
| 182 | + applySafeNumericParser(field); |
| 183 | + const parser = field.parsers?.[0] as (v: unknown, f: FormlyFieldConfig) => unknown; |
| 184 | + |
| 185 | + expect(parser("", field)).toBeNull(); |
| 186 | + expect(parser(null, field)).toBeNull(); |
| 187 | + }); |
| 188 | + |
| 189 | + it("collapses multiple existing parsers into a single safe one", () => { |
| 190 | + const field: FormlyFieldConfig = { type: "integer", parsers: [() => 1, () => 2] }; |
| 191 | + |
| 192 | + applySafeNumericParser(field); |
| 193 | + |
| 194 | + expect(field.parsers).toHaveLength(1); |
| 195 | + expect((field.parsers?.[0] as (v: unknown, f: FormlyFieldConfig) => unknown)("7", field)).toEqual(7); |
| 196 | + }); |
| 197 | + |
| 198 | + it("leaves an empty parsers array untouched (nothing to replace)", () => { |
| 199 | + const field: FormlyFieldConfig = { type: "integer", parsers: [] }; |
| 200 | + |
| 201 | + applySafeNumericParser(field); |
| 202 | + |
| 203 | + expect(field.parsers).toEqual([]); |
| 204 | + }); |
| 205 | +}); |
| 206 | + |
| 207 | +describe("applySafeNumericParser with the real FormlyJsonschema (integration)", () => { |
| 208 | + // Uses the actual @ngx-formly/core json-schema conversion (not hand-built fields) to |
| 209 | + // prove, against real library behavior, that only numeric fields are affected. This is |
| 210 | + // the exact concern behind the earlier "all operators can't input values" regression: |
| 211 | + // FormlyJsonschema sets a parser on BOTH string and number fields, so the guard must |
| 212 | + // touch only the numeric one. |
| 213 | + const jsonSchema = new FormlyJsonschema(); |
| 214 | + |
| 215 | + it("replaces a real integer field's parser but leaves a real string field's parser intact", () => { |
| 216 | + const stringField = jsonSchema.toFieldConfig({ type: "string" }); |
| 217 | + const integerField = jsonSchema.toFieldConfig({ type: "integer" }); |
| 218 | + |
| 219 | + // premise: the library really does set parsers on both, and the types are as expected |
| 220 | + expect(stringField.type).toEqual("string"); |
| 221 | + expect(integerField.type).toEqual("integer"); |
| 222 | + expect((stringField.parsers ?? []).length).toBeGreaterThan(0); |
| 223 | + expect((integerField.parsers ?? []).length).toBeGreaterThan(0); |
| 224 | + |
| 225 | + const originalStringParser = stringField.parsers![0]; |
| 226 | + |
| 227 | + applySafeNumericParser(stringField); |
| 228 | + applySafeNumericParser(integerField); |
| 229 | + |
| 230 | + // the string field is completely untouched → text still passes through unchanged, |
| 231 | + // i.e. every text property on every operator keeps working |
| 232 | + expect(stringField.parsers![0]).toBe(originalStringParser); |
| 233 | + expect( |
| 234 | + (stringField.parsers![0] as (v: unknown, f: FormlyFieldConfig) => unknown)("hello world", stringField) |
| 235 | + ).toEqual("hello world"); |
| 236 | + |
| 237 | + // the integer field is switched to the safe numeric parser |
| 238 | + expect((integerField.parsers![0] as (v: unknown, f: FormlyFieldConfig) => unknown)("10", integerField)).toEqual(10); |
| 239 | + expect((integerField.parsers![0] as (v: unknown, f: FormlyFieldConfig) => unknown)(null, integerField)).toBeNull(); |
| 240 | + }); |
| 241 | + |
| 242 | + it("leaves a real string field WITH an enum (attribute-selector style) intact", () => { |
| 243 | + // An attribute selector is a string schema with an enum → the library makes it type |
| 244 | + // 'enum'; its parser (set during the string phase) must not be swapped. |
| 245 | + const enumField = jsonSchema.toFieldConfig({ type: "string", enum: ["a", "b", "c"] }); |
| 246 | + expect(enumField.type).toEqual("enum"); |
| 247 | + const originalParser = enumField.parsers?.[0]; |
| 248 | + |
| 249 | + applySafeNumericParser(enumField); |
| 250 | + |
| 251 | + expect(enumField.parsers?.[0]).toBe(originalParser); |
| 252 | + }); |
| 253 | +}); |
0 commit comments