-
Notifications
You must be signed in to change notification settings - Fork 439
Expand file tree
/
Copy pathtest-hydrate.js
More file actions
60 lines (47 loc) · 1.81 KB
/
test-hydrate.js
File metadata and controls
60 lines (47 loc) · 1.81 KB
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
import * as LWC from 'lwc';
import { spyConsole } from './console';
import { setHooks } from './hooks';
setHooks({ sanitizeHtmlContent: (content) => content });
function parseStringToDom(html) {
return Document.parseHTMLUnsafe(html).body.firstChild;
}
function appendTestTarget(ssrText) {
const div = document.createElement('div');
const testTarget = parseStringToDom(ssrText);
div.appendChild(testTarget);
document.body.appendChild(div);
return div;
}
function setFeatureFlags(requiredFeatureFlags, value) {
requiredFeatureFlags?.forEach((featureFlag) => {
LWC.setFeatureFlagForTest(featureFlag, value);
});
}
async function runTest(ssrRendered, Component, testConfig) {
const container = appendTestTarget(ssrRendered);
const selector = container.firstChild.tagName.toLowerCase();
let target = container.querySelector(selector);
let testResult;
const consoleSpy = spyConsole();
setFeatureFlags(testConfig.requiredFeatureFlags, true);
if (testConfig.test) {
const snapshot = testConfig.snapshot ? testConfig.snapshot(target) : {};
const props = testConfig.props || {};
const clientProps = testConfig.clientProps || props;
LWC.hydrateComponent(target, Component, clientProps);
// let's select again the target, it should be the same elements as in the snapshot
target = container.querySelector(selector);
testResult = await testConfig.test(target, snapshot, consoleSpy.calls);
} else if (testConfig.advancedTest) {
testResult = await testConfig.advancedTest(target, {
Component,
hydrateComponent: LWC.hydrateComponent.bind(LWC),
consoleSpy,
container,
selector,
});
}
consoleSpy.reset();
return testResult;
}
export { runTest };