|
| 1 | +/** |
| 2 | + * Copyright 2025 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import { render, waitFor } from "@testing-library/react"; |
| 18 | +import { describe, expect, it, vi } from "vitest"; |
| 19 | +import { DrivePicker } from "./DrivePicker"; |
| 20 | +import * as LoaderModule from "./loader"; |
| 21 | + |
| 22 | +describe("DrivePicker", () => { |
| 23 | + it("should call onWebComponentLoadError if import fails", async () => { |
| 24 | + const onWebComponentLoadError = vi.fn(); |
| 25 | + const consoleErrorSpy = vi |
| 26 | + .spyOn(console, "error") |
| 27 | + .mockImplementation(() => {}); |
| 28 | + |
| 29 | + // Mock loader failure |
| 30 | + vi.spyOn(LoaderModule, "loadDrivePickerElement").mockRejectedValue( |
| 31 | + new Error("Import failed"), |
| 32 | + ); |
| 33 | + |
| 34 | + render( |
| 35 | + <DrivePicker |
| 36 | + client-id="test-client-id" |
| 37 | + app-id="test-app-id" |
| 38 | + onWebComponentLoadError={onWebComponentLoadError} |
| 39 | + />, |
| 40 | + ); |
| 41 | + |
| 42 | + await waitFor(() => { |
| 43 | + expect(onWebComponentLoadError).toHaveBeenCalledWith(expect.any(Error)); |
| 44 | + }); |
| 45 | + |
| 46 | + expect(onWebComponentLoadError).toHaveBeenCalledWith( |
| 47 | + expect.objectContaining({ |
| 48 | + message: "Import failed", |
| 49 | + }), |
| 50 | + ); |
| 51 | + |
| 52 | + consoleErrorSpy.mockRestore(); |
| 53 | + vi.restoreAllMocks(); |
| 54 | + }); |
| 55 | +}); |
0 commit comments