@@ -10,15 +10,14 @@ import {ɵresetJitOptions as resetJitOptions} from '@angular/core';
10
10
/**
11
11
* Wraps a function in a new function which sets up document and HTML for running a test.
12
12
*
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.
15
15
*
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.
19
18
*
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.
22
21
*
23
22
* Example:
24
23
*
@@ -32,14 +31,51 @@ import {ɵresetJitOptions as resetJitOptions} from '@angular/core';
32
31
* });
33
32
* ```
34
33
*
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`.
36
35
* @param blockFn function to wrap. The function can return promise or be `async`.
37
- * @publicApi
38
36
*/
39
37
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 {
40
76
return function ( done : DoneFn ) {
41
77
if ( typeof blockFn === 'function' ) {
42
- document . body . innerHTML = html ;
78
+ elementGetter ( ) . innerHTML = html ;
43
79
const blockReturn = blockFn ( ) ;
44
80
if ( blockReturn instanceof Promise ) {
45
81
blockReturn . then ( done , done . fail ) ;
0 commit comments