|
| 1 | +import {describe, ddescribe, it, iit, xit, xdescribe, expect, beforeEach, async, tick} from 'test_lib/test_lib'; |
| 2 | +import {Log, once} from 'test_lib/utils'; |
| 3 | +import {PromiseWrapper} from 'facade/async'; |
| 4 | +import {BaseException} from 'facade/lang'; |
| 5 | +import {VmTurnZone} from 'core/zone/vm_turn_zone'; |
| 6 | + |
| 7 | +export function main() { |
| 8 | + describe("VmTurnZone", () => { |
| 9 | + var log, zone; |
| 10 | + |
| 11 | + beforeEach(() => { |
| 12 | + log = new Log(); |
| 13 | + zone = new VmTurnZone(); |
| 14 | + zone.initCallbacks({ |
| 15 | + onTurnStart: log.fn('onTurnStart'), |
| 16 | + onTurnDone: log.fn('onTurnDone') |
| 17 | + }); |
| 18 | + }); |
| 19 | + |
| 20 | + describe("run", () => { |
| 21 | + it('should call onTurnStart and onTurnDone', () => { |
| 22 | + zone.run(log.fn('run')); |
| 23 | + |
| 24 | + expect(log.result()).toEqual('onTurnStart; run; onTurnDone'); |
| 25 | + }); |
| 26 | + |
| 27 | + it('should return the body return value from run', () => { |
| 28 | + expect(zone.run(() => 6)).toEqual(6); |
| 29 | + }); |
| 30 | + |
| 31 | + it('should not run onTurnStart and onTurnDone for nested Zone.run', () => { |
| 32 | + zone.run(() => { |
| 33 | + zone.run(log.fn('run')); |
| 34 | + }); |
| 35 | + expect(log.result()).toEqual('onTurnStart; run; onTurnDone'); |
| 36 | + }); |
| 37 | + |
| 38 | + |
| 39 | + it('should call onTurnStart and onTurnDone before and after each top-level run', () => { |
| 40 | + zone.run(log.fn('run1')); |
| 41 | + zone.run(log.fn('run2')); |
| 42 | + |
| 43 | + expect(log.result()).toEqual('onTurnStart; run1; onTurnDone; onTurnStart; run2; onTurnDone'); |
| 44 | + }); |
| 45 | + |
| 46 | + |
| 47 | + it('should call onTurnStart and onTurnDone before and after each turn', (done) => { |
| 48 | + var a = PromiseWrapper.completer(); |
| 49 | + var b = PromiseWrapper.completer(); |
| 50 | + |
| 51 | + zone.run(() => { |
| 52 | + log.add('run start'); |
| 53 | + a.promise.then((_) => log.add('a then')); |
| 54 | + b.promise.then((_) => log.add('b then')); |
| 55 | + }); |
| 56 | + |
| 57 | + a.complete("a"); |
| 58 | + b.complete("b"); |
| 59 | + |
| 60 | + PromiseWrapper.all([a.promise, b.promise]).then((_) => { |
| 61 | + expect(log.result()).toEqual('onTurnStart; run start; onTurnDone; onTurnStart; a then; onTurnDone; onTurnStart; b then; onTurnDone'); |
| 62 | + done(); |
| 63 | + }); |
| 64 | + }); |
| 65 | + }); |
| 66 | + |
| 67 | + describe("runOutsideAngular", () => { |
| 68 | + it("should run a function outside of the angular zone", () => { |
| 69 | + zone.runOutsideAngular(log.fn('run')); |
| 70 | + |
| 71 | + expect(log.result()).toEqual('run'); |
| 72 | + }); |
| 73 | + }); |
| 74 | + |
| 75 | + describe("exceptions", () => { |
| 76 | + it('should rethrow exceptions from the body', () => { |
| 77 | + expect(() => { |
| 78 | + zone.run(() => { |
| 79 | + throw new BaseException('hello'); |
| 80 | + }); |
| 81 | + }).toThrowError('hello'); |
| 82 | + }); |
| 83 | + }); |
| 84 | + }); |
| 85 | +} |
0 commit comments