Skip to content

Commit dbaa9fb

Browse files
authored
fix(testing): initialise mock-doc childNodes with get / set. Allows patching during tests (#6564)
* fix(testing): initialise mock-doc `childNodes` with get / set. Allows patching during tests * chore: node / element now return mock counterparts
1 parent 5ca9668 commit dbaa9fb

File tree

4 files changed

+18
-29
lines changed

4 files changed

+18
-29
lines changed

src/mock-doc/document.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,7 @@ const DOC_KEY_KEEPERS = new Set([
294294
'ownerDocument',
295295
'parentNode',
296296
'childNodes',
297+
'_childNodes',
297298
'_shadowRoot',
298299
]);
299300

src/mock-doc/node.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,21 @@ export class MockNode {
2222
nodeType: number;
2323
ownerDocument: any;
2424
parentNode: MockNode | null;
25-
childNodes: MockNode[];
25+
private _childNodes: MockNode[] = [];
2626

2727
constructor(ownerDocument: any, nodeType: number, nodeName: string | null, nodeValue: string | null) {
2828
this.ownerDocument = ownerDocument;
2929
this.nodeType = nodeType;
3030
this.nodeName = nodeName;
3131
this._nodeValue = nodeValue;
3232
this.parentNode = null;
33-
this.childNodes = [];
33+
}
34+
35+
get childNodes(): MockNode[] {
36+
return this._childNodes;
37+
}
38+
set childNodes(value: MockNode[]) {
39+
this._childNodes = value;
3440
}
3541

3642
appendChild(newNode: MockNode) {

src/mock-doc/window.ts

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -224,16 +224,7 @@ export class MockWindow {
224224
}
225225

226226
get Element() {
227-
if (this.__elementCstr == null) {
228-
const ownerDocument = this.document;
229-
this.__elementCstr = class extends MockElement {
230-
constructor() {
231-
super(ownerDocument, '');
232-
throw new Error('Illegal constructor: cannot construct Element');
233-
}
234-
};
235-
}
236-
return this.__elementCstr;
227+
return MockElement;
237228
}
238229

239230
fetch(input: any, init?: any): any {
@@ -362,16 +353,7 @@ export class MockWindow {
362353
}
363354

364355
get Node() {
365-
if (this.__nodeCstr == null) {
366-
const ownerDocument = this.document;
367-
this.__nodeCstr = class extends MockNode {
368-
constructor() {
369-
super(ownerDocument, 0, 'test', '');
370-
throw new Error('Illegal constructor: cannot construct Node');
371-
}
372-
};
373-
}
374-
return this.__nodeCstr;
356+
return MockNode;
375357
}
376358

377359
get NodeList() {

src/runtime/test/hydrate-slotted-content-order.spec.tsx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -336,8 +336,8 @@ describe("hydrated components' slotted node order", () => {
336336
hydrateClientSide: true,
337337
});
338338

339-
// patches this element in the same way we patch all elements in the browser
340-
patchPseudoShadowDom(clientHydrated.root);
339+
// patches this element's prototype in the same way we patch all elements in the browser
340+
patchPseudoShadowDom(Object.getPrototypeOf(clientHydrated.root));
341341

342342
expect(clientHydrated.root.outerHTML).toEqualHtml(`
343343
<cmp-a class="hydrated sc-cmp-a-h">
@@ -429,8 +429,8 @@ describe("hydrated components' slotted node order", () => {
429429
hydrateClientSide: true,
430430
});
431431

432-
// patches this element in the same way we patch all elements in the browser
433-
patchPseudoShadowDom(clientHydrated.root);
432+
// patches this element's prototype in the same way we patch all elements in the browser
433+
patchPseudoShadowDom(Object.getPrototypeOf(clientHydrated.root));
434434

435435
const childNodes = clientHydrated.root.childNodes;
436436

@@ -523,12 +523,12 @@ describe("hydrated components' slotted node order", () => {
523523
hydrateClientSide: true,
524524
});
525525

526-
// patches this element in the same way we patch all elements in the browser
527-
patchPseudoShadowDom(clientHydrated.root);
526+
// patches this element's prototype in the same way we patch all elements in the browser
527+
patchPseudoShadowDom(Object.getPrototypeOf(clientHydrated.root));
528528

529529
const childNodes = clientHydrated.root.childNodes;
530530

531-
patchPseudoShadowDom(childNodes[4]);
531+
patchPseudoShadowDom(Object.getPrototypeOf(childNodes[4]));
532532

533533
expect(nodeOrEle(childNodes[0])).toBe(`<p>slotted item 1a</p>`);
534534
expect(nodeOrEle(childNodes[1])).toBe(` a comment `);

0 commit comments

Comments
 (0)