Skip to content
This repository was archived by the owner on Mar 13, 2018. It is now read-only.

Commit 856dcdb

Browse files
author
ajo
committed
Fix test. Migrate to animationFrameFlush(locally defined for now).
1 parent f7b4b6e commit 856dcdb

File tree

1 file changed

+60
-39
lines changed

1 file changed

+60
-39
lines changed

test/core-ajax-response-and-error.html

+60-39
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,19 @@
1414
<script src="../../webcomponentsjs/webcomponents.js"></script>
1515
<script src="../../web-component-tester/browser.js"></script>
1616

17-
<!--
17+
18+
19+
<link rel="import" href="../core-ajax.html">
20+
21+
</head>
22+
<body>
23+
24+
<core-ajax
25+
id="ajax"
26+
handleAs="json"
27+
auto></core-ajax>
28+
29+
<!--
1830
Test that when core-ajax fires multiple times as requests are updated,
1931
only the response from the most recent request is used to update the response
2032
object.
@@ -23,47 +35,56 @@
2335
test('race-condition', function(done) {
2436
var ajax = document.querySelector("core-ajax");
2537
var xhr = sinon.useFakeXMLHttpRequest();
38+
var headers = {
39+
"Content-Type": "text/json"
40+
};
41+
var body = function(url) {
42+
return '{"url": "' + url + '"}';
43+
};
44+
var requests = this.requests = [];
2645
xhr.onCreate = function (xhr) {
27-
var headers = {
28-
"Content-Type": "text/html"
29-
}
30-
var body = '{"url": "' + xhr.url + '"}';
31-
if (xhr.url.match('request1')) {
32-
window.setTimeout(function(){
33-
xhr.respond(200, headers, body);
34-
console.log('response 1 received');
35-
}, 200);
36-
} else if (xhr.url.match('request2')) {
37-
window.setTimeout(function(){
38-
xhr.respond(200, headers, body);
39-
console.log('response 2 received');
40-
}, 100);
41-
}
46+
requests.push(xhr);
47+
};
48+
var animationFrameFlush = function(callback) {
49+
requestAnimationFrame(function(){
50+
flush(callback);
51+
})
4252
};
43-
// Make a request that will return in .2 seconds.
44-
ajax.url="http://example.org/request1";
45-
// Make a request that will return in .1 seconds.
46-
ajax.url="http://example.org/request2";
47-
// Wait for the requests to resolve, then check that the later return
48-
// of request1 doesn't override.
49-
setTimeout(function(){
50-
assert(ajax.response.match('request1'),
51-
"Race condition detected. An earlier request's delayed response " +
53+
54+
// Make request1, then request2. request2 returns first, followed by request1.
55+
async.series([
56+
function(cb) {
57+
ajax.url="http://example.org/request1"
58+
cb();
59+
},
60+
animationFrameFlush,
61+
function(cb) {
62+
ajax.url="http://example.org/request2"
63+
cb();
64+
},
65+
animationFrameFlush,
66+
function(cb) {
67+
debugger;
68+
requests[0].respond(200, headers, body("http://example.org/request2"));
69+
cb();
70+
},
71+
flush,
72+
function(cb) {
73+
debugger;
74+
requests[1].respond(200, headers, body("http://example.org/request1"));
75+
cb();
76+
},
77+
flush,
78+
function(cb) {
79+
debugger
80+
assert(ajax.response.url.match('request1'),
81+
"Race condition detected. An earlier request's delayed response " +
5282
"caused the more recent request's response to be overwritten.");
53-
}, 300);
54-
console.log("test done");
55-
done();
83+
done();
84+
cb();
85+
}
86+
], function(){});
5687
});
57-
</script>
58-
59-
<link rel="import" href="../core-ajax.html">
60-
61-
</head>
62-
<body>
63-
64-
<core-ajax
65-
handleAs="json"
66-
auto></core-ajax>
67-
88+
</script>
6889
</body>
6990
</html>

0 commit comments

Comments
 (0)