This repository has been archived by the owner on Sep 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 232
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Simplify ng_zone_test, and re-write to be more straightforward.
The Dart language/SDK already has tests around the use of the Zone API, so no reason to assert so many different states - just stick to the core ones that Angular itself will need to verify. Had to disable checking for long-stacks in DDC, bug filed. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=155659393
- Loading branch information
1 parent
c6ff2c1
commit dce8d27
Showing
1 changed file
with
211 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,211 @@ | ||
@TestOn('browser') | ||
import 'dart:async'; | ||
|
||
import 'package:angular2/angular2.dart'; | ||
import 'package:meta/meta.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
void main() { | ||
group('$NgZone', () { | ||
NgZone zone; | ||
List<String> log; | ||
List errors; | ||
List traces; | ||
|
||
List<StreamSubscription> subs; | ||
|
||
void createNgZone({@required bool enableLongStackTrace}) { | ||
zone = new NgZone(enableLongStackTrace: enableLongStackTrace); | ||
subs = <StreamSubscription>[ | ||
zone.onError.listen((e) { | ||
errors.add(e.error); | ||
traces.add(e.stackTrace); | ||
}), | ||
zone.onEventDone.listen((_) => log.add('onEventDone')), | ||
zone.onMicrotaskEmpty.listen((_) => log.add('onMicrotaskEmpty')), | ||
zone.onTurnDone.listen((_) => log.add('onTurnDone')), | ||
zone.onTurnStart.listen((_) => log.add('onTurnStart')), | ||
]; | ||
} | ||
|
||
setUp(() { | ||
log = <String>[]; | ||
errors = []; | ||
traces = []; | ||
}); | ||
|
||
tearDown(() { | ||
if (subs != null) { | ||
for (final sub in subs) { | ||
sub.cancel(); | ||
} | ||
} | ||
}); | ||
|
||
group('hasPendingMicrotasks', () { | ||
setUp(() => createNgZone(enableLongStackTrace: false)); | ||
|
||
test('should initially be false', () { | ||
expect(zone.hasPendingMicrotasks, isFalse); | ||
}); | ||
|
||
test('should be true when a microtask is queued', () async { | ||
final onCompleter = new Completer<Null>(); | ||
zone.run(() { | ||
log.add('--- entered zone ---'); | ||
scheduleMicrotask(() { | ||
log.add('--- ran microtask ---'); | ||
onCompleter.complete(); | ||
}); | ||
}); | ||
expect(zone.hasPendingMicrotasks, isTrue); | ||
await onCompleter.future; | ||
expect(zone.hasPendingMicrotasks, isFalse); | ||
expect(log, [ | ||
'onTurnStart', | ||
'--- entered zone ---', | ||
'--- ran microtask ---', | ||
'onEventDone', | ||
'onMicrotaskEmpty', | ||
'onTurnDone', | ||
]); | ||
}); | ||
}); | ||
|
||
group('hasPendingMacrotasks', () { | ||
setUp(() => createNgZone(enableLongStackTrace: false)); | ||
|
||
test('should initially be false', () { | ||
expect(zone.hasPendingMacrotasks, isFalse); | ||
}); | ||
|
||
test('should be true when a timer is queued', () async { | ||
final onCompleter = new Completer<Null>(); | ||
zone.run(() { | ||
log.add('--- entered zone ---'); | ||
Timer.run(() { | ||
log.add('--- ran timer ---'); | ||
onCompleter.complete(); | ||
}); | ||
}); | ||
expect(zone.hasPendingMacrotasks, isTrue); | ||
await onCompleter.future; | ||
expect(zone.hasPendingMacrotasks, isFalse); | ||
expect(log, [ | ||
'onTurnStart', | ||
'--- entered zone ---', | ||
'onEventDone', | ||
'onMicrotaskEmpty', | ||
'onTurnDone', | ||
'onTurnStart', | ||
'--- ran timer ---', | ||
'onEventDone', | ||
'onMicrotaskEmpty', | ||
'onTurnDone', | ||
]); | ||
}); | ||
}); | ||
|
||
group('isInAngularZone', () { | ||
setUp(() => createNgZone(enableLongStackTrace: false)); | ||
|
||
test('should be false outside of the zone', () { | ||
zone.runOutsideAngular(() { | ||
expect(NgZone.isInAngularZone(), isFalse); | ||
}); | ||
}); | ||
|
||
test('should be true inside of the zone', () { | ||
zone.run(() { | ||
expect(NgZone.isInAngularZone(), isTrue); | ||
}); | ||
}); | ||
}); | ||
|
||
group('run', () { | ||
setUp(() => createNgZone(enableLongStackTrace: false)); | ||
|
||
test('should return the body return value', () async { | ||
final result = zone.run(() => 'Hello World'); | ||
expect(result, 'Hello World'); | ||
}); | ||
|
||
test('should run subscriber listeners inside the zone', () async { | ||
final someEvents = new Stream.fromIterable([1, 2, 3]); | ||
zone.run(() { | ||
someEvents.listen((_) { | ||
log.add('--- subscription event: ${NgZone.isInAngularZone()} ---'); | ||
}); | ||
}); | ||
await new Future.delayed(Duration.ZERO); | ||
expect(log, [ | ||
'onTurnStart', | ||
'--- subscription event: true ---', | ||
'--- subscription event: true ---', | ||
'--- subscription event: true ---', | ||
'onEventDone', | ||
'onMicrotaskEmpty', | ||
'onTurnDone', | ||
]); | ||
}); | ||
}); | ||
|
||
group('without longStackTrace', () { | ||
setUp(() => createNgZone(enableLongStackTrace: false)); | ||
|
||
test('should capture an error and stack trace', () async { | ||
zone.runGuarded(() { | ||
void bar() { | ||
throw new StateError('How did I end up here?'); | ||
} | ||
|
||
void foo() { | ||
scheduleMicrotask(bar); | ||
} | ||
|
||
scheduleMicrotask(foo); | ||
}); | ||
await new Future.delayed(Duration.ZERO); | ||
expect(errors.map((e) => e.toString()), [ | ||
'Bad state: How did I end up here?', | ||
]); | ||
final fullStackTrace = traces.map((t) => t.toString()).join(''); | ||
expect(fullStackTrace, contains('bar')); | ||
expect(fullStackTrace, isNot(contains('foo'))); | ||
}); | ||
}); | ||
|
||
group('with longStackTrace', () { | ||
setUp(() => createNgZone(enableLongStackTrace: true)); | ||
|
||
test('should capture an error and a long stack trace', () async { | ||
zone.runGuarded(() { | ||
void bar() { | ||
throw new StateError('How did I end up here?'); | ||
} | ||
|
||
void foo() { | ||
scheduleMicrotask(bar); | ||
} | ||
|
||
scheduleMicrotask(foo); | ||
}); | ||
await new Future.delayed(Duration.ZERO); | ||
expect(errors.map((e) => e.toString()), [ | ||
'Bad state: How did I end up here?', | ||
]); | ||
final fullStackTrace = traces.map((t) => t.toString()).join(''); | ||
expect(fullStackTrace, contains('bar')); | ||
|
||
// Skip this part of the test in DDC, we seem to be only getting: | ||
// a short stack trace (perhaps pkg/stack_trace doesn't work the same | ||
// inside this test environment). | ||
// | ||
// Internal bug: b/38171558. | ||
if (!fullStackTrace.contains('ng_zone_test_library.js')) { | ||
expect(fullStackTrace, contains('foo')); | ||
} | ||
}); | ||
}); | ||
}); | ||
} |