Skip to content

Commit 9344ebe

Browse files
committed
test for shallow navigation with effects
1 parent e6381c9 commit 9344ebe

File tree

5 files changed

+42
-4
lines changed

5 files changed

+42
-4
lines changed

.changeset/chatty-pears-notice.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
'@sveltejs/kit': patch
33
---
44

5-
fix: prevent infinite loop when calling `pushState`/`replaceState` in effect
5+
fix: prevent infinite loop when calling `pushState`/`replaceState` in `$effect`

packages/kit/test/apps/basics/src/app.d.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,9 @@ declare global {
1010
}
1111

1212
interface PageState {
13-
active: boolean;
13+
active?: boolean;
14+
count?: number;
1415
}
15-
16-
interface Platform {}
1716
}
1817
}
1918

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<script lang="ts">
2+
import { pushState } from '$app/navigation';
3+
4+
let count = $state(0);
5+
6+
$effect(() => {
7+
if (count) pushState('', { count });
8+
});
9+
</script>
10+
11+
<p>count: {count}</p>
12+
<button onclick={() => count++}>Increment</button>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<script lang="ts">
2+
import { pushState as replaceState } from '$app/navigation';
3+
4+
let count = $state(0);
5+
6+
$effect(() => {
7+
if (count) replaceState('', { count });
8+
});
9+
</script>
10+
11+
<p>count: {count}</p>
12+
<button onclick={() => count++}>Increment</button>

packages/kit/test/apps/basics/test/client.test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1477,6 +1477,21 @@ test.describe('Shallow routing', () => {
14771477
await expect(page.locator('h1')).toHaveText('parent');
14781478
await expect(page.locator('p')).toHaveText('active: true');
14791479
});
1480+
1481+
// TODO: pushState/replaceState infinite loop in $effect
1482+
test('pushState does not loop infinitely in $effect', async ({ page }) => {
1483+
await page.goto('/shallow-routing/push-state/effect');
1484+
await expect(page.locator('p')).toHaveText('count: 0');
1485+
await page.locator('button').click();
1486+
await expect(page.locator('p')).toHaveText('count: 1');
1487+
});
1488+
1489+
test('replaceState does not loop infinitely in $effect', async ({ page }) => {
1490+
await page.goto('/shallow-routing/replace-state/effect');
1491+
await expect(page.locator('p')).toHaveText('count: 0');
1492+
await page.locator('button').click();
1493+
await expect(page.locator('p')).toHaveText('count: 1');
1494+
});
14801495
});
14811496

14821497
test.describe('reroute', () => {

0 commit comments

Comments
 (0)