kunwp1 commented on code in PR #9382:
URL: https://github.com/apache/texera/pull/9382#discussion_r4516392391


##########
frontend/src/app/common/formly/preset-wrapper/preset-wrapper.component.spec.ts:
##########
@@ -214,32 +201,21 @@ describe("PresetWrapperComponent", () => {
       ).toBe("first, second");
     });
 
-    describe("getSearchResults", () => {
-      it("returns a copy of all presets when showAllResults is true", () => {
+    describe("preset list copies", () => {
+      it("copies the fetched presets when the dropdown opens", () => {
         const presets: Preset[] = [testPreset, otherPreset];
-        const results = component.getSearchResults(presets, "anything", true);
+        presetServiceStub.getPresets.mockReturnValue(of(presets));
+        component.onDropdownVisibilityEvent(true);
+        const results = component.searchResults;
         expect(results).toEqual(presets);
         expect(results).not.toBe(presets);
       });
 
-      it("returns all presets when showAllResults is true even if the search 
term doesn't match", () => {
-        expect(component.getSearchResults([testPreset], "no-match", 
true)).toEqual([testPreset]);
-      });
-
-      it("filters by case-insensitive prefix match on the entry title when 
showAllResults is false", () => {
-        const presets: Preset[] = [testPreset, otherPreset];
-        // testPreset title 'testPresetValue' starts with 'TEST'
-        expect(component.getSearchResults(presets, "TEST", 
false)).toEqual([testPreset]);
-        // otherPreset title 'otherPresetValue2' starts with 'other'
-        expect(component.getSearchResults(presets, "other", 
false)).toEqual([otherPreset]);
-      });
-
-      it("returns the full list when search term is empty and showAllResults 
is false", () => {
-        expect(component.getSearchResults([testPreset], "", 
false)).toEqual([testPreset]);
-      });
-
-      it("returns an empty list when the search term matches nothing", () => {
-        expect(component.getSearchResults([testPreset], "zzzz", 
false)).toEqual([]);
+      it("shows all fetched presets even when the field value does not match", 
() => {
+        formControl.setValue("no-match");
+        presetServiceStub.getPresets.mockReturnValue(of([testPreset]));
+        component.onDropdownVisibilityEvent(true);
+        expect(component.searchResults).toEqual([testPreset]);
       });
     });

Review Comment:
   This block repeats the dropdown-visibility test below, apart from the 
`not.toBe` check on the copy. The second case sets a value the component never 
reads.
   
   The two new "keeps the preset list" tests further down check subscriptions 
the component no longer has, so they can't fail. One still waits a tick for a 
debounce that's gone, and the stub's `applyPresetStream` exists only for them.



##########
frontend/src/app/common/formly/preset-wrapper/preset-wrapper.component.ts:
##########
@@ -101,16 +98,13 @@ export class PresetWrapperComponent extends FieldWrapper 
implements OnInit, OnDe
       );
     }
     const presetKey = <PresetKey>this.field.templateOptions.presetKey;
-    this.searchTerm = this.formControl.value !== null ? this.formControl.value 
: "";
     this.presetType = presetKey.presetType;
     this.saveTarget = presetKey.saveTarget;
     this.applyTarget = presetKey.applyTarget;
     this.updateSearchResults();
-    this.basePreset = this.filterPresetFromForm();
+    this.filterPresetFromForm();

Review Comment:
   The result is discarded, so this call now matters only for the `TypeError` 
it throws from the unchecked cast when the field has no parent, which the new 
spec pins as validation. If rejecting that case at init is intended, an 
explicit `this.field.parent?.fieldGroup === undefined` check in the guard above 
says so. It also throws before `updateSearchResults()` subscribes, and with a 
message. The test can then expect that message instead of a `TypeError`.



##########
frontend/src/app/common/formly/preset-wrapper/preset-wrapper.component.html:
##########
@@ -25,7 +25,7 @@
   nzBackdrop="false"
   [nzOverlayClassName]="searchResults.length === 0 ? 'preset-dropdown 
no-results' : 'preset-dropdown'"
   (nzVisibleChange)="onDropdownVisibilityEvent($event)"
-  [nzVisible]="presetMenuVisible"
+  [nzVisible]="false"

Review Comment:
   `nzVisible` already defaults to false, and a constant binding never changes, 
so the click trigger alone controls the menu either way. Dropping the line 
avoids it reading as "this dropdown is never visible".



##########
frontend/src/app/common/formly/preset-wrapper/preset-wrapper.component.ts:
##########
@@ -254,36 +206,15 @@ export class PresetWrapperComponent extends FieldWrapper 
implements OnInit, OnDe
     return preset;
   }
 
-  /**
-   * handles user typing into form field
-   * updates earch results to account for filtering
-   */
-  private handleFieldValueChanges() {
-    // WIERD CODE EXPLANATION: debounceTime(0)?
-    // After a preset is applied (by clicking a dropdown entry), it changes a 
field value and
-    // activates this handler function.
-    // updating the searchResults (which also changes the HTML template due to 
binding) too quickly
-    // can sometimes interrupt the dropdown closing animation. (dropdown 
should close after clicking dropdown entry, but instead stays open)
-    // hence the debounceTime(0) to slow this function down.
-    this.formControl.valueChanges.pipe(debounceTime(0), 
takeUntil(this.teardownObservable)).subscribe({
-      next: (value: string | number | boolean) => {
-        this.searchTerm = (value ?? "").toString();
-        if (this.presetMenuVisible) {
-          this.updateSearchResults(false);
-        }
-      },
-    });
-  }
-
   /**
    * updates search results
    */
-  private updateSearchResults(showAllResults = true) {
+  private updateSearchResults() {
     this.presetService
       .getPresets(this.presetType, this.saveTarget)
       .pipe(first(), takeUntil(this.teardownObservable))
       .subscribe(presets => {
-        this.searchResults = this.getSearchResults(presets, this.searchTerm, 
showAllResults);
+        this.searchResults = presets.slice();

Review Comment:
   `getPresets` returns a freshly parsed array each time, and 
`savePresetsStream` only carries newly built arrays, so this copy and the one 
in `handleSavePresets` are there only because `searchResults` is typed as a 
mutable `Preset[]`. Declaring it `readonly Preset[]` lets both places assign 
directly.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to