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

fix(mount): Handle multiple calls to componentDidMount #191

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
29 changes: 23 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,14 @@ class HCaptcha extends React.Component {
this.apiScriptRequested = true;
}

renderCaptcha(onReady) {
const { isApiReady } = this.state;
if (!isApiReady) return;
renderCaptcha(onRender) {
const { isApiReady, captchaId } = this.state;
const { onReady } = this.props;

// Prevent calling hCaptcha render on two conditions:
// • API is not ready
// • Component has already been mounted
if (!isApiReady || captchaId) return;

const renderParams = Object.assign({
"open-callback" : this.handleOpen,
Expand All @@ -209,10 +214,11 @@ class HCaptcha extends React.Component {

const hcaptcha = this._hcaptcha;
//Render hCaptcha widget and provide necessary callbacks - hCaptcha
const captchaId = hcaptcha.render(this.ref.current, renderParams);
const id = hcaptcha.render(this.ref.current, renderParams);

this.setState({ isRemoved: false, captchaId }, () => {
onReady && onReady();
this.setState({ isRemoved: false, captchaId: id }, () => {
onRender && onRender();
onReady && onReady();
zoryana94 marked this conversation as resolved.
Show resolved Hide resolved
});
}

Expand Down Expand Up @@ -341,6 +347,17 @@ class HCaptcha extends React.Component {
return hcaptcha.execute(captchaId, opts);
}

close() {
const { captchaId } = this.state;
const hcaptcha = this._hcaptcha;

if (!this.isReady()) {
return;
}

return hcaptcha.close(captchaId);
}

setData (data) {
const { captchaId } = this.state;
const hcaptcha = this._hcaptcha;
Expand Down
15 changes: 10 additions & 5 deletions tests/hcaptcha.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,19 @@ describe("hCaptcha", () => {
expect(window.hcaptcha.setData.mock.calls[0][1]).toBe(dataObj);
});

it("emits onLoad event", () => {
it("should emit onLoad event if no hCaptcha ID is stored", () => {
instance.state.captchaId = '';
expect(mockFns.onLoad.mock.calls.length).toBe(0);
instance.handleOnLoad();
expect(mockFns.onLoad.mock.calls.length).toBe(1);
});

it("should not emit onLoad event if hCapthcha ID is found", () => {
instance.handleOnLoad();
expect(mockFns.onLoad.mock.calls.length).toBe(0);
});


it("emits verify with token and eKey", () => {
expect(mockFns.onVerify.mock.calls.length).toBe(0);
instance.handleSubmit();
Expand Down Expand Up @@ -324,19 +331,17 @@ describe("hCaptcha", () => {

it("should not set id if no id prop is passed", (done) => {

const onLoad = jest.fn(() => {
const onReady = jest.fn(() => {
expect(instance.state.captchaId).toBe(MOCK_WIDGET_ID);
done();
});

instance = ReactTestUtils.renderIntoDocument(
<HCaptcha
sitekey={TEST_PROPS.sitekey}
onLoad={onLoad}
onReady={onReady}
/>,
);

instance.handleOnLoad();
});


Expand Down