Skip to content

Commit 627cdf3

Browse files
AndrewKushniralxhub
authored andcommitted
test: add an internal helper to populate document.head before a test (angular#46250)
This commit reuses the logic of the `withBody` helper and implements the `withHead` based on it.The helper method is useful for tests that rely on a certain tags being present in document's `<head>` element. PR Close angular#46250
1 parent 8c15eea commit 627cdf3

File tree

1 file changed

+46
-10
lines changed

1 file changed

+46
-10
lines changed

packages/private/testing/src/utils.ts

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,14 @@ import {ɵresetJitOptions as resetJitOptions} from '@angular/core';
1010
/**
1111
* Wraps a function in a new function which sets up document and HTML for running a test.
1212
*
13-
* This function is intended to wrap an existing testing function. The wrapper
14-
* adds HTML to the `body` element of the `document` and subsequently tears it down.
13+
* This function wraps an existing testing function. The wrapper adds HTML to the `body` element of
14+
* the `document` and subsequently tears it down.
1515
*
16-
* This function is intended to be used with `async await` and `Promise`s. If the wrapped
17-
* function returns a promise (or is `async`) then the teardown is delayed until that `Promise`
18-
* is resolved.
16+
* This function can be used with `async await` and `Promise`s. If the wrapped function returns a
17+
* promise (or is `async`) then the teardown is delayed until that `Promise` is resolved.
1918
*
20-
* On `node` this function detects if `document` is present and if not it will create one by
21-
* loading `domino` and installing it.
19+
* In the NodeJS environment this function detects if `document` is present and if not, it creates
20+
* one by loading `domino` and installing it.
2221
*
2322
* Example:
2423
*
@@ -32,14 +31,51 @@ import {ɵresetJitOptions as resetJitOptions} from '@angular/core';
3231
* });
3332
* ```
3433
*
35-
* @param html HTML which should be inserted into `body` of the `document`.
34+
* @param html HTML which should be inserted into the `body` of the `document`.
3635
* @param blockFn function to wrap. The function can return promise or be `async`.
37-
* @publicApi
3836
*/
3937
export function withBody<T extends Function>(html: string, blockFn: T): T {
38+
return wrapTestFn(() => document.body, html, blockFn);
39+
}
40+
41+
/**
42+
* Wraps a function in a new function which sets up document and HTML for running a test.
43+
*
44+
* This function wraps an existing testing function. The wrapper adds HTML to the `head` element of
45+
* the `document` and subsequently tears it down.
46+
*
47+
* This function can be used with `async await` and `Promise`s. If the wrapped function returns a
48+
* promise (or is `async`) then the teardown is delayed until that `Promise` is resolved.
49+
*
50+
* In the NodeJS environment this function detects if `document` is present and if not, it creates
51+
* one by loading `domino` and installing it.
52+
*
53+
* Example:
54+
*
55+
* ```
56+
* describe('something', () => {
57+
* it('should do something', withHead('<link rel="preconnect" href="...">', async () => {
58+
* // ...
59+
* }));
60+
* });
61+
* ```
62+
*
63+
* @param html HTML which should be inserted into the `head` of the `document`.
64+
* @param blockFn function to wrap. The function can return promise or be `async`.
65+
*/
66+
export function withHead<T extends Function>(html: string, blockFn: T): T {
67+
return wrapTestFn(() => document.head, html, blockFn);
68+
}
69+
70+
/**
71+
* Wraps provided function (which typically contains the code of a test) into a new function that
72+
* performs the necessary setup of the environment.
73+
*/
74+
function wrapTestFn<T extends Function>(
75+
elementGetter: () => HTMLElement, html: string, blockFn: T): T {
4076
return function(done: DoneFn) {
4177
if (typeof blockFn === 'function') {
42-
document.body.innerHTML = html;
78+
elementGetter().innerHTML = html;
4379
const blockReturn = blockFn();
4480
if (blockReturn instanceof Promise) {
4581
blockReturn.then(done, done.fail);

0 commit comments

Comments
 (0)