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

Commit e7f1cf3

Browse files
committed
feat(tests): async tests, remove HttpFutures
1 parent 021a571 commit e7f1cf3

File tree

7 files changed

+109
-48
lines changed

7 files changed

+109
-48
lines changed

lib/angular.dart

-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,6 @@ class AngularModule extends Module {
131131
type(CacheFactory, CacheFactory);
132132
type(Http, Http);
133133
type(UrlRewriter, UrlRewriter);
134-
type(HttpFutures, HttpFutures);
135134
type(HttpBackend, HttpBackend);
136135
type(BlockCache, BlockCache);
137136
type(TemplateCache, TemplateCache);

lib/http.dart

+2-9
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
part of angular;
22

3-
// NOTE(deboer): This should be a generic utility class, but lets make sure
4-
// it works in this case first!
5-
class HttpFutures {
6-
async.Future value(x) => new async.Future.value(x);
7-
}
8-
93
class UrlRewriter {
104
String call(url) => url;
115
}
@@ -36,9 +30,8 @@ class Http {
3630
Map<String, async.Future<String>> _pendingRequests = <String, async.Future<String>>{};
3731
UrlRewriter rewriter;
3832
HttpBackend backend;
39-
HttpFutures futures;
4033

41-
Http(UrlRewriter this.rewriter, HttpBackend this.backend, HttpFutures this.futures);
34+
Http(UrlRewriter this.rewriter, HttpBackend this.backend);
4235

4336
async.Future<String> getString(String url,
4437
{bool withCredentials, void onProgress(ProgressEvent e), Cache cache}) {
@@ -64,7 +57,7 @@ class Http {
6457
}
6558
var cachedValue = cache != null ? cache.get(url) : null;
6659
if (cachedValue != null) {
67-
return futures.value(cachedValue);
60+
return new async.Future.value(cachedValue);
6861
}
6962
var result = backend.request(url,
7063
method: method,

test/_http.dart

+1-15
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class MockHttp extends Http {
88
Map<String, MockHttpData> gets = {};
99
List futures = [];
1010

11-
MockHttp(UrlRewriter rewriter, HttpBackend backend, HttpFutures futures) : super(rewriter, backend, futures);
11+
MockHttp(UrlRewriter rewriter, HttpBackend backend) : super(rewriter, backend);
1212

1313
expectGET(String url, String content, {int times: 1}) {
1414
gets[url] = new MockHttpData(content, times);
@@ -47,20 +47,6 @@ class MockHttpData {
4747
toString() => value;
4848
}
4949

50-
class MockHttpFutures extends HttpFutures {
51-
List completersAndValues = [];
52-
Future value(x) {
53-
var completer = new Completer.sync();
54-
completersAndValues.add([completer, x]);
55-
return completer.future;
56-
}
57-
58-
trigger() {
59-
completersAndValues.forEach((cv) => cv[0].complete(cv[1]));
60-
completersAndValues = [];
61-
}
62-
}
63-
6450
class MockHttpBackend extends HttpBackend {
6551
Map<String, MockHttpData> gets = {};
6652
List completersAndValues = [];

test/_specs.dart

+16
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ library ng_specs;
22

33

44
import 'dart:html';
5+
import 'dart:async' as dartAsync;
56
import 'package:unittest/unittest.dart' as unit;
67
import 'package:angular/debug.dart';
78
import 'dart:mirrors' as mirror;
@@ -154,6 +155,21 @@ class Logger implements List {
154155
noSuchMethod(Invocation invocation) => mirror.reflect(_list).delegate(invocation);
155156
}
156157

158+
List<Function> _asyncQueue = [];
159+
160+
nextTurn() {
161+
// copy the queue as it may change.
162+
var toRun = new List.from(_asyncQueue);
163+
_asyncQueue = [];
164+
toRun.forEach((fn) => fn());
165+
}
166+
167+
async(Function fn) =>
168+
() {
169+
dartAsync.runZonedExperimental(fn, onRunAsync: (asyncFn) => _asyncQueue.add(asyncFn));
170+
expect(_asyncQueue.isEmpty).toBe(true);
171+
};
172+
157173
class SpecInjector {
158174
Injector moduleInjector;
159175
Injector injector;

test/_specs_spec.dart

+72
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import "_specs.dart";
2+
import "dart:async";
23

34
main() {
45
describe('renderedText', () {
@@ -32,6 +33,77 @@ main() {
3233
expect(div.html()).toEqual('text');
3334
});
3435
});
36+
});
37+
38+
describe('async', () {
39+
it('should run synchronous code', () {
40+
var ran = false;
41+
async(() { ran = true; })();
42+
expect(ran).toBe(true);
43+
});
44+
45+
46+
it('should run async code', () {
47+
var ran = false;
48+
var thenRan = false;
49+
async(() {
50+
new Future.value('s').then((_) { thenRan = true; });
51+
expect(thenRan).toBe(false);
52+
nextTurn();
53+
expect(thenRan).toBe(true);
54+
ran = true;
55+
})();
56+
expect(ran).toBe(true);
57+
});
58+
59+
60+
it('should run chained thens', () {
61+
var log = [];
62+
async(() {
63+
new Future.value('s')
64+
.then((_) { log.add('firstThen'); })
65+
.then((_) { log.add('2ndThen'); });
66+
expect(log.join(' ')).toEqual('');
67+
nextTurn();
68+
expect(log.join(' ')).toEqual('firstThen 2ndThen');
69+
})();
70+
});
3571

72+
73+
it('shold run futures created in futures', () {
74+
var log = [];
75+
async(() {
76+
new Future.value('s')
77+
.then((_) {
78+
log.add('firstThen');
79+
new Future.value('t').then((_) {
80+
log.add('2ndThen');
81+
});
82+
});
83+
expect(log.join(' ')).toEqual('');
84+
nextTurn();
85+
expect(log.join(' ')).toEqual('firstThen');
86+
nextTurn();
87+
expect(log.join(' ')).toEqual('firstThen 2ndThen');
88+
})();
89+
});
90+
91+
92+
it('should complain if you dangle callbacks', () {
93+
expect(() {
94+
async(() {
95+
new Future.value("s").then((_) {});
96+
})();
97+
}).toThrow();
98+
});
99+
100+
101+
it('should complain if the test throws an exception', () {
102+
expect(() {
103+
async(() {
104+
throw "blah";
105+
})();
106+
}).toThrow("blah");
107+
});
36108
});
37109
}

test/directives/ng_include_spec.dart

+4-7
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,7 @@ main() {
88

99
beforeEach(beforeEachTestBed((tb) => _ = tb));
1010

11-
beforeEach(module((AngularModule module) {
12-
module.type(HttpFutures, MockHttpFutures);
13-
}));
14-
15-
it('should fetch tempalte from url', inject((Scope scope, TemplateCache cache, HttpFutures futures) {
11+
it('should fetch tempalte from url', async(inject((Scope scope, TemplateCache cache) {
1612
cache.put('tpl.html', new HttpResponse(200, 'my name is {{name}}'));
1713

1814
var element = _.compile('<div ng-include="template"></div>');
@@ -23,9 +19,10 @@ main() {
2319
scope['name'] = 'Vojta';
2420
scope['template'] = 'tpl.html';
2521
});
26-
futures.trigger();
22+
23+
nextTurn(); // load the template from cache.
2724
expect(element.text()).toEqual('my name is Vojta');
28-
}));
25+
})));
2926

3027

3128
it('should support inlined templates', inject((Scope scope) {

test/http_spec.dart

+14-16
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,14 @@ main() {
2020
var rewriter, futures, backend, cache;
2121
beforeEach(() {
2222
rewriter = new SubstringRewriter();
23-
futures = new MockHttpFutures();
2423
backend = new MockHttpBackend();
2524
cache = new FakeCache();
2625
});
2726

2827
it('should rewrite URLs before calling the backend', () {
2928
backend.expectGET('a', VALUE, times: 1);
3029

31-
var http = new Http(rewriter, backend, futures);
30+
var http = new Http(rewriter, backend);
3231
var called = 0;
3332
http.getString('a[not sent to backed]').then((v) {
3433
expect(v).toBe(VALUE);
@@ -46,7 +45,7 @@ main() {
4645
it('should support pending requests for different raw URLs', () {
4746
backend.expectGET('a', VALUE, times: 1);
4847

49-
var http = new Http(rewriter, backend, futures);
48+
var http = new Http(rewriter, backend);
5049
var called = 0;
5150
http.getString('a[some string]', cache: cache).then((v) {
5251
expect(v).toBe(VALUE);
@@ -63,8 +62,8 @@ main() {
6362
backend.assertAllGetsCalled();
6463
});
6564

66-
it('should support caching', () {
67-
var http = new Http(rewriter, backend, futures);
65+
it('should support caching', async(() {
66+
var http = new Http(rewriter, backend);
6867
var called = 0;
6968
http.getString('fromCache', cache: cache).then((v) {
7069
expect(v).toBe(CACHED_VALUE);
@@ -73,25 +72,24 @@ main() {
7372

7473
expect(called).toEqual(0);
7574
backend.flush();
76-
futures.trigger();
75+
nextTurn();
7776

7877
expect(called).toEqual(1);
7978
backend.assertAllGetsCalled();
80-
});
79+
}));
8180
});
8281

8382
describe('http caching', () {
84-
var rewriter, futures, backend, cache;
83+
var rewriter, backend, cache;
8584
beforeEach(() {
8685
rewriter = new UrlRewriter();
87-
futures = new MockHttpFutures();
8886
backend = new MockHttpBackend();
8987
cache = new FakeCache();
9088
});
9189
it('should not cache if no cache is present', () {
9290
backend.expectGET('a', VALUE, times: 2);
9391

94-
var http = new Http(rewriter, backend, futures);
92+
var http = new Http(rewriter, backend);
9593
var called = 0;
9694
http.getString('a').then((v) {
9795
expect(v).toBe(VALUE);
@@ -114,7 +112,7 @@ main() {
114112
it('should return a pending request', inject(() {
115113
backend.expectGET('a', VALUE, times: 1);
116114

117-
var http = new Http(rewriter, backend, futures);
115+
var http = new Http(rewriter, backend);
118116
var called = 0;
119117
http.getString('a', cache: cache).then((v) {
120118
expect(v).toBe(VALUE);
@@ -135,7 +133,7 @@ main() {
135133
it('should not return a pending request after the request is complete', () {
136134
backend.expectGET('a', VALUE, times: 2);
137135

138-
var http = new Http(rewriter, backend, futures);
136+
var http = new Http(rewriter, backend);
139137
var called = 0;
140138
http.getString('a', cache: cache).then((v) {
141139
expect(v).toBe(VALUE);
@@ -157,8 +155,8 @@ main() {
157155
});
158156

159157

160-
it('should return a cached value if present', () {
161-
var http = new Http(rewriter, backend, futures);
158+
it('should return a cached value if present', async(() {
159+
var http = new Http(rewriter, backend);
162160
var called = 0;
163161
// The URL string 'f' is primed in the FakeCache
164162
http.getString('f', cache: cache).then((v) {
@@ -168,10 +166,10 @@ main() {
168166

169167
expect(called).toEqual(0);
170168
backend.flush();
171-
futures.trigger();
169+
nextTurn();
172170

173171
expect(called).toEqual(1);
174172
backend.assertAllGetsCalled();
175-
});
173+
}));
176174
});
177175
}

0 commit comments

Comments
 (0)