Skip to content

Commit 06bc093

Browse files
authored
feat: allow OutputEmitterRef keys in triggerEventHandler (#672)
1 parent 02ed01d commit 06bc093

File tree

4 files changed

+37
-13
lines changed

4 files changed

+37
-13
lines changed

projects/spectator/src/lib/base/dom-spectator.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ export abstract class DomSpectator<I> extends BaseSpectator {
265265
return event;
266266
}
267267

268-
public triggerEventHandler<C = any, K extends KeysMatching<C, EventEmitter<any>> = any>(
268+
public triggerEventHandler<C = any, K extends KeysMatching<C, EventEmitter<any> | OutputEmitterRef<any>> = any>(
269269
directiveOrSelector: Type<C> | string | DebugElement,
270270
eventName: K,
271271
eventObj: OutputType<C[K]>,

projects/spectator/test/child-custom-event/child-custom-event-parent.component.spec.ts

+24-8
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,31 @@ describe('ChildCustomEventParentComponent', () => {
1313
declareComponent: false,
1414
});
1515

16-
it('should trigger custom event with directive selector', () => {
17-
spectator = createComponent();
18-
spectator.triggerEventHandler(ChildCustomEventComponent, 'customEvent', 'hello');
19-
expect(spectator.query(byText('hello'))).toExist();
16+
describe('new EventEmitter()', () => {
17+
it('should trigger custom event with directive selector', () => {
18+
spectator = createComponent();
19+
spectator.triggerEventHandler(ChildCustomEventComponent, 'customEventUsingEventEmitter', 'hello');
20+
expect(spectator.query(byText('hello'))).toExist();
21+
});
22+
23+
it('should trigger custom event with string selector', () => {
24+
spectator = createComponent();
25+
spectator.triggerEventHandler('app-child-custom-event', 'customEventUsingEventEmitter', 'hello');
26+
expect(spectator.query(byText('hello'))).toExist();
27+
});
2028
});
2129

22-
it('should trigger custom event with string selector', () => {
23-
spectator = createComponent();
24-
spectator.triggerEventHandler('app-child-custom-event', 'customEvent', 'hello');
25-
expect(spectator.query(byText('hello'))).toExist();
30+
describe('output()', () => {
31+
it('should trigger custom event with directive selector', () => {
32+
spectator = createComponent();
33+
spectator.triggerEventHandler(ChildCustomEventComponent, 'customEventUsingOutputEmitter', 'hello');
34+
expect(spectator.query(byText('hello'))).toExist();
35+
});
36+
37+
it('should trigger custom event with string selector', () => {
38+
spectator = createComponent();
39+
spectator.triggerEventHandler('app-child-custom-event', 'customEventUsingOutputEmitter', 'hello');
40+
expect(spectator.query(byText('hello'))).toExist();
41+
});
2642
});
2743
});

projects/spectator/test/child-custom-event/child-custom-event-parent.component.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,21 @@ import { Component } from '@angular/core';
33
@Component({
44
selector: 'app-child-custom-event-parent',
55
template: `
6-
<app-child-custom-event (customEvent)="onCustomEvent($event)"></app-child-custom-event>
6+
<app-child-custom-event
7+
(customEventUsingEventEmitter)="onCustomEventUsingEventEmitter($event)"
8+
(customEventUsingOutputEmitter)="onCustomEventUsingOutputEmitter($event)"
9+
/>
710
<p>{{ eventValue }}</p>
811
`,
912
})
1013
export class ChildCustomEventParentComponent {
1114
public eventValue = '';
1215

13-
public onCustomEvent(eventValue: string): void {
16+
public onCustomEventUsingEventEmitter(eventValue: string): void {
17+
this.eventValue = eventValue;
18+
}
19+
20+
public onCustomEventUsingOutputEmitter(eventValue: string): void {
1421
this.eventValue = eventValue;
1522
}
1623
}
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import { Component, Output, EventEmitter } from '@angular/core';
1+
import { Component, Output, output, EventEmitter } from '@angular/core';
22

33
@Component({
44
selector: 'app-child-custom-event',
55
template: ` <p>Custom child</p> `,
66
})
77
export class ChildCustomEventComponent {
8-
@Output() customEvent = new EventEmitter<string>();
8+
@Output() customEventUsingEventEmitter = new EventEmitter<string>();
9+
customEventUsingOutputEmitter = output<string>();
910
}

0 commit comments

Comments
 (0)