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: allow OutputEmitterRef keys in triggerEventHandler #672

Merged
merged 1 commit into from
Sep 12, 2024
Merged
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
2 changes: 1 addition & 1 deletion projects/spectator/src/lib/base/dom-spectator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ export abstract class DomSpectator<I> extends BaseSpectator {
return event;
}

public triggerEventHandler<C = any, K extends KeysMatching<C, EventEmitter<any>> = any>(
public triggerEventHandler<C = any, K extends KeysMatching<C, EventEmitter<any> | OutputEmitterRef<any>> = any>(
directiveOrSelector: Type<C> | string | DebugElement,
eventName: K,
eventObj: OutputType<C[K]>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,31 @@ describe('ChildCustomEventParentComponent', () => {
declareComponent: false,
});

it('should trigger custom event with directive selector', () => {
spectator = createComponent();
spectator.triggerEventHandler(ChildCustomEventComponent, 'customEvent', 'hello');
expect(spectator.query(byText('hello'))).toExist();
describe('new EventEmitter()', () => {
it('should trigger custom event with directive selector', () => {
spectator = createComponent();
spectator.triggerEventHandler(ChildCustomEventComponent, 'customEventUsingEventEmitter', 'hello');
expect(spectator.query(byText('hello'))).toExist();
});

it('should trigger custom event with string selector', () => {
spectator = createComponent();
spectator.triggerEventHandler('app-child-custom-event', 'customEventUsingEventEmitter', 'hello');
expect(spectator.query(byText('hello'))).toExist();
});
});

it('should trigger custom event with string selector', () => {
spectator = createComponent();
spectator.triggerEventHandler('app-child-custom-event', 'customEvent', 'hello');
expect(spectator.query(byText('hello'))).toExist();
describe('output()', () => {
it('should trigger custom event with directive selector', () => {
spectator = createComponent();
spectator.triggerEventHandler(ChildCustomEventComponent, 'customEventUsingOutputEmitter', 'hello');
expect(spectator.query(byText('hello'))).toExist();
});

it('should trigger custom event with string selector', () => {
spectator = createComponent();
spectator.triggerEventHandler('app-child-custom-event', 'customEventUsingOutputEmitter', 'hello');
expect(spectator.query(byText('hello'))).toExist();
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,21 @@ import { Component } from '@angular/core';
@Component({
selector: 'app-child-custom-event-parent',
template: `
<app-child-custom-event (customEvent)="onCustomEvent($event)"></app-child-custom-event>
<app-child-custom-event
(customEventUsingEventEmitter)="onCustomEventUsingEventEmitter($event)"
(customEventUsingOutputEmitter)="onCustomEventUsingOutputEmitter($event)"
/>
<p>{{ eventValue }}</p>
`,
})
export class ChildCustomEventParentComponent {
public eventValue = '';

public onCustomEvent(eventValue: string): void {
public onCustomEventUsingEventEmitter(eventValue: string): void {
this.eventValue = eventValue;
}

public onCustomEventUsingOutputEmitter(eventValue: string): void {
this.eventValue = eventValue;
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Component, Output, EventEmitter } from '@angular/core';
import { Component, Output, output, EventEmitter } from '@angular/core';

@Component({
selector: 'app-child-custom-event',
template: ` <p>Custom child</p> `,
})
export class ChildCustomEventComponent {
@Output() customEvent = new EventEmitter<string>();
@Output() customEventUsingEventEmitter = new EventEmitter<string>();
customEventUsingOutputEmitter = output<string>();
}