Skip to content

Commit 1af5a7d

Browse files
committed
found a way
1 parent 0c7505b commit 1af5a7d

File tree

1 file changed

+26
-4
lines changed

1 file changed

+26
-4
lines changed

test/router.test.js

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { jest, describe, it, beforeEach, expect } from '@jest/globals';
2-
import { h, hydrate, render } from 'preact';
2+
import { h, hydrate, options, render } from 'preact';
33
import { useState } from 'preact/hooks';
44
import { html } from 'htm/preact';
55
import { LocationProvider, Router, useLocation, Route, useRoute } from '../src/router.js';
@@ -629,7 +629,7 @@ describe('hydration', () => {
629629
history.replaceState(null, null, '/');
630630
});
631631

632-
it('should wait for asynchronous routes', async () => {
632+
it.only('should wait for asynchronous routes', async () => {
633633
scratch.innerHTML = '<div><h1>A</h1><p>hello</p></div>';
634634
const route = name => html`
635635
<div>
@@ -663,13 +663,35 @@ describe('hydration', () => {
663663

664664
expect(scratch).toHaveProperty('innerHTML', '<div><h1>A</h1><p>hello</p></div>');
665665
expect(A).toHaveBeenCalledWith({ path: '/', query: {}, params: {}, rest: '' }, expect.anything());
666-
666+
const oldOptionsVnode = options.__b;
667+
let hasMatched = false;
668+
options.__b = (vnode) => {
669+
if (vnode.type === A && !hasMatched) {
670+
hasMatched = true;
671+
if (vnode.__ && vnode.__.__h) {
672+
expect(vnode.__.__h).toBe(true)
673+
} else if (vnode.__ && vnode.__.__u) {
674+
expect(!!(vnode.__.__u & MODE_SUSPENDED)).toBe(true);
675+
expect(!!(vnode.__.__u & MODE_HYDRATE)).toBe(true);
676+
} else {
677+
expect(true).toBe(false);
678+
}
679+
}
680+
681+
if (oldOptionsVnode) {
682+
oldOptionsVnode(vnode);
683+
}
684+
}
667685
A.mockClear();
668686
await sleep(10);
669-
await sleep(10);
670687

671688
expect(scratch).toHaveProperty('innerHTML', '<div><h1>A</h1><p>hello</p></div>');
672689
expect(A).toHaveBeenCalledWith({ path: '/', query: {}, params: {}, rest: '' }, expect.anything());
673690
expect(mutations).toHaveLength(0);
691+
692+
options.__b = oldOptionsVnode;
674693
});
675694
})
695+
696+
const MODE_HYDRATE = 1 << 5;
697+
const MODE_SUSPENDED = 1 << 7;

0 commit comments

Comments
 (0)