|
1 | 1 | import { jest, describe, it, beforeEach, expect } from '@jest/globals';
|
2 |
| -import { h, hydrate, render } from 'preact'; |
| 2 | +import { h, hydrate, options, render } from 'preact'; |
3 | 3 | import { useState } from 'preact/hooks';
|
4 | 4 | import { html } from 'htm/preact';
|
5 | 5 | import { LocationProvider, Router, useLocation, Route, useRoute } from '../src/router.js';
|
@@ -629,7 +629,7 @@ describe('hydration', () => {
|
629 | 629 | history.replaceState(null, null, '/');
|
630 | 630 | });
|
631 | 631 |
|
632 |
| - it('should wait for asynchronous routes', async () => { |
| 632 | + it.only('should wait for asynchronous routes', async () => { |
633 | 633 | scratch.innerHTML = '<div><h1>A</h1><p>hello</p></div>';
|
634 | 634 | const route = name => html`
|
635 | 635 | <div>
|
@@ -663,13 +663,35 @@ describe('hydration', () => {
|
663 | 663 |
|
664 | 664 | expect(scratch).toHaveProperty('innerHTML', '<div><h1>A</h1><p>hello</p></div>');
|
665 | 665 | 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 | + } |
667 | 685 | A.mockClear();
|
668 | 686 | await sleep(10);
|
669 |
| - await sleep(10); |
670 | 687 |
|
671 | 688 | expect(scratch).toHaveProperty('innerHTML', '<div><h1>A</h1><p>hello</p></div>');
|
672 | 689 | expect(A).toHaveBeenCalledWith({ path: '/', query: {}, params: {}, rest: '' }, expect.anything());
|
673 | 690 | expect(mutations).toHaveLength(0);
|
| 691 | + |
| 692 | + options.__b = oldOptionsVnode; |
674 | 693 | });
|
675 | 694 | })
|
| 695 | + |
| 696 | +const MODE_HYDRATE = 1 << 5; |
| 697 | +const MODE_SUSPENDED = 1 << 7; |
0 commit comments