-
Notifications
You must be signed in to change notification settings - Fork 600
/
fixture.ts
165 lines (140 loc) · 4.26 KB
/
fixture.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import {
defaultExecutionContext,
ExecutionContext,
HTMLView,
ViewTemplate,
} from "@microsoft/fast-element";
/**
* Options used to customize the creation of the test fixture.
*/
export interface FixtureOptions {
/**
* The document to run the fixture in.
* @defaultValue `globalThis.document`
*/
document?: Document;
/**
* The parent element to append the fixture to.
* @defaultValue An instance of `HTMLDivElement`.
*/
parent?: HTMLElement;
/**
* The data source to bind the HTML to.
* @defaultValue An empty object.
*/
source?: any;
/**
* The execution context to use during binding.
* @defaultValue {@link @microsoft/fast-element#defaultExecutionContext}
*/
context?: ExecutionContext;
}
export interface Fixture<TElement = HTMLElement> {
/**
* The document the fixture is running in.
*/
document: Document;
/**
* The template the fixture was created from.
*/
template: ViewTemplate;
/**
* The view that was created from the fixture's template.
*/
view: HTMLView;
/**
* The parent element that the view was appended to.
* @remarks
* This element will be appended to the DOM only
* after {@link Fixture.connect} has been called.
*/
parent: HTMLElement;
/**
* The first element in the {@link Fixture.view}.
*/
element: TElement;
/**
* Adds the {@link Fixture.parent} to the DOM, causing the
* connect lifecycle to begin.
* @remarks
* Yields control to the caller one Microtask later, in order to
* ensure that the DOM has settled.
*/
connect(): Promise<void>;
/**
* Removes the {@link Fixture.parent} from the DOM, causing the
* disconnect lifecycle to begin.
* @remarks
* Yields control to the caller one Microtask later, in order to
* ensure that the DOM has settled.
*/
disconnect(): Promise<void>;
}
function findElement(view: HTMLView): HTMLElement {
let current: Node | null = view.firstChild;
while (current !== null && current.nodeType !== 1) {
current = current.nextSibling;
}
return current as any;
}
/**
* Creates a random, unique name suitable for use as a Custom Element name.
*/
export function uniqueElementName(): string {
return `fast-unique-${Math.random().toString(36).substring(7)}`;
}
/**
* Creates a test fixture suitable for testing custom elements, templates, and bindings.
* @param templateOrElementName An HTML template or single element name to create the fixture for.
* @param options Enables customizing fixture creation behavior.
* @remarks
* Yields control to the caller one Microtask later, in order to
* ensure that the DOM has settled.
*/
export async function fixture<TElement = HTMLElement>(
templateOrElementName: ViewTemplate | string,
options: FixtureOptions = {}
): Promise<Fixture<TElement>> {
if (typeof templateOrElementName === "string") {
const html = `<${templateOrElementName}></${templateOrElementName}>`;
templateOrElementName = new ViewTemplate(html, []);
}
const view = templateOrElementName.create();
const document = options.document || globalThis.document;
const parent = options.parent || document.createElement("div");
const source = options.source || {};
const context = options.context || defaultExecutionContext;
const element = findElement(view) as any;
let isConnected = false;
view.bind(source, context);
view.appendTo(parent);
customElements.upgrade(parent);
// Hook into the Microtask Queue to ensure the DOM is settled
// before yielding control to the caller.
await Promise.resolve();
const connect = async () => {
if (isConnected) {
return;
}
isConnected = true;
document.body.appendChild(parent);
await Promise.resolve();
};
const disconnect = async () => {
if (!isConnected) {
return;
}
isConnected = false;
document.body.removeChild(parent);
await Promise.resolve();
};
return {
document,
template: templateOrElementName,
view,
parent,
element,
connect,
disconnect,
};
}