diff --git a/src/index.js b/src/index.js index 1642b82..fc89911 100644 --- a/src/index.js +++ b/src/index.js @@ -162,6 +162,10 @@ class HCaptcha extends React.Component { // render captcha this.renderCaptcha(); + + if (this.executeOnLoad) { + this.execute(); + } }); } @@ -199,7 +203,14 @@ class HCaptcha extends React.Component { execute () { const { isApiReady, isRemoved, captchaId } = this.state; - if (!isApiReady || isRemoved) return + if (!isApiReady) { + this.executeOnLoad = true; + return; + } + + if (isRemoved) { + return; + } hcaptcha.execute(captchaId) } diff --git a/tests/hcaptcha.spec.js b/tests/hcaptcha.spec.js index d88de89..a30d352 100644 --- a/tests/hcaptcha.spec.js +++ b/tests/hcaptcha.spec.js @@ -140,6 +140,31 @@ describe("hCaptcha", () => { expect(node.getAttribute("id")).toBe(null); }); + it("correctly executes even if execute() was called before load", () => { + const hcaptchaGlobal = window.hcaptcha; + delete window.hcaptcha; + + instance = ReactTestUtils.renderIntoDocument( + , + ); + + instance.execute(); + expect(hcaptchaGlobal.execute.mock.calls.length).toBe(0); + window.hcaptcha = hcaptchaGlobal; + instance.handleOnLoad(); + expect(hcaptchaGlobal.execute.mock.calls.length).toBe(1); + }) + describe("Query parameter", () => { beforeEach(() => {