Skip to content
This repository has been archived by the owner on Sep 16, 2022. It is now read-only.

Commit

Permalink
cleanup(Test): Various API/language cleanups/upgrades.
Browse files Browse the repository at this point in the history
No-op to the actual behavior of this package.

Replaces (old) #744.

PiperOrigin-RevId: 181207342
  • Loading branch information
matanlurey authored and alorenzen committed Jan 8, 2018
1 parent de4d3f8 commit d0bac8d
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 34 deletions.
52 changes: 29 additions & 23 deletions angular_test/lib/src/frontend/bed.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ import 'fixture.dart';
import 'stabilizer.dart';

/// Used to determine if there is an actively executing test.
NgTestFixture activeTest;
NgTestFixture<Object> activeTest;

/// Returns a new [List] merging iterables [a] and [b].
List<E> _concat<E>(Iterable<E> a, Iterable<E> b) {
return new List<E>.from(a)..addAll(b);
return a.toList()..addAll(b);
}

/// If any [NgTestFixture] is currently executing, calls `dispose` on it.
Expand All @@ -38,7 +38,7 @@ Future<Null> disposeAnyRunningTest() async => activeTest?.dispose();
Future<NgTestFixture<T>> createDynamicFixture<T>(
NgTestBed<T> bed,
Type type, {
void beforeChangeDetection(T componentInstance),
void Function(T componentInstance) beforeChangeDetection,
}) {
return bed._createDynamic(type, beforeChangeDetection: beforeChangeDetection);
}
Expand Down Expand Up @@ -121,19 +121,19 @@ class NgTestBed<T> {
return host;
}

static const _lifecycleProviders = const <Provider>[
static const _lifecycleProviders = const <Object>[
const Provider(
NgZoneStabilizer,
useFactory: createZoneStabilizer,
deps: const [NgZone],
),
];
static const _lifecycleStabilizers = const <Type>[NgZoneStabilizer];
static const _lifecycleStabilizers = const <Object>[NgZoneStabilizer];

final Element _host;
final PageLoader Function(Element, NgTestFixture<T>) _pageLoaderFactory;
final List _providers;
final List _stabilizers;
final List<Object> _providers;
final List<Object> _stabilizers;

/// Create a new empty [NgTestBed] that creates a component type [T].
///
Expand Down Expand Up @@ -162,29 +162,30 @@ class NgTestBed<T> {
}) {
return new NgTestBed<T>._(
host: host,
providers: watchAngularLifecycle ? _lifecycleProviders : const [],
stabilizers: watchAngularLifecycle ? _lifecycleStabilizers : const [],
providers: watchAngularLifecycle ? _lifecycleProviders : const <Object>[],
stabilizers:
watchAngularLifecycle ? _lifecycleStabilizers : const <Object>[],
);
}

NgTestBed._({
Element host,
Iterable providers,
Iterable stabilizers,
PageLoader createPageLoader(Element element, NgTestFixture<T> fixture),
Iterable<Object> providers,
Iterable<Object> stabilizers,
PageLoader Function(Element element, NgTestFixture<T> fixture) pageLoader,
})
: _host = host,
_providers = providers.toList(),
_stabilizers = stabilizers.toList(),
_pageLoaderFactory = createPageLoader;
_pageLoaderFactory = pageLoader;

/// Returns a new instance of [NgTestBed] with [providers] added.
NgTestBed<T> addProviders(Iterable providers) {
NgTestBed<T> addProviders(Iterable<Object> providers) {
return fork(providers: _concat(_providers, providers));
}

/// Returns a new instance of [NgTestBed] with [stabilizers] added.
NgTestBed<T> addStabilizers(Iterable stabilizers) {
NgTestBed<T> addStabilizers(Iterable<Object> stabilizers) {
return fork(stabilizers: _concat(_stabilizers, stabilizers));
}

Expand All @@ -202,16 +203,20 @@ class NgTestBed<T> {
/// might be required).
///
/// Returns a future that completes with a fixture around the component.
Future<NgTestFixture<T>> create({void beforeChangeDetection(T instance)}) {
Future<NgTestFixture<T>> create({
void Function(T instance) beforeChangeDetection,
}) {
return _createDynamic(
T,
beforeChangeDetection: beforeChangeDetection,
);
}

// Used for compatibility only. See `create` for public API.
Future<NgTestFixture<T>> _createDynamic(Type type,
{void beforeChangeDetection(T instance)}) {
Future<NgTestFixture<T>> _createDynamic(
Type type, {
void Function(T instance) beforeChangeDetection,
}) {
// We *purposefully* do not use async/await here - that always adds an
// additional micro-task - we want this to fail fast without entering an
// asynchronous event if another test is running.
Expand All @@ -237,7 +242,7 @@ class NgTestBed<T> {
}),
);
await allStabilizers.stabilize();
final testFixture = new NgTestFixture(
final testFixture = new NgTestFixture<T>(
componentRef.injector.get(ApplicationRef),
_pageLoaderFactory ?? _createPageLoader,
componentRef,
Expand All @@ -255,15 +260,16 @@ class NgTestBed<T> {
/// Any non-null value overrides the existing properties.
NgTestBed<T> fork({
Element host,
Iterable providers,
Iterable stabilizers,
PageLoader createPageLoader(Element element, NgTestFixture<T> fixture),
Iterable<Object> providers,
Iterable<Object> stabilizers,
PageLoader Function(Element element, NgTestFixture<T> fixture)
createPageLoader,
}) {
return new NgTestBed<T>._(
host: host ?? _host,
providers: providers ?? _providers,
stabilizers: stabilizers ?? _stabilizers,
createPageLoader: createPageLoader ?? _pageLoaderFactory,
pageLoader: createPageLoader ?? _pageLoaderFactory,
);
}

Expand Down
17 changes: 10 additions & 7 deletions angular_test/lib/src/frontend/fixture.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import 'stabilizer.dart';
/// Inject a service for [tokenOrType] from [fixture].
///
/// This is for compatibility reasons only and should not be used otherwise.
T injectFromFixture<T>(NgTestFixture fixture, tokenOrType) {
T injectFromFixture<T>(NgTestFixture fixture, Object tokenOrType) {
return fixture._rootComponentRef.injector.get(tokenOrType);
}

Expand Down Expand Up @@ -97,7 +97,10 @@ class NgTestFixture<T> {
/// Calls [run] with `null` if there was no matching element.
///
/// **NOTE**: The root component is _not_ query-able. See [update] instead.
Future<Null> query<E>(bool test(DebugElement element), run(E instance)) {
Future<Null> query<E>(
bool Function(DebugElement element) test,
void Function(E instance) run,
) {
final instance = _debugElement.query(test)?.componentInstance;
return update((_) => run(instance));
}
Expand All @@ -118,8 +121,8 @@ class NgTestFixture<T> {
///
/// **NOTE**: The root component is _not_ query-able. See [update] instead.
Future<Null> queryAll<E>(
bool test(DebugElement element),
run(Iterable<E> instances),
bool Function(DebugElement element) test,
void run(Iterable<E> instances),
) {
return update((_) {
return run(_debugElement.queryAll(test).map((e) => e.componentInstance));
Expand Down Expand Up @@ -147,7 +150,7 @@ class NgTestFixture<T> {
/// c.value = 5;
/// });
/// expect(fixture.text, contains('5 little piggies'));
Future<Null> update([run(T instance)]) {
Future<Null> update([void Function(T instance) run]) {
return _testStabilizer.stabilize(run: () {
if (run != null) {
new Future<Null>.sync(() {
Expand All @@ -165,10 +168,10 @@ class NgTestFixture<T> {
/// A component instance to use for read-only operations (expect, assert)
/// ONLY.
///
/// Warning this instance is not stabalized and so the test will not be in a
/// Warning this instance is not stabilized and so the test will not be in a
/// stable state likely leading to unexpected results. State changes to
/// the instance should be done through the `update` call, or external
/// stablalized mechanism such as page objects. Use this **ONLY** for simple
/// stabilized mechanism such as page objects. Use this **ONLY** for simple
/// expects of the instance state.
///
/// #Example
Expand Down
8 changes: 4 additions & 4 deletions angular_test/lib/src/frontend/stabilizer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ abstract class NgTestStabilizer {
/// return true;
/// }
/// ```
Future<bool> update([void fn()]) {
Future<bool> update([void Function() fn]) {
return new Future<bool>.sync(() {
if (fn != null) {
fn();
Expand All @@ -74,7 +74,7 @@ abstract class NgTestStabilizer {
/// Runs [update] until it completes with `false`, reporting stabilized.
///
/// If more then [threshold] attempts occur, throws [WillNeverStabilizeError].
Future<Null> stabilize({void run(), int threshold: 100}) async {
Future<Null> stabilize({void Function() run, int threshold: 100}) async {
if (threshold == null) {
throw new ArgumentError.notNull('threshold');
}
Expand All @@ -95,7 +95,7 @@ class _DelegatingNgTestStabilizer extends NgTestStabilizer {
: _delegates = stabilizers.toList(growable: false);

@override
Future<bool> update([void fn()]) async {
Future<bool> update([void Function() fn]) async {
if (_delegates.isEmpty) {
return false;
}
Expand All @@ -116,7 +116,7 @@ class NgZoneStabilizer extends NgTestStabilizer {
}

@override
Future<bool> update([void fn()]) {
Future<bool> update([void Function() fn]) {
return new Future<Null>.sync(() => _waitForZone(fn)).then((_) => isStable);
}

Expand Down

0 comments on commit d0bac8d

Please sign in to comment.