refactor(example): refactor forward_ref example into a spec and unignore example specs (#11088)
This commit is contained in:
parent
e220a80093
commit
27539c8b80
|
@ -39,7 +39,6 @@ module.exports = function(config) {
|
||||||
|
|
||||||
exclude: [
|
exclude: [
|
||||||
'dist/all/@angular/**/e2e_test/**',
|
'dist/all/@angular/**/e2e_test/**',
|
||||||
'dist/all/@angular/examples/**',
|
|
||||||
'dist/all/@angular/router/**',
|
'dist/all/@angular/router/**',
|
||||||
'dist/all/@angular/compiler-cli/**',
|
'dist/all/@angular/compiler-cli/**',
|
||||||
'dist/all/angular1_router.js',
|
'dist/all/angular1_router.js',
|
||||||
|
|
|
@ -15,7 +15,7 @@ import {Type} from '../type';
|
||||||
*
|
*
|
||||||
* ### Example
|
* ### Example
|
||||||
*
|
*
|
||||||
* {@example core/di/ts/forward_ref/forward_ref.ts region='forward_ref_fn'}
|
* {@example core/di/ts/forward_ref/forward_ref_spec.ts region='forward_ref_fn'}
|
||||||
* @experimental
|
* @experimental
|
||||||
*/
|
*/
|
||||||
export interface ForwardRefFn { (): any; }
|
export interface ForwardRefFn { (): any; }
|
||||||
|
@ -29,7 +29,7 @@ export interface ForwardRefFn { (): any; }
|
||||||
* yet defined.
|
* yet defined.
|
||||||
*
|
*
|
||||||
* ### Example
|
* ### Example
|
||||||
* {@example core/di/ts/forward_ref/forward_ref.ts region='forward_ref'}
|
* {@example core/di/ts/forward_ref/forward_ref_spec.ts region='forward_ref'}
|
||||||
* @experimental
|
* @experimental
|
||||||
*/
|
*/
|
||||||
export function forwardRef(forwardRefFn: ForwardRefFn): Type<any> {
|
export function forwardRef(forwardRefFn: ForwardRefFn): Type<any> {
|
||||||
|
@ -45,11 +45,7 @@ export function forwardRef(forwardRefFn: ForwardRefFn): Type<any> {
|
||||||
*
|
*
|
||||||
* ### Example ([live demo](http://plnkr.co/edit/GU72mJrk1fiodChcmiDR?p=preview))
|
* ### Example ([live demo](http://plnkr.co/edit/GU72mJrk1fiodChcmiDR?p=preview))
|
||||||
*
|
*
|
||||||
* ```typescript
|
* {@example core/di/ts/forward_ref/forward_ref_spec.ts region='resolve_forward_ref'}
|
||||||
* var ref = forwardRef(() => "refValue");
|
|
||||||
* expect(resolveForwardRef(ref)).toEqual("refValue");
|
|
||||||
* expect(resolveForwardRef("regularValue")).toEqual("regularValue");
|
|
||||||
* ```
|
|
||||||
*
|
*
|
||||||
* See: {@link forwardRef}
|
* See: {@link forwardRef}
|
||||||
* @experimental
|
* @experimental
|
||||||
|
|
|
@ -1,35 +0,0 @@
|
||||||
/**
|
|
||||||
* @license
|
|
||||||
* Copyright Google Inc. All Rights Reserved.
|
|
||||||
*
|
|
||||||
* Use of this source code is governed by an MIT-style license that can be
|
|
||||||
* found in the LICENSE file at https://angular.io/license
|
|
||||||
*/
|
|
||||||
|
|
||||||
import {ForwardRefFn, Inject, ReflectiveInjector, forwardRef, resolveForwardRef} from '@angular/core';
|
|
||||||
|
|
||||||
|
|
||||||
// #docregion forward_ref_fn
|
|
||||||
var ref = forwardRef(() => Lock);
|
|
||||||
// #enddocregion
|
|
||||||
|
|
||||||
// #docregion forward_ref
|
|
||||||
class Door {
|
|
||||||
lock: Lock;
|
|
||||||
constructor(@Inject(forwardRef(() => Lock)) lock: Lock) { this.lock = lock; }
|
|
||||||
}
|
|
||||||
|
|
||||||
// Only at this point Lock is defined.
|
|
||||||
class Lock {}
|
|
||||||
|
|
||||||
var injector = ReflectiveInjector.resolveAndCreate([Door, Lock]);
|
|
||||||
var door = injector.get(Door);
|
|
||||||
expect(door instanceof Door).toBe(true);
|
|
||||||
expect(door.lock instanceof Lock).toBe(true);
|
|
||||||
// #enddocregion
|
|
||||||
|
|
||||||
// #docregion resolve_forward_ref
|
|
||||||
ref = forwardRef(() => 'refValue');
|
|
||||||
expect(resolveForwardRef(ref)).toEqual('refValue');
|
|
||||||
expect(resolveForwardRef('regularValue')).toEqual('regularValue');
|
|
||||||
// #enddocregion
|
|
|
@ -0,0 +1,50 @@
|
||||||
|
/**
|
||||||
|
* @license
|
||||||
|
* Copyright Google Inc. All Rights Reserved.
|
||||||
|
*
|
||||||
|
* Use of this source code is governed by an MIT-style license that can be
|
||||||
|
* found in the LICENSE file at https://angular.io/license
|
||||||
|
*/
|
||||||
|
|
||||||
|
import {ForwardRefFn, Inject, ReflectiveInjector, forwardRef, resolveForwardRef} from '@angular/core';
|
||||||
|
|
||||||
|
export function main() {
|
||||||
|
describe('forwardRef examples', () => {
|
||||||
|
it('ForwardRefFn example works', () => {
|
||||||
|
// #docregion forward_ref_fn
|
||||||
|
let ref = forwardRef(() => Lock);
|
||||||
|
// #enddocregion
|
||||||
|
expect(ref).not.toBeNull();
|
||||||
|
|
||||||
|
class Lock {}
|
||||||
|
});
|
||||||
|
|
||||||
|
it('can be used to inject a class defined later', () => {
|
||||||
|
// #docregion forward_ref
|
||||||
|
class Door {
|
||||||
|
lock: Lock;
|
||||||
|
|
||||||
|
// Door attempts to inject Lock, despite it not being defined yet.
|
||||||
|
// forwardRef makes this possible.
|
||||||
|
constructor(@Inject(forwardRef(() => Lock)) lock: Lock) { this.lock = lock; }
|
||||||
|
}
|
||||||
|
|
||||||
|
// Only at this point Lock is defined.
|
||||||
|
class Lock {}
|
||||||
|
|
||||||
|
let injector = ReflectiveInjector.resolveAndCreate([Door, Lock]);
|
||||||
|
let door = injector.get(Door);
|
||||||
|
expect(door instanceof Door).toBeTruthy();
|
||||||
|
expect(door.lock instanceof Lock).toBeTruthy();
|
||||||
|
// #enddocregion
|
||||||
|
});
|
||||||
|
|
||||||
|
it('can be unwrapped', () => {
|
||||||
|
// #docregion resolve_forward_ref
|
||||||
|
let ref = forwardRef(() => 'refValue');
|
||||||
|
expect(resolveForwardRef(ref)).toEqual('refValue');
|
||||||
|
expect(resolveForwardRef('regularValue')).toEqual('regularValue');
|
||||||
|
// #enddocregion
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
|
@ -52,7 +52,6 @@ var specFiles: any =
|
||||||
'@angular/router/test/integration/bootstrap_spec.*',
|
'@angular/router/test/integration/bootstrap_spec.*',
|
||||||
'@angular/integration_test/symbol_inspector/**',
|
'@angular/integration_test/symbol_inspector/**',
|
||||||
'@angular/upgrade/**',
|
'@angular/upgrade/**',
|
||||||
'@angular/examples/**',
|
|
||||||
'angular1_router/**',
|
'angular1_router/**',
|
||||||
'payload_tests/**',
|
'payload_tests/**',
|
||||||
]
|
]
|
||||||
|
|
Loading…
Reference in New Issue