|
1 | 1 | import { jest, describe, it, beforeEach, expect } from '@jest/globals';
|
2 | 2 | import { h, render } from 'preact';
|
| 3 | +import { useState } from 'preact/hooks'; |
3 | 4 | import { html } from 'htm/preact';
|
4 | 5 | import { LocationProvider, Router, useLocation, Route, useRoute } from '../src/router.js';
|
5 | 6 | import lazy, { ErrorBoundary } from '../src/lazy.js';
|
@@ -55,6 +56,56 @@ describe('Router', () => {
|
55 | 56 | });
|
56 | 57 | });
|
57 | 58 |
|
| 59 | + it('should allow updating props in a route', async () => { |
| 60 | + const Home = jest.fn(() => html`<h1>Home</h1>`); |
| 61 | + const stack = []; |
| 62 | + let loc, set; |
| 63 | + |
| 64 | + const App = () => { |
| 65 | + const [test, setTest] = useState('2'); |
| 66 | + set = setTest; |
| 67 | + return html` |
| 68 | + <${LocationProvider}> |
| 69 | + <${Router} |
| 70 | + onRouteChange=${url => { |
| 71 | + stack.push(url); |
| 72 | + }} |
| 73 | + > |
| 74 | + <${Home} path="/" test=${test} /> |
| 75 | + <//> |
| 76 | + <${() => { |
| 77 | + loc = useLocation(); |
| 78 | + }} /> |
| 79 | + <//> |
| 80 | + `; |
| 81 | + } |
| 82 | + render( |
| 83 | + html`<${App} />`, |
| 84 | + scratch |
| 85 | + ); |
| 86 | + |
| 87 | + expect(scratch).toHaveProperty('textContent', 'Home'); |
| 88 | + expect(Home).toHaveBeenCalledWith({ path: '/', query: {}, params: {}, rest: '', test: '2' }, expect.anything()); |
| 89 | + expect(loc).toMatchObject({ |
| 90 | + url: '/', |
| 91 | + path: '/', |
| 92 | + query: {}, |
| 93 | + route: expect.any(Function) |
| 94 | + }); |
| 95 | + |
| 96 | + set('3') |
| 97 | + await sleep(1); |
| 98 | + |
| 99 | + expect(Home).toHaveBeenCalledWith({ path: '/', query: {}, params: {}, rest: '', test: '3' }, expect.anything()); |
| 100 | + expect(loc).toMatchObject({ |
| 101 | + url: '/', |
| 102 | + path: '/', |
| 103 | + query: {}, |
| 104 | + route: expect.any(Function) |
| 105 | + }); |
| 106 | + expect(scratch).toHaveProperty('textContent', 'Home'); |
| 107 | + }); |
| 108 | + |
58 | 109 | it('should switch between synchronous routes', async () => {
|
59 | 110 | const Home = jest.fn(() => html`<h1>Home</h1>`);
|
60 | 111 | const Profiles = jest.fn(() => html`<h1>Profiles</h1>`);
|
|
0 commit comments