|
2 | 2 | * Copyright (c) Bentley Systems, Incorporated. All rights reserved. |
3 | 3 | * See LICENSE.md in the project root for license terms and full copyright notice. |
4 | 4 | *--------------------------------------------------------------------------------------------*/ |
| 5 | +import * as moq from "typemoq"; |
| 6 | +import { UiFramework } from "@itwin/appui-react"; |
| 7 | +import { BeEvent } from "@itwin/core-bentley"; |
5 | 8 | import { ImageMapLayerSettings } from "@itwin/core-common"; |
6 | | -import { MapLayerIndex, MapLayerSource, MapLayerSources, NoRenderApp, NotificationManager } from "@itwin/core-frontend"; |
7 | | -import { act, fireEvent, getAllByTestId, getByTestId, queryAllByTestId, queryByText, render, RenderResult } from "@testing-library/react"; |
| 9 | +import { |
| 10 | + MapLayerImageryProvider, |
| 11 | + MapLayerImageryProviderStatus, |
| 12 | + MapLayerIndex, |
| 13 | + MapLayerSource, |
| 14 | + MapLayerSources, |
| 15 | + NoRenderApp, |
| 16 | + NotificationManager, |
| 17 | +} from "@itwin/core-frontend"; |
| 18 | +import { |
| 19 | + act, |
| 20 | + fireEvent, |
| 21 | + getAllByRole, |
| 22 | + getAllByTestId, |
| 23 | + getByTestId, |
| 24 | + queryAllByRole, |
| 25 | + queryAllByTestId, |
| 26 | + queryByText, |
| 27 | + render, |
| 28 | + renderHook, |
| 29 | + RenderResult, |
| 30 | +} from "@testing-library/react"; |
8 | 31 | import { MapLayerPreferences, MapLayerSourceChangeType } from "../MapLayerPreferences"; |
| 32 | +import { useMapLayerSources } from "../ui/hooks/map-layer/useMapLayerSources"; |
9 | 33 | import { MapLayerManager } from "../ui/widget/MapLayerManager"; |
10 | 34 | import { TestUtils } from "./TestUtils"; |
11 | 35 | import { ViewportMock } from "./ViewportMock"; |
@@ -434,4 +458,162 @@ describe("MapLayerManager", () => { |
434 | 458 | await checkLayerSection(layersSections[0]); |
435 | 459 | await checkLayerSection(layersSections[1]); |
436 | 460 | }); |
| 461 | + |
| 462 | + describe("offline mode", () => { |
| 463 | + const layerSettings = ImageMapLayerSettings.fromJSON({ |
| 464 | + formatId: "WMS", |
| 465 | + name: "background", |
| 466 | + visible: true, |
| 467 | + transparentBackground: true, |
| 468 | + subLayers: [{ name: "subLayer1", visible: false }], |
| 469 | + accessKey: undefined, |
| 470 | + transparency: 0, |
| 471 | + url: "https://server/MapServer", |
| 472 | + }); |
| 473 | + |
| 474 | + const managerElement = (offlineMode?: boolean) => ( |
| 475 | + <div> |
| 476 | + <MapLayerManager activeViewport={viewportMock.object} mapLayerOptions={offlineMode === undefined ? undefined : { offlineMode }}></MapLayerManager> |
| 477 | + </div> |
| 478 | + ); |
| 479 | + |
| 480 | + async function flush() { |
| 481 | + await act(async () => { |
| 482 | + await TestUtils.flushAsyncOperations(); |
| 483 | + }); |
| 484 | + } |
| 485 | + |
| 486 | + async function renderManager(offlineMode?: boolean, setupViewport?: () => void) { |
| 487 | + viewportMock.reset(); |
| 488 | + viewportMock.backgroundLayers = [layerSettings]; |
| 489 | + viewportMock.overlayLayers = [ImageMapLayerSettings.fromJSON({ ...layerSettings.toJSON(), name: "overlay" })]; |
| 490 | + viewportMock.setup(); |
| 491 | + setupViewport?.(); |
| 492 | + |
| 493 | + const result = render(managerElement(offlineMode)); |
| 494 | + await flush(); |
| 495 | + return result; |
| 496 | + } |
| 497 | + |
| 498 | + it("hides actions that change map layer configuration", async () => { |
| 499 | + const { container } = await renderManager(true); |
| 500 | + |
| 501 | + expect(container.querySelector(attachLayerButtonSelector)).toBeNull(); |
| 502 | + expect(container.querySelector(".maplayers-settings-popup-button")).toBeNull(); |
| 503 | + expect(queryAllByTestId(container, "select-all-checkbox")).toHaveLength(0); |
| 504 | + expect(queryAllByTestId(container, "detach-label-button")).toHaveLength(0); |
| 505 | + expect(queryAllByTestId(container, "select-item-checkbox")).toHaveLength(0); |
| 506 | + |
| 507 | + const baseMapSelect = getByTestId(container, "base-map-select").querySelector('div[role="combobox"]'); |
| 508 | + expect(baseMapSelect?.getAttribute("aria-disabled")).toBe("true"); |
| 509 | + }); |
| 510 | + |
| 511 | + it("hides detach from the layer menu but keeps zoom to layer", async () => { |
| 512 | + const { container } = await renderManager(true); |
| 513 | + |
| 514 | + const moreOptionsButton = container.querySelector<HTMLElement>('[aria-label="LayerMenu.MoreOptions"]'); |
| 515 | + expect(moreOptionsButton).not.toBeNull(); |
| 516 | + fireEvent.click(moreOptionsButton!); |
| 517 | + |
| 518 | + expect(queryByText(document.body, "LayerMenu.ZoomToLayer")).not.toBeNull(); |
| 519 | + expect(queryByText(document.body, "LayerMenu.Detach")).toBeNull(); |
| 520 | + }); |
| 521 | + |
| 522 | + it("keeps layer visibility controls", async () => { |
| 523 | + const { container } = await renderManager(true); |
| 524 | + |
| 525 | + const sections = getAllByTestId(container, "map-manager-layer-section"); |
| 526 | + expect(sections).toHaveLength(2); |
| 527 | + sections.forEach((section) => { |
| 528 | + expect(queryAllByTestId(section, "layer-visibility-icon-show")).toHaveLength(1); |
| 529 | + expect(getByTestId<HTMLButtonElement>(section, "hide-all-label-button").disabled).toBe(false); |
| 530 | + expect(getByTestId<HTMLButtonElement>(section, "show-all-label-button").disabled).toBe(false); |
| 531 | + expect(getByTestId<HTMLButtonElement>(section, "invert-all-label-button").disabled).toBe(false); |
| 532 | + }); |
| 533 | + }); |
| 534 | + |
| 535 | + it("does not load map layer sources", async () => { |
| 536 | + const getSourcesSpy = vi.spyOn(MapLayerPreferences, "getSources"); |
| 537 | + const createSourcesSpy = vi.spyOn(MapLayerSources, "create"); |
| 538 | + |
| 539 | + await renderManager(true); |
| 540 | + |
| 541 | + expect(getSourcesSpy).not.toHaveBeenCalled(); |
| 542 | + expect(createSourcesSpy).not.toHaveBeenCalled(); |
| 543 | + }); |
| 544 | + |
| 545 | + it("loads map layer sources when offline mode is turned off", async () => { |
| 546 | + const createSourcesSpy = vi.spyOn(MapLayerSources, "create"); |
| 547 | + |
| 548 | + const { rerender } = await renderManager(true); |
| 549 | + expect(createSourcesSpy).not.toHaveBeenCalled(); |
| 550 | + |
| 551 | + rerender(managerElement(false)); |
| 552 | + await flush(); |
| 553 | + expect(createSourcesSpy).toHaveBeenCalledTimes(1); |
| 554 | + }); |
| 555 | + |
| 556 | + it("stops a pending source load when switching to offline mode", async () => { |
| 557 | + let resolveCreate: (value: MapLayerSources) => void = () => {}; |
| 558 | + vi.spyOn(MapLayerSources, "create").mockImplementation(async () => new Promise<MapLayerSources>((resolve) => (resolveCreate = resolve))); |
| 559 | + const getSourcesSpy = vi.spyOn(MapLayerPreferences, "getSources").mockResolvedValue([]); |
| 560 | + |
| 561 | + const { rerender } = await renderManager(false); |
| 562 | + rerender(managerElement(true)); |
| 563 | + await act(async () => { |
| 564 | + resolveCreate({ allSource: [] } as unknown as MapLayerSources); |
| 565 | + await TestUtils.flushAsyncOperations(); |
| 566 | + }); |
| 567 | + |
| 568 | + expect(getSourcesSpy).not.toHaveBeenCalled(); |
| 569 | + }); |
| 570 | + |
| 571 | + it("clears the loading state when switching to offline mode during a source load", async () => { |
| 572 | + vi.spyOn(MapLayerSources, "create").mockImplementation(async () => new Promise<MapLayerSources>(() => {})); |
| 573 | + |
| 574 | + const { result, rerender } = renderHook((skipLoading: boolean) => |
| 575 | + useMapLayerSources({ activeViewport: viewportMock.object, fetchPublicMapLayerSources: false, hideExternalMapLayersSection: false, skipLoading }), |
| 576 | + { initialProps: false }, |
| 577 | + ); |
| 578 | + expect(result.current.loadingSources).toBe(true); |
| 579 | + |
| 580 | + rerender(true); |
| 581 | + await flush(); |
| 582 | + expect(result.current.loadingSources).toBe(false); |
| 583 | + }); |
| 584 | + |
| 585 | + it("closes the sign-in dialog when switching to offline mode", async () => { |
| 586 | + const openSpy = vi.spyOn(UiFramework.dialogs.modal, "open").mockImplementation(() => {}); |
| 587 | + const closeSpy = vi.spyOn(UiFramework.dialogs.modal, "close").mockImplementation(() => {}); |
| 588 | + |
| 589 | + const { container, rerender } = await renderManager(false, () => { |
| 590 | + viewportMock.viewportMock |
| 591 | + .setup((vp) => vp.getMapLayerImageryProvider(moq.It.isAny())) |
| 592 | + .returns(() => ({ status: MapLayerImageryProviderStatus.RequireAuth, onStatusChanged: new BeEvent() }) as unknown as MapLayerImageryProvider); |
| 593 | + viewportMock.displayStyleMock |
| 594 | + .setup((ds) => ds.findMapLayerIndexByNameAndSource(moq.It.isAny(), moq.It.isAny(), moq.It.isAny())) |
| 595 | + .returns(() => 0); |
| 596 | + viewportMock.displayStyleMock.setup((ds) => ds.mapLayerAtIndex(moq.It.isAny())).returns(() => layerSettings); |
| 597 | + }); |
| 598 | + |
| 599 | + const signInButtons = getAllByRole(container, "button", { name: "Widget.RequireAuthTooltip" }); |
| 600 | + expect(signInButtons).toHaveLength(4); |
| 601 | + fireEvent.click(signInButtons[0]); |
| 602 | + expect(openSpy).toHaveBeenCalledTimes(1); |
| 603 | + |
| 604 | + rerender(managerElement(true)); |
| 605 | + await flush(); |
| 606 | + expect(closeSpy).toHaveBeenCalledTimes(1); |
| 607 | + expect(queryAllByRole(container, "button", { name: "Widget.RequireAuthTooltip" })).toHaveLength(0); |
| 608 | + }); |
| 609 | + |
| 610 | + it("shows all actions when offlineMode is not set", async () => { |
| 611 | + const { container } = await renderManager(); |
| 612 | + |
| 613 | + expect(container.querySelector(attachLayerButtonSelector)).not.toBeNull(); |
| 614 | + expect(container.querySelector(".maplayers-settings-popup-button")).not.toBeNull(); |
| 615 | + expect(queryAllByTestId(container, "detach-label-button")).toHaveLength(2); |
| 616 | + expect(queryAllByTestId(container, "select-item-checkbox")).toHaveLength(2); |
| 617 | + }); |
| 618 | + }); |
437 | 619 | }); |
0 commit comments