|
| 1 | +/** |
| 2 | + * @jest-environment node |
| 3 | + * |
| 4 | + * Copyright 2026 Google LLC |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +import { jest } from "@jest/globals"; |
| 20 | + |
| 21 | +type TrustedTypesStub = { |
| 22 | + createPolicy: jest.Mock; |
| 23 | +}; |
| 24 | + |
| 25 | +declare global { |
| 26 | + // Node does not provide this, but tests attach a stub. |
| 27 | + var trustedTypes: TrustedTypesStub | undefined; |
| 28 | +} |
| 29 | + |
| 30 | +describe("setScriptSrc()", () => { |
| 31 | + beforeEach(() => { |
| 32 | + jest.resetModules(); |
| 33 | + jest.clearAllMocks(); |
| 34 | + |
| 35 | + // make sure these tests are running without a window-instance |
| 36 | + if (typeof window !== "undefined") { |
| 37 | + throw new Error("Expected Node test environment without window."); |
| 38 | + } |
| 39 | + }); |
| 40 | + |
| 41 | + afterEach(() => { |
| 42 | + globalThis.trustedTypes = undefined; |
| 43 | + }); |
| 44 | + |
| 45 | + it("falls back to a plain string when Trusted Types are not available", async () => { |
| 46 | + globalThis.trustedTypes = undefined; |
| 47 | + |
| 48 | + const { setScriptSrc } = await import("./setScriptSrc.js"); |
| 49 | + const script = { src: "" } as HTMLScriptElement; |
| 50 | + |
| 51 | + setScriptSrc(script, "https://maps.googleapis.com/maps/api/js"); |
| 52 | + |
| 53 | + expect(script.src).toBe("https://maps.googleapis.com/maps/api/js"); |
| 54 | + }); |
| 55 | + |
| 56 | + it("creates and uses a Trusted Types policy when available", async () => { |
| 57 | + const createScriptURL = jest.fn((url: string) => `tt:${url}`); |
| 58 | + const createPolicy = jest.fn(() => ({ createScriptURL })); |
| 59 | + |
| 60 | + globalThis.trustedTypes = { |
| 61 | + createPolicy, |
| 62 | + }; |
| 63 | + |
| 64 | + const { setScriptSrc } = await import("./setScriptSrc.js"); |
| 65 | + const script = { src: "" } as HTMLScriptElement; |
| 66 | + |
| 67 | + setScriptSrc(script, "https://maps.googleapis.com/maps/api/js"); |
| 68 | + |
| 69 | + expect(createPolicy).toHaveBeenCalledTimes(1); |
| 70 | + expect(createPolicy).toHaveBeenCalledWith( |
| 71 | + "@googlemaps/js-api-loader", |
| 72 | + expect.objectContaining({ |
| 73 | + createScriptURL: expect.any(Function), |
| 74 | + }) |
| 75 | + ); |
| 76 | + expect(createScriptURL).toHaveBeenCalledWith( |
| 77 | + "https://maps.googleapis.com/maps/api/js" |
| 78 | + ); |
| 79 | + expect(script.src).toBe("tt:https://maps.googleapis.com/maps/api/js"); |
| 80 | + }); |
| 81 | + |
| 82 | + it("falls back when policy creation fails", async () => { |
| 83 | + const createPolicy = jest.fn(() => { |
| 84 | + throw new Error("policy denied"); |
| 85 | + }); |
| 86 | + |
| 87 | + globalThis.trustedTypes = { |
| 88 | + createPolicy, |
| 89 | + }; |
| 90 | + |
| 91 | + const { setScriptSrc } = await import("./setScriptSrc.js"); |
| 92 | + const script = { src: "" } as HTMLScriptElement; |
| 93 | + |
| 94 | + setScriptSrc(script, "https://maps.googleapis.com/maps/api/js"); |
| 95 | + |
| 96 | + expect(createPolicy).toHaveBeenCalledTimes(1); |
| 97 | + expect(script.src).toBe("https://maps.googleapis.com/maps/api/js"); |
| 98 | + }); |
| 99 | +}); |
0 commit comments