Skip to content

Commit 285d829

Browse files
committed
ACP2E-2509: JS errors on login form when re-captcha enabled
- Added test coverage
1 parent 38a6fd0 commit 285d829

File tree

1 file changed

+134
-0
lines changed
  • dev/tests/js/jasmine/tests/app/code/Magento/ReCaptchaFrontendUi/frontend/js

1 file changed

+134
-0
lines changed
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)