Skip to content

Commit 8544d1e

Browse files
committed
fix(testing): get page.root when using setContent()
1 parent 8d424df commit 8544d1e

File tree

2 files changed

+26
-12
lines changed

2 files changed

+26
-12
lines changed

src/runtime/test/render-text.spec.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ describe('render-text', () => {
4545
expect(page.body).toEqualHtml(`
4646
<cmp-a>Hello World</cmp-a>
4747
`);
48+
expect(page.root).toEqualHtml(`<cmp-a>Hello World</cmp-a>`);
49+
expect(page.rootInstance).not.toBeUndefined();
50+
expect(page.rootInstance).not.toBeNull();
4851
});
4952

5053
it('Hello World, re-render, waitForChanges', async () => {

src/testing/spec-page.ts

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,6 @@ export async function newSpecPage(opts: d.NewSpecPageOptions): Promise<d.SpecPag
5252
win: win,
5353
doc: doc,
5454
body: doc.body as any,
55-
root: null as any,
56-
rootInstance: null as any,
5755
build: bc.BUILD as d.Build,
5856
styles: platform.styles as Map<string, string>,
5957
setContent: (html: string) => {
@@ -163,19 +161,32 @@ export async function newSpecPage(opts: d.NewSpecPageOptions): Promise<d.SpecPag
163161
await page.waitForChanges();
164162
}
165163

166-
page.root = findRootComponent(cmpTags, page.body);
167-
if (page.root != null) {
168-
const hostRef = platform.getHostRef(page.root);
169-
if (hostRef != null) {
170-
page.rootInstance = hostRef.$lazyInstance$;
164+
let rootComponent: any = null;
165+
Object.defineProperty(page, 'root', {
166+
get() {
167+
if (rootComponent == null) {
168+
rootComponent = findRootComponent(cmpTags, page.body);
169+
if (rootComponent != null) {
170+
return rootComponent;
171+
}
172+
}
173+
const firstElementChild = page.body.firstElementChild;
174+
if (firstElementChild != null) {
175+
return firstElementChild as any;
176+
}
177+
return null;
171178
}
179+
});
172180

173-
} else {
174-
const firstElementChild = page.body.firstElementChild;
175-
if (firstElementChild != null) {
176-
page.root = firstElementChild as any;
181+
Object.defineProperty(page, 'rootInstance', {
182+
get() {
183+
const hostRef = platform.getHostRef(page.root);
184+
if (hostRef != null) {
185+
return hostRef.$lazyInstance$;
186+
}
187+
return null;
177188
}
178-
}
189+
});
179190

180191
if (opts.hydrateServerSide) {
181192
platform.insertVdomAnnotations(doc);

0 commit comments

Comments
 (0)