OLD | NEW |
| (Empty) |
1 library angular.mock.zone_spec; | |
2 | |
3 import '../_specs.dart'; | |
4 import 'dart:async'; | |
5 | |
6 main() => describe('mock zones', () { | |
7 describe('sync', () { | |
8 it('should throw an error on scheduleMicrotask', () { | |
9 expect(sync(() { | |
10 scheduleMicrotask(() => dump("i never run")); | |
11 })).toThrow('scheduleMicrotask called from sync function'); | |
12 }); | |
13 | |
14 | |
15 it('should throw an error on timer', () { | |
16 expect(sync(() { | |
17 Timer.run(() => dump("i never run")); | |
18 })).toThrow('Timer created from sync function'); | |
19 }); | |
20 | |
21 | |
22 it('should throw an error on periodic timer', () { | |
23 expect(sync(() { | |
24 new Timer.periodic(new Duration(milliseconds: 10), | |
25 (_) => dump("i never run")); | |
26 })).toThrow('periodic Timer created from sync function'); | |
27 }); | |
28 }); | |
29 | |
30 describe('async', () { | |
31 it('should run synchronous code', () { | |
32 var ran = false; | |
33 async(() { ran = true; })(); | |
34 expect(ran).toBe(true); | |
35 }); | |
36 | |
37 | |
38 it('should run async code', () { | |
39 var ran = false; | |
40 var thenRan = false; | |
41 async(() { | |
42 new Future.value('s').then((_) { thenRan = true; }); | |
43 expect(thenRan).toBe(false); | |
44 microLeap(); | |
45 expect(thenRan).toBe(true); | |
46 ran = true; | |
47 })(); | |
48 expect(ran).toBe(true); | |
49 }); | |
50 | |
51 | |
52 it('should run async code with scheduleMicrotask', () { | |
53 var ran = false; | |
54 var thenRan = false; | |
55 async(() { | |
56 scheduleMicrotask(() { thenRan = true; }); | |
57 expect(thenRan).toBe(false); | |
58 microLeap(); | |
59 expect(thenRan).toBe(true); | |
60 ran = true; | |
61 })(); | |
62 expect(ran).toBe(true); | |
63 }); | |
64 | |
65 | |
66 it('should run chained thens', () { | |
67 var log = []; | |
68 async(() { | |
69 new Future.value('s') | |
70 .then((_) { log.add('firstThen'); }) | |
71 .then((_) { log.add('2ndThen'); }); | |
72 expect(log.join(' ')).toEqual(''); | |
73 microLeap(); | |
74 expect(log.join(' ')).toEqual('firstThen 2ndThen'); | |
75 })(); | |
76 }); | |
77 | |
78 | |
79 it('shold run futures created in futures', () { | |
80 var log = []; | |
81 async(() { | |
82 new Future.value('s') | |
83 .then((_) { | |
84 log.add('firstThen'); | |
85 new Future.value('t').then((_) { | |
86 log.add('2ndThen'); | |
87 }); | |
88 }); | |
89 expect(log.join(' ')).toEqual(''); | |
90 microLeap(); | |
91 expect(log.join(' ')).toEqual('firstThen 2ndThen'); | |
92 })(); | |
93 }); | |
94 | |
95 it('should run all the async calls if asked', () { | |
96 var log = []; | |
97 async(() { | |
98 new Future.value('s') | |
99 .then((_) { | |
100 log.add('firstThen'); | |
101 new Future.value('t').then((_) { | |
102 log.add('2ndThen'); | |
103 }); | |
104 }); | |
105 expect(log.join(' ')).toEqual(''); | |
106 microLeap(); | |
107 expect(log.join(' ')).toEqual('firstThen 2ndThen'); | |
108 })(); | |
109 }); | |
110 | |
111 | |
112 it('should not complain if you dangle callbacks', () { | |
113 async(() { | |
114 new Future.value("s").then((_) {}); | |
115 })(); | |
116 }); | |
117 | |
118 | |
119 it('should complain if you dangle exceptions', () { | |
120 expect(() { | |
121 async(() { | |
122 new Future.value("s").then((_) { | |
123 throw ["dangling"]; | |
124 }); | |
125 })(); | |
126 }).toThrow("dangling"); | |
127 }); | |
128 | |
129 | |
130 it('should complain if the test throws an exception', () { | |
131 expect(() { | |
132 async(() { | |
133 throw "blah"; | |
134 })(); | |
135 }).toThrow("blah"); | |
136 }); | |
137 | |
138 | |
139 it('should complain if the test throws an exception during async calls', ()
{ | |
140 expect(async(() { | |
141 new Future.value('s').then((_) { throw "blah then"; }); | |
142 microLeap(); | |
143 })).toThrow("blah then"); | |
144 }); | |
145 | |
146 it('should throw errors from the microLeap call', async(() { | |
147 new Future.value('s').then((_) { throw "blah then 2"; }); | |
148 expect(() { | |
149 microLeap(); | |
150 }).toThrow("blah then 2"); | |
151 })); | |
152 | |
153 describe('timers', () { | |
154 it('should not run queued timer on insufficient clock tick', async(() { | |
155 bool timerRan = false; | |
156 var timer = new Timer(new Duration(milliseconds: 10), | |
157 () => timerRan = true); | |
158 | |
159 clockTick(milliseconds: 9); | |
160 expect(timerRan).toBeFalsy(); | |
161 | |
162 timer.cancel(); | |
163 })); | |
164 | |
165 | |
166 it('should run queued zero duration timer on zero tick', async(() { | |
167 bool timerRan = false; | |
168 Timer.run(() => timerRan = true); | |
169 | |
170 clockTick(); | |
171 expect(timerRan).toBeTruthy(); | |
172 })); | |
173 | |
174 | |
175 it('should run queued timer after sufficient clock ticks', async(() { | |
176 bool timerRan = false; | |
177 new Timer(new Duration(milliseconds: 10), () => timerRan = true); | |
178 | |
179 clockTick(milliseconds: 9); | |
180 expect(timerRan).toBeFalsy(); | |
181 clockTick(milliseconds: 1); | |
182 expect(timerRan).toBeTruthy(); | |
183 })); | |
184 | |
185 | |
186 it('should run queued timer only once', async(() { | |
187 int timerRan = 0; | |
188 new Timer(new Duration(milliseconds: 10), () => timerRan++); | |
189 | |
190 clockTick(milliseconds: 10); | |
191 expect(timerRan).toBe(1); | |
192 clockTick(milliseconds: 10); | |
193 expect(timerRan).toBe(1); | |
194 clockTick(minutes: 10); | |
195 expect(timerRan).toBe(1); | |
196 })); | |
197 | |
198 | |
199 it('should run periodic timer', async(() { | |
200 int timerRan = 0; | |
201 var timer = new Timer.periodic(new Duration(milliseconds: 10), | |
202 (_) => timerRan++); | |
203 | |
204 clockTick(milliseconds: 9); | |
205 expect(timerRan).toBe(0); | |
206 clockTick(milliseconds: 1); | |
207 expect(timerRan).toBe(1); | |
208 clockTick(milliseconds: 30); | |
209 expect(timerRan).toBe(4); | |
210 | |
211 timer.cancel(); | |
212 })); | |
213 | |
214 | |
215 it('should not run cancelled timer', async(() { | |
216 bool timerRan = false; | |
217 var timer = new Timer(new Duration(milliseconds: 10), | |
218 () => timerRan = true); | |
219 | |
220 timer.cancel(); | |
221 | |
222 clockTick(milliseconds: 10); | |
223 expect(timerRan).toBeFalsy(); | |
224 })); | |
225 | |
226 | |
227 it('should not run cancelled periodic timer', async(() { | |
228 bool timerRan = false; | |
229 var timer = new Timer.periodic(new Duration(milliseconds: 10), | |
230 (_) => timerRan = true); | |
231 | |
232 timer.cancel(); | |
233 | |
234 clockTick(milliseconds: 10); | |
235 expect(timerRan).toBeFalsy(); | |
236 })); | |
237 | |
238 | |
239 it('should be able to cancel periodic timer from callback', async(() { | |
240 int timerRan = 0; | |
241 var timer = new Timer.periodic(new Duration(milliseconds: 10), | |
242 (t) { | |
243 timerRan++; | |
244 t.cancel(); | |
245 }); | |
246 | |
247 clockTick(milliseconds: 10); | |
248 expect(timerRan).toBe(1); | |
249 | |
250 clockTick(milliseconds: 10); | |
251 expect(timerRan).toBe(1); | |
252 | |
253 timer.cancel(); | |
254 })); | |
255 | |
256 | |
257 it('should process micro-tasks before timers', async(() { | |
258 var log = []; | |
259 | |
260 scheduleMicrotask(() => log.add('scheduleMicrotask')); | |
261 new Timer(new Duration(milliseconds: 10), | |
262 () => log.add('timer')); | |
263 var timer = new Timer.periodic(new Duration(milliseconds: 10), | |
264 (_) => log.add('periodic_timer')); | |
265 | |
266 expect(log.join(' ')).toEqual(''); | |
267 | |
268 clockTick(milliseconds: 10); | |
269 | |
270 expect(log.join(' ')).toEqual('scheduleMicrotask timer periodic_timer'); | |
271 | |
272 timer.cancel(); | |
273 })); | |
274 | |
275 | |
276 it('should process micro-tasks created in timers before next timers', asyn
c(() { | |
277 var log = []; | |
278 | |
279 scheduleMicrotask(() => log.add('scheduleMicrotask')); | |
280 new Timer(new Duration(milliseconds: 10), | |
281 () { | |
282 log.add('timer'); | |
283 scheduleMicrotask(() => log.add('timer_scheduleMicrotask')); | |
284 }); | |
285 var timer = new Timer.periodic(new Duration(milliseconds: 10), | |
286 (_) { | |
287 log.add('periodic_timer'); | |
288 scheduleMicrotask(() => log.add('periodic_timer_scheduleMicrotask'
)); | |
289 }); | |
290 | |
291 expect(log.join(' ')).toEqual(''); | |
292 | |
293 clockTick(milliseconds: 10); | |
294 expect(log.join(' ')).toEqual('scheduleMicrotask timer timer_scheduleMic
rotask periodic_timer'); | |
295 | |
296 clockTick(); | |
297 expect(log.join(' ')).toEqual('scheduleMicrotask timer timer_scheduleMic
rotask periodic_timer'); | |
298 | |
299 clockTick(milliseconds: 10); | |
300 expect(log.join(' ')).toEqual('scheduleMicrotask timer timer_scheduleMic
rotask periodic_timer periodic_timer_scheduleMicrotask periodic_timer'); | |
301 | |
302 timer.cancel(); | |
303 })); | |
304 | |
305 | |
306 it('should not leak timers between asyncs', () { | |
307 var log = []; | |
308 | |
309 async(() { | |
310 var timer = new Timer.periodic(new Duration(milliseconds: 10), | |
311 (_) => log.add('periodic_timer')); | |
312 new Timer(new Duration(milliseconds: 10), | |
313 () => log.add('timer')); | |
314 clockTick(milliseconds: 10); | |
315 timer.cancel(); | |
316 })(); | |
317 expect(log.join(' ')).toEqual('periodic_timer timer'); | |
318 | |
319 async(() { | |
320 clockTick(milliseconds: 10); | |
321 })(); | |
322 expect(log.join(' ')).toEqual('periodic_timer timer'); | |
323 }); | |
324 | |
325 | |
326 it('should throw an error on dangling timers', () { | |
327 expect(async(() { | |
328 new Timer.periodic(new Duration(milliseconds: 10), | |
329 (_) => dump("i never run")); | |
330 })).toThrow('1 active timer(s) are still in the queue.'); | |
331 }); | |
332 }); | |
333 }); | |
334 }); | |
OLD | NEW |