Skip to content

Commit

Permalink
test: add example of using input.required
Browse files Browse the repository at this point in the history
  • Loading branch information
Anh Pham authored and ahnpnl committed May 6, 2024
1 parent 9344fee commit b501628
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 36 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 @@ -50,25 +38,9 @@ describe('DashboardHeroComponent when tested directly', () => {
expect(heroEl.textContent).toContain(expectedPipedName);
});

it('should raise selected event when clicked (triggerEventHandler)', () => {
let selectedHero: Hero | undefined;
comp.selected.pipe(first()).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));

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 +49,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,22 @@
import { UpperCasePipe } from '@angular/common';
import { Component, EventEmitter, Input, Output } from '@angular/core';
import { Component, input, output } from '@angular/core';

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>();

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

0 comments on commit b501628

Please sign in to comment.