diff --git a/test/e2e/specs/block-icons.test.js b/test/e2e/specs/block-icons.test.js new file mode 100644 index 00000000000000..bd9f78c845a6de --- /dev/null +++ b/test/e2e/specs/block-icons.test.js @@ -0,0 +1,148 @@ +/** + * Internal dependencies + */ +import '../support/bootstrap'; +import { + newPost, + newDesktopBrowserPage, + insertBlock, + searchForBlock, +} from '../support/utils'; +import { activatePlugin, deactivatePlugin } from '../support/plugins'; + +const INSERTER_BUTTON_SELECTOR = '.components-popover__content .editor-block-types-list__item'; +const INSERTER_ICON_SELECTOR = `${ INSERTER_BUTTON_SELECTOR } .editor-block-types-list__item-icon`; +const INSPECTOR_ICON_SELECTOR = '.edit-post-sidebar .editor-block-icon__colors'; + +async function getInnerHTML( selector ) { + return await page.$eval( selector, ( element ) => element.innerHTML ); +} + +async function getBackgroundColor( selector ) { + return await page.$eval( selector, ( element ) => { + return window.getComputedStyle( element ).backgroundColor; + } ); +} + +async function getColor( selector ) { + return await page.$eval( selector, ( element ) => { + return window.getComputedStyle( element ).color; + } ); +} + +async function getFirstInserterIcon() { + return await getInnerHTML( INSERTER_ICON_SELECTOR ); +} + +describe( 'Correctly Renders Block Icons on Inserter and Inspector', () => { + const dashIconRegex = /.*<\/svg>/; + const circleString = ''; + const svgIcon = `${ circleString }`; + + const validateSvgIcon = ( iconHtml ) => { + expect( iconHtml ).toMatch( svgIcon ); + }; + + const validateDashIcon = ( iconHtml ) => { + expect( iconHtml ).toMatch( dashIconRegex ); + }; + + beforeAll( async () => { + await newDesktopBrowserPage(); + await activatePlugin( 'gutenberg-test-block-icons' ); + // accept the prompt if the post is "dirty" + await page.on( 'dialog', async ( dialog ) => { + if ( dialog ) { + await dialog.accept(); + } + } ); + } ); + + beforeEach( async () => { + await newPost(); + } ); + + afterAll( async () => { + await deactivatePlugin( 'gutenberg-test-block-icons' ); + } ); + + function testIconsOfBlock( blockName, blockTitle, validateIcon ) { + it( 'Renders correctly the icon in the inserter', async () => { + await searchForBlock( blockTitle ); + validateIcon( await getFirstInserterIcon() ); + } ); + + it( 'Can insert the block', async () => { + await insertBlock( blockTitle ); + expect( + await getInnerHTML( `[data-type="${ blockName }"] [data-type="core/paragraph"] p` ) + ).toEqual( blockTitle ); + } ); + + it( 'Renders correctly the icon on the inspector', async () => { + await insertBlock( blockTitle ); + await page.focus( `[data-type="${ blockName }"]` ); + validateIcon( await getInnerHTML( INSPECTOR_ICON_SELECTOR ) ); + } ); + } + + describe( 'Block with svg icon', () => { + const blockName = 'test/test-single-svg-icon'; + const blockTitle = 'TestSimpleSvgIcon'; + testIconsOfBlock( blockName, blockTitle, validateSvgIcon ); + } ); + + describe( 'Block with dash icon', () => { + const blockName = 'test/test-dash-icon'; + const blockTitle = 'TestDashIcon'; + testIconsOfBlock( blockName, blockTitle, validateDashIcon ); + } ); + + describe( 'Block with function icon', () => { + const blockName = 'test/test-function-icon'; + const blockTitle = 'TestFunctionIcon'; + testIconsOfBlock( blockName, blockTitle, validateSvgIcon ); + } ); + + describe( 'Block with dash icon and background and foreground colors', () => { + const blockName = 'test/test-dash-icon-colors'; + const blockTitle = 'TestDashIconColors'; + it( 'Renders the icon in the inserter with the correct colors', async () => { + await searchForBlock( blockTitle ); + validateDashIcon( await getFirstInserterIcon() ); + expect( await getBackgroundColor( INSERTER_ICON_SELECTOR ) ).toEqual( 'rgb(1, 0, 0)' ); + expect( await getColor( INSERTER_ICON_SELECTOR ) ).toEqual( 'rgb(254, 0, 0)' ); + } ); + + it( 'Renders the icon in the inspector with the correct colors', async () => { + await insertBlock( blockTitle ); + await page.focus( `[data-type="${ blockName }"]` ); + validateDashIcon( + await getInnerHTML( INSPECTOR_ICON_SELECTOR ) + ); + expect( await getBackgroundColor( INSPECTOR_ICON_SELECTOR ) ).toEqual( 'rgb(1, 0, 0)' ); + expect( await getColor( INSPECTOR_ICON_SELECTOR ) ).toEqual( 'rgb(254, 0, 0)' ); + } ); + } ); + + describe( 'Block with svg icon and background color', () => { + const blockName = 'test/test-svg-icon-background'; + const blockTitle = 'TestSvgIconBackground'; + it( 'Renders the icon in the inserter with the correct background color and an automatically compute readable foreground color', async () => { + await searchForBlock( blockTitle ); + validateSvgIcon( await getFirstInserterIcon() ); + expect( await getBackgroundColor( INSERTER_ICON_SELECTOR ) ).toEqual( 'rgb(1, 0, 0)' ); + expect( await getColor( INSERTER_ICON_SELECTOR ) ).toEqual( 'rgb(248, 249, 249)' ); + } ); + + it( 'Renders correctly the icon on the inspector', async () => { + await insertBlock( blockTitle ); + await page.focus( `[data-type="${ blockName }"]` ); + validateSvgIcon( + await getInnerHTML( INSPECTOR_ICON_SELECTOR ) + ); + expect( await getBackgroundColor( INSPECTOR_ICON_SELECTOR ) ).toEqual( 'rgb(1, 0, 0)' ); + expect( await getColor( INSPECTOR_ICON_SELECTOR ) ).toEqual( 'rgb(248, 249, 249)' ); + } ); + } ); +} ); diff --git a/test/e2e/support/utils.js b/test/e2e/support/utils.js index eb347ef3d67e54..1dc474a46704e4 100644 --- a/test/e2e/support/utils.js +++ b/test/e2e/support/utils.js @@ -174,18 +174,26 @@ export async function ensureSidebarOpened() { } /** - * Opens the inserter, searches for the given term, then selects the first - * result that appears. + * Search for block in the global inserter * * @param {string} searchTerm The text to search the inserter for. */ -export async function insertBlock( searchTerm ) { +export async function searchForBlock( searchTerm ) { await page.click( '.edit-post-header [aria-label="Add block"]' ); // Waiting here is necessary because sometimes the inserter takes more time to // render than Puppeteer takes to complete the 'click' action await page.waitForSelector( '.editor-inserter__menu' ); - // Filter and reveal buttons. await page.keyboard.type( searchTerm ); +} + +/** + * Opens the inserter, searches for the given term, then selects the first + * result that appears. + * + * @param {string} searchTerm The text to search the inserter for. + */ +export async function insertBlock( searchTerm ) { + await searchForBlock( searchTerm ); await page.click( `button[aria-label="${ searchTerm }"]` ); } diff --git a/test/e2e/test-plugins/block-icons.php b/test/e2e/test-plugins/block-icons.php new file mode 100644 index 00000000000000..c89997d093ae11 --- /dev/null +++ b/test/e2e/test-plugins/block-icons.php @@ -0,0 +1,22 @@ +