Skip to content

Commit

Permalink
feat: support outputFromObservable and input.required
Browse files Browse the repository at this point in the history
  • Loading branch information
Anh Pham committed Apr 20, 2024
1 parent 8fef3e5 commit 095cc97
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 22 deletions.
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>();
selected = output<Hero>();
selected$ = new Subject<Hero>();
selectedFromObservable = outputFromObservable(this.selected$);
selectedToObservable$ = outputToObservable(this.selected);

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

0 comments on commit 095cc97

Please sign in to comment.