From 55405c2980430d96633967e7844da7841052fe96 Mon Sep 17 00:00:00 2001 From: ZhangYiJiang Date: Tue, 7 Sep 2021 14:27:03 -0700 Subject: [PATCH 1/2] Call execute after load if execute was called before ready --- src/index.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) 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) } From 8cb53d4ad2a242be0f7bac7b5c25d613f6b4b9f6 Mon Sep 17 00:00:00 2001 From: ZhangYiJiang Date: Mon, 13 Sep 2021 16:39:00 -0700 Subject: [PATCH 2/2] Add test case for execute race condition --- tests/hcaptcha.spec.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) 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(() => {