-
Notifications
You must be signed in to change notification settings - Fork 67
fix: stackable should follow editor breakpoints from theme if valid #3755
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
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,150 @@ | ||||||
| import { test, expect } from 'e2e/test-utils' | ||||||
|
|
||||||
| const TABLET_VIEWPORT = '1000px' | ||||||
| const MOBILE_VIEWPORT = '690px' | ||||||
|
|
||||||
| const getUserGlobalStylesId = async requestUtils => { | ||||||
| const themes = await requestUtils.rest( { path: '/wp/v2/themes?status=active' } ) | ||||||
| const href = themes?.[ 0 ]?._links?.[ 'wp:user-global-styles' ]?.[ 0 ]?.href | ||||||
| if ( ! href ) { | ||||||
| return null | ||||||
| } | ||||||
| return String( href ).split( '/' ).pop() | ||||||
| } | ||||||
|
|
||||||
| const setUserViewportSettings = async ( requestUtils, viewport ) => { | ||||||
| const id = await getUserGlobalStylesId( requestUtils ) | ||||||
| if ( ! id ) { | ||||||
| return null | ||||||
| } | ||||||
|
|
||||||
| const current = await requestUtils.rest( { path: `/wp/v2/global-styles/${ id }` } ) | ||||||
| await requestUtils.rest( { | ||||||
| method: 'POST', | ||||||
| path: `/wp/v2/global-styles/${ id }`, | ||||||
| data: { | ||||||
| settings: { | ||||||
| ...( current.settings || {} ), | ||||||
| viewport, | ||||||
| }, | ||||||
| }, | ||||||
| } ) | ||||||
| return id | ||||||
| } | ||||||
|
|
||||||
| const setEditorDeviceType = async ( page, deviceType ) => { | ||||||
| await page.evaluate( device => { | ||||||
| const dispatch = window.wp.data.dispatch | ||||||
| if ( dispatch( 'core/editor' )?.setDeviceType ) { | ||||||
| dispatch( 'core/editor' ).setDeviceType( device ) | ||||||
| return | ||||||
| } | ||||||
| if ( dispatch( 'core/edit-post' )?.__experimentalSetPreviewDeviceType ) { | ||||||
| dispatch( 'core/edit-post' ).__experimentalSetPreviewDeviceType( device ) | ||||||
| } | ||||||
| }, deviceType ) | ||||||
| } | ||||||
|
|
||||||
| test.describe( 'Editor theme viewports', () => { | ||||||
| let pid = null | ||||||
| let stylesId = null | ||||||
| let previousSettings = null | ||||||
|
|
||||||
| test.beforeEach( async ( { editor, admin, requestUtils } ) => { | ||||||
| const wpVersion = process.env.WP_VERSION || 'latest' | ||||||
| test.skip( wpVersion !== 'latest' && wpVersion < '7.1', 'settings.viewport requires WordPress 7.1.' ) | ||||||
|
|
||||||
| const id = await getUserGlobalStylesId( requestUtils ) | ||||||
| test.skip( ! id, 'User global styles REST is unavailable.' ) | ||||||
|
|
||||||
| stylesId = id | ||||||
| previousSettings = ( await requestUtils.rest( { | ||||||
| path: `/wp/v2/global-styles/${ id }`, | ||||||
| } ) ).settings || {} | ||||||
|
|
||||||
| const saved = await setUserViewportSettings( requestUtils, { | ||||||
| tablet: TABLET_VIEWPORT, | ||||||
| mobile: MOBILE_VIEWPORT, | ||||||
| } ) | ||||||
| test.skip( ! saved, 'Could not write settings.viewport via Global Styles REST.' ) | ||||||
|
|
||||||
| const after = await requestUtils.rest( { path: `/wp/v2/global-styles/${ id }` } ) | ||||||
| test.skip( | ||||||
| after?.settings?.viewport?.tablet !== TABLET_VIEWPORT || | ||||||
| after?.settings?.viewport?.mobile !== MOBILE_VIEWPORT, | ||||||
| 'Global Styles REST did not persist custom viewport settings.' | ||||||
| ) | ||||||
|
|
||||||
| await admin.createNewPost( { title: 'Editor theme viewports' } ) | ||||||
| await editor.saveDraft() | ||||||
| pid = new URLSearchParams( new URL( editor.page.url() ).search ).get( 'post' ) | ||||||
| } ) | ||||||
|
|
||||||
| test.afterEach( async ( { requestUtils } ) => { | ||||||
| if ( stylesId ) { | ||||||
| await requestUtils.rest( { | ||||||
| method: 'POST', | ||||||
| path: `/wp/v2/global-styles/${ stylesId }`, | ||||||
| data: { settings: previousSettings || {} }, | ||||||
| } ).catch( () => undefined ) | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🩺 Stability & Availability | 🟠 Major | ⚡ Quick win Fail teardown after Global Styles cleanup.
Proposed fix test.afterEach( async ( { requestUtils } ) => {
- if ( stylesId ) {
- await requestUtils.rest( {
- method: 'POST',
- path: `/wp/v2/global-styles/${ stylesId }`,
- data: { settings: previousSettings || {} },
- } ).catch( () => undefined )
+ let restoreError: unknown = null
+ try {
+ if ( stylesId ) {
+ await requestUtils.rest( {
+ method: 'POST',
+ path: `/wp/v2/global-styles/${ stylesId }`,
+ data: { settings: previousSettings || {} },
+ } )
+ }
+ } catch ( error ) {
+ restoreError = error
}
- if ( pid ) {
- await requestUtils.deletePost( pid )
+ try {
+ if ( pid ) {
+ await requestUtils.deletePost( pid )
+ }
+ } finally {
+ if ( restoreError !== null ) {
+ throw restoreError
+ }
}
} )📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||
| } | ||||||
| if ( pid ) { | ||||||
| await requestUtils.deletePost( pid ) | ||||||
| } | ||||||
| } ) | ||||||
|
|
||||||
| test( 'tablet preview uses theme viewport settings for Stackable styles', async ( { | ||||||
| page, | ||||||
| editor, | ||||||
| } ) => { | ||||||
| await editor.insertBlock( { | ||||||
| name: 'stackable/text', | ||||||
| attributes: { | ||||||
| text: 'theme viewport preview', | ||||||
| fontSize: '16', | ||||||
| fontSizeTablet: '48', | ||||||
| }, | ||||||
| } ) | ||||||
|
|
||||||
| const text = editor.canvas.locator( '[data-type="stackable/text"] p' ).first() | ||||||
| await expect( text ).toBeVisible() | ||||||
|
|
||||||
| await setEditorDeviceType( page, 'Tablet' ) | ||||||
|
|
||||||
| await expect.poll( async () => { | ||||||
| return page.evaluate( () => { | ||||||
| return window.wp.data.select( 'core/editor' )?.getDeviceType?.() || | ||||||
| window.wp.data.select( 'core/edit-post' )?.__experimentalGetPreviewDeviceType?.() || | ||||||
| '' | ||||||
| } ) | ||||||
| } ).toBe( 'Tablet' ) | ||||||
|
|
||||||
| const preview = await page.evaluate( () => { | ||||||
| const editorSettings = window.wp.data.select( 'core/editor' )?.getEditorSettings?.() || {} | ||||||
| const blockSettings = window.wp.data.select( 'core/block-editor' )?.getSettings?.() || {} | ||||||
| const canvas = document.querySelector( 'iframe[name="editor-canvas"], iframe.edit-post-visual-editor__content-area' ) | ||||||
| const canvasWidth = canvas ? Math.round( canvas.getBoundingClientRect().width ) : null | ||||||
| const features = blockSettings.__experimentalFeatures || editorSettings.__experimentalFeatures || {} | ||||||
| return { | ||||||
| stackableViewports: window.stackable?.settings?.stackable_editor_viewport_breakpoints || null, | ||||||
| featuresViewport: features.viewport || null, | ||||||
| canvasWidth, | ||||||
| } | ||||||
| } ) | ||||||
|
|
||||||
| expect( | ||||||
| preview.featuresViewport?.tablet === TABLET_VIEWPORT || | ||||||
| preview.stackableViewports?.tablet === TABLET_VIEWPORT, | ||||||
| `Editor did not see custom viewports: ${ JSON.stringify( preview ) }` | ||||||
| ).toBeTruthy() | ||||||
|
|
||||||
| // Custom tablet is 1000px. The editor chrome can clamp the canvas below | ||||||
| // that, but it must stay wider than Stackable's old 781px query. | ||||||
| expect( | ||||||
| preview.canvasWidth, | ||||||
| `Tablet canvas should be wider than 781px so the pre-fix query misses. Got ${ JSON.stringify( preview ) }` | ||||||
| ).toBeGreaterThan( 781 ) | ||||||
|
|
||||||
| await expect( text ).toHaveCSS( 'font-size', '48px' ) | ||||||
| } ) | ||||||
| } ) | ||||||
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.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
Repository: gambitph/Stackable
Length of output: 50375
🤖 get_repo_knowledge executed:
get_repo_knowledge gambitph/Stackable /tmp/coderabbit-repo-knowledge/gambitph-stackable-99cf3cd2/conventionsLength of output: 2997
🏁 Script executed:
Repository: gambitph/Stackable
Length of output: 2939
🏁 Script executed:
Repository: gambitph/Stackable
Length of output: 2616
🏁 Script executed:
Repository: gambitph/Stackable
Length of output: 2039
Compare WordPress versions by numeric components.
WP_VERSIONis a string. JavaScript compares strings lexicographically, so10.0is treated as older than7.1and the test is skipped incorrectly. Parse the major and minor components before callingtest.skip.Proposed fix
📝 Committable suggestion
🤖 Prompt for AI Agents