|
| 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 { TestBed } from "@angular/core/testing"; |
| 22 | +import { ReactiveFormsModule, UntypedFormGroup } from "@angular/forms"; |
| 23 | +import { FieldType, FieldTypeConfig, FormlyFieldConfig, FormlyForm, FormlyModule } from "@ngx-formly/core"; |
| 24 | +import { NzInputModule } from "ng-zorro-antd/input"; |
| 25 | +import { NotificationService } from "../../../common/service/notification/notification.service"; |
| 26 | +import { UiUdfParametersSyncService } from "../../service/code-editor/ui-udf-parameters-sync.service"; |
| 27 | +import { WorkflowActionService } from "../../service/workflow-graph/model/workflow-action.service"; |
| 28 | +import { UiUdfParametersComponent } from "./ui-udf-parameters.component"; |
| 29 | + |
| 30 | +/** |
| 31 | + * The cell formly builds a string column into: an nz-input bound to the control, as in the app. |
| 32 | + * nz-input paints its disabled attribute from the control's status changes, so a lock that the |
| 33 | + * control carries silently is not one the reader sees. |
| 34 | + */ |
| 35 | +@Component({ |
| 36 | + template: '<input nz-input [formControl]="formControl" />', |
| 37 | + imports: [ReactiveFormsModule, NzInputModule], |
| 38 | +}) |
| 39 | +class InputCellType extends FieldType<FieldTypeConfig> {} |
| 40 | + |
| 41 | +/** A form holding one parameter table, with the builder row template formly-jsonschema produces. */ |
| 42 | +@Component({ |
| 43 | + template: |
| 44 | + '<form [formGroup]="form"><formly-form [form]="form" [model]="model" [fields]="fields"></formly-form></form>', |
| 45 | + imports: [ReactiveFormsModule, FormlyForm], |
| 46 | +}) |
| 47 | +class HostComponent { |
| 48 | + form = new UntypedFormGroup({}); |
| 49 | + model = { |
| 50 | + uiParameters: [ |
| 51 | + { value: "0.5", attribute: { attributeName: "threshold", attributeType: "double" } }, |
| 52 | + { value: "high", attribute: { attributeName: "label", attributeType: "string" } }, |
| 53 | + ], |
| 54 | + }; |
| 55 | + fields: FormlyFieldConfig[] = [ |
| 56 | + { |
| 57 | + key: "uiParameters", |
| 58 | + type: "ui-udf-parameters", |
| 59 | + fieldArray: () => ({ |
| 60 | + fieldGroup: [ |
| 61 | + { key: "value", type: "plain" }, |
| 62 | + { |
| 63 | + key: "attribute", |
| 64 | + fieldGroup: [ |
| 65 | + { key: "attributeName", type: "plain" }, |
| 66 | + { key: "attributeType", type: "plain" }, |
| 67 | + ], |
| 68 | + }, |
| 69 | + ], |
| 70 | + }), |
| 71 | + }, |
| 72 | + ]; |
| 73 | +} |
| 74 | + |
| 75 | +/** |
| 76 | + * Real formly, no mocks of it: the operator property panel enables and disables its whole form |
| 77 | + * group (setInteractivity), and formly mirrors a control's state into props.disabled, so the |
| 78 | + * widget's lock on the Name and Type columns has to survive a wholesale enable(). The widget's own |
| 79 | + * spec drives populate on hand-made rows; this one proves the lock under formly's build. |
| 80 | + */ |
| 81 | +describe("UiUdfParametersComponent under real formly", () => { |
| 82 | + /** The canvas's modification flag, which the Form View holds off outside edit mode. */ |
| 83 | + let modificationEnabled = true; |
| 84 | + |
| 85 | + beforeEach(async () => { |
| 86 | + modificationEnabled = true; |
| 87 | + await TestBed.configureTestingModule({ |
| 88 | + imports: [ |
| 89 | + HostComponent, |
| 90 | + FormlyModule.forRoot({ |
| 91 | + types: [ |
| 92 | + { name: "ui-udf-parameters", component: UiUdfParametersComponent }, |
| 93 | + { name: "plain", component: InputCellType }, |
| 94 | + ], |
| 95 | + }), |
| 96 | + ], |
| 97 | + providers: [ |
| 98 | + { |
| 99 | + provide: WorkflowActionService, |
| 100 | + useValue: { |
| 101 | + checkWorkflowModificationEnabled: () => modificationEnabled, |
| 102 | + getJointGraphWrapper: () => ({ getCurrentHighlightedOperatorIDs: () => ["op-1"] }), |
| 103 | + }, |
| 104 | + }, |
| 105 | + { provide: UiUdfParametersSyncService, useValue: { addParameter: vi.fn() } }, |
| 106 | + { provide: NotificationService, useValue: { error: vi.fn() } }, |
| 107 | + ], |
| 108 | + }).compileComponents(); |
| 109 | + }); |
| 110 | + |
| 111 | + /** |
| 112 | + * Renders the host; `topProps` go on the table's field (the Form View puts its marks there), `rows` |
| 113 | + * replace the two parameters the model declares by default. |
| 114 | + */ |
| 115 | + function render(topProps?: Record<string, unknown>, rows?: unknown[]) { |
| 116 | + const fixture = TestBed.createComponent(HostComponent); |
| 117 | + if (topProps) { |
| 118 | + fixture.componentInstance.fields[0].props = topProps; |
| 119 | + } |
| 120 | + if (rows) { |
| 121 | + fixture.componentInstance.model = { uiParameters: rows as HostComponent["model"]["uiParameters"] }; |
| 122 | + } |
| 123 | + fixture.detectChanges(); |
| 124 | + const form = fixture.componentInstance.form; |
| 125 | + const cell = (row: number, path: string) => form.get(`uiParameters.${row}.${path}`)!; |
| 126 | + const locks = () => [ |
| 127 | + cell(0, "value").disabled, |
| 128 | + cell(0, "attribute.attributeName").disabled, |
| 129 | + cell(0, "attribute.attributeType").disabled, |
| 130 | + cell(1, "attribute.attributeName").disabled, |
| 131 | + ]; |
| 132 | + return { fixture, form, locks }; |
| 133 | + } |
| 134 | + |
| 135 | + it("builds the rows with the Name and Type cells locked and the Value cell editable", () => { |
| 136 | + const { locks } = render(); |
| 137 | + |
| 138 | + expect(locks()).toEqual([false, true, true, true]); |
| 139 | + }); |
| 140 | + |
| 141 | + const addButton = (fixture: { nativeElement: HTMLElement }) => |
| 142 | + fixture.nativeElement.querySelector(".add-parameter-button"); |
| 143 | + |
| 144 | + it("offers Add parameter on a writer's Form View card without the canvas's flag, and not on a reader's, whose field is disabled", () => { |
| 145 | + modificationEnabled = false; |
| 146 | + |
| 147 | + const writer = render({ operatorID: "op-form" }); |
| 148 | + expect(addButton(writer.fixture)).not.toBeNull(); |
| 149 | + |
| 150 | + const reader = render({ operatorID: "op-form", disabled: true }); |
| 151 | + expect(reader.locks()).toEqual([true, true, true, true]); |
| 152 | + expect(addButton(reader.fixture)).toBeNull(); |
| 153 | + }); |
| 154 | + |
| 155 | + // formly disables the controls it builds under a disabled field, not the array itself, and an |
| 156 | + // array with no rows reads as enabled; the form's mark on the field is what says "a reader's". |
| 157 | + it("offers Add parameter to a writer whose UDF declares no parameters yet, and not to a reader, though the empty array control is enabled", () => { |
| 158 | + modificationEnabled = false; |
| 159 | + |
| 160 | + const writer = render({ operatorID: "op-form" }, []); |
| 161 | + expect(writer.form.get("uiParameters")!.enabled).toBe(true); |
| 162 | + expect(addButton(writer.fixture)).not.toBeNull(); |
| 163 | + |
| 164 | + const reader = render({ operatorID: "op-form", disabled: true }, []); |
| 165 | + expect(reader.form.get("uiParameters")!.enabled).toBe(true); |
| 166 | + expect(addButton(reader.fixture)).toBeNull(); |
| 167 | + }); |
| 168 | + |
| 169 | + // The lock goes back on in the microtask after the host's enable() (see keepLocked), so the |
| 170 | + // assertions wait one; what the reader sees is read from the inputs, not only the controls. |
| 171 | + const settled = async (fixture: { detectChanges(): void }) => { |
| 172 | + await Promise.resolve(); |
| 173 | + fixture.detectChanges(); |
| 174 | + }; |
| 175 | + const inputsDisabled = (fixture: { nativeElement: HTMLElement }) => |
| 176 | + Array.from(fixture.nativeElement.querySelectorAll<HTMLInputElement>(".ui-udf-parameter-row:not(.header) input")) |
| 177 | + .slice(0, 3) |
| 178 | + .map(input => input.disabled); |
| 179 | + |
| 180 | + it("keeps the Name and Type cells locked when the host enables the whole form group, as the panel does", async () => { |
| 181 | + const { fixture, form, locks } = render(); |
| 182 | + |
| 183 | + form.enable(); |
| 184 | + await settled(fixture); |
| 185 | + |
| 186 | + expect(locks()).toEqual([false, true, true, true]); |
| 187 | + expect(inputsDisabled(fixture)).toEqual([false, true, true]); |
| 188 | + }); |
| 189 | + |
| 190 | + it("locks everything for a read-only host and gives only the Value cell back on enable", async () => { |
| 191 | + const { fixture, form, locks } = render(); |
| 192 | + |
| 193 | + form.disable(); |
| 194 | + await settled(fixture); |
| 195 | + expect(locks()).toEqual([true, true, true, true]); |
| 196 | + expect(inputsDisabled(fixture)).toEqual([true, true, true]); |
| 197 | + |
| 198 | + form.enable(); |
| 199 | + await settled(fixture); |
| 200 | + expect(locks()).toEqual([false, true, true, true]); |
| 201 | + expect(inputsDisabled(fixture)).toEqual([false, true, true]); |
| 202 | + }); |
| 203 | +}); |
0 commit comments