-
Notifications
You must be signed in to change notification settings - Fork 601
/
Copy pathfixture.ts
208 lines (179 loc) · 5.84 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import {
Constructable,
defaultExecutionContext,
ExecutionContext,
HTMLView,
ViewTemplate,
} from "@microsoft/fast-element";
import { DesignSystem, DesignSystemRegistrationContext } from "../design-system";
import { DI } from "../di";
import type {
FoundationElementDefinition,
FoundationElementRegistry,
} from "../foundation-element/foundation-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;
/**
* A pre-configured design system instance used in setting up the fixture.
*/
designSystem?: DesignSystem;
}
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)}`;
}
function isElementRegistry<T>(
obj: any
): obj is FoundationElementRegistry<FoundationElementDefinition, any> {
return typeof obj.register === "function";
}
/**
* Creates a test fixture suitable for testing custom elements, templates, and bindings.
* @param templateNameOrRegistry 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>(
templateNameOrRegistry:
| ViewTemplate
| string
| FoundationElementRegistry<FoundationElementDefinition, Constructable<TElement>>
| [
FoundationElementRegistry<
FoundationElementDefinition,
Constructable<TElement>
>,
...FoundationElementRegistry<FoundationElementDefinition, Constructable>[]
],
options: FixtureOptions = {}
): Promise<Fixture<TElement>> {
const document = options.document || globalThis.document;
const parent = options.parent || document.createElement("div");
const source = options.source || {};
const context = options.context || defaultExecutionContext;
if (typeof templateNameOrRegistry === "string") {
const html = `<${templateNameOrRegistry}></${templateNameOrRegistry}>`;
templateNameOrRegistry = new ViewTemplate(html, []);
} else if (isElementRegistry(templateNameOrRegistry)) {
templateNameOrRegistry = [templateNameOrRegistry];
}
if (Array.isArray(templateNameOrRegistry)) {
const first = templateNameOrRegistry[0];
(options.designSystem || DesignSystem.getOrCreate(parent)).register(
templateNameOrRegistry
);
const container = DI.getOrCreateDOMContainer(parent);
const context = container.get(DesignSystemRegistrationContext);
const elementName = `${context.elementPrefix}-${first.definition.baseName}`;
const html = `<${elementName}></${elementName}>`;
templateNameOrRegistry = new ViewTemplate(html, []);
}
const view = templateNameOrRegistry.create();
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: templateNameOrRegistry,
view,
parent,
element,
connect,
disconnect,
};
}