/** * @license * Copyright 2026 Google LLC * * Licensed 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 { FirebaseError } from '@firebase/util'; import { testAuth, TestAuth } from '../../../test/helpers/mock_auth'; import { _window } from '../auth_window'; import * as js from '../load_js'; import { _loadGapi, _resetLoader } from './gapi'; import { MockInstance } from 'vitest'; vi.mock('../load_js', { spy: true }); describe('platform_browser/iframe/gapi', () => { let library: typeof gapi; let auth: TestAuth; let loadJsStub: MockInstance; function onJsLoad(globalLoadFnName: string): void { _window().gapi = library as typeof gapi; _window()[globalLoadFnName](); } beforeEach(async () => { vi.clearAllMocks(); loadJsStub = vi.spyOn(js, '_loadJS').mockImplementation(url => { onJsLoad(url.split('onload=')[1]); return Promise.resolve(new Event('load')); }); auth = await testAuth(); }); function makeGapi(result: unknown, timesout = false): typeof gapi { const callbackFn = timesout === false ? 'callback' : 'ontimeout'; return { load: vi.fn((_name: string, params: Record void>) => params[callbackFn]() ), iframes: { getContext: () => result as gapi.iframes.Context } } as unknown as typeof gapi; } afterEach(() => { vi.restoreAllMocks(); delete _window().gapi; delete _window().___jsl; _resetLoader(); }); it('calls gapi.load once it is ready', async () => { const gapi = makeGapi('context!'); library = gapi; expect(await _loadGapi(auth)).toBe('context!'); expect(gapi.load).toHaveBeenCalled(); }); it('resets the gapi.load state', async () => { _window().___jsl = { H: { something: { r: ['requested'], L: ['loaded', 'test'] } }, CP: [1, 2, 3, 4] }; library = makeGapi('iframes'); await _loadGapi(auth); // Expect deep equality, but *not* pointer equality expect(_window().___jsl!.H.something.r).toEqual( _window().___jsl!.H.something.L ); expect(_window().___jsl!.H.something.r).not.toBe( _window().___jsl!.H.something.L ); expect(_window().___jsl!.CP).toEqual([null, null, null, null]); }); it('returns the cached object without reloading', async () => { library = makeGapi('test'); expect(await _loadGapi(auth)).toBe('test'); expect(await _loadGapi(auth)).toBe('test'); expect(js._loadJS).toHaveBeenCalledTimes(1); }); it('rejects with a network error if load fails', async () => { library = {} as typeof gapi; const promise2 = _loadGapi(auth); await expect(promise2).rejects.toThrow('auth/network-request-failed'); await expect(promise2).rejects.toBeInstanceOf(FirebaseError); }); it('rejects with a network error if ontimeout called', async () => { library = makeGapi(undefined, /* timesout */ true); const promise2 = _loadGapi(auth); await expect(promise2).rejects.toThrow('auth/network-request-failed'); await expect(promise2).rejects.toBeInstanceOf(FirebaseError); }); it('resets the load promise if the load errors', async () => { library = {} as typeof gapi; const firstAttempt = _loadGapi(auth); await expect(firstAttempt).rejects.toThrow('auth/network-request-failed'); await expect(firstAttempt).rejects.toBeInstanceOf(FirebaseError); const secondAttempt = _loadGapi(auth); expect(secondAttempt).not.toBe(firstAttempt); await expect(secondAttempt).rejects.toThrow('auth/network-request-failed'); await expect(secondAttempt).rejects.toBeInstanceOf(FirebaseError); }); it('rejects if gapi itself does not load', async () => { const error = new Error(); loadJsStub.mockRejectedValue(error); await expect(_loadGapi(auth)).rejects.toThrow(error); }); });