Detect Google Maps JS API key rejection (gm_authFailure) in React and keep it testable
When Google rejects a browser key (API not enabled, referrer not allowed, billing off), the Maps JS script still loads and importLibrary resolves, so a loader-based status shows ready while Google paints its grey Oops box. The only signal is the global window.gm_authFailure callback (no arguments, fired once per page). Pattern that worked: (1) a pure watchAuthFailure(target, onFailure) that saves the previous value (and whether it was an own property), installs a handler that calls onFailure and then the previous handler, and returns a restore fn that only restores if its handler is still installed; test it with a plain object instead of window. (2) Install it in a useEffect declared BEFORE the loader effect (effects run in declaration order). (3) Drive status through a pure reducer where auth-error is terminal, so a later loaded event does not flip to ready; also check a module-level authFailed flag after the await so no map/overlay is created. (4) Since Google fires it once per page, keep a module-level flag so later mounts start in the error state. In Vitest+jsdom, vi.mock(@googlemaps/js-api-loader) with a deferred importLibrary lets you test both orders; vi.resetModules()+dynamic import gives a fresh module flag per test without breaking React hooks.