-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow teleport bindings to be passed asynchronously
- Loading branch information
Showing
3 changed files
with
121 additions
and
37 deletions.
There are no files selected for viewing
19 changes: 12 additions & 7 deletions
19
projects/ngneat/overview/src/lib/teleport/teleport-outlet.directive.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 |
---|---|---|
@@ -1,20 +1,25 @@ | ||
import { Directive, Input, OnDestroy, OnInit, ViewContainerRef } from '@angular/core'; | ||
import { Directive, Input, OnChanges, OnDestroy, SimpleChanges, ViewContainerRef } from '@angular/core'; | ||
import { TeleportService } from './teleport.service'; | ||
|
||
@Directive({ | ||
selector: '[teleportOutlet]', | ||
}) | ||
export class TeleportOutletDirective implements OnInit, OnDestroy { | ||
@Input() teleportOutlet: string; | ||
export class TeleportOutletDirective implements OnChanges, OnDestroy { | ||
// We could've also used the `ngAcceptInputType`, but it's being deprecated in newer Angular versions. | ||
@Input() teleportOutlet: string | null | undefined; | ||
|
||
constructor(private vcr: ViewContainerRef, private service: TeleportService) {} | ||
|
||
ngOnInit() { | ||
this.service.ports.set(this.teleportOutlet, this.vcr); | ||
this.service.newOutlet(this.teleportOutlet); | ||
ngOnChanges(changes: SimpleChanges): void { | ||
// The `teleportOutlet` might be `null|undefined`, but we don't want nullable values to be used | ||
// as keys for the `ports` map. | ||
if (changes.teleportOutlet && typeof this.teleportOutlet === 'string') { | ||
this.service.ports.set(this.teleportOutlet, this.vcr); | ||
this.service.newOutlet(this.teleportOutlet); | ||
} | ||
} | ||
|
||
ngOnDestroy() { | ||
ngOnDestroy(): void { | ||
this.service.ports.delete(this.teleportOutlet); | ||
} | ||
} |
95 changes: 77 additions & 18 deletions
95
projects/ngneat/overview/src/lib/teleport/teleport.module.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 |
---|---|---|
@@ -1,26 +1,85 @@ | ||
import { Component } from '@angular/core'; | ||
import { createComponentFactory, Spectator } from '@ngneat/spectator'; | ||
import { TeleportModule } from './teleport.module'; | ||
|
||
@Component({ | ||
template: ` <div *teleportTo="'projectHere'">Some view</div> | ||
import { ChangeDetectionStrategy, ChangeDetectorRef, Component, OnDestroy, OnInit } from '@angular/core'; | ||
import { createComponentFactory } from '@ngneat/spectator'; | ||
|
||
<section> | ||
<ng-container teleportOutlet="projectHere"></ng-container> | ||
</section>`, | ||
}) | ||
class TestComponent {} | ||
import { TeleportModule } from './teleport.module'; | ||
|
||
describe('TeleportDirective', () => { | ||
let spectator: Spectator<TestComponent>; | ||
describe('Synchronous behavior', () => { | ||
@Component({ | ||
template: ` | ||
<div *teleportTo="'projectHere'">Some view</div> | ||
<section> | ||
<ng-container teleportOutlet="projectHere"></ng-container> | ||
</section> | ||
`, | ||
}) | ||
class TestComponent {} | ||
|
||
const createComponent = createComponentFactory({ | ||
component: TestComponent, | ||
imports: [TeleportModule], | ||
const createComponent = createComponentFactory({ | ||
component: TestComponent, | ||
imports: [TeleportModule], | ||
}); | ||
|
||
it('should render the view as sibling to the given outlet', () => { | ||
// Arrange & act | ||
const spectator = createComponent(); | ||
// Assert | ||
expect(spectator.query('section')).toHaveText('Some view'); | ||
}); | ||
}); | ||
|
||
it('should render the view as sibling to the given outlet', () => { | ||
spectator = createComponent(); | ||
expect(spectator.query('section')).toHaveText('Some view'); | ||
describe('Asynchronous behavior', () => { | ||
@Component({ | ||
selector: 'app-hello', | ||
template: '<div>Some view</div>', | ||
}) | ||
class HelloComponent implements OnInit, OnDestroy { | ||
ngOnInit(): void {} | ||
ngOnDestroy(): void {} | ||
} | ||
|
||
@Component({ | ||
template: ` | ||
<app-hello *teleportTo="teleportTo"></app-hello> | ||
<section> | ||
<ng-template [teleportOutlet]="teleportTo"></ng-template> | ||
</section> | ||
`, | ||
changeDetection: ChangeDetectionStrategy.OnPush, | ||
}) | ||
class AsynchronousTestComponent { | ||
teleportTo: string | null = null; | ||
|
||
constructor(private ref: ChangeDetectorRef) {} | ||
|
||
setTeleportToAsynchronously(): Promise<void> { | ||
return Promise.resolve().then(() => { | ||
this.teleportTo = 'projectHere'; | ||
// Run the change detection manually since we're inside an OnPush component. | ||
this.ref.detectChanges(); | ||
}); | ||
} | ||
} | ||
|
||
const createComponent = createComponentFactory({ | ||
component: AsynchronousTestComponent, | ||
imports: [TeleportModule], | ||
declarations: [HelloComponent], | ||
}); | ||
|
||
it('should render the view as sibling to the given outlet asynchronously', async () => { | ||
// Arrange | ||
const spectator = createComponent(); | ||
// Act | ||
const ngOnInitSpy = spyOn(HelloComponent.prototype, 'ngOnInit').and.callThrough(); | ||
const ngOnDestroySpy = spyOn(HelloComponent.prototype, 'ngOnDestroy').and.callThrough(); | ||
await spectator.component.setTeleportToAsynchronously(); | ||
// Assert | ||
expect(spectator.query('app-hello')).toExist(); | ||
expect(spectator.query('app-hello')).toContainText('Some view'); | ||
spectator.fixture.destroy(); | ||
expect(ngOnInitSpy).toHaveBeenCalled(); | ||
expect(ngOnDestroySpy).toHaveBeenCalled(); | ||
}); | ||
}); | ||
}); |
44 changes: 32 additions & 12 deletions
44
projects/ngneat/overview/src/lib/teleport/teleport.module.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