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 @@ -2166,7 +2166,7 @@ export function pushState(url, state) {
2166
2166
history . pushState ( opts , '' , resolve_url ( url ) ) ;
2167
2167
has_navigated = true ;
2168
2168
2169
- page . state = state ;
2169
+ page . state = unstringify ( opts [ STATES_KEY ] , app . hooks . transport ) ;
2170
2170
root . $set ( {
2171
2171
// we need to assign a new page object so that subscribers are correctly notified
2172
2172
page : untrack ( ( ) => clone_page ( page ) )
@@ -2200,7 +2200,7 @@ export function replaceState(url, state) {
2200
2200
2201
2201
history . replaceState ( opts , '' , resolve_url ( url ) ) ;
2202
2202
2203
- page . state = state ;
2203
+ page . state = unstringify ( opts [ STATES_KEY ] , app . hooks . transport ) ;
2204
2204
root . $set ( {
2205
2205
page : untrack ( ( ) => clone_page ( page ) )
2206
2206
} ) ;
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 @@ -1500,13 +1500,20 @@ test.describe('Shallow routing', () => {
1500
1500
1501
1501
test ( 'pushState properly serializes objects' , async ( { page } ) => {
1502
1502
await page . goto ( '/shallow-routing/push-state/foo' ) ;
1503
- await expect ( page . locator ( 'p' ) ) . toHaveText ( 'foo: nope' ) ;
1503
+ await expect ( page . locator ( '[data-testid=foo]' ) ) . toHaveText ( 'foo: nope' ) ;
1504
+ await expect ( page . locator ( '[data-testid=count]' ) ) . toHaveText ( 'count: nope' ) ;
1504
1505
1505
- await page . locator ( 'button' ) . click ( ) ;
1506
- await expect ( page . locator ( 'p' ) ) . toHaveText ( 'foo: it works?!' ) ;
1506
+ await page . getByText ( 'push state' ) . click ( ) ;
1507
+ await expect ( page . locator ( '[data-testid=foo]' ) ) . toHaveText ( 'foo: it works?!' ) ;
1508
+ await expect ( page . locator ( '[data-testid=count]' ) ) . toHaveText ( 'count: 0' ) ;
1509
+
1510
+ await page . getByText ( 'bump count' ) . click ( ) ;
1511
+ await expect ( page . locator ( '[data-testid=foo]' ) ) . toHaveText ( 'foo: it works?!' ) ;
1512
+ await expect ( page . locator ( '[data-testid=count]' ) ) . toHaveText ( 'count: 0' ) ; // Ensure count is not bumped
1507
1513
1508
1514
await page . goBack ( ) ;
1509
1515
await expect ( page . locator ( 'p' ) ) . toHaveText ( 'foo: nope' ) ;
1516
+ await expect ( page . locator ( '[data-testid=count]' ) ) . toHaveText ( 'count: nope' ) ;
1510
1517
} ) ;
1511
1518
} ) ;
1512
1519
You can’t perform that action at this time.
0 commit comments