diff --git a/.wp-env.json b/.wp-env.json index 093a5fb9..699c99cd 100644 --- a/.wp-env.json +++ b/.wp-env.json @@ -1,5 +1,9 @@ { - "plugins": [".", "./tests/cypress/test-plugin"], + "plugins": [ + ".", + "./tests/cypress/test-plugin/e2e-test-plugin.php", + "./tests/cypress/test-plugin/e2e-test-plugin-optimizer.php" + ], "env": { "tests": { "mappings": { diff --git a/assets/js/admin/admin.js b/assets/js/admin/admin.js index 8d05eef6..d6a29e51 100644 --- a/assets/js/admin/admin.js +++ b/assets/js/admin/admin.js @@ -246,6 +246,10 @@ import {select, subscribe} from '@wordpress/data'; subscribe(() => { if (editorStore.isSavingPost()) { const changes = editorStore.getPostEdits(); + // make sure we have blocks to process + if (!Array.isArray(changes.blocks) || !changes.blocks.length) { + return; + } for (const changedBlock of changes.blocks) { const blockName = changedBlock?.name ?? ''; const innerBlocks = changedBlock?.innerBlocks ?? []; diff --git a/tests/cypress/cypress.config.js b/tests/cypress/cypress.config.js index bb87f0e1..fc61a5be 100644 --- a/tests/cypress/cypress.config.js +++ b/tests/cypress/cypress.config.js @@ -1,6 +1,7 @@ const { defineConfig } = require('cypress') module.exports = defineConfig({ + chromeWebSecurity: false, fixturesFolder: 'tests/cypress/fixtures', screenshotsFolder: 'tests/cypress/screenshots', videosFolder: 'tests/cypress/videos', diff --git a/tests/cypress/e2e/safe-svg.cy.js b/tests/cypress/e2e/safe-svg.cy.js index 30f0c561..7f95718f 100644 --- a/tests/cypress/e2e/safe-svg.cy.js +++ b/tests/cypress/e2e/safe-svg.cy.js @@ -72,4 +72,13 @@ describe('Safe SVG Tests', () => { cy.get('.media-item .error-div.error').should('exist').contains('has failed to upload'); }); + + + // Test plugin doesn't break the block editor when no blocks are added + it('Plugin should not break the block editor when optimizer enabled', () => { + // Activate Test Plugin + cy.deactivatePlugin('safe-svg-cypress-test-plugin'); + cy.activatePlugin('safe-svg-cypress-optimizer-test-plugin'); + cy.createPost('Hello World'); + }); }); diff --git a/tests/cypress/test-plugin/e2e-test-plugin-optimizer.php b/tests/cypress/test-plugin/e2e-test-plugin-optimizer.php new file mode 100644 index 00000000..7dac07e7 --- /dev/null +++ b/tests/cypress/test-plugin/e2e-test-plugin-optimizer.php @@ -0,0 +1,12 @@ +