Skip to content

Commit

Permalink
refactor(core): properly type withBody and withHead testing helpe…
Browse files Browse the repository at this point in the history
…rs (#54801)

This commit addresses a typing mismatch, where these functions were declared to return whichever
value their callback returned, but this was inaccurate: it's always a test callback function
with `done` argument.

PR Close #54801
  • Loading branch information
JoostK authored and atscott committed Mar 11, 2024
1 parent b6a7d50 commit e82173e
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions packages/private/testing/src/utils.ts
Expand Up @@ -34,7 +34,10 @@ import {ɵresetJitOptions as resetJitOptions} from '@angular/core';
* @param html HTML which should be inserted into the `body` of the `document`.
* @param blockFn function to wrap. The function can return promise or be `async`.
*/
export function withBody<T extends Function>(html: string, blockFn: T): T {
export function withBody(
html: string,
blockFn: () => Promise<unknown> | void,
): jasmine.ImplementationCallback {
return wrapTestFn(() => document.body, html, blockFn);
}

Expand Down Expand Up @@ -63,23 +66,26 @@ export function withBody<T extends Function>(html: string, blockFn: T): T {
* @param html HTML which should be inserted into the `head` of the `document`.
* @param blockFn function to wrap. The function can return promise or be `async`.
*/
export function withHead<T extends Function>(html: string, blockFn: T): T {
export function withHead(
html: string,
blockFn: () => Promise<unknown> | void,
): jasmine.ImplementationCallback {
return wrapTestFn(() => document.head, html, blockFn);
}

/**
* Wraps provided function (which typically contains the code of a test) into a new function that
* performs the necessary setup of the environment.
*/
function wrapTestFn<T extends Function>(
function wrapTestFn(
elementGetter: () => HTMLElement,
html: string,
blockFn: T,
): T {
return function () {
blockFn: () => Promise<unknown> | void,
): jasmine.ImplementationCallback {
return () => {
elementGetter().innerHTML = html;
return blockFn();
} as any;
};
}

/**
Expand Down

0 comments on commit e82173e

Please sign in to comment.