Skip to content
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

feat: support outputFromObservable and input.required #2389

Closed
wants to merge 1 commit into from
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,12 @@ import { DebugElement } from '@angular/core';
import { Component } from '@angular/core';
import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing';
import { By } from '@angular/platform-browser';
import { first } from 'rxjs/operators';

import { click } from '../../testing';
import { Hero } from '../model/hero';

import { DashboardHeroComponent } from './dashboard-hero.component';

describe('DashboardHeroComponent class only', () => {
it('raises the selected event when clicked', () => {
const comp = new DashboardHeroComponent();
const hero: Hero = { id: 42, name: 'Test' };
comp.hero = hero;

comp.selected.pipe(first()).subscribe((selectedHero: Hero) => expect(selectedHero).toBe(hero));
comp.click();
});
});

describe('DashboardHeroComponent when tested directly', () => {
let comp: DashboardHeroComponent;
let expectedHero: Hero;
Expand All @@ -40,7 +28,7 @@ describe('DashboardHeroComponent when tested directly', () => {

expectedHero = { id: 42, name: 'Test Name' };

comp.hero = expectedHero;
fixture.componentRef.setInput('hero', expectedHero);

fixture.detectChanges();
});
Expand All @@ -52,23 +40,23 @@ describe('DashboardHeroComponent when tested directly', () => {

it('should raise selected event when clicked (triggerEventHandler)', () => {
let selectedHero: Hero | undefined;
comp.selected.pipe(first()).subscribe((hero: Hero) => (selectedHero = hero));
comp.selectedFromObservable.subscribe((hero: Hero) => (selectedHero = hero));

heroDe.triggerEventHandler('click', null);
expect(selectedHero).toBe(expectedHero);
});

it('should raise selected event when clicked (element.click)', () => {
let selectedHero: Hero | undefined;
comp.selected.pipe(first()).subscribe((hero: Hero) => (selectedHero = hero));
comp.selectedToObservable$.subscribe((hero: Hero) => (selectedHero = hero));

heroEl.click();
expect(selectedHero).toBe(expectedHero);
});

it('should raise selected event when clicked (click helper with DebugElement)', () => {
let selectedHero: Hero | undefined;
comp.selected.pipe(first()).subscribe((hero: Hero) => (selectedHero = hero));
comp.selected.subscribe((hero: Hero) => (selectedHero = hero));

click(heroDe);

Expand All @@ -77,7 +65,7 @@ describe('DashboardHeroComponent when tested directly', () => {

it('should raise selected event when clicked (click helper with native element)', () => {
let selectedHero: Hero | undefined;
comp.selected.pipe(first()).subscribe((hero: Hero) => (selectedHero = hero));
comp.selected.subscribe((hero: Hero) => (selectedHero = hero));

click(heroEl);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
import { UpperCasePipe } from '@angular/common';
import { Component, EventEmitter, Input, Output } from '@angular/core';
import { Component, input, output } from '@angular/core';
import { outputFromObservable, outputToObservable } from '@angular/core/rxjs-interop';
import { Subject } from 'rxjs';

import { Hero } from '../model/hero';

@Component({
selector: 'dashboard-hero',
template: ` <div (click)="click()" class="hero">
{{ hero.name | uppercase }}
{{ hero().name | uppercase }}
</div>`,
styleUrls: ['./dashboard-hero.component.css'],
standalone: true,
imports: [UpperCasePipe],
})
export class DashboardHeroComponent {
@Input() hero!: Hero;
@Output() selected = new EventEmitter<Hero>();
hero = input.required<Hero>();
readonly selected = output<Hero>();
private readonly selected$ = new Subject<Hero>();
readonly selectedFromObservable = outputFromObservable(this.selected$);
readonly selectedToObservable$ = outputToObservable(this.selected);

click() {
this.selected.emit(this.hero);
this.selected$.next(this.hero());
this.selected.emit(this.hero());
}
}
Loading