Skip to content

Commit ca36699

Browse files
committed
add tests for expired and errored
1 parent 3ab161d commit ca36699

File tree

1 file changed

+94
-41
lines changed

1 file changed

+94
-41
lines changed

test/recaptcha.spec.js

Lines changed: 94 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,85 @@ describe("ReCAPTCHA", () => {
3737
});
3838
});
3939
it("reset, should call grecaptcha.reset with the widget id", () => {
40-
return new Promise(resolve => {
40+
const WIDGET_ID = "someWidgetId";
41+
const grecaptchaMock = {
42+
render() {
43+
return WIDGET_ID;
44+
},
45+
reset: jest.fn(),
46+
};
47+
const ReCaptchaRef = React.createRef();
48+
ReactTestUtils.renderIntoDocument(
49+
<ReCAPTCHA
50+
sitekey="xxx"
51+
grecaptcha={grecaptchaMock}
52+
ref={ReCaptchaRef}
53+
onChange={jest.fn()}
54+
/>,
55+
);
56+
ReCaptchaRef.current.reset();
57+
expect(grecaptchaMock.reset).toBeCalledWith(WIDGET_ID);
58+
});
59+
it("execute, should call grecaptcha.execute with the widget id", () => {
60+
const WIDGET_ID = "someWidgetId";
61+
const grecaptchaMock = {
62+
render() {
63+
return WIDGET_ID;
64+
},
65+
execute: jest.fn(),
66+
};
67+
// wrapping component example that applies a ref to ReCAPTCHA
68+
class WrappingComponent extends React.Component {
69+
constructor(props) {
70+
super(props);
71+
this._internalRef = React.createRef();
72+
}
73+
render() {
74+
return (
75+
<div>
76+
<ReCAPTCHA
77+
sitekey="xxx"
78+
size="invisible"
79+
grecaptcha={grecaptchaMock}
80+
onChange={jest.fn()}
81+
ref={this._internalRef}
82+
/>
83+
</div>
84+
);
85+
}
86+
}
87+
const instance = ReactTestUtils.renderIntoDocument(React.createElement(WrappingComponent));
88+
instance._internalRef.current.execute();
89+
expect(grecaptchaMock.execute).toBeCalledWith(WIDGET_ID);
90+
});
91+
describe("Expired", () => {
92+
it("should call onChange with null when response is expired", () => {
4193
const WIDGET_ID = "someWidgetId";
94+
const onChange = jest.fn();
4295
const grecaptchaMock = {
4396
render() {
4497
return WIDGET_ID;
4598
},
46-
reset(widgetId) {
47-
expect(widgetId).toBe(WIDGET_ID);
48-
resolve();
99+
};
100+
const ReCaptchaRef = React.createRef();
101+
ReactTestUtils.renderIntoDocument(
102+
<ReCAPTCHA
103+
sitekey="xxx"
104+
grecaptcha={grecaptchaMock}
105+
ref={ReCaptchaRef}
106+
onChange={onChange}
107+
/>,
108+
);
109+
ReCaptchaRef.current.handleExpired();
110+
expect(onChange).toBeCalledWith(null);
111+
});
112+
it("should call onExpired when response is expired", () => {
113+
const WIDGET_ID = "someWidgetId";
114+
const onChange = jest.fn();
115+
const onExpired = jest.fn();
116+
const grecaptchaMock = {
117+
render() {
118+
return WIDGET_ID;
49119
},
50120
};
51121
const ReCaptchaRef = React.createRef();
@@ -54,53 +124,36 @@ describe("ReCAPTCHA", () => {
54124
sitekey="xxx"
55125
grecaptcha={grecaptchaMock}
56126
ref={ReCaptchaRef}
57-
onChange={jest.fn()}
127+
onChange={onChange}
128+
onExpired={onExpired}
58129
/>,
59130
);
60-
ReCaptchaRef.current.reset();
131+
ReCaptchaRef.current.handleExpired();
132+
expect(onChange).not.toHaveBeenCalled();
133+
expect(onExpired).toHaveBeenCalled();
61134
});
62135
});
63-
it("execute, should call grecaptcha.execute with the widget id", () => {
64-
return new Promise(resolve => {
136+
describe("Errored", () => {
137+
it("should call onErrored when grecaptcha errored", () => {
65138
const WIDGET_ID = "someWidgetId";
139+
const onErrored = jest.fn();
66140
const grecaptchaMock = {
67141
render() {
68142
return WIDGET_ID;
69143
},
70-
execute(widgetId) {
71-
expect(widgetId).toBe(WIDGET_ID);
72-
resolve();
73-
},
74144
};
75-
// wrapping component example that applies a ref to ReCAPTCHA
76-
class WrappingComponent extends React.Component {
77-
constructor(props) {
78-
super(props);
79-
this._internalRef = React.createRef();
80-
}
81-
render() {
82-
return (
83-
<div>
84-
<ReCAPTCHA
85-
sitekey="xxx"
86-
size="invisible"
87-
grecaptcha={grecaptchaMock}
88-
onChange={jest.fn()}
89-
ref={this._internalRef}
90-
/>
91-
</div>
92-
);
93-
}
94-
}
95-
const instance = ReactTestUtils.renderIntoDocument(React.createElement(WrappingComponent));
96-
instance._internalRef.current.execute();
145+
const ReCaptchaRef = React.createRef();
146+
ReactTestUtils.renderIntoDocument(
147+
<ReCAPTCHA
148+
sitekey="xxx"
149+
grecaptcha={grecaptchaMock}
150+
ref={ReCaptchaRef}
151+
onChange={jest.fn()}
152+
onErrored={onErrored}
153+
/>,
154+
);
155+
ReCaptchaRef.current.handleErrored();
156+
expect(onErrored).toHaveBeenCalled();
97157
});
98158
});
99-
describe.skip("Expired", () => {
100-
it("should call onChange with null when response is expired");
101-
it("should call onExpired when response is expired");
102-
});
103-
describe.skip("Errored", () => {
104-
it("should call onErrored when grecaptcha errored");
105-
});
106159
});

0 commit comments

Comments
 (0)