forked from dozoisch/react-google-recaptcha
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecaptcha.spec.js
More file actions
103 lines (101 loc) · 3.21 KB
/
recaptcha.spec.js
File metadata and controls
103 lines (101 loc) · 3.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import React from "react";
import ReactDOM from "react-dom";
import ReactTestUtils from "react-dom/test-utils";
import ReCAPTCHA from "../src/recaptcha"; // eslint-disable-line no-unused-vars
describe("ReCAPTCHA", () => {
it("Rendered Component should be a div", () => {
const instance = ReactTestUtils.renderIntoDocument(
<ReCAPTCHA sitekey="xxx" onChange={jest.fn()} />,
);
expect(ReactDOM.findDOMNode(instance).nodeName).toBe("DIV");
});
it("Rendered Component should contained passed props", () => {
const props = {
className: "TheClassName",
id: "superdefinedId",
onChange: jest.fn(),
};
const instance = ReactTestUtils.renderIntoDocument(<ReCAPTCHA sitekey="xxx" {...props} />);
expect(ReactDOM.findDOMNode(instance).id).toBe(props.id);
expect(ReactDOM.findDOMNode(instance).className).toBe(props.className);
});
it("should call grecaptcha.render, when it is already loaded", () => {
return new Promise(resolve => {
const grecaptchaMock = {
render(node, options) {
expect(node).toBeTruthy();
expect(options.sitekey).toBe("xxx");
resolve();
},
};
const instance = ReactTestUtils.renderIntoDocument(
<ReCAPTCHA sitekey="xxx" grecaptcha={grecaptchaMock} onChange={jest.fn()} />,
);
expect(instance).toBeTruthy();
});
});
it("reset, should call grecaptcha.reset with the widget id", () => {
return new Promise(resolve => {
const WIDGET_ID = "someWidgetId";
const grecaptchaMock = {
render() {
return WIDGET_ID;
},
reset(widgetId) {
expect(widgetId).toBe(WIDGET_ID);
resolve();
},
};
const ReCaptchaRef = React.createRef();
ReactTestUtils.renderIntoDocument(
<ReCAPTCHA
sitekey="xxx"
grecaptcha={grecaptchaMock}
ref={ReCaptchaRef}
onChange={jest.fn()}
/>,
);
ReCaptchaRef.current.reset();
});
});
it("execute, should call grecaptcha.execute with the widget id", () => {
return new Promise(resolve => {
const WIDGET_ID = "someWidgetId";
const grecaptchaMock = {
render() {
return WIDGET_ID;
},
execute(widgetId) {
expect(widgetId).toBe(WIDGET_ID);
resolve();
},
};
// wrapping component example that applies a ref to ReCAPTCHA
class WrappingComponent extends React.Component {
constructor(props) {
super(props);
this._internalRef = React.createRef();
}
render() {
return (
<div>
<ReCAPTCHA
sitekey="xxx"
size="invisible"
grecaptcha={grecaptchaMock}
onChange={jest.fn()}
ref={this._internalRef}
/>
</div>
);
}
}
const instance = ReactTestUtils.renderIntoDocument(React.createElement(WrappingComponent));
instance._internalRef.current.execute();
});
});
describe.skip("Expired", () => {
it("should call onChange with null when response is expired");
it("should call onExpired when response is expired");
});
});