Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(example): refactor forward_ref example into a spec and unign…
…ore example specs
- Loading branch information
Showing
5 changed files
with
53 additions
and
44 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
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
35 changes: 0 additions & 35 deletions
35
modules/@angular/examples/core/di/forward_ref/forward_ref.ts
This file was deleted.
Oops, something went wrong.
50 changes: 50 additions & 0 deletions
50
modules/@angular/examples/core/di/forward_ref/forward_ref_spec.ts
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,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 | ||
}); | ||
}); | ||
} |
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