File tree Expand file tree Collapse file tree 3 files changed +29
-7
lines changed
src/routes/shallow-routing/push-state/foo Expand file tree Collapse file tree 3 files changed +29
-7
lines changed Original file line number Diff line number Diff line change @@ -2216,7 +2216,7 @@ export function pushState(url, state) {
2216
2216
history . pushState ( opts , '' , resolve_url ( url ) ) ;
2217
2217
has_navigated = true ;
2218
2218
2219
- page . state = state ;
2219
+ page . state = unstringify ( opts [ STATES_KEY ] , app . hooks . transport ) ;
2220
2220
root . $set ( {
2221
2221
// we need to assign a new page object so that subscribers are correctly notified
2222
2222
page : untrack ( ( ) => clone_page ( page ) )
@@ -2250,7 +2250,7 @@ export function replaceState(url, state) {
2250
2250
2251
2251
history . replaceState ( opts , '' , resolve_url ( url ) ) ;
2252
2252
2253
- page . state = state ;
2253
+ page . state = unstringify ( opts [ STATES_KEY ] , app . hooks . transport ) ;
2254
2254
root . $set ( {
2255
2255
page : untrack ( ( ) => clone_page ( page ) )
2256
2256
} ) ;
Original file line number Diff line number Diff line change 4
4
import { Foo } from ' $lib' ;
5
5
</script >
6
6
7
- <button onclick ={() => pushState (' ' , { foo: new Foo (' it works?' ) })}>do the thing</button >
7
+ <button
8
+ onclick ={() => {
9
+ const state = $state ({ foo: new Foo (' it works?' ), count: 0 });
10
+ pushState (' ' , state );
11
+ }}>push state</button
12
+ >
8
13
9
- <p >foo: {page .state .foo ?.bar () ?? ' nope' }</p >
14
+ <!-- Make sure that page.state.count loses its reactivity -->
15
+ <button
16
+ onclick ={() => {
17
+ page .state .count ++ ;
18
+ }}
19
+ >
20
+ bump count
21
+ </button >
22
+
23
+ <p data-testid ="foo" >foo: {page .state .foo ?.bar () ?? ' nope' }</p >
24
+ <p data-testid ="count" >count: {page .state .count ?? ' nope' }</p >
Original file line number Diff line number Diff line change @@ -1624,13 +1624,20 @@ test.describe('Shallow routing', () => {
1624
1624
1625
1625
test ( 'pushState properly serializes objects' , async ( { page } ) => {
1626
1626
await page . goto ( '/shallow-routing/push-state/foo' ) ;
1627
- await expect ( page . locator ( 'p' ) ) . toHaveText ( 'foo: nope' ) ;
1627
+ await expect ( page . locator ( '[data-testid=foo]' ) ) . toHaveText ( 'foo: nope' ) ;
1628
+ await expect ( page . locator ( '[data-testid=count]' ) ) . toHaveText ( 'count: nope' ) ;
1628
1629
1629
- await page . locator ( 'button' ) . click ( ) ;
1630
- await expect ( page . locator ( 'p' ) ) . toHaveText ( 'foo: it works?!' ) ;
1630
+ await page . getByText ( 'push state' ) . click ( ) ;
1631
+ await expect ( page . locator ( '[data-testid=foo]' ) ) . toHaveText ( 'foo: it works?!' ) ;
1632
+ await expect ( page . locator ( '[data-testid=count]' ) ) . toHaveText ( 'count: 0' ) ;
1633
+
1634
+ await page . getByText ( 'bump count' ) . click ( ) ;
1635
+ await expect ( page . locator ( '[data-testid=foo]' ) ) . toHaveText ( 'foo: it works?!' ) ;
1636
+ await expect ( page . locator ( '[data-testid=count]' ) ) . toHaveText ( 'count: 0' ) ; // Ensure count is not bumped
1631
1637
1632
1638
await page . goBack ( ) ;
1633
1639
await expect ( page . locator ( 'p' ) ) . toHaveText ( 'foo: nope' ) ;
1640
+ await expect ( page . locator ( '[data-testid=count]' ) ) . toHaveText ( 'count: nope' ) ;
1634
1641
} ) ;
1635
1642
} ) ;
1636
1643
You can’t perform that action at this time.
0 commit comments