Skip to content

Commit 54d9e17

Browse files
author
Simon Mumenthaler
committed
fix type OutputRefKeysWithCallback and add typings tests
1 parent 46bc4a0 commit 54d9e17

File tree

2 files changed

+40
-8
lines changed

2 files changed

+40
-8
lines changed

projects/testing-library/src/lib/models.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
import { Type, DebugElement, OutputRef } from '@angular/core';
1+
import { Type, DebugElement, OutputRef, EventEmitter } from '@angular/core';
22
import { ComponentFixture, DeferBlockBehavior, DeferBlockState, TestBed } from '@angular/core/testing';
33
import { Routes } from '@angular/router';
44
import { BoundFunction, Queries, queries, Config as dtlConfig, PrettyDOMOptions } from '@testing-library/dom';
55

66
export type OutputRefKeysWithCallback<T> = {
7-
[key in keyof T as T[key] extends OutputRef<any> ? key : never]?: T[key] extends OutputRef<infer U>
7+
[key in keyof T]?: T[key] extends EventEmitter<infer U>
8+
? (val: U) => void
9+
: T[key] extends OutputRef<infer U>
810
? (val: U) => void
911
: never;
1012
};
@@ -229,7 +231,7 @@ export interface RenderComponentOptions<ComponentType, Q extends Queries = typeo
229231

230232
/**
231233
* @description
232-
* An object to subscribe to EventEmitters/Observables of the component
234+
* An object with callbacks to subscribe to EventEmitters/Observables of the component
233235
*
234236
* @default
235237
* {}

projects/testing-library/tests/render.spec.ts

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import {
1717
import { outputFromObservable } from '@angular/core/rxjs-interop';
1818
import { NoopAnimationsModule, BrowserAnimationsModule } from '@angular/platform-browser/animations';
1919
import { TestBed } from '@angular/core/testing';
20-
import { render, fireEvent, screen } from '../src/public_api';
20+
import { render, fireEvent, screen, OutputRefKeysWithCallback } from '../src/public_api';
2121
import { ActivatedRoute, Resolve, RouterModule } from '@angular/router';
2222
import { fromEvent, map } from 'rxjs';
2323
import { AsyncPipe, NgIf } from '@angular/common';
@@ -203,6 +203,11 @@ describe('on', () => {
203203
readonly event = output<string>();
204204
}
205205

206+
@Component({ template: ``, standalone: true })
207+
class TestFixtureWithFunctionalDerivedEventComponent {
208+
readonly event = outputFromObservable(fromEvent<MouseEvent>(inject(ElementRef).nativeElement, 'click'));
209+
}
210+
206211
it('should subscribe passed listener to the component EventEmitter', async () => {
207212
const spy = jest.fn();
208213
const { fixture } = await render(TestFixtureWithEventEmitterComponent, { on: { event: spy } });
@@ -268,17 +273,42 @@ describe('on', () => {
268273
});
269274

270275
it('should subscribe passed listener to a functional derived component output', async () => {
271-
@Component({ template: ``, standalone: true })
272-
class TestFixtureWithFunctionalDerivedEventComponent {
273-
readonly event = outputFromObservable(fromEvent<MouseEvent>(inject(ElementRef).nativeElement, 'click'));
274-
}
275276
const spy = jest.fn();
276277
const { fixture } = await render(TestFixtureWithFunctionalDerivedEventComponent, {
277278
on: { event: spy },
278279
});
279280
fireEvent.click(fixture.nativeElement);
280281
expect(spy).toHaveBeenCalled();
281282
});
283+
284+
it('OutputRefKeysWithCallback is correctly typed', () => {
285+
const fnWithVoidArg = (_: void) => void 0;
286+
const fnWithNumberArg = (_: number) => void 0;
287+
const fnWithStringArg = (_: string) => void 0;
288+
const fnWithMouseEventArg = (_: MouseEvent) => void 0;
289+
290+
// eslint-disable-next-line @typescript-eslint/no-empty-function
291+
function _test<T>(_on: OutputRefKeysWithCallback<T>) {}
292+
293+
// @ts-expect-error
294+
_test<TestFixtureWithEventEmitterComponent>({ event: fnWithNumberArg });
295+
_test<TestFixtureWithEventEmitterComponent>({ event: fnWithVoidArg });
296+
297+
// @ts-expect-error
298+
_test<TestFixtureWithDerivedEventComponent>({ event: fnWithNumberArg });
299+
_test<TestFixtureWithDerivedEventComponent>({ event: fnWithMouseEventArg });
300+
301+
// @ts-expect-error
302+
_test<TestFixtureWithFunctionalOutputComponent>({ event: fnWithNumberArg });
303+
_test<TestFixtureWithFunctionalOutputComponent>({ event: fnWithStringArg });
304+
305+
// @ts-expect-error
306+
_test<TestFixtureWithFunctionalDerivedEventComponent>({ event: fnWithNumberArg });
307+
_test<TestFixtureWithFunctionalDerivedEventComponent>({ event: fnWithMouseEventArg });
308+
309+
// add a statement so the test succeeds
310+
expect(true).toBeTruthy();
311+
});
282312
});
283313

284314
describe('animationModule', () => {

0 commit comments

Comments
 (0)