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

Commit 3bcaa8f

Browse files
committed
Merge pull request #40 from garlicnation/wct
Migrate core-ajax to web-component-tester tests.
2 parents f6fa664 + 8a1ba31 commit 3bcaa8f

10 files changed

+551
-298
lines changed

test/core-ajax-progress.html

+113
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
<!doctype html>
2+
<!--
3+
Copyright (c) 2014 The Polymer Project Authors. All rights reserved.
4+
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
5+
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
6+
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
7+
Code distributed by Google as part of the polymer project is also
8+
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
9+
-->
10+
<html>
11+
<head>
12+
<title>core-ajax</title>
13+
14+
<script src="../../webcomponentsjs/webcomponents.js"></script>
15+
<script src="../../web-component-tester/browser.js"></script>
16+
17+
18+
19+
<link rel="import" href="../core-ajax.html">
20+
21+
</head>
22+
<body>
23+
24+
<core-ajax
25+
handleAs="json"
26+
auto></core-ajax>
27+
28+
<!--
29+
Test consistency of core-ajax's loading properties.
30+
-->
31+
<script>
32+
test('progress', function(done) {
33+
var ajax = document.querySelector("core-ajax");
34+
var xhr = sinon.useFakeXMLHttpRequest();
35+
var headers = {
36+
"Content-Type": "text/json"
37+
};
38+
var body = '{"content": "plentiful"}'
39+
var requests = this.requests = [];
40+
xhr.onCreate = function (xhr) {
41+
requests.push(xhr);
42+
// Polymer inspects the xhr object for the precense of onprogress to determine
43+
// whether to attach an event listener.
44+
xhr['onprogress'] = null;
45+
};
46+
var animationFrameFlush = function(callback) {
47+
requestAnimationFrame(function(){
48+
flush(callback);
49+
})
50+
};
51+
var progressEvent = function(lengthComputable, loaded, total) {
52+
var progress = new ProgressEvent('progress', {
53+
lengthComputable: lengthComputable,
54+
loaded: loaded,
55+
total: total
56+
});
57+
return progress;
58+
}
59+
60+
// Fake a file download by sending multiple progress events.
61+
async.series([
62+
function(cb) {
63+
ajax.url="http://example.org/downloadLargeFile"
64+
cb();
65+
},
66+
flush,
67+
animationFrameFlush,
68+
function(cb) {
69+
requests[0].dispatchEvent(progressEvent(true, 10, 100));
70+
cb();
71+
},
72+
flush,
73+
animationFrameFlush,
74+
function(cb) {
75+
assert(ajax.loading === true,
76+
"Request partially complete, but loading property was false.");
77+
var progress = ajax.progress;
78+
assert(progress.lengthComputable, "Progress should be computable");
79+
assert(progress.loaded == 10, "Expected 10 bytes loaded, got " + progress.loaded);
80+
assert(progress.total == 100, "Expected 100 bytes total, got " + progress.total);
81+
cb();
82+
},
83+
animationFrameFlush,
84+
function(cb) {
85+
requests[0].dispatchEvent(progressEvent(true, 100, 100));
86+
cb();
87+
},
88+
animationFrameFlush,
89+
function(cb) {
90+
assert(ajax.loading === true,
91+
"Request partially complete, but loading property was false.");
92+
var progress = ajax.progress;
93+
assert(progress.lengthComputable, "Progress should be computable");
94+
assert(progress.loaded == 100, "Expected 10 bytes loaded, got " + progress.loaded);
95+
assert(progress.total == 100, "Expected 100 bytes total, got " + progress.total);
96+
cb();
97+
},
98+
function(cb) {
99+
requests[0].respond(200, headers, body);
100+
cb();
101+
},
102+
animationFrameFlush,
103+
function(cb) {
104+
assert(ajax.loading === false,
105+
"Request complete, but loading property was true.");
106+
assert(ajax.response.content === "plentiful", "response not parsed");
107+
cb();
108+
}
109+
], done);
110+
});
111+
</script>
112+
</body>
113+
</html>

test/core-ajax-race.html

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<!doctype html>
2+
<!--
3+
Copyright (c) 2014 The Polymer Project Authors. All rights reserved.
4+
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
5+
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
6+
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
7+
Code distributed by Google as part of the polymer project is also
8+
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
9+
-->
10+
<html>
11+
<head>
12+
<title>core-ajax</title>
13+
14+
<script src="../../webcomponentsjs/webcomponents.js"></script>
15+
<script src="../../web-component-tester/browser.js"></script>
16+
17+
18+
19+
<link rel="import" href="../core-ajax.html">
20+
21+
</head>
22+
<body>
23+
24+
<core-ajax
25+
handleAs="json"
26+
auto></core-ajax>
27+
28+
<!--
29+
Test that when core-ajax fires multiple times as requests are updated,
30+
only the response from the most recent request is used to update the response
31+
object.
32+
-->
33+
<script>
34+
test('race-condition', function(done) {
35+
var ajax = document.querySelector("core-ajax");
36+
var xhr = sinon.useFakeXMLHttpRequest();
37+
var headers = {
38+
"Content-Type": "text/json"
39+
};
40+
var body = function(url) {
41+
return '{"url": "' + url + '"}';
42+
};
43+
var requests = [];
44+
xhr.onCreate = function (xhr) {
45+
requests.push(xhr);
46+
};
47+
var animationFrameFlush = function(callback) {
48+
requestAnimationFrame(function(){
49+
flush(callback);
50+
})
51+
};
52+
53+
// Make request1, then request2. request2 returns first, followed by request1.
54+
async.series([
55+
function(cb) {
56+
ajax.url="http://example.org/request1"
57+
cb();
58+
},
59+
flush,
60+
animationFrameFlush,
61+
function(cb) {
62+
ajax.url="http://example.org/request2"
63+
cb();
64+
},
65+
flush,
66+
animationFrameFlush,
67+
function(cb) {
68+
requests[0].respond(200, headers, body("http://example.org/request2"));
69+
cb();
70+
},
71+
flush,
72+
function(cb) {
73+
requests[1].respond(200, headers, body("http://example.org/request1"));
74+
cb();
75+
},
76+
flush,
77+
function(cb) {
78+
assert(ajax.response.url.match('request1'),
79+
"Race condition detected. An earlier request's delayed response " +
80+
"caused the more recent request's response to be overwritten.");
81+
done();
82+
cb();
83+
}
84+
], function(){});
85+
});
86+
</script>
87+
</body>
88+
</html>

0 commit comments

Comments
 (0)