-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add `afterLoad` callbacks * Add tests for `afterLoad` callbacks * Add changelog * Update changelog link * Add docs for `afterLoad` * Move store options to the end
- Loading branch information
Showing
8 changed files
with
207 additions
and
4 deletions.
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
packages/e2e-tests/plugins/interactive-blocks/store-afterload/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/store-afterload", | ||
"title": "E2E Interactivity tests - store afterload", | ||
"category": "text", | ||
"icon": "heart", | ||
"description": "", | ||
"supports": { | ||
"interactivity": true | ||
}, | ||
"textdomain": "e2e-interactivity", | ||
"viewScript": "store-afterload-view", | ||
"render": "file:./render.php" | ||
} |
41 changes: 41 additions & 0 deletions
41
packages/e2e-tests/plugins/interactive-blocks/store-afterload/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,41 @@ | ||
<?php | ||
/** | ||
* HTML for testing `afterLoad` callbacks added to the store. | ||
* | ||
* @package gutenberg-test-interactive-blocks | ||
*/ | ||
|
||
?> | ||
<div data-wp-interactive> | ||
<h3>Store statuses</h3> | ||
<p data-store-status data-wp-text="state.status1">waiting</p> | ||
<p data-store-status data-wp-text="state.status2">waiting</p> | ||
<p data-store-status data-wp-text="state.status3">waiting</p> | ||
<p data-store-status data-wp-text="state.status4">waiting</p> | ||
|
||
<h3><code>afterLoad</code> executions</h3> | ||
<p>All stores ready: | ||
<span | ||
data-testid="all-stores-ready" | ||
data-wp-text="state.allStoresReady"> | ||
>waiting</span> | ||
</p> | ||
<p>vDOM ready: | ||
<span | ||
data-testid="vdom-ready" | ||
data-wp-text="state.vdomReady"> | ||
>waiting</span> | ||
</p> | ||
<p><code>afterLoad</code> exec times: | ||
<span | ||
data-testid="after-load-exec-times" | ||
data-wp-text="state.execTimes.afterLoad"> | ||
>0</span> | ||
</p> | ||
<p><code>sharedAfterLoad</code> exec times: | ||
<span | ||
data-testid="shared-after-load-exec-times" | ||
data-wp-text="state.execTimes.sharedAfterLoad"> | ||
>0</span> | ||
</p> | ||
</div> |
60 changes: 60 additions & 0 deletions
60
packages/e2e-tests/plugins/interactive-blocks/store-afterload/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,60 @@ | ||
( ( { wp } ) => { | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
const { store } = wp.interactivity; | ||
|
||
const afterLoad = ({ state }) => { | ||
// Check the state is correctly initialized. | ||
const { status1, status2, status3, status4 } = state; | ||
state.allStoresReady = | ||
[ status1, status2, status3, status4 ] | ||
.every( ( t ) => t === 'ready' ) | ||
.toString(); | ||
|
||
// Check the HTML has been processed as well. | ||
const selector = '[data-store-status]'; | ||
state.vdomReady = | ||
document.querySelector( selector ) && | ||
Array.from( | ||
document.querySelectorAll( selector ) | ||
).every( ( el ) => el.textContent === 'ready' ).toString(); | ||
|
||
// Increment exec times everytime this function runs. | ||
state.execTimes.afterLoad += 1; | ||
} | ||
|
||
const sharedAfterLoad = ({ state }) => { | ||
// Increment exec times everytime this function runs. | ||
state.execTimes.sharedAfterLoad += 1; | ||
} | ||
|
||
// Case 1: without afterload callback | ||
store( { | ||
state: { status1: 'ready' }, | ||
} ); | ||
|
||
// Case 2: non-shared afterload callback | ||
store( { | ||
state: { | ||
status2: 'ready', | ||
allStoresReady: false, | ||
vdomReady: false, | ||
execTimes: { afterLoad: 0 }, | ||
}, | ||
}, { afterLoad } ); | ||
|
||
// Case 3: shared afterload callback | ||
store( { | ||
state: { | ||
status3: 'ready', | ||
execTimes: { sharedAfterLoad: 0 }, | ||
}, | ||
}, { afterLoad: sharedAfterLoad } ); | ||
store( { | ||
state: { | ||
status4: 'ready', | ||
execTimes: { sharedAfterLoad: 0 }, | ||
}, | ||
}, { afterLoad: sharedAfterLoad } ); | ||
} )( 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
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,40 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import { test, expect } from './fixtures'; | ||
|
||
test.describe( 'store afterLoad callbacks', () => { | ||
test.beforeAll( async ( { interactivityUtils: utils } ) => { | ||
await utils.activatePlugins(); | ||
await utils.addPostWithBlock( 'test/store-afterload' ); | ||
} ); | ||
|
||
test.beforeEach( async ( { interactivityUtils: utils, page } ) => { | ||
await page.goto( utils.getLink( 'test/store-afterload' ) ); | ||
} ); | ||
|
||
test.afterAll( async ( { interactivityUtils: utils } ) => { | ||
await utils.deactivatePlugins(); | ||
await utils.deleteAllPosts(); | ||
} ); | ||
|
||
test( 'run after the vdom and store are ready', async ( { page } ) => { | ||
const allStoresReady = page.getByTestId( 'all-stores-ready' ); | ||
const vdomReady = page.getByTestId( 'vdom-ready' ); | ||
|
||
await expect( allStoresReady ).toHaveText( 'true' ); | ||
await expect( vdomReady ).toHaveText( 'true' ); | ||
} ); | ||
|
||
test( 'run once even if shared between several store calls', async ( { | ||
page, | ||
} ) => { | ||
const afterLoadTimes = page.getByTestId( 'after-load-exec-times' ); | ||
const sharedAfterLoadTimes = page.getByTestId( | ||
'shared-after-load-exec-times' | ||
); | ||
|
||
await expect( afterLoadTimes ).toHaveText( '1' ); | ||
await expect( sharedAfterLoadTimes ).toHaveText( '1' ); | ||
} ); | ||
} ); |