Skip to content

Commit 811af9c

Browse files
committed
test(*): fix tests involving submit on Chrome 60
On Chrome 60 (at least on Windows) the `submit` event when clicking on a submit button is not fired on the form element, unless it is already part of the DOM.
1 parent a784fab commit 811af9c

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

test/ng/directive/formSpec.js

+14-2
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,9 @@ describe('form', function() {
386386
doc = jqLite('<form ng-submit="submitMe()">' +
387387
'<input type="submit" value="submit">' +
388388
'</form>');
389+
// Support: Chrome 60+ (on Windows)
390+
// We need to add the form to the DOM in order for `submit` events to be properly fired.
391+
window.document.body.appendChild(doc[0]);
389392

390393
var assertPreventDefaultListener = function(e) {
391394
reloadPrevented = e.defaultPrevented || (e.returnValue === false);
@@ -420,15 +423,18 @@ describe('form', function() {
420423
inject(function($timeout) {
421424
doc = jqLite('<div>' +
422425
'<form ng-submit="submitMe()">' +
423-
'<button ng-click="destroy()"></button>' +
426+
'<button type="submit" ng-click="destroy()"></button>' +
424427
'</form>' +
425428
'</div>');
429+
// Support: Chrome 60+ (on Windows)
430+
// We need to add the form to the DOM in order for `submit` events to be properly fired.
431+
window.document.body.appendChild(doc[0]);
426432

427433
var form = doc.find('form'),
428434
destroyed = false,
429435
nextTurn = false,
430436
submitted = false,
431-
reloadPrevented;
437+
reloadPrevented = 'never called';
432438

433439
scope.destroy = function() {
434440
// yes, I know, scope methods should not do direct DOM manipulation, but I wanted to keep
@@ -466,6 +472,12 @@ describe('form', function() {
466472
// the issue in the wild, I'm not going to bother to do it
467473
// now. (i)
468474

475+
// Support: Chrome 60+ (on Windows)
476+
// Chrome 60+ on Windows does not fire `submit` events when the form is not attached to
477+
// the DOM. Verify that the `submit` listener was either never fired or (if fired) the
478+
// reload was prevented.
479+
expect(reloadPrevented).not.toBe(false);
480+
469481
// prevent mem leak in test
470482
form[0].removeEventListener('submit', assertPreventDefaultListener);
471483
})

test/ng/directive/ngEventDirsSpec.js

+16-6
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,20 @@ describe('event directives', function() {
1212
describe('ngSubmit', function() {
1313

1414
it('should get called on form submit', inject(function($rootScope, $compile) {
15-
element = $compile('<form action="/foo" ng-submit="submitted = true">' +
16-
'<input type="submit"/>' +
15+
element = $compile(
16+
'<form action="/foo" ng-submit="submitted = true">' +
17+
'<input type="submit" />' +
1718
'</form>')($rootScope);
1819
$rootScope.$digest();
1920

21+
// Support: Chrome 60+
22+
// We need to add the form to the DOM in order for `submit` events to be properly fired.
23+
window.document.body.appendChild(element[0]);
24+
2025
// prevent submit within the test harness
2126
element.on('submit', function(e) { e.preventDefault(); });
2227

23-
expect($rootScope.submitted).not.toBeDefined();
28+
expect($rootScope.submitted).toBeUndefined();
2429

2530
browserTrigger(element.children()[0]);
2631
expect($rootScope.submitted).toEqual(true);
@@ -33,15 +38,20 @@ describe('event directives', function() {
3338
}
3439
};
3540

36-
element = $compile('<form action="/foo" ng-submit="formSubmission($event)">' +
37-
'<input type="submit"/>' +
41+
element = $compile(
42+
'<form action="/foo" ng-submit="formSubmission($event)">' +
43+
'<input type="submit" />' +
3844
'</form>')($rootScope);
3945
$rootScope.$digest();
4046

47+
// Support: Chrome 60+ (on Windows)
48+
// We need to add the form to the DOM in order for `submit` events to be properly fired.
49+
window.document.body.appendChild(element[0]);
50+
4151
// prevent submit within the test harness
4252
element.on('submit', function(e) { e.preventDefault(); });
4353

44-
expect($rootScope.formSubmitted).not.toBeDefined();
54+
expect($rootScope.formSubmitted).toBeUndefined();
4555

4656
browserTrigger(element.children()[0]);
4757
expect($rootScope.formSubmitted).toEqual('foo');

0 commit comments

Comments
 (0)