Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 5cec324

Browse files
committed
test(form): fix broken preventDefault test
the original test relied on incorrect assumptions about how jasmine async tests work (when setTimeout is triggered) and how browser reloads a page (the sequence of events) and thus the test passes even when the default is not prevented. this change fixes the test by registering an extra submit event handler that checks if the default was prevented. if the default was not prevented, the test will fail and the page will be reloaded causing the test runner to panic.
1 parent c25cb7d commit 5cec324

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

test/ng/directive/formSpec.js

+14-6
Original file line numberDiff line numberDiff line change
@@ -128,17 +128,25 @@ describe('form', function() {
128128
describe('preventing default submission', function() {
129129

130130
it('should prevent form submission', function() {
131-
var startingUrl = '' + window.location;
132-
doc = jqLite('<form name="myForm"><input type="submit" value="submit" />');
131+
var nextTurn = false,
132+
reloadPrevented;
133+
134+
doc = jqLite('<form><input type="submit" value="submit" ></form>');
133135
$compile(doc)(scope);
134136

137+
doc.bind('submit', function(e) {
138+
reloadPrevented = e.defaultPrevented;
139+
});
140+
135141
browserTrigger(doc.find('input'));
136-
waitsFor(
137-
function() { return true; },
138-
'let browser breath, so that the form submission can manifest itself', 10);
142+
143+
// let the browser process all events (and potentially reload the page)
144+
setTimeout(function() { nextTurn = true;});
145+
146+
waitsFor(function() { return nextTurn; });
139147

140148
runs(function() {
141-
expect('' + window.location).toEqual(startingUrl);
149+
expect(reloadPrevented).toBe(true);
142150
});
143151
});
144152

0 commit comments

Comments
 (0)