test(svelte): migrate svelte-navigator to custom minimal router (#30225)

Repro: `cd tests/components/ct-svelte && rm -rf node_modules
package-lock.json && npm i && npx playwright test --project=chromium`

Follow-up based on
https://github.com/microsoft/playwright/pull/28624#issuecomment-1858608101.

Svelte has no router by default, only SvelteKit - so lets remove the
package which is not maintained anymore and not recommended.
This commit is contained in:
Max Schmitt 2024-04-04 16:01:08 +02:00 committed by GitHub
parent 345240b9df
commit 18b51308ff
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 62 additions and 40 deletions

View File

@ -18,12 +18,6 @@
"vite": "^5.0.11"
},
"dependencies": {
"svelte": "^4.2.8",
"svelte-navigator": "^3.2.2"
},
"overrides": {
"svelte-navigator": {
"svelte": "$svelte"
}
"svelte": "^4.2.8"
}
}

View File

@ -1,18 +1,34 @@
<script lang="ts">
import { Link, Route, Router } from "svelte-navigator";
import logo from './assets/svelte.png'
<script>
import { onMount, onDestroy } from 'svelte';
import LoginPage from './pages/LoginPage.svelte';
import DashboardPage from './pages/DashboardPage.svelte';
let path = '';
function updatePath() {
path = window.location.pathname;
}
onMount(() => {
updatePath();
window.addEventListener('popstate', updatePath);
});
onDestroy(() => {
window.removeEventListener('popstate', updatePath);
});
/**
* @param newPath {string}
*/
function navigate(newPath) {
history.pushState({}, '', newPath);
updatePath();
}
</script>
<Router>
<header>
<img src="{logo}" alt="logo" width="125" height="125" />
<Link to="/">Login</Link>
<Link to="/dashboard">Dashboard</Link>
</header>
<Route path="/*">
<Route component="{LoginPage}" />
<Route path="dashboard" component="{DashboardPage}" />
</Route>
</Router>
<header>
<a on:click={(e) => { e.preventDefault(); navigate('/'); }} href='/login'>Login</a>
<a on:click={(e) => { e.preventDefault(); navigate('/dashboard'); }} href='/dashboard'>Dashboard</a>
</header>
{#if path === '/'}
<LoginPage />
{:else if path === '/dashboard'}
<DashboardPage />
{/if}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 5.1 KiB

View File

@ -19,12 +19,6 @@
"sirv-cli": "^2.0.0"
},
"dependencies": {
"svelte": "^4.2.8",
"svelte-navigator": "^3.2.2"
},
"overrides": {
"svelte-navigator": {
"svelte": "$svelte"
}
"svelte": "^4.2.8"
}
}

View File

@ -1,16 +1,34 @@
<script lang="ts">
import { Link, Route, Router } from "svelte-navigator";
<script>
import { onMount, onDestroy } from 'svelte';
import LoginPage from './pages/LoginPage.svelte';
import DashboardPage from './pages/DashboardPage.svelte';
let path = '';
function updatePath() {
path = window.location.pathname;
}
onMount(() => {
updatePath();
window.addEventListener('popstate', updatePath);
});
onDestroy(() => {
window.removeEventListener('popstate', updatePath);
});
/**
* @param newPath {string}
*/
function navigate(newPath) {
history.pushState({}, '', newPath);
updatePath();
}
</script>
<Router>
<header>
<Link to="/">Login</Link>
<Link to="/dashboard">Dashboard</Link>
</header>
<Route path="/*">
<Route component="{LoginPage}" />
<Route path="dashboard" component="{DashboardPage}" />
</Route>
</Router>
<header>
<a on:click={(e) => { e.preventDefault(); navigate('/'); }} href='/login'>Login</a>
<a on:click={(e) => { e.preventDefault(); navigate('/dashboard'); }} href='/dashboard'>Dashboard</a>
</header>
{#if path === '/'}
<LoginPage />
{:else if path === '/dashboard'}
<DashboardPage />
{/if}