Skip to content

Commit

Permalink
fix: 🐛 type check on component outputs (#693)
Browse files Browse the repository at this point in the history
Allow subjects as component output

✅ Closes: #689
  • Loading branch information
lneumeier authored Feb 7, 2025
1 parent bfd6ad5 commit 08c9a21
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,30 @@ describe('FunctionOutputComponent', () => {
});

it('should emit the event on button click', () => {
let output;
let output = false;
spectator.output('buttonClick').subscribe((result) => (output = result));

spectator.click('button');

expect(output).toEqual(true);
});

it('should emit the event on button click - EventEmitter', () => {
let output = false;
spectator.output('buttonClickedEvent').subscribe((result) => (output = result));

spectator.click('button');

expect(output).toEqual(true);
});

it('should emit the event on button click - Subject', () => {
let output = false;
spectator.output('buttonClickedSubject').subscribe((result) => (output = result));
spectator.click('button');

expect(output).toEqual(true);
});
});

describe('with SpectatorHost', () => {
Expand All @@ -36,12 +53,30 @@ describe('FunctionOutputComponent', () => {
});

it('should emit the event on button click', () => {
let output;
let output = false;
host.output('buttonClick').subscribe((result) => (output = result));

host.click('button');

expect(output).toEqual(true);
});

it('should emit the event on button click - EventEmitter', () => {
let output = false;
host.output('buttonClickedEvent').subscribe((result) => (output = result));

host.click('button');

expect(output).toEqual(true);
});

it('should emit the event on button click - Subject', () => {
let output = false;
host.output('buttonClickedSubject').subscribe((result) => (output = result));

host.click('button');

expect(output).toEqual(true);
});
});
});
4 changes: 2 additions & 2 deletions projects/spectator/src/lib/base/dom-spectator.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { DebugElement, ElementRef, EventEmitter, OutputEmitterRef, Type } from '@angular/core';
import { ComponentFixture, tick } from '@angular/core/testing';
import { By } from '@angular/platform-browser';
import { Observable } from 'rxjs';
import { Observable, Subject } from 'rxjs';

import { dispatchFakeEvent, dispatchKeyboardEvent, dispatchMouseEvent, dispatchTouchEvent } from '../dispatch-events';
import { DOMSelector } from '../dom-selectors';
Expand All @@ -20,7 +20,7 @@ const KEY_UP = 'keyup';

type KeysMatchingReturnType<T, V> = keyof { [P in keyof T as T[P] extends V ? P : never]: P } & keyof T;
type KeysMatchingOutputFunction<T> = KeysMatchingReturnType<T, OutputEmitterRef<any>>;
type KeysMatchingClassicOutput<T> = KeysMatchingReturnType<T, EventEmitter<any>>;
type KeysMatchingClassicOutput<T> = KeysMatchingReturnType<T, EventEmitter<any> | Subject<any>>;
type KeysMatchingOutput<T> = KeysMatchingOutputFunction<T> | KeysMatchingClassicOutput<T>;

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { createComponentFactory, createHostFactory, Spectator, SpectatorHost } from '@ngneat/spectator';
import { FunctionOutputComponent } from './function-output.component';
import { fakeAsync, tick } from '@angular/core/testing';

describe('FunctionOutputComponent', () => {
describe('with Spectator', () => {
Expand All @@ -14,13 +15,30 @@ describe('FunctionOutputComponent', () => {
});

it('should emit the event on button click', () => {
let output;
let output = false;
spectator.output('buttonClick').subscribe((result) => (output = result));

spectator.click('button');

expect(output).toEqual(true);
});

it('should emit the event on button click - EventEmitter', () => {
let output = false;
spectator.output('buttonClickedEvent').subscribe((result) => (output = result));

spectator.click('button');

expect(output).toEqual(true);
});

it('should emit the event on button click - Subject', () => {
let output = false;
spectator.output('buttonClickedSubject').subscribe((result) => (output = result));
spectator.click('button');

expect(output).toEqual(true);
});
});

describe('with SpectatorHost', () => {
Expand All @@ -36,12 +54,30 @@ describe('FunctionOutputComponent', () => {
});

it('should emit the event on button click', () => {
let output;
let output = false;
host.output('buttonClick').subscribe((result) => (output = result));

host.click('button');

expect(output).toEqual(true);
});

it('should emit the event on button click - EventEmitter', () => {
let output = false;
host.output('buttonClickedEvent').subscribe((result) => (output = result));

host.click('button');

expect(output).toEqual(true);
});

it('should emit the event on button click - Subject', () => {
let output = false;
host.output('buttonClickedSubject').subscribe((result) => (output = result));

host.click('button');

expect(output).toEqual(true);
});
});
});
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
import { Component, input, output, ɵINPUT_SIGNAL_BRAND_WRITE_TYPE } from '@angular/core';
import { Component, EventEmitter, input, Output, output, ɵINPUT_SIGNAL_BRAND_WRITE_TYPE } from '@angular/core';
import { ReplaySubject } from 'rxjs';

@Component({
selector: 'app-function-output',
template: ` <button (click)="buttonClick.emit(true)">Emit function output</button> `,
template: ` <button (click)="buttonClicked()">Emit function output</button> `,
standalone: true,
})
export class FunctionOutputComponent {
public buttonClick = output<boolean>();

@Output()
public buttonClickedEvent = new EventEmitter<boolean>();

@Output()
public buttonClickedSubject = new ReplaySubject<boolean>();

protected buttonClicked(): void {
this.buttonClick.emit(true);
this.buttonClickedEvent.emit(true);
this.buttonClickedSubject.next(true);
}
}

0 comments on commit 08c9a21

Please sign in to comment.