From 4029788aca473a4e16e08fd93adf6cac6ecb8ef4 Mon Sep 17 00:00:00 2001 From: e271828- Date: Sat, 1 Aug 2026 06:35:46 -0400 Subject: [PATCH] change loader error handling --- .../unit/hcaptcha-loader-error.test.tsx | 49 +++++++++++++++++++ lib/__tests__/unit/hcaptcha.test.tsx | 25 ---------- lib/src/component/HCaptcha.jsx | 15 ++++-- 3 files changed, 60 insertions(+), 29 deletions(-) create mode 100644 lib/__tests__/unit/hcaptcha-loader-error.test.tsx diff --git a/lib/__tests__/unit/hcaptcha-loader-error.test.tsx b/lib/__tests__/unit/hcaptcha-loader-error.test.tsx new file mode 100644 index 0000000..9d5ede8 --- /dev/null +++ b/lib/__tests__/unit/hcaptcha-loader-error.test.tsx @@ -0,0 +1,49 @@ +import React from 'react'; +import { afterEach, beforeEach, describe, expect, it, jest } from '@jest/globals'; +import { act, cleanup, render } from '@testing-library/react'; + +jest.mock('@hcaptcha/loader', () => ({ + hCaptchaLoader: jest.fn(), +})); + +const mockHCaptchaLoader = require('@hcaptcha/loader').hCaptchaLoader; +const HCaptcha = require('../../src/index.js').default; + +describe('hCaptcha loader errors', () => { + beforeEach(() => { + window.hcaptcha = undefined; + mockHCaptchaLoader.mockReset(); + }); + + afterEach(() => { + cleanup(); + }); + + it('normalizes script load errors', async () => { + const onError = jest.fn(); + mockHCaptchaLoader.mockRejectedValueOnce(new Error('script-error')); + + await act(async () => { + render(); + await Promise.resolve(); + }); + + expect(onError).toHaveBeenCalledWith('script-error'); + }); + + it('reports when initial script error handling throws', async () => { + const onError = jest.fn() + .mockImplementationOnce(() => { throw new Error('error-handler-failed'); }); + mockHCaptchaLoader.mockRejectedValueOnce(new Error('script-error')); + + await act(async () => { + render(); + await Promise.resolve(); + }); + + expect(onError.mock.calls).toEqual([ + ['script-error'], + ['script-load-error-other'], + ]); + }); +}); diff --git a/lib/__tests__/unit/hcaptcha.test.tsx b/lib/__tests__/unit/hcaptcha.test.tsx index cbea6a8..4ca0e23 100644 --- a/lib/__tests__/unit/hcaptcha.test.tsx +++ b/lib/__tests__/unit/hcaptcha.test.tsx @@ -346,31 +346,6 @@ describe("hCaptcha", () => { jest.restoreAllMocks(); }); - // TODO: This test needs to be updated to work with @hcaptcha/loader - // The error callback is now triggered by loader rejection, not script onerror - it.skip("emits error when script is failed", async () => { - const onError = jest.fn(); - - // Make hCaptchaLoader reject to simulate script loading failure - const loader = require('@hcaptcha/loader'); - loader.hCaptchaLoader.mockImplementationOnce(() => { - return Promise.reject(new Error("script-error")); - }); - - await act(async () => { - render(); - // Wait for promise rejection to be handled - await new Promise(resolve => setTimeout(resolve, 0)); - }); - - expect(onError.mock.calls.length).toBe(1); - expect((onError.mock.calls[0][0] as Error).message).toEqual("script-error"); - }); - it("validate src without", () => { render( { + try { + this.handleError('script-error'); + } catch { + this.handleError('script-load-error-other'); + } + } + ); this.apiScriptRequested = true; } @@ -475,4 +482,4 @@ export class HCaptcha extends React.Component { const { elementId } = this.state; return
; } - } \ No newline at end of file + }