|
| 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 { Component } from "@angular/core"; |
| 21 | +import { ComponentFixture, TestBed } from "@angular/core/testing"; |
| 22 | +import { FormControl, FormGroup, ReactiveFormsModule } from "@angular/forms"; |
| 23 | +import { By } from "@angular/platform-browser"; |
| 24 | +import { NoopAnimationsModule } from "@angular/platform-browser/animations"; |
| 25 | +import { FieldTypeConfig, FormlyFieldConfig, FormlyModule } from "@ngx-formly/core"; |
| 26 | +import { FormlyJsonschema } from "@ngx-formly/core/json-schema"; |
| 27 | +import { JSONSchema7 } from "json-schema"; |
| 28 | +import { LoopVariableInputComponent } from "./loop-variable-input.component"; |
| 29 | +import { |
| 30 | + applyLoopVariableField, |
| 31 | + LOOP_VARIABLE_INPUT_TYPE, |
| 32 | + primitiveSchemaType, |
| 33 | +} from "../../util/loop-variable-field.util"; |
| 34 | + |
| 35 | +/** A formly form around the fields a test builds, as the property panel hosts them. */ |
| 36 | +@Component({ |
| 37 | + template: `<form [formGroup]="form"> |
| 38 | + <formly-form |
| 39 | + [form]="form" |
| 40 | + [fields]="fields" |
| 41 | + [model]="model"></formly-form> |
| 42 | + </form>`, |
| 43 | + imports: [ReactiveFormsModule, FormlyModule], |
| 44 | +}) |
| 45 | +class FormHostComponent { |
| 46 | + form = new FormGroup({}); |
| 47 | + model: Record<string, unknown> = {}; |
| 48 | + fields: FormlyFieldConfig[] = []; |
| 49 | +} |
| 50 | + |
| 51 | +describe("LoopVariableInputComponent", () => { |
| 52 | + describe("the options it offers", () => { |
| 53 | + let component: LoopVariableInputComponent; |
| 54 | + |
| 55 | + beforeEach(async () => { |
| 56 | + await TestBed.configureTestingModule({ |
| 57 | + imports: [LoopVariableInputComponent, NoopAnimationsModule], |
| 58 | + }).compileComponents(); |
| 59 | + component = TestBed.createComponent(LoopVariableInputComponent).componentInstance; |
| 60 | + }); |
| 61 | + |
| 62 | + function holding(value: unknown, loopVariableOptions?: string[]): void { |
| 63 | + component.field = { |
| 64 | + formControl: new FormControl(value), |
| 65 | + props: loopVariableOptions === undefined ? {} : { loopVariableOptions }, |
| 66 | + } as FieldTypeConfig; |
| 67 | + } |
| 68 | + |
| 69 | + it("offers every variable while the box is empty", () => { |
| 70 | + holding("", ["$K", "$prev"]); |
| 71 | + expect(component.referenceOptions).toEqual(["$K", "$prev"]); |
| 72 | + holding(undefined, ["$K", "$prev"]); |
| 73 | + expect(component.referenceOptions).toEqual(["$K", "$prev"]); |
| 74 | + }); |
| 75 | + |
| 76 | + it("offers the variables matching a typed $ prefix, ignoring case", () => { |
| 77 | + holding("$p", ["$K", "$prev"]); |
| 78 | + expect(component.referenceOptions).toEqual(["$prev"]); |
| 79 | + holding("$P", ["$K", "$prev"]); |
| 80 | + expect(component.referenceOptions).toEqual(["$prev"]); |
| 81 | + holding("$", ["$K", "$prev"]); |
| 82 | + expect(component.referenceOptions).toEqual(["$K", "$prev"]); |
| 83 | + }); |
| 84 | + |
| 85 | + it("offers nothing while a plain value is typed, or when the block declares no variable", () => { |
| 86 | + holding("5", ["$K", "$prev"]); |
| 87 | + expect(component.referenceOptions).toEqual([]); |
| 88 | + holding(5, ["$K"]); |
| 89 | + expect(component.referenceOptions).toEqual([]); |
| 90 | + holding("", undefined); |
| 91 | + expect(component.referenceOptions).toEqual([]); |
| 92 | + }); |
| 93 | + }); |
| 94 | + |
| 95 | + // Rendered through formly, as the property panel renders it, so that the parser formly runs on every |
| 96 | + // keystroke and the value it writes back into the box are the real ones. |
| 97 | + describe("rendered in a form", () => { |
| 98 | + const schema: JSONSchema7 = { |
| 99 | + type: "object", |
| 100 | + properties: { |
| 101 | + limit: { type: "integer", title: "limit" }, |
| 102 | + fraction: { type: "number", title: "fraction" }, |
| 103 | + prefix: { type: "string", title: "prefix" }, |
| 104 | + }, |
| 105 | + }; |
| 106 | + |
| 107 | + let fixture: ComponentFixture<FormHostComponent>; |
| 108 | + let host: FormHostComponent; |
| 109 | + |
| 110 | + beforeEach(async () => { |
| 111 | + await TestBed.configureTestingModule({ |
| 112 | + imports: [ |
| 113 | + FormHostComponent, |
| 114 | + NoopAnimationsModule, |
| 115 | + FormlyModule.forRoot({ types: [{ name: LOOP_VARIABLE_INPUT_TYPE, component: LoopVariableInputComponent }] }), |
| 116 | + ], |
| 117 | + }).compileComponents(); |
| 118 | + fixture = TestBed.createComponent(FormHostComponent); |
| 119 | + host = fixture.componentInstance; |
| 120 | + // the property panel's mapping: every primitive property of an operator inside a block |
| 121 | + const root = TestBed.inject(FormlyJsonschema).toFieldConfig(schema, { |
| 122 | + map: (field: FormlyFieldConfig, source: JSONSchema7) => { |
| 123 | + const schemaType = primitiveSchemaType(source.type); |
| 124 | + if (schemaType !== undefined) { |
| 125 | + applyLoopVariableField(field, schemaType, ["K"]); |
| 126 | + field.props = { ...field.props, placeholder: `the ${field.key}`, attributes: { style: "color: red" } }; |
| 127 | + } |
| 128 | + return field; |
| 129 | + }, |
| 130 | + }); |
| 131 | + root.type = undefined; // render the properties as a plain group, without the object type |
| 132 | + host.fields = [root]; |
| 133 | + fixture.detectChanges(); |
| 134 | + await fixture.whenStable(); |
| 135 | + }); |
| 136 | + |
| 137 | + function input(key: string): HTMLInputElement { |
| 138 | + return fixture.debugElement.query(By.css(`input[placeholder="the ${key}"]`)).nativeElement; |
| 139 | + } |
| 140 | + |
| 141 | + /** Types the text one key at a time, letting the box redraw between keys as it does for a user. */ |
| 142 | + async function typeInto(key: string, text: string): Promise<void> { |
| 143 | + const box = input(key); |
| 144 | + for (const char of text) { |
| 145 | + box.value += char; |
| 146 | + box.dispatchEvent(new Event("input")); |
| 147 | + fixture.detectChanges(); |
| 148 | + await fixture.whenStable(); |
| 149 | + await Promise.resolve(); // the autocomplete writes a new value into the box in a microtask |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + it("renders one text box per field, carrying the field's attributes", () => { |
| 154 | + const box = input("limit"); |
| 155 | + expect(box.getAttribute("nz-input")).not.toBeNull(); |
| 156 | + expect(box.getAttribute("style")).toContain("color: red"); |
| 157 | + expect(box.id).not.toBe(""); |
| 158 | + }); |
| 159 | + |
| 160 | + it("stores a decimal typed one key at a time and keeps the box as typed", async () => { |
| 161 | + await typeInto("fraction", "1.05"); |
| 162 | + expect(host.model["fraction"]).toBe(1.05); |
| 163 | + expect(input("fraction").value).toBe("1.05"); |
| 164 | + }); |
| 165 | + |
| 166 | + it("keeps a leading minus and a leading zero while they are typed", async () => { |
| 167 | + await typeInto("fraction", "-0.5"); |
| 168 | + expect(host.model["fraction"]).toBe(-0.5); |
| 169 | + expect(input("fraction").value).toBe("-0.5"); |
| 170 | + }); |
| 171 | + |
| 172 | + it("keeps a numeral still being typed as text, flagged until it is complete", async () => { |
| 173 | + await typeInto("fraction", "7."); |
| 174 | + expect(input("fraction").value).toBe("7."); |
| 175 | + expect(host.model["fraction"]).toBe("7."); |
| 176 | + expect(host.form.get("fraction")?.hasError("type")).toBe(true); |
| 177 | + |
| 178 | + await typeInto("fraction", "5"); |
| 179 | + expect(host.model["fraction"]).toBe(7.5); |
| 180 | + expect(host.form.get("fraction")?.valid).toBe(true); |
| 181 | + }); |
| 182 | + |
| 183 | + it("stores an integer and a reference as typed", async () => { |
| 184 | + await typeInto("limit", "12"); |
| 185 | + expect(host.model["limit"]).toBe(12); |
| 186 | + expect(input("limit").value).toBe("12"); |
| 187 | + |
| 188 | + input("limit").value = ""; |
| 189 | + await typeInto("limit", "$K"); |
| 190 | + expect(host.model["limit"]).toBe("$K"); |
| 191 | + expect(input("limit").value).toBe("$K"); |
| 192 | + }); |
| 193 | + |
| 194 | + it("gives a string field its empty-string default", () => { |
| 195 | + expect(host.model["prefix"]).toBe(""); |
| 196 | + }); |
| 197 | + }); |
| 198 | +}); |
0 commit comments