From 6bec4712eacf6f0d0d1a937a04febb8c3f54ab80 Mon Sep 17 00:00:00 2001 From: Brad Peters Date: Thu, 4 Nov 2021 16:37:49 -0700 Subject: [PATCH 1/3] Fix onLoad race condition --- package.json | 2 +- src/index.js | 20 +++++++++++++------- tests/hcaptcha.spec.js | 21 ++++++++++++++++++++- 3 files changed, 34 insertions(+), 9 deletions(-) diff --git a/package.json b/package.json index dd4b12a..73bc663 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@hcaptcha/react-hcaptcha", - "version": "0.3.8", + "version": "0.3.9", "types": "types/index.d.ts", "main": "dist/index.js", "files": [ diff --git a/src/index.js b/src/index.js index df653c4..70e0d8e 100644 --- a/src/index.js +++ b/src/index.js @@ -59,6 +59,7 @@ class HCaptcha extends React.Component { } componentDidMount () { //Once captcha is mounted intialize hCaptcha - hCaptcha + const { apihost, assethost, endpoint, host, imghost, languageOverride:hl, reCaptchaCompat, reportapi, sentry, custom } = this.props; const { isApiReady } = this.state; @@ -88,6 +89,7 @@ class HCaptcha extends React.Component { } componentWillUnmount() { + const { isApiReady, isRemoved, captchaId } = this.state; if(!isApiReady || isRemoved) return @@ -119,7 +121,8 @@ class HCaptcha extends React.Component { } } - renderCaptcha() { + renderCaptcha(onReady) { + const { isApiReady } = this.state; if (!isApiReady) return; @@ -132,7 +135,9 @@ class HCaptcha extends React.Component { "callback" : this.handleSubmit, }); - this.setState({ isRemoved: false, captchaId }); + this.setState({ isRemoved: false, captchaId }, () => { + onReady && onReady(); + }); } resetCaptcha() { @@ -156,12 +161,13 @@ class HCaptcha extends React.Component { handleOnLoad () { this.setState({ isApiReady: true }, () => { - // trigger onLoad if it exists - const { onLoad } = this.props; - if (onLoad) onLoad(); - // render captcha - this.renderCaptcha(); + // render captcha and wait for captcha id + this.renderCaptcha(() => { + // trigger onLoad if it exists + const { onLoad } = this.props; + if (onLoad) onLoad(); + }); }); } diff --git a/tests/hcaptcha.spec.js b/tests/hcaptcha.spec.js index c78d532..795c183 100644 --- a/tests/hcaptcha.spec.js +++ b/tests/hcaptcha.spec.js @@ -1,6 +1,7 @@ -import React from "react"; +import React, { useRef } from "react"; import ReactDOM from "react-dom"; import ReactTestUtils, { act } from "react-dom/test-utils"; +import waitForExpect from "wait-for-expect"; import {getMockedHcaptcha, MOCK_EKEY, MOCK_TOKEN, MOCK_WIDGET_ID} from "./hcaptcha.mock"; let HCaptcha; @@ -177,6 +178,24 @@ describe("hCaptcha", () => { expect(node.getAttribute("id")).toBe(null); }); + it("should not set id if no id prop is passed", (done) => { + + const onLoad = jest.fn(() => { + expect(instance.state.captchaId).toBe(MOCK_WIDGET_ID); + done(); + }); + + instance = ReactTestUtils.renderIntoDocument( + , + ); + + instance.handleOnLoad(); + }); + + describe("Query parameter", () => { beforeEach(() => { From 9bf548ffab813c41a2696671c98f1d023c48c907 Mon Sep 17 00:00:00 2001 From: Brad Peters Date: Thu, 4 Nov 2021 16:41:09 -0700 Subject: [PATCH 2/3] remove unused npm package --- tests/hcaptcha.spec.js | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/hcaptcha.spec.js b/tests/hcaptcha.spec.js index 795c183..03b301c 100644 --- a/tests/hcaptcha.spec.js +++ b/tests/hcaptcha.spec.js @@ -1,7 +1,6 @@ import React, { useRef } from "react"; import ReactDOM from "react-dom"; import ReactTestUtils, { act } from "react-dom/test-utils"; -import waitForExpect from "wait-for-expect"; import {getMockedHcaptcha, MOCK_EKEY, MOCK_TOKEN, MOCK_WIDGET_ID} from "./hcaptcha.mock"; let HCaptcha; From c012af6617ecf9627040b9c64e7eb08cc16141c9 Mon Sep 17 00:00:00 2001 From: Brad Peters Date: Thu, 4 Nov 2021 16:42:07 -0700 Subject: [PATCH 3/3] remove extra new lines --- src/index.js | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/index.js b/src/index.js index 70e0d8e..e44146b 100644 --- a/src/index.js +++ b/src/index.js @@ -59,7 +59,6 @@ class HCaptcha extends React.Component { } componentDidMount () { //Once captcha is mounted intialize hCaptcha - hCaptcha - const { apihost, assethost, endpoint, host, imghost, languageOverride:hl, reCaptchaCompat, reportapi, sentry, custom } = this.props; const { isApiReady } = this.state; @@ -89,7 +88,6 @@ class HCaptcha extends React.Component { } componentWillUnmount() { - const { isApiReady, isRemoved, captchaId } = this.state; if(!isApiReady || isRemoved) return @@ -122,7 +120,6 @@ class HCaptcha extends React.Component { } renderCaptcha(onReady) { - const { isApiReady } = this.state; if (!isApiReady) return;