Skip to content

Commit

Permalink
Add invisible props and execute method
Browse files Browse the repository at this point in the history
  • Loading branch information
ptondereau committed Mar 16, 2017
1 parent cbfe092 commit 34d5e0c
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 51 deletions.
2 changes: 1 addition & 1 deletion src/recaptcha-wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ const globalName = "grecaptcha";
export default makeAsyncScriptLoader(ReCAPTCHA, URL, {
callbackName: callbackName,
globalName: globalName,
exposeFuncs: ["getValue", "reset"],
exposeFuncs: ["getValue", "reset", "execute"],
});
11 changes: 10 additions & 1 deletion src/recaptcha.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const ReCAPTCHA = React.createClass({
type: PropTypes.oneOf(["image", "audio"]),
tabindex: PropTypes.number,
onExpired: PropTypes.func,
size: PropTypes.oneOf(["compact", "normal"]),
size: PropTypes.oneOf(["compact", "normal", "invisible"]),
stoken: PropTypes.string,
},

Expand All @@ -34,6 +34,15 @@ const ReCAPTCHA = React.createClass({
return null;
},

execute() {
const { grecaptcha } = this.props;
const { widgetId } = this.state;

if (grecaptcha && widgetId !== undefined) {
return grecaptcha.execute(widgetId);
}
},

reset() {
if (this.props.grecaptcha && this.state.widgetId !== undefined) {
this.props.grecaptcha.reset(this.state.widgetId);
Expand Down
114 changes: 65 additions & 49 deletions test/recaptcha-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,55 +4,71 @@ import ReactTestUtils from "react-addons-test-utils";
import ReCAPTCHA from "../src/recaptcha";

describe("ReCAPTCHA", () => {
it("Rendered Component should be a div", () => {
let instance = ReactTestUtils.renderIntoDocument(
<ReCAPTCHA sitekey="xxx" />
);
assert.equal(ReactDOM.findDOMNode(instance).nodeName, "DIV");
});
it("Rendered Component should contained passed props", () => {
let props = {
className: "TheClassName",
id: "superdefinedId",
};
let instance = ReactTestUtils.renderIntoDocument(
<ReCAPTCHA sitekey="xxx" {...props} />
);
assert.equal(ReactDOM.findDOMNode(instance).id, props.id);
assert.match(ReactDOM.findDOMNode(instance).className, new RegExp(props.className));
});
it("Rendered Component should be a div", () => {
let instance = ReactTestUtils.renderIntoDocument(
<ReCAPTCHA sitekey="xxx" />
);
assert.equal(ReactDOM.findDOMNode(instance).nodeName, "DIV");
});
it("Rendered Component should contained passed props", () => {
let props = {
className: "TheClassName",
id: "superdefinedId",
};
let instance = ReactTestUtils.renderIntoDocument(
<ReCAPTCHA sitekey="xxx" {...props} />
);
assert.equal(ReactDOM.findDOMNode(instance).id, props.id);
assert.match(ReactDOM.findDOMNode(instance).className, new RegExp(props.className));
});

it("should call grecaptcha.render, when it is already loaded", (done) => {
let grecaptchaMock = {
render(node, options) {
assert.isNotNull(node);
assert.equal(options.sitekey, "xxx");
done();
},
};
let instance = ReactTestUtils.renderIntoDocument(
<ReCAPTCHA sitekey="xxx" grecaptcha={grecaptchaMock} />
);
assert.ok(instance);
});
it("reset, should call grecaptcha.reset with the widget id", (done) => {
let grecaptchaMock = {
render() {
return "someWidgetId";
},
it("should call grecaptcha.render, when it is already loaded", (done) => {
let grecaptchaMock = {
render(node, options) {
assert.isNotNull(node);
assert.equal(options.sitekey, "xxx");
done();
},
};
let instance = ReactTestUtils.renderIntoDocument(
<ReCAPTCHA sitekey="xxx" grecaptcha={grecaptchaMock} />
);
assert.ok(instance);
});
it("reset, should call grecaptcha.reset with the widget id", (done) => {
let grecaptchaMock = {
render() {
return "someWidgetId";
},

reset(widgetId) {
assert.isNotNull(widgetId);
done();
},
};
let instance = ReactTestUtils.renderIntoDocument(
<ReCAPTCHA sitekey="xxx" grecaptcha={grecaptchaMock} />
);
instance.reset();
});
describe("Expired", () => {
it("should call onChange with null when response is expired");
it("should call onExpired when response is expired");
});
reset(widgetId) {
assert.isNotNull(widgetId);
done();
},
};
let instance = ReactTestUtils.renderIntoDocument(
<ReCAPTCHA sitekey="xxx" grecaptcha={grecaptchaMock} />
);
instance.reset();
});
it("execute, should call grecaptcha.execute with the widget id", (done) => {
let grecaptchaMock = {
render() {
return "someWidgetId";
},

execute(widgetId) {
assert.isNotNull(widgetId);
done();
},
};
let instance = ReactTestUtils.renderIntoDocument(
<ReCAPTCHA sitekey="xxx" size="invisible" grecaptcha={grecaptchaMock} />
);
instance.execute();
});
describe("Expired", () => {
it("should call onChange with null when response is expired");
it("should call onExpired when response is expired");
});
});

0 comments on commit 34d5e0c

Please sign in to comment.