Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c9e9c94

Browse files
feat(template): accept subscribable on rxLet input
resolves #1541
1 parent dde9efe commit c9e9c94

File tree

3 files changed

+80
-4
lines changed

3 files changed

+80
-4
lines changed

libs/cdk/notifications/src/lib/create-template-notifier.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
Observable,
66
ObservableInput,
77
ReplaySubject,
8+
Subscribable,
89
} from 'rxjs';
910
import {
1011
distinctUntilChanged,
@@ -91,11 +92,13 @@ const handleSuspenseAndLastValueInNotifications = <T>() => {
9192
*/
9293
export function createTemplateNotifier<U>(): {
9394
values$: Observable<RxNotification<U>>;
94-
next(observable: ObservableInput<U> | U): void;
95+
next(observable: ObservableInput<U> | U | Subscribable<U>): void;
9596
withInitialSuspense(withInitialSuspense: boolean): void;
9697
} {
9798
// A Subject driven from the outside, it can contain Observables, static values null and undefined on purpose of from unassigned properties
98-
const observablesSubject = new ReplaySubject<ObservableInput<U> | U>(1);
99+
const observablesSubject = new ReplaySubject<
100+
ObservableInput<U> | U | Subscribable<U>
101+
>(1);
99102

100103
let emittedValueOnce = false;
101104

@@ -121,7 +124,7 @@ export function createTemplateNotifier<U>(): {
121124
);
122125

123126
return {
124-
next(observable: ObservableInput<U> | U) {
127+
next(observable: ObservableInput<U> | U | Subscribable<U>) {
125128
observablesSubject.next(observable);
126129
},
127130
withInitialSuspense(withInitialSuspense: boolean) {

libs/template/let/src/lib/let.directive.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import {
3939
ObservableInput,
4040
ReplaySubject,
4141
Subject,
42+
Subscribable,
4243
Subscription,
4344
} from 'rxjs';
4445
import { filter, map } from 'rxjs/operators';
@@ -130,7 +131,7 @@ export class RxLet<U> implements OnInit, OnDestroy, OnChanges {
130131
*
131132
* @param { ObservableInput<U> | U | null | undefined } rxLet
132133
*/
133-
@Input() rxLet: ObservableInput<U> | U | null | undefined;
134+
@Input() rxLet: ObservableInput<U> | Subscribable<U> | U | null | undefined;
134135

135136
/**
136137
* @description
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import { RxLet } from '../let.directive';
2+
import { TestBed, fakeAsync } from '@angular/core/testing';
3+
import {
4+
ChangeDetectorRef,
5+
Component,
6+
TemplateRef,
7+
ViewContainerRef,
8+
} from '@angular/core';
9+
import { Subscribable, BehaviorSubject, Observable } from 'rxjs';
10+
import { MockChangeDetectorRef } from './fixtures';
11+
import { RX_RENDER_STRATEGIES_CONFIG } from '@rx-angular/cdk/render-strategies';
12+
import { mockConsole } from '@test-helpers/rx-angular';
13+
14+
@Component({
15+
template: `
16+
<ng-container *rxLet="value$; let val">
17+
<div>{{ val }}</div>
18+
</ng-container>
19+
`,
20+
})
21+
class LetDirectiveTestComponent {
22+
value$: Subscribable<number> = new BehaviorSubject<number>(0);
23+
}
24+
25+
let fixtureLetDirectiveTestComponent: any;
26+
let letDirectiveTestComponent: {
27+
strategy: string;
28+
value$: Observable<unknown> | unknown | undefined | null;
29+
};
30+
let componentNativeElement: any;
31+
32+
const setupLetDirectiveTestComponent = (): void => {
33+
TestBed.configureTestingModule({
34+
declarations: [LetDirectiveTestComponent],
35+
imports: [RxLet],
36+
providers: [
37+
{ provide: ChangeDetectorRef, useClass: MockChangeDetectorRef },
38+
TemplateRef,
39+
ViewContainerRef,
40+
{
41+
provide: RX_RENDER_STRATEGIES_CONFIG,
42+
useValue: {
43+
primaryStrategy: 'native',
44+
},
45+
},
46+
],
47+
teardown: { destroyAfterEach: true },
48+
});
49+
fixtureLetDirectiveTestComponent = TestBed.createComponent(
50+
LetDirectiveTestComponent
51+
);
52+
letDirectiveTestComponent =
53+
fixtureLetDirectiveTestComponent.componentInstance;
54+
componentNativeElement = fixtureLetDirectiveTestComponent.nativeElement;
55+
};
56+
57+
describe('RxLet Directive with Subscribable input', () => {
58+
beforeAll(() => mockConsole());
59+
beforeEach(setupLetDirectiveTestComponent);
60+
61+
it('should be instantiable', () => {
62+
expect(fixtureLetDirectiveTestComponent).toBeDefined();
63+
expect(letDirectiveTestComponent).toBeDefined();
64+
expect(componentNativeElement).toBeDefined();
65+
});
66+
67+
it('should display value from Subscribable', fakeAsync(() => {
68+
letDirectiveTestComponent.value$ = 42;
69+
fixtureLetDirectiveTestComponent.detectChanges();
70+
expect(componentNativeElement.textContent.trim()).toBe('42');
71+
}));
72+
});

0 commit comments

Comments
 (0)