This is an automated email from the ASF dual-hosted git repository. github-merge-queue[bot] pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/texera.git
commit 586949232445df31dffe510f7f7492ced2810684 Author: lie18uci <[email protected]> AuthorDate: Tue Jun 16 13:51:34 2026 -0700 test(frontend): add FormlyRepeatDndComponent unit tests (#5632) ### What changes were proposed in this PR? This PR adds frontend unit tests for `FormlyRepeatDndComponent`. The new spec verifies that: * The component is created successfully. * onDrop does nothing when previousIndex == currentIndex. * onDrop does nothing when model is undefined. * onDrop reorders model, field.fieldGroup, and formControl on the happy path. * props.reorder is called after a successful reorder. This improves test coverage for the drag-and-drop reorder behavior without changing existing component behavior. ### Any related issues, documentation, discussions? Closes #5464 ### How was this PR tested? Ran the following command locally from the frontend directory: yarn test --include='**/repeat-dnd.component.spec.ts' The test passed successfully with 1 test file passed and 4 tests passed. Also ran: yarn lint ^ yarn lint completed successfully. ### Was this PR authored or co-authored using generative AI tooling? Generated-by: ChatGPT --- .../formly/repeat-dnd/repeat-dnd.component.spec.ts | 95 ++++++++++++++++++++++ 1 file changed, 95 insertions(+) diff --git a/frontend/src/app/common/formly/repeat-dnd/repeat-dnd.component.spec.ts b/frontend/src/app/common/formly/repeat-dnd/repeat-dnd.component.spec.ts new file mode 100644 index 0000000000..86d74a1a96 --- /dev/null +++ b/frontend/src/app/common/formly/repeat-dnd/repeat-dnd.component.spec.ts @@ -0,0 +1,95 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import { CdkDragDrop } from "@angular/cdk/drag-drop"; +import { FormArray, FormControl } from "@angular/forms"; +import { ComponentFixture, TestBed } from "@angular/core/testing"; +import { FormlyRepeatDndComponent } from "./repeat-dnd.component"; + +describe("FormlyRepeatDndComponent", () => { + let component: FormlyRepeatDndComponent; + let fixture: ComponentFixture<FormlyRepeatDndComponent>; + + const createDropEvent = (previousIndex: number, currentIndex: number): CdkDragDrop<string[]> => + ({ previousIndex, currentIndex }) as CdkDragDrop<string[]>; + + const setComponentState = (reorder = vi.fn()) => { + const formControl = new FormArray([new FormControl("a"), new FormControl("b"), new FormControl("c")]); + + component.field = { + model: ["a", "b", "c"], + fieldGroup: [{ key: "a" }, { key: "b" }, { key: "c" }], + formControl, + props: { reorder }, + } as any; + + return reorder; + }; + + beforeEach(async () => { + await TestBed.configureTestingModule({ + imports: [FormlyRepeatDndComponent], + }).compileComponents(); + + fixture = TestBed.createComponent(FormlyRepeatDndComponent); + component = fixture.componentInstance; + }); + + it("should create", () => { + setComponentState(); + + expect(component).toBeTruthy(); + }); + + it("should do nothing when previousIndex equals currentIndex", () => { + const reorder = setComponentState(); + + component.onDrop(createDropEvent(1, 1)); + + expect(component.model).toEqual(["a", "b", "c"]); + expect(component.field.fieldGroup?.map(field => field.key)).toEqual(["a", "b", "c"]); + expect((component.formControl as FormArray).controls.map(control => control.value)).toEqual(["a", "b", "c"]); + expect(reorder).not.toHaveBeenCalled(); + }); + + it("should do nothing when model is undefined", () => { + const reorder = setComponentState(); + component.field = { + ...component.field, + model: undefined, + } as any; + + component.onDrop(createDropEvent(0, 2)); + + expect(component.field.fieldGroup?.map(field => field.key)).toEqual(["a", "b", "c"]); + expect((component.formControl as FormArray).controls.map(control => control.value)).toEqual(["a", "b", "c"]); + expect(reorder).not.toHaveBeenCalled(); + }); + + it("should reorder model, fieldGroup, formControl, and call reorder callback", () => { + const reorder = setComponentState(); + + component.onDrop(createDropEvent(0, 2)); + + expect(component.model).toEqual(["b", "c", "a"]); + expect(component.field.fieldGroup?.map(field => field.key)).toEqual(["b", "c", "a"]); + expect((component.formControl as FormArray).controls.map(control => control.value)).toEqual(["b", "c", "a"]); + expect(reorder).toHaveBeenCalledOnce(); + }); +});
