Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions lib/__tests__/unit/hcaptcha-loader-error.test.tsx
Original file line number Diff line number Diff line change
@@ -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(<HCaptcha sitekey="test-sitekey" onError={onError} />);
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(<HCaptcha sitekey="test-sitekey" onError={onError} />);
await Promise.resolve();
});

expect(onError.mock.calls).toEqual([
['script-error'],
['script-load-error-other'],
]);
});
});
25 changes: 0 additions & 25 deletions lib/__tests__/unit/hcaptcha.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(<HCaptcha
onError={onError}
sitekey={TEST_PROPS.sitekey}
sentry={false}
/>);
// 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(<HCaptcha
sitekey={TEST_PROPS.sitekey}
Expand Down
15 changes: 11 additions & 4 deletions lib/src/component/HCaptcha.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,16 @@ export class HCaptcha extends React.Component {
uj: userJourneys !== undefined ? userJourneys : false,
};

hCaptchaLoader(mountParams)
.then(this.handleOnLoad, this.handleError)
.catch(this.handleError);
hCaptchaLoader(mountParams).then(
this.handleOnLoad,
() => {
try {
this.handleError('script-error');
} catch {
this.handleError('script-load-error-other');
}
}
);

this.apiScriptRequested = true;
}
Expand Down Expand Up @@ -475,4 +482,4 @@ export class HCaptcha extends React.Component {
const { elementId } = this.state;
return <div ref={this.ref} id={elementId}></div>;
}
}
}
Loading