|
| 1 | +library angular2_testing.angular2_testing; |
| 2 | + |
| 3 | +import 'package:test/test.dart'; |
| 4 | +import 'package:test/src/backend/invoker.dart'; |
| 5 | +import 'package:test/src/backend/live_test.dart'; |
| 6 | + |
| 7 | +import 'package:angular2/angular2.dart'; |
| 8 | +import 'package:angular2/src/core/di/injector.dart' show Injector; |
| 9 | +import 'package:angular2/src/core/di/metadata.dart' show InjectMetadata; |
| 10 | +import 'package:angular2/src/core/di/exceptions.dart' show NoAnnotationError; |
| 11 | +import 'package:angular2/platform/browser_static.dart' show BrowserDomAdapter; |
| 12 | +import 'package:angular2/src/core/reflection/reflection.dart'; |
| 13 | +import 'package:angular2/src/core/reflection/reflection_capabilities.dart'; |
| 14 | +import 'package:angular2/src/testing/test_injector.dart'; |
| 15 | +export 'package:angular2/src/testing/test_component_builder.dart'; |
| 16 | +export 'package:angular2/src/testing/test_injector.dart' show inject; |
| 17 | + |
| 18 | +/// One time initialization that must be done for Angular2 component |
| 19 | +/// tests. Call before any test methods. |
| 20 | +/// |
| 21 | +/// Example: |
| 22 | +/// |
| 23 | +/// ``` |
| 24 | +/// main() { |
| 25 | +/// initAngularTests(); |
| 26 | +/// group(...); |
| 27 | +/// } |
| 28 | +/// ``` |
| 29 | +void initAngularTests() { |
| 30 | + BrowserDomAdapter.makeCurrent(); |
| 31 | + reflector.reflectionCapabilities = new ReflectionCapabilities(); |
| 32 | +} |
| 33 | + |
| 34 | +/// Allows overriding default bindings defined in test_injector.dart. |
| 35 | +/// |
| 36 | +/// The given function must return a list of DI providers. |
| 37 | +/// |
| 38 | +/// Example: |
| 39 | +/// |
| 40 | +/// ``` |
| 41 | +/// setUpProviders(() => [ |
| 42 | +/// provide(Compiler, useClass: MockCompiler), |
| 43 | +/// provide(SomeToken, useValue: myValue), |
| 44 | +/// ]); |
| 45 | +/// ``` |
| 46 | +void setUpProviders(Iterable<Provider> providerFactory()) { |
| 47 | + setUp(() { |
| 48 | + if (_currentInjector != null) { |
| 49 | + throw 'setUpProviders was called after the injector had ' |
| 50 | + 'been used in a setUp or test block. This invalidates the ' |
| 51 | + 'test injector'; |
| 52 | + } |
| 53 | + _currentTestProviders.addAll(providerFactory()); |
| 54 | + }); |
| 55 | +} |
| 56 | + |
| 57 | + |
| 58 | +dynamic _runInjectableFunction(Function fn) { |
| 59 | + var params = reflector.parameters(fn); |
| 60 | + List<dynamic> tokens = <dynamic>[]; |
| 61 | + for (var param in params) { |
| 62 | + var token = null; |
| 63 | + for (var paramMetadata in param) { |
| 64 | + if (paramMetadata is Type) { |
| 65 | + token = paramMetadata; |
| 66 | + } else if (paramMetadata is InjectMetadata) { |
| 67 | + token = paramMetadata.token; |
| 68 | + } |
| 69 | + } |
| 70 | + if (token == null) { |
| 71 | + throw new NoAnnotationError(fn, params); |
| 72 | + } |
| 73 | + tokens.add(token); |
| 74 | + } |
| 75 | + |
| 76 | + if (_currentInjector == null) { |
| 77 | + _currentInjector = createTestInjector(_currentTestProviders); |
| 78 | + } |
| 79 | + var injectFn = new FunctionWithParamTokens(tokens, fn, false); |
| 80 | + return injectFn.execute(_currentInjector); |
| 81 | +} |
| 82 | + |
| 83 | +/// Use the test injector to get bindings and run a function. |
| 84 | +/// |
| 85 | +/// Example: |
| 86 | +/// |
| 87 | +/// ``` |
| 88 | +/// ngSetUp((SomeToken token) { |
| 89 | +/// token.init(); |
| 90 | +/// }); |
| 91 | +/// ``` |
| 92 | +void ngSetUp(Function fn) { |
| 93 | + setUp(() async { |
| 94 | + await _runInjectableFunction(fn); |
| 95 | + }); |
| 96 | +} |
| 97 | + |
| 98 | +/// Add a test which can use the test injector. |
| 99 | +/// |
| 100 | +/// Example: |
| 101 | +/// |
| 102 | +/// ``` |
| 103 | +/// ngTest('description', (SomeToken token) { |
| 104 | +/// expect(token, equals('expected')); |
| 105 | +/// }); |
| 106 | +/// ``` |
| 107 | +void ngTest(String description, Function fn, |
| 108 | + {String testOn, Timeout timeout, skip, Map<String, dynamic> onPlatform}) { |
| 109 | + test(description, () async { |
| 110 | + await _runInjectableFunction(fn); |
| 111 | + }, testOn: testOn, timeout: timeout, skip: skip, onPlatform: onPlatform); |
| 112 | +} |
| 113 | + |
| 114 | +final _providersExpando = new Expando<List<Provider>>('Providers for the current test'); |
| 115 | +final _injectorExpando = new Expando<Injector>('Angular Injector for the current test'); |
| 116 | + |
| 117 | +List get _currentTestProviders { |
| 118 | + if (_providersExpando[_currentTest] == null) { |
| 119 | + return _providersExpando[_currentTest] = []; |
| 120 | + } |
| 121 | + return _providersExpando[_currentTest]; |
| 122 | +} |
| 123 | +Injector get _currentInjector => _injectorExpando[_currentTest]; |
| 124 | +void set _currentInjector(Injector newInjector) { |
| 125 | + _injectorExpando[_currentTest] = newInjector; |
| 126 | +} |
| 127 | + |
| 128 | +// TODO: warning, the Invoker.current.liveTest is not a settled API and is |
| 129 | +// subject to change in future versions of package:test. |
| 130 | +LiveTest get _currentTest => Invoker.current.liveTest; |
0 commit comments