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

Skip to content

Accept subscribable on rxLet input #1657

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions libs/cdk/notifications/src/lib/create-template-notifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
Observable,
ObservableInput,
ReplaySubject,
Subscribable,
} from 'rxjs';
import {
distinctUntilChanged,
Expand Down Expand Up @@ -91,11 +92,13 @@ const handleSuspenseAndLastValueInNotifications = <T>() => {
*/
export function createTemplateNotifier<U>(): {
values$: Observable<RxNotification<U>>;
next(observable: ObservableInput<U> | U): void;
next(observable: ObservableInput<U> | U | Subscribable<U>): void;
withInitialSuspense(withInitialSuspense: boolean): void;
} {
// A Subject driven from the outside, it can contain Observables, static values null and undefined on purpose of from unassigned properties
const observablesSubject = new ReplaySubject<ObservableInput<U> | U>(1);
const observablesSubject = new ReplaySubject<
ObservableInput<U> | U | Subscribable<U>
>(1);

let emittedValueOnce = false;

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

return {
next(observable: ObservableInput<U> | U) {
next(observable: ObservableInput<U> | U | Subscribable<U>) {
observablesSubject.next(observable);
},
withInitialSuspense(withInitialSuspense: boolean) {
Expand Down
3 changes: 2 additions & 1 deletion libs/template/let/src/lib/let.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import {
ObservableInput,
ReplaySubject,
Subject,
Subscribable,
Subscription,
} from 'rxjs';
import { filter, map } from 'rxjs/operators';
Expand Down Expand Up @@ -130,7 +131,7 @@ export class RxLet<U> implements OnInit, OnDestroy, OnChanges {
*
* @param { ObservableInput<U> | U | null | undefined } rxLet
*/
@Input() rxLet: ObservableInput<U> | U | null | undefined;
@Input() rxLet: ObservableInput<U> | Subscribable<U> | U | null | undefined;

/**
* @description
Expand Down
72 changes: 72 additions & 0 deletions libs/template/let/src/lib/tests/let.directive.subscribable.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { RxLet } from '../let.directive';
import { TestBed, fakeAsync } from '@angular/core/testing';
import {
ChangeDetectorRef,
Component,
TemplateRef,
ViewContainerRef,
} from '@angular/core';
import { Subscribable, BehaviorSubject, Observable } from 'rxjs';
import { MockChangeDetectorRef } from './fixtures';
import { RX_RENDER_STRATEGIES_CONFIG } from '@rx-angular/cdk/render-strategies';
import { mockConsole } from '@test-helpers/rx-angular';

@Component({
template: `
<ng-container *rxLet="value$; let val">
<div>{{ val }}</div>
</ng-container>
`,
})
class LetDirectiveTestComponent {
value$: Subscribable<number> = new BehaviorSubject<number>(0);
}

let fixtureLetDirectiveTestComponent: any;
let letDirectiveTestComponent: {
strategy: string;
value$: Observable<unknown> | unknown | undefined | null;
};
let componentNativeElement: any;

const setupLetDirectiveTestComponent = (): void => {
TestBed.configureTestingModule({
declarations: [LetDirectiveTestComponent],
imports: [RxLet],
providers: [
{ provide: ChangeDetectorRef, useClass: MockChangeDetectorRef },
TemplateRef,
ViewContainerRef,
{
provide: RX_RENDER_STRATEGIES_CONFIG,
useValue: {
primaryStrategy: 'native',
},
},
],
teardown: { destroyAfterEach: true },
});
fixtureLetDirectiveTestComponent = TestBed.createComponent(
LetDirectiveTestComponent
);
letDirectiveTestComponent =
fixtureLetDirectiveTestComponent.componentInstance;
componentNativeElement = fixtureLetDirectiveTestComponent.nativeElement;
};

describe('RxLet Directive with Subscribable input', () => {
beforeAll(() => mockConsole());
beforeEach(setupLetDirectiveTestComponent);

it('should be instantiable', () => {
expect(fixtureLetDirectiveTestComponent).toBeDefined();
expect(letDirectiveTestComponent).toBeDefined();
expect(componentNativeElement).toBeDefined();
});

it('should display value from Subscribable', fakeAsync(() => {
letDirectiveTestComponent.value$ = 42;
fixtureLetDirectiveTestComponent.detectChanges();
expect(componentNativeElement.textContent.trim()).toBe('42');
}));
Comment on lines +67 to +71
Copy link
Member

@hoebbelsB hoebbelsB Jan 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this test doesn't test the subscribable. You are essentially overwriting the BehaviorSubject with a number which is getting rendered.

Instead, you should provide an actual Subscribable.

Suggested change
it('should display value from Subscribable', fakeAsync(() => {
letDirectiveTestComponent.value$ = 42;
fixtureLetDirectiveTestComponent.detectChanges();
expect(componentNativeElement.textContent.trim()).toBe('42');
}));
it('should display value from Subscribable', () => {
letDirectiveTestComponent.value$ = {
subscribe: ({ next }) => {
next(42)
}
};
fixtureLetDirectiveTestComponent.detectChanges();
expect(componentNativeElement.textContent.trim()).toBe('42');
});

});