|
1 | 1 | import { jest, describe, it, beforeEach, expect } from '@jest/globals';
|
2 |
| -import { h, render } from 'preact'; |
| 2 | +import { h, hydrate, 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';
|
@@ -616,3 +616,60 @@ describe('Router', () => {
|
616 | 616 | replaceState.mockRestore();
|
617 | 617 | });
|
618 | 618 | });
|
| 619 | + |
| 620 | +describe('hydration', () => { |
| 621 | + let scratch; |
| 622 | + beforeEach(() => { |
| 623 | + if (scratch) { |
| 624 | + render(null, scratch); |
| 625 | + scratch.remove(); |
| 626 | + } |
| 627 | + scratch = document.createElement('scratch'); |
| 628 | + document.body.appendChild(scratch); |
| 629 | + history.replaceState(null, null, '/'); |
| 630 | + }); |
| 631 | + |
| 632 | + it('should wait for asynchronous routes', async () => { |
| 633 | + scratch.innerHTML = '<div><h1>A</h1><p>hello</p></div>'; |
| 634 | + const route = name => html` |
| 635 | + <div> |
| 636 | + <h1>${name}</h1> |
| 637 | + <p>hello</p> |
| 638 | + </div> |
| 639 | + `; |
| 640 | + const A = jest.fn(groggy(() => route('A'), 1)); |
| 641 | + let loc; |
| 642 | + hydrate( |
| 643 | + html` |
| 644 | + <${ErrorBoundary}> |
| 645 | + <${LocationProvider}> |
| 646 | + <${Router}> |
| 647 | + <${A} path="/" /> |
| 648 | + <//> |
| 649 | + <${() => { |
| 650 | + loc = useLocation(); |
| 651 | + }} /> |
| 652 | + <//> |
| 653 | + <//> |
| 654 | + `, |
| 655 | + scratch |
| 656 | + ); |
| 657 | + |
| 658 | + const mutations = []; |
| 659 | + const mutationObserver = new MutationObserver((x) => { |
| 660 | + mutations.push(...x) |
| 661 | + }); |
| 662 | + mutationObserver.observe(scratch, { childList: true, subtree: true }); |
| 663 | + |
| 664 | + expect(scratch).toHaveProperty('innerHTML', '<div><h1>A</h1><p>hello</p></div>'); |
| 665 | + expect(A).toHaveBeenCalledWith({ path: '/', query: {}, params: {}, rest: '' }, expect.anything()); |
| 666 | + |
| 667 | + A.mockClear(); |
| 668 | + await sleep(10); |
| 669 | + await sleep(10); |
| 670 | + |
| 671 | + expect(scratch).toHaveProperty('innerHTML', '<div><h1>A</h1><p>hello</p></div>'); |
| 672 | + expect(A).toHaveBeenCalledWith({ path: '/', query: {}, params: {}, rest: '' }, expect.anything()); |
| 673 | + expect(mutations).toHaveLength(0); |
| 674 | + }); |
| 675 | +}) |
0 commit comments