fix(XHRImpl): fix errors, add a spec

fixes #1715
This commit is contained in:
Victor Berchet 2015-05-19 19:27:03 +02:00
parent ec90fcd290
commit 91ccc9af98
7 changed files with 59 additions and 10 deletions

View File

@ -19,7 +19,8 @@ module.exports = function(config) {
{pattern: 'packages/**/*.dart', included: false, watched: false}, {pattern: 'packages/**/*.dart', included: false, watched: false},
// Init and configure guiness. // Init and configure guiness.
{pattern: 'test-main.dart', included: true} {pattern: 'test-main.dart', included: true},
{pattern: 'modules/**/test/**/static_assets/**', included: false, watched: false},
], ],
exclude: [ exclude: [

View File

@ -23,7 +23,8 @@ module.exports = function(config) {
'node_modules/rx/dist/rx.js', 'node_modules/rx/dist/rx.js',
'node_modules/reflect-metadata/Reflect.js', 'node_modules/reflect-metadata/Reflect.js',
'tools/build/file2modulename.js', 'tools/build/file2modulename.js',
'test-main.js' 'test-main.js',
{pattern: 'modules/**/test/**/static_assets/**', included: false, watched: false}
], ],
exclude: [ exclude: [

View File

@ -1,15 +1,17 @@
library angular2.src.services.xhr_impl; library angular2.src.services.xhr_impl;
import 'dart:async'; import 'dart:async' show Future;
import 'dart:html'; import 'dart:html' show HttpRequest;
import 'package:angular2/di.dart'; import 'package:angular2/di.dart';
import './xhr.dart' show XHR; import './xhr.dart' show XHR;
@Injectable() @Injectable()
class XHRImpl extends XHR { class XHRImpl extends XHR {
Future<String> get(String url) { Future<String> get(String url) {
return HttpRequest.request(url).then(
(HttpRequest request) => request.responseText, return HttpRequest
onError: (Error e) => throw 'Failed to load $url'); .request(url)
.then((HttpRequest req) => req.responseText,
onError: (_) => new Future.error('Failed to load $url'));
} }
} }

View File

@ -15,11 +15,11 @@ export class XHRImpl extends XHR {
if (200 <= status && status <= 300) { if (200 <= status && status <= 300) {
completer.resolve(xhr.responseText); completer.resolve(xhr.responseText);
} else { } else {
completer.reject(`Failed to load ${url}`); completer.reject(`Failed to load ${url}`, null);
} }
}; };
xhr.onerror = function() { completer.reject(`Failed to load ${url}`); }; xhr.onerror = function() { completer.reject(`Failed to load ${url}`, null); };
xhr.send(); xhr.send();
return completer.promise; return completer.promise;

View File

@ -0,0 +1 @@
<p>hey</p>

View File

@ -0,0 +1,43 @@
import {
AsyncTestCompleter,
beforeEach,
ddescribe,
describe,
expect,
iit,
inject,
it,
xit
} from 'angular2/test_lib';
import {XHRImpl} from 'angular2/src/services/xhr_impl';
import {PromiseWrapper} from 'angular2/src/facade/async';
export function main() {
describe('XHRImpl', () => {
var xhr;
var url200 = '/base/modules/angular2/test/services/static_assets/200.html';
var url404 = '/base/modules/angular2/test/services/static_assets/404.html';
beforeEach(() => {
xhr = new XHRImpl();
});
it('should resolve the Promise with the file content on success', inject([AsyncTestCompleter], (async) => {
xhr.get(url200).then((text) => {
expect(text.trim()).toEqual('<p>hey</p>');
async.done();
});
}));
it('should reject the Promise on failure', inject([AsyncTestCompleter], (async) => {
PromiseWrapper.catchError(
xhr.get(url404),
(e) => {
expect(e).toEqual(`Failed to load ${url404}`);
async.done();
}
);
}));
});
}

View File

@ -22,7 +22,8 @@ module.exports = function makeNodeTree(destinationPath) {
exclude: [ exclude: [
// the following code and tests are not compatible with CJS/node environment // the following code and tests are not compatible with CJS/node environment
'angular2/test/core/zone/**', 'angular2/test/core/zone/**',
'angular2/test/test_lib/fake_async_spec.js' 'angular2/test/test_lib/fake_async_spec.js',
'angular2/test/services/xhr_impl_spec.js'
] ]
}); });