Skip to content
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
4 changes: 2 additions & 2 deletions projects/testing-library/src/lib/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
} from '@angular/core';
import { ComponentFixture, DeferBlockBehavior, DeferBlockState, TestBed } from '@angular/core/testing';
import { Routes } from '@angular/router';
import { BoundFunction, Queries, queries, Config as dtlConfig, PrettyDOMOptions } from '@testing-library/dom';
import { BoundFunctions, Queries, queries, Config as dtlConfig, PrettyDOMOptions } from '@testing-library/dom';

// TODO: import from Angular (is a breaking change)
interface OutputRef<T> {
Expand All @@ -29,7 +29,7 @@ export type OutputRefKeysWithCallback<T> = {
: never;
};

export type RenderResultQueries<Q extends Queries = typeof queries> = { [P in keyof Q]: BoundFunction<Q[P]> };
export type RenderResultQueries<Q extends Queries = typeof queries> = BoundFunctions<Q>;
export interface RenderResult<ComponentType, WrapperType = ComponentType> extends RenderResultQueries {
/**
* @description
Expand Down
23 changes: 23 additions & 0 deletions projects/testing-library/src/tests/render.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,29 @@ describe('DTL functionality', () => {
// eslint-disable-next-line testing-library/prefer-screen-queries
fireEvent.click(view.getByText('button'));
});

test('render result queries should support generic type parameter', async () => {
const view = await render(FixtureComponent);

// eslint-disable-next-line testing-library/prefer-screen-queries
const inputByTestId: HTMLInputElement = view.getByTestId<HTMLInputElement>('input');
expect(inputByTestId).toBeInTheDocument();

// screen.getByTestId should also accept a generic type parameter
const inputByScreen: HTMLInputElement = screen.getByTestId<HTMLInputElement>('input');
expect(inputByScreen).toBeInTheDocument();

// eslint-disable-next-line testing-library/prefer-screen-queries
const button: HTMLButtonElement = view.getByRole<HTMLButtonElement>('button');
expect(button).toBeInTheDocument();

// @ts-expect-error - generic narrows the type, so assigning HTMLInputElement to HTMLButtonElement should fail
// eslint-disable-next-line testing-library/prefer-screen-queries
const _wrongType: HTMLButtonElement = view.getByTestId<HTMLInputElement>('input');
void _wrongType;

expect(true).toBeTruthy();
});
});

describe('components', () => {
Expand Down