-
Notifications
You must be signed in to change notification settings - Fork 439
Expand file tree
/
Copy pathtest-hydrate.js
More file actions
60 lines (48 loc) · 1.93 KB
/
test-hydrate.js
File metadata and controls
60 lines (48 loc) · 1.93 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
/*
* Copyright (c) 2018, salesforce.com, inc.
* All rights reserved.
* SPDX-License-Identifier: MIT
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
*/
window.HydrateTest = (function (lwc, testUtils) {
testUtils.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 runTest(ssrRendered, Component, testConfig) {
const container = appendTestTarget(ssrRendered);
const selector = container.firstChild.tagName.toLowerCase();
let target = container.querySelector(selector);
let testResult;
const consoleSpy = testUtils.spyConsole();
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 = testConfig.test(target, snapshot, consoleSpy.calls);
} else if (testConfig.advancedTest) {
testResult = testConfig.advancedTest(target, {
Component,
hydrateComponent: lwc.hydrateComponent.bind(lwc),
consoleSpy,
container,
selector,
});
}
consoleSpy.reset();
return testResult;
}
return { runTest };
})(window.LWC, window.TestUtils);