zyratlo commented on code in PR #5271: URL: https://github.com/apache/texera/pull/5271#discussion_r3677841024
########## frontend/src/app/workspace/component/jupyter-notebook-panel/jupyter-notebook-panel.component.spec.ts: ########## @@ -0,0 +1,226 @@ +/** + * 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 { ComponentFixture, fakeAsync, TestBed, tick } from "@angular/core/testing"; +import { JupyterNotebookPanelComponent } from "./jupyter-notebook-panel.component"; +import { JupyterPanelService } from "../../service/jupyter-panel/jupyter-panel.service"; +import { NotebookMigrationService } from "../../service/notebook-migration/notebook-migration.service"; +import { Subject } from "rxjs"; +import { ElementRef } from "@angular/core"; + +describe("JupyterNotebookPanelComponent", () => { + let component: JupyterNotebookPanelComponent; + let fixture: ComponentFixture<JupyterNotebookPanelComponent>; + + let mockJupyterPanelService: any; + let mockNotebookMigrationService: any; + + beforeEach(async () => { + mockJupyterPanelService = { + jupyterNotebookPanelVisible$: new Subject<boolean>(), + setIframeRef: vi.fn(), + closeJupyterNotebookPanel: vi.fn(), + minimizeJupyterNotebookPanel: vi.fn(), + }; + + mockNotebookMigrationService = { + getJupyterIframeURL: vi.fn().mockResolvedValue("http://localhost:8888"), + }; + + await TestBed.configureTestingModule({ + imports: [JupyterNotebookPanelComponent], + providers: [ + { provide: JupyterPanelService, useValue: mockJupyterPanelService }, + { provide: NotebookMigrationService, useValue: mockNotebookMigrationService }, + ], + }).compileComponents(); + }); + + beforeEach(() => { + fixture = TestBed.createComponent(JupyterNotebookPanelComponent); + component = fixture.componentInstance; + fixture.detectChanges(); + }); + + // Destroy the component so its subscriptions complete, and restore spies so a + // shared spy's call history (e.g. console.error) does not accumulate across + // tests. Mocks are not auto-reset by the Vitest config. + afterEach(() => { + fixture.destroy(); + vi.restoreAllMocks(); + }); + + it("should create", () => { + vi.spyOn(component, "checkIframeRef").mockImplementation(() => {}); + expect(component).toBeTruthy(); + }); + + it("should be hidden by default", () => { + vi.spyOn(component, "checkIframeRef").mockImplementation(() => {}); + expect(component.isVisible).toBe(false); + }); + + it("should update visibility when service emits", async () => { + vi.spyOn(component, "checkIframeRef").mockImplementation(() => {}); + mockJupyterPanelService.jupyterNotebookPanelVisible$.next(true); + + await fixture.whenStable(); + fixture.detectChanges(); + + expect(component.isVisible).toBe(true); + }); + + it("should fetch and sanitize URL when panel becomes visible", async () => { + vi.spyOn(component, "checkIframeRef").mockImplementation(() => {}); + mockJupyterPanelService.jupyterNotebookPanelVisible$.next(true); + + await fixture.whenStable(); + fixture.detectChanges(); + + expect(mockNotebookMigrationService.getJupyterIframeURL).toHaveBeenCalled(); + expect(component.jupyterUrl.toString()).toContain("http://localhost:8888"); + }); + + it("should not render the iframe while visible without a URL", () => { + vi.spyOn(component, "checkIframeRef").mockImplementation(() => {}); + component.isVisible = true; + + // Rendering with an unset jupyterUrl must not throw NG0904 (unsafe resource + // URL); the iframe should simply be absent until a URL is available. + expect(() => fixture.detectChanges()).not.toThrow(); + expect(fixture.nativeElement.querySelector("iframe")).toBeNull(); + }); + + it("should render the iframe once a URL is available", async () => { + vi.spyOn(component, "checkIframeRef").mockImplementation(() => {}); + mockJupyterPanelService.jupyterNotebookPanelVisible$.next(true); + + await fixture.whenStable(); + fixture.detectChanges(); + + const iframe = fixture.nativeElement.querySelector("iframe"); + expect(iframe).not.toBeNull(); + expect(iframe.getAttribute("src")).toContain("http://localhost:8888"); + }); + + it("should not update jupyterUrl when the iframe URL fetch rejects", async () => { + vi.spyOn(component, "checkIframeRef").mockImplementation(() => {}); + vi.spyOn(console, "error").mockImplementation(() => {}); + mockNotebookMigrationService.getJupyterIframeURL.mockRejectedValueOnce(new Error("network error")); + + mockJupyterPanelService.jupyterNotebookPanelVisible$.next(true); + + await fixture.whenStable(); + + expect(mockNotebookMigrationService.getJupyterIframeURL).toHaveBeenCalled(); + expect(component.jupyterUrl.toString()).toBe(""); + }); Review Comment: done in [a11a4f9](https://github.com/apache/texera/pull/5271/commits/a11a4f96889ac69d2b7e78d7698f5986389ca25a) -- 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]
