refactor(example): refactor forward_ref example into a spec and unignore example specs (#11088)

This commit is contained in:
Alex Rickabaugh 2016-08-26 15:40:46 -07:00 committed by Victor Berchet
parent e220a80093
commit 27539c8b80
5 changed files with 53 additions and 44 deletions

View File

@ -39,7 +39,6 @@ module.exports = function(config) {
exclude: [
'dist/all/@angular/**/e2e_test/**',
'dist/all/@angular/examples/**',
'dist/all/@angular/router/**',
'dist/all/@angular/compiler-cli/**',
'dist/all/angular1_router.js',

View File

@ -15,7 +15,7 @@ import {Type} from '../type';
*
* ### 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
*/
export interface ForwardRefFn { (): any; }
@ -29,7 +29,7 @@ export interface ForwardRefFn { (): any; }
* yet defined.
*
* ### 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
*/
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))
*
* ```typescript
* var ref = forwardRef(() => "refValue");
* expect(resolveForwardRef(ref)).toEqual("refValue");
* expect(resolveForwardRef("regularValue")).toEqual("regularValue");
* ```
* {@example core/di/ts/forward_ref/forward_ref_spec.ts region='resolve_forward_ref'}
*
* See: {@link forwardRef}
* @experimental

View File

@ -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

View File

@ -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
});
});
}

View File

@ -52,7 +52,6 @@ var specFiles: any =
'@angular/router/test/integration/bootstrap_spec.*',
'@angular/integration_test/symbol_inspector/**',
'@angular/upgrade/**',
'@angular/examples/**',
'angular1_router/**',
'payload_tests/**',
]