Skip to content

Commit 8f476ac

Browse files
authored
Merge pull request #130 from magento-l3/Tier4-PR-Delivery-11-18-23
Tier4 PR Delivery 11.18.23
2 parents 5c6d694 + ad07241 commit 8f476ac

File tree

2 files changed

+146
-7
lines changed

2 files changed

+146
-7
lines changed

ReCaptchaFrontendUi/view/frontend/web/js/reCaptcha.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,14 @@ define(
7373
* @param {String} token
7474
*/
7575
reCaptchaCallback: function (token) {
76+
var submitButton;
77+
7678
if (this.getIsInvisibleRecaptcha()) {
7779
this.tokenField.value = token;
80+
submitButton = this.$parentForm.find('button:not([type]), [type=submit]');
81+
if (submitButton.length) { //eslint-disable-line max-depth
82+
submitButton.attr('disabled', false);
83+
}
7884
this.$parentForm.submit();
7985
}
8086
},
@@ -155,18 +161,17 @@ define(
155161

156162
if (this.getIsInvisibleRecaptcha() && parentForm.length > 0) {
157163
parentForm.submit(function (event) {
164+
var submitButton;
165+
158166
if (!this.tokenField.value) {
167+
submitButton = this.$parentForm.find('button:not([type]), [type=submit]');
168+
if (submitButton.length) { //eslint-disable-line max-depth
169+
submitButton.attr('disabled', true);
170+
}
159171
// eslint-disable-next-line no-undef
160172
grecaptcha.execute(widgetId);
161173
event.preventDefault(event);
162174
event.stopImmediatePropagation();
163-
if (this.$parentForm.valid()) {
164-
let formSubmitButton = this.$parentForm.find('button:not([type]), [type=submit]');
165-
166-
if (formSubmitButton.length) { //eslint-disable-line max-depth
167-
formSubmitButton.attr('disabled', true);
168-
}
169-
}
170175
}
171176
}.bind(this));
172177

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
/************************************************************************
2+
*
3+
* Copyright 2023 Adobe
4+
* All Rights Reserved.
5+
*
6+
* NOTICE: All information contained herein is, and remains
7+
* the property of Adobe and its suppliers, if any. The intellectual
8+
* and technical concepts contained herein are proprietary to Adobe
9+
* and its suppliers and are protected by all applicable intellectual
10+
* property laws, including trade secret and copyright laws.
11+
* Dissemination of this information or reproduction of this material
12+
* is strictly forbidden unless prior written permission is obtained
13+
* from Adobe.
14+
* ************************************************************************
15+
*/
16+
17+
/* eslint-disable max-nested-callbacks */
18+
define([
19+
'squire'
20+
], function (Squire) {
21+
'use strict';
22+
23+
var injector = new Squire(),
24+
mocks = {
25+
'Magento_ReCaptchaFrontendUi/js/registry': {
26+
ds: [],
27+
captchaList: [],
28+
tokenFields: []
29+
},
30+
'Magento_ReCaptchaFrontendUi/js/reCaptchaScriptLoader': {
31+
addReCaptchaScriptTag: jasmine.createSpy('reCaptchaScriptLoader.addReCaptchaScriptTag')
32+
},
33+
'Magento_ReCaptchaFrontendUi/js/nonInlineReCaptchaRenderer': {
34+
add: jasmine.createSpy('nonInlineReCaptchaRenderer.add')
35+
}
36+
},
37+
reCaptchaModel,
38+
formElement,
39+
submitButtonElement,
40+
$;
41+
42+
beforeEach(function (done) {
43+
injector.mock(mocks);
44+
injector.require(['jquery', 'Magento_ReCaptchaFrontendUi/js/reCaptcha'], function (jq, reCaptchaUiComponent) {
45+
reCaptchaModel = new reCaptchaUiComponent();
46+
$ = jq;
47+
done();
48+
});
49+
formElement = document.createElement('form');
50+
submitButtonElement = document.createElement('button');
51+
formElement.appendChild(submitButtonElement);
52+
window.document.body.appendChild(formElement);
53+
window.grecaptcha = {
54+
render: jasmine.createSpy('window.grecaptcha.render'),
55+
execute: jasmine.createSpy('window.grecaptcha.execute')
56+
};
57+
});
58+
59+
afterEach(function () {
60+
try {
61+
injector.clean();
62+
injector.remove();
63+
} catch (e) {
64+
}
65+
formElement.remove();
66+
formElement = undefined;
67+
submitButtonElement = undefined;
68+
});
69+
70+
describe('Magento_ReCaptchaFrontendUi/js/reCaptcha', function () {
71+
describe('Invisible ReCaptcha', function () {
72+
beforeEach(function () {
73+
reCaptchaModel.getIsInvisibleRecaptcha = jasmine.createSpy().and.returnValue(true);
74+
});
75+
76+
describe('"initParentForm" method', function () {
77+
it(
78+
'should disable submit button, prevent submit handlers from executing and execute recaptcha' +
79+
' on submit',
80+
function () {
81+
var request = {
82+
send: jasmine.createSpy('request.send')
83+
};
84+
85+
// check that submit button is enabled
86+
expect(submitButtonElement.disabled).toBeFalse();
87+
$(formElement).on('submit', function (event) {
88+
event.preventDefault();
89+
request.send();
90+
});
91+
reCaptchaModel.initParentForm($(formElement), 'test');
92+
$(formElement).submit();
93+
// check that submit button is disabled
94+
expect(submitButtonElement.disabled).toBeTrue();
95+
// check that grecaptcha is executed
96+
expect(window.grecaptcha.execute).toHaveBeenCalledOnceWith('test');
97+
// check that other submit handlers are not executed
98+
expect(request.send).not.toHaveBeenCalled();
99+
});
100+
101+
it('should add a token input field to the form', function () {
102+
submitButtonElement.disabled = true;
103+
reCaptchaModel.initParentForm($(formElement), 'test');
104+
expect(submitButtonElement.disabled).toBeFalse();
105+
expect(reCaptchaModel.tokenField).not.toBeNull();
106+
expect($(reCaptchaModel.tokenField).parents('form').is($(formElement))).toBeTrue();
107+
});
108+
});
109+
describe('"reCaptchaCallback" method', function () {
110+
it('should enable submit button, set token input value and submit the form', function () {
111+
var request = {
112+
send: jasmine.createSpy('request.send')
113+
};
114+
115+
submitButtonElement.disabled = true;
116+
reCaptchaModel.$parentForm = $(formElement);
117+
reCaptchaModel.tokenField = $('<input type="text" name="token" style="display: none" />')[0];
118+
119+
$(formElement).on('submit', function (event) {
120+
event.preventDefault();
121+
request.send();
122+
});
123+
reCaptchaModel.reCaptchaCallback('testtoken');
124+
// check that submit button is enabled
125+
expect(submitButtonElement.disabled).toBeFalse();
126+
// check that token input value is set
127+
expect(reCaptchaModel.tokenField.value).toEqual('testtoken');
128+
// check that form is submitted
129+
expect(request.send).toHaveBeenCalled();
130+
});
131+
});
132+
});
133+
});
134+
});

0 commit comments

Comments
 (0)