Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Call execute after load if execute was called before ready #97

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
13 changes: 12 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,10 @@ class HCaptcha extends React.Component {

// render captcha
this.renderCaptcha();

if (this.executeOnLoad) {
this.execute();
Comment on lines +166 to +167
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are going to want to reset the this.executeOnLoad after you call this.execute.

}
});
}

Expand Down Expand Up @@ -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)
}
Expand Down
25 changes: 25 additions & 0 deletions tests/hcaptcha.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
<HCaptcha
sitekey={TEST_PROPS.sitekey}
theme={TEST_PROPS.theme}
size={TEST_PROPS.size}
tabindex={TEST_PROPS.tabindex}
onChange={mockFns.onChange}
onVerify={mockFns.onVerify}
onError={mockFns.onError}
onExpire={mockFns.onExpire}
onLoad={mockFns.onLoad}
/>,
);

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(() => {
Expand Down