Copilot commented on code in PR #5849:
URL: https://github.com/apache/texera/pull/5849#discussion_r3449124806


##########
frontend/src/app/app.component.spec.ts:
##########
@@ -0,0 +1,139 @@
+/**
+ * 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 { CommonModule } from "@angular/common";
+import { ComponentFixture, TestBed } from "@angular/core/testing";
+import { RouterTestingModule } from "@angular/router/testing";
+import { AppComponent } from "./app.component";
+import { GuiConfigService } from "./common/service/gui-config.service";
+import { DeploymentVersionService } from 
"./common/service/deployment-version/deployment-version.service";
+import { Version } from "../environments/version";
+
+// GuiConfigService stub whose env getter either returns a value or throws,
+// mirroring "config loaded" vs "config failed to load by APP_INITIALIZER".
+class StubGuiConfigService {
+  shouldThrow = false;
+  get env(): unknown {
+    if (this.shouldThrow) {
+      throw new Error("config not loaded");
+    }
+    return {};
+  }
+}
+
+// Records start() invocations without a spy framework.
+class FakeDeploymentVersionService {
+  startCalls = 0;
+  start(): void {
+    this.startCalls++;
+  }
+}
+
+describe("AppComponent", () => {
+  let config: StubGuiConfigService;
+  let deployment: FakeDeploymentVersionService;
+
+  beforeEach(() => {
+    Version.buildNumber = "dev";
+    config = new StubGuiConfigService();
+    deployment = new FakeDeploymentVersionService();
+
+    TestBed.configureTestingModule({
+      imports: [CommonModule, RouterTestingModule],
+      declarations: [AppComponent],
+      providers: [
+        { provide: GuiConfigService, useValue: config },
+        { provide: DeploymentVersionService, useValue: deployment },
+      ],
+    });
+  });
+
+  // Version is a shared module singleton; restore the dev default so a test
+  // that flips buildNumber cannot leak into other suites in the same worker.
+  afterEach(() => {
+    Version.buildNumber = "dev";
+  });
+
+  function create(): ComponentFixture<AppComponent> {
+    return TestBed.createComponent(AppComponent);
+  }
+
+  describe("config-loaded detection", () => {
+    it("marks config as loaded when env is accessible", () => {
+      config.shouldThrow = false;
+      const component = create().componentInstance;
+      expect(component.configLoaded).toBe(true);
+    });
+
+    it("marks config as not loaded when accessing env throws", () => {
+      config.shouldThrow = true;
+      const component = create().componentInstance;
+      expect(component.configLoaded).toBe(false);
+    });
+
+    it("renders the configuration-error panel when config is not loaded", () 
=> {
+      config.shouldThrow = true;
+      const fixture = create();
+      fixture.detectChanges();
+      const el: HTMLElement = fixture.nativeElement;
+      expect(el.querySelector("#config-error")).not.toBeNull();
+    });
+
+    it("does not render the configuration-error panel when config is loaded", 
() => {
+      config.shouldThrow = false;
+      const fixture = create();
+      fixture.detectChanges();
+      const el: HTMLElement = fixture.nativeElement;
+      expect(el.querySelector("#config-error")).toBeNull();
+    });
+  });
+
+  describe("deployment-version polling guard", () => {
+    it("does not start polling for the 'dev' placeholder build", () => {
+      Version.buildNumber = "dev";
+      create();
+      expect(deployment.startCalls).toBe(0);
+    });
+
+    it("starts polling for a real (non-'dev') build", () => {
+      Version.buildNumber = "prod-build-123";
+      create();
+      expect(deployment.startCalls).toBe(1);
+    });
+  });
+
+  describe("retry", () => {
+    it("reloads the page", () => {
+      const reload = vi.fn();
+      // location.reload itself is non-configurable, so swap the whole
+      // location object for the duration of the test.
+      const original = window.location;
+      Object.defineProperty(window, "location", {
+        configurable: true,
+        value: { ...original, reload },
+      });
+      try {
+        create().componentInstance.retry();
+        expect(reload).toHaveBeenCalledTimes(1);
+      } finally {
+        Object.defineProperty(window, "location", { configurable: true, value: 
original });
+      }
+    });

Review Comment:
   This test tries to redefine `window.location` via `Object.defineProperty`. 
In jsdom, `window.location` is typically non-configurable, so redefining it can 
throw `TypeError: Cannot redefine property: location` and make the suite 
fail/flaky. Prefer spying on `Location.prototype.reload` (or 
`window.location.reload` if configurable) instead of replacing the whole 
`location` object.



-- 
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