-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Interactivity API: Override unfinished navigate
calls
#54201
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b0fa1e4
Add failing test
DAreRodz 855cb75
Update navigate function
DAreRodz dabe2bf
Remove click delays
DAreRodz 43556a0
Update changelog
DAreRodz 83ae05f
Add failing test for URLs with hashes
DAreRodz 578029c
Use href instead of url for the current navigation
DAreRodz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
packages/e2e-tests/plugins/interactive-blocks/router-navigate/block.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"apiVersion": 2, | ||
"name": "test/router-navigate", | ||
"title": "E2E Interactivity tests - router navigate", | ||
"category": "text", | ||
"icon": "heart", | ||
"description": "", | ||
"supports": { | ||
"interactivity": true | ||
}, | ||
"textdomain": "e2e-interactivity", | ||
"viewScript": "router-navigate-view", | ||
"render": "file:./render.php" | ||
} |
38 changes: 38 additions & 0 deletions
38
packages/e2e-tests/plugins/interactive-blocks/router-navigate/render.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
/** | ||
* HTML for testing the router navigate function. | ||
* | ||
* @package gutenberg-test-interactive-blocks | ||
* @phpcs:disable VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable | ||
*/ | ||
|
||
?> | ||
|
||
|
||
<div data-wp-interactive data-wp-navigation-id="region-1"> | ||
<h2 data-testid="title"><?php echo $attributes['title']; ?></h2> | ||
|
||
<output | ||
data-testid="router navigations" | ||
data-wp-text="state.router.navigations" | ||
>NaN</output> | ||
<output | ||
data-testid="router status" | ||
data-wp-text="state.router.status" | ||
>undefined</output> | ||
|
||
<?php | ||
if ( isset( $attributes['links'] ) ) { | ||
foreach ( $attributes['links'] as $key => $link ) { | ||
$i = $key += 1; | ||
echo <<<HTML | ||
<a | ||
data-testid="link $i" | ||
data-wp-on--click="actions.router.navigate" | ||
href="$link" | ||
>link $i</a> | ||
HTML; | ||
} | ||
} | ||
?> | ||
</div> |
33 changes: 33 additions & 0 deletions
33
packages/e2e-tests/plugins/interactive-blocks/router-navigate/view.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
( ( { wp } ) => { | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
const { store, navigate } = wp.interactivity; | ||
|
||
store( { | ||
state: { | ||
router: { | ||
status: 'idle', | ||
navigations: 0, | ||
} | ||
}, | ||
actions: { | ||
router: { | ||
navigate: async ( { state, event: e } ) => { | ||
e.preventDefault(); | ||
|
||
state.router.navigations += 1; | ||
state.router.status = 'busy'; | ||
|
||
await navigate( e.target.href ); | ||
|
||
state.router.navigations -= 1; | ||
|
||
if ( state.router.navigations === 0) { | ||
state.router.status = 'idle'; | ||
} | ||
}, | ||
}, | ||
}, | ||
} ); | ||
} )( window ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import { test, expect } from './fixtures'; | ||
|
||
test.describe( 'Router navigate', () => { | ||
test.beforeAll( async ( { interactivityUtils: utils } ) => { | ||
await utils.activatePlugins(); | ||
const link2 = await utils.addPostWithBlock( 'test/router-navigate', { | ||
alias: 'router navigate - link 2', | ||
attributes: { title: 'Link 2' }, | ||
} ); | ||
const link1 = await utils.addPostWithBlock( 'test/router-navigate', { | ||
alias: 'router navigate - link 1', | ||
attributes: { title: 'Link 1' }, | ||
} ); | ||
await utils.addPostWithBlock( 'test/router-navigate', { | ||
alias: 'router navigate - main', | ||
attributes: { title: 'Main', links: [ link1, link2 ] }, | ||
} ); | ||
} ); | ||
|
||
test.beforeEach( async ( { interactivityUtils: utils, page } ) => { | ||
await page.goto( utils.getLink( 'router navigate - main' ) ); | ||
} ); | ||
|
||
test.afterAll( async ( { interactivityUtils: utils } ) => { | ||
await utils.deactivatePlugins(); | ||
await utils.deleteAllPosts(); | ||
} ); | ||
|
||
test( 'should update the HTML only for the latest navigation', async ( { | ||
page, | ||
interactivityUtils: utils, | ||
} ) => { | ||
const link1 = utils.getLink( 'router navigate - link 1' ); | ||
const link2 = utils.getLink( 'router navigate - link 2' ); | ||
|
||
const navigations = page.getByTestId( 'router navigations' ); | ||
const status = page.getByTestId( 'router status' ); | ||
const title = page.getByTestId( 'title' ); | ||
|
||
await expect( navigations ).toHaveText( '0' ); | ||
await expect( status ).toHaveText( 'idle' ); | ||
|
||
let resolveLink1: Function; | ||
let resolveLink2: Function; | ||
|
||
await page.route( link1, async ( route ) => { | ||
await new Promise( ( r ) => ( resolveLink1 = r ) ); | ||
await route.continue(); | ||
} ); | ||
await page.route( link2, async ( route ) => { | ||
await new Promise( ( r ) => ( resolveLink2 = r ) ); | ||
await route.continue(); | ||
} ); | ||
|
||
await page.getByTestId( 'link 1' ).click(); | ||
await page.getByTestId( 'link 2' ).click(); | ||
|
||
await expect( navigations ).toHaveText( '2' ); | ||
await expect( status ).toHaveText( 'busy' ); | ||
await expect( title ).toHaveText( 'Main' ); | ||
|
||
await Promise.resolve().then( () => resolveLink2() ); | ||
|
||
await expect( navigations ).toHaveText( '1' ); | ||
await expect( status ).toHaveText( 'busy' ); | ||
await expect( title ).toHaveText( 'Link 2' ); | ||
|
||
await Promise.resolve().then( () => resolveLink1() ); | ||
|
||
await expect( navigations ).toHaveText( '0' ); | ||
await expect( status ).toHaveText( 'idle' ); | ||
await expect( title ).toHaveText( 'Link 2' ); | ||
} ); | ||
} ); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the logic for using
url
instead ofhref
here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a good question. 😄 I think I assumed that, as the
url
would be the same forhref
with hashes, both navigations would wait for the same Promise so that they would happen in order and there would be no problem.I missed considering that:
force
and request the HTML again.I added a failing test in 83ae05f and fixed it in 578029c.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I think it's a bit safer this way. That way, the user will end up with the correct hash in the browser URL (even though we don't support scrolling to the id yet).
Thanks David!