Skip to content

Commit 91ccc9a

Browse files
committed
fix(XHRImpl): fix errors, add a spec
fixes angular#1715
1 parent ec90fcd commit 91ccc9a

File tree

7 files changed

+59
-10
lines changed

7 files changed

+59
-10
lines changed

karma-dart.conf.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ module.exports = function(config) {
1919
{pattern: 'packages/**/*.dart', included: false, watched: false},
2020

2121
// Init and configure guiness.
22-
{pattern: 'test-main.dart', included: true}
22+
{pattern: 'test-main.dart', included: true},
23+
{pattern: 'modules/**/test/**/static_assets/**', included: false, watched: false},
2324
],
2425

2526
exclude: [

karma-js.conf.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ module.exports = function(config) {
2323
'node_modules/rx/dist/rx.js',
2424
'node_modules/reflect-metadata/Reflect.js',
2525
'tools/build/file2modulename.js',
26-
'test-main.js'
26+
'test-main.js',
27+
{pattern: 'modules/**/test/**/static_assets/**', included: false, watched: false}
2728
],
2829

2930
exclude: [
Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
library angular2.src.services.xhr_impl;
22

3-
import 'dart:async';
4-
import 'dart:html';
3+
import 'dart:async' show Future;
4+
import 'dart:html' show HttpRequest;
55
import 'package:angular2/di.dart';
66
import './xhr.dart' show XHR;
77

88
@Injectable()
99
class XHRImpl extends XHR {
1010
Future<String> get(String url) {
11-
return HttpRequest.request(url).then(
12-
(HttpRequest request) => request.responseText,
13-
onError: (Error e) => throw 'Failed to load $url');
11+
12+
return HttpRequest
13+
.request(url)
14+
.then((HttpRequest req) => req.responseText,
15+
onError: (_) => new Future.error('Failed to load $url'));
1416
}
1517
}

modules/angular2/src/services/xhr_impl.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ export class XHRImpl extends XHR {
1515
if (200 <= status && status <= 300) {
1616
completer.resolve(xhr.responseText);
1717
} else {
18-
completer.reject(`Failed to load ${url}`);
18+
completer.reject(`Failed to load ${url}`, null);
1919
}
2020
};
2121

22-
xhr.onerror = function() { completer.reject(`Failed to load ${url}`); };
22+
xhr.onerror = function() { completer.reject(`Failed to load ${url}`, null); };
2323

2424
xhr.send();
2525
return completer.promise;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<p>hey</p>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import {
2+
AsyncTestCompleter,
3+
beforeEach,
4+
ddescribe,
5+
describe,
6+
expect,
7+
iit,
8+
inject,
9+
it,
10+
xit
11+
} from 'angular2/test_lib';
12+
13+
import {XHRImpl} from 'angular2/src/services/xhr_impl';
14+
import {PromiseWrapper} from 'angular2/src/facade/async';
15+
16+
export function main() {
17+
describe('XHRImpl', () => {
18+
var xhr;
19+
var url200 = '/base/modules/angular2/test/services/static_assets/200.html';
20+
var url404 = '/base/modules/angular2/test/services/static_assets/404.html';
21+
22+
beforeEach(() => {
23+
xhr = new XHRImpl();
24+
});
25+
26+
it('should resolve the Promise with the file content on success', inject([AsyncTestCompleter], (async) => {
27+
xhr.get(url200).then((text) => {
28+
expect(text.trim()).toEqual('<p>hey</p>');
29+
async.done();
30+
});
31+
}));
32+
33+
it('should reject the Promise on failure', inject([AsyncTestCompleter], (async) => {
34+
PromiseWrapper.catchError(
35+
xhr.get(url404),
36+
(e) => {
37+
expect(e).toEqual(`Failed to load ${url404}`);
38+
async.done();
39+
}
40+
);
41+
}));
42+
});
43+
}

tools/broccoli/trees/node_tree.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ module.exports = function makeNodeTree(destinationPath) {
2222
exclude: [
2323
// the following code and tests are not compatible with CJS/node environment
2424
'angular2/test/core/zone/**',
25-
'angular2/test/test_lib/fake_async_spec.js'
25+
'angular2/test/test_lib/fake_async_spec.js',
26+
'angular2/test/services/xhr_impl_spec.js'
2627
]
2728
});
2829

0 commit comments

Comments
 (0)