diff --git a/test/e2e/specs/editor/blocks/image.spec.js b/test/e2e/specs/editor/blocks/image.spec.js index ad07a9b3914fd..3905fc1216286 100644 --- a/test/e2e/specs/editor/blocks/image.spec.js +++ b/test/e2e/specs/editor/blocks/image.spec.js @@ -32,7 +32,10 @@ test.describe( 'Image', () => { await requestUtils.deleteAllMedia(); } ); - test( 'can be inserted', async ( { editor, imageBlockUtils } ) => { + test( 'can be inserted via image upload', async ( { + editor, + imageBlockUtils, + } ) => { await editor.insertBlock( { name: 'core/image' } ); const imageBlock = editor.canvas.locator( @@ -704,7 +707,7 @@ test.describe( 'Image', () => { ); } ); - test( 'should appear in the frontend published post content', async ( { + test( 'image inserted via upload should appear in the frontend published post content', async ( { editor, imageBlockUtils, page, @@ -739,11 +742,92 @@ test.describe( 'Image', () => { new RegExp( filename ) ); } ); + + test( 'image inserted via link should appear in the frontend published post content', async ( { + editor, + page, + } ) => { + await editor.insertBlock( { name: 'core/image' } ); + const imageBlock = editor.canvas.locator( + 'role=document[name="Block: Image"i]' + ); + await expect( imageBlock ).toBeVisible(); + + await imageBlock + .getByRole( 'button' ) + .filter( { hasText: 'Insert from URL' } ) + .click(); + + // This form lacks distinguishing qualities other than the + // class name, so we use page.locator() instead of page.getByRole() + const form = page.locator( + 'form.block-editor-media-placeholder__url-input-form' + ); + + const imgUrl = + 'https://wp20.wordpress.net/wp-content/themes/twentyseventeen-wp20/images/wp20-logo-white.svg'; + + await form.getByLabel( 'URL' ).fill( imgUrl ); + await form.getByRole( 'button', { name: 'Apply' } ).click(); + + const imageInEditor = imageBlock.locator( 'role=img' ); + await expect( imageInEditor ).toBeVisible(); + await expect( imageInEditor ).toHaveAttribute( 'src', imgUrl ); + + const postId = await editor.publishPost(); + await page.goto( `/?p=${ postId }` ); + + const figureDom = page.getByRole( 'figure' ); + await expect( figureDom ).toBeVisible(); + + const imageDom = figureDom.locator( 'img' ); + await expect( imageDom ).toBeVisible(); + await expect( imageDom ).toHaveAttribute( 'src', imgUrl ); + } ); + + test( 'adding a link should reflect configuration in published post content', async ( { + editor, + page, + imageBlockUtils, + } ) => { + await editor.insertBlock( { name: 'core/image' } ); + const imageBlock = editor.canvas.locator( + 'role=document[name="Block: Image"i]' + ); + await expect( imageBlock ).toBeVisible(); + + await imageBlockUtils.upload( + imageBlock.locator( 'data-testid=form-file-upload-input' ) + ); + + await page + .getByLabel( 'Block tools' ) + .getByLabel( 'Insert link' ) + .click(); + + // This form lacks distinguishing qualities other than the + // class name, so we use page.locator() instead of page.getByRole() + const form = page.locator( '.block-editor-url-popover__link-editor' ); + + const url = 'https://wordpress.org'; + + await form.getByLabel( 'URL' ).fill( url ); + + await form.getByRole( 'button', { name: 'Apply' } ).click(); + + const postId = await editor.publishPost(); + await page.goto( `/?p=${ postId }` ); + + const figureDom = page.getByRole( 'figure' ); + await expect( figureDom ).toBeVisible(); + + const linkDom = figureDom.locator( 'a' ); + await expect( linkDom ).toBeVisible(); + await expect( linkDom ).toHaveAttribute( 'href', url ); + } ); } ); test.describe( 'Image - interactivity', () => { - let filename = null; - test.beforeAll( async ( { requestUtils } ) => { await requestUtils.deleteAllMedia(); } ); @@ -752,7 +836,7 @@ test.describe( 'Image - interactivity', () => { await requestUtils.deleteAllMedia(); } ); - test.beforeEach( async ( { admin, page, editor, imageBlockUtils } ) => { + test.beforeEach( async ( { admin, page, editor } ) => { await admin.visitAdminPage( '/admin.php', 'page=gutenberg-experiments' @@ -767,20 +851,6 @@ test.describe( 'Image - interactivity', () => { await admin.createNewPost(); await editor.insertBlock( { name: 'core/image' } ); - - const imageBlock = editor.canvas.locator( - 'role=document[name="Block: Image"i]' - ); - await expect( imageBlock ).toBeVisible(); - - filename = await imageBlockUtils.upload( - imageBlock.locator( 'data-testid=form-file-upload-input' ) - ); - const image = imageBlock.locator( 'role=img' ); - await expect( image ).toBeVisible(); - await expect( image ).toHaveAttribute( 'src', new RegExp( filename ) ); - - await editor.openDocumentSettingsSidebar(); } ); test.afterEach( async ( { requestUtils, admin, page } ) => { @@ -800,142 +870,251 @@ test.describe( 'Image - interactivity', () => { await page.waitForLoadState(); } ); - test( 'should toggle "lightbox" in saved attributes', async ( { - editor, - page, - } ) => { - await page.getByRole( 'button', { name: 'Advanced' } ).click(); - await page - .getByRole( 'combobox', { name: 'Behaviors' } ) - .selectOption( 'lightbox' ); + test.describe( 'tests using uploaded image', () => { + let filename = null; - let blocks = await editor.getBlocks(); - expect( blocks[ 0 ].attributes ).toMatchObject( { - behaviors: { - lightbox: { - animation: 'zoom', - enabled: true, - }, - }, - linkDestination: 'none', + test.beforeEach( async ( { editor, imageBlockUtils } ) => { + const imageBlock = editor.canvas.locator( + 'role=document[name="Block: Image"i]' + ); + await expect( imageBlock ).toBeVisible(); + + filename = await imageBlockUtils.upload( + imageBlock.locator( 'data-testid=form-file-upload-input' ) + ); + const image = imageBlock.locator( 'role=img' ); + await expect( image ).toBeVisible(); + await expect( image ).toHaveAttribute( + 'src', + new RegExp( filename ) + ); + + await editor.openDocumentSettingsSidebar(); } ); - expect( blocks[ 0 ].attributes.url ).toContain( filename ); - - await page.getByLabel( 'Behaviors' ).selectOption( '' ); - blocks = await editor.getBlocks(); - expect( blocks[ 0 ].attributes ).toMatchObject( { - behaviors: { - lightbox: { - animation: '', - enabled: false, + + test( 'should toggle "lightbox" in saved attributes', async ( { + editor, + page, + } ) => { + await page.getByRole( 'button', { name: 'Advanced' } ).click(); + await page + .getByRole( 'combobox', { name: 'Behaviors' } ) + .selectOption( 'lightbox' ); + + let blocks = await editor.getBlocks(); + expect( blocks[ 0 ].attributes ).toMatchObject( { + behaviors: { + lightbox: { + animation: 'zoom', + enabled: true, + }, }, - }, - linkDestination: 'none', + linkDestination: 'none', + } ); + expect( blocks[ 0 ].attributes.url ).toContain( filename ); + + await page.getByLabel( 'Behaviors' ).selectOption( '' ); + blocks = await editor.getBlocks(); + expect( blocks[ 0 ].attributes ).toMatchObject( { + behaviors: { + lightbox: { + animation: '', + enabled: false, + }, + }, + linkDestination: 'none', + } ); + expect( blocks[ 0 ].attributes.url ).toContain( filename ); } ); - expect( blocks[ 0 ].attributes.url ).toContain( filename ); - } ); - test( 'should open and close the image in a lightbox using the mouse', async ( { - editor, - page, - } ) => { - await page.getByRole( 'button', { name: 'Advanced' } ).click(); - await page - .getByRole( 'combobox', { name: 'Behaviors' } ) - .selectOption( 'lightbox' ); + test( 'should open and close the image in a lightbox when using a mouse and dynamically load src', async ( { + editor, + page, + } ) => { + await page.getByRole( 'button', { name: 'Advanced' } ).click(); + await page + .getByRole( 'combobox', { name: 'Behaviors' } ) + .selectOption( 'lightbox' ); - const postId = await editor.publishPost(); - await page.goto( `/?p=${ postId }` ); + const postId = await editor.publishPost(); + await page.goto( `/?p=${ postId }` ); - const lightbox = page.locator( '.wp-lightbox-overlay' ); - await expect( lightbox ).toBeHidden(); + const lightbox = page.locator( '.wp-lightbox-overlay' ); + await expect( lightbox ).toBeHidden(); + const image = lightbox.locator( 'img' ); - await page.getByRole( 'button', { name: 'Enlarge image' } ).click(); + await expect( image ).toHaveAttribute( 'src', '' ); - const image = lightbox.locator( 'img' ); - await expect( image ).toHaveAttribute( 'src', new RegExp( filename ) ); + await page.getByRole( 'button', { name: 'Enlarge image' } ).click(); - await expect( lightbox ).toBeVisible(); + await expect( image ).toHaveAttribute( + 'src', + new RegExp( filename ) + ); - const closeButton = lightbox.getByRole( 'button', { - name: 'Close', - } ); - await closeButton.click(); + await expect( lightbox ).toBeVisible(); - await expect( lightbox ).toBeHidden(); - } ); + const closeButton = lightbox.getByRole( 'button', { + name: 'Close', + } ); + await closeButton.click(); - test.describe( 'keyboard navigation', () => { - let openLightboxButton; - let lightbox; - let closeButton; + await expect( lightbox ).toBeHidden(); + } ); - test.beforeEach( async ( { page, editor } ) => { + test( 'lightbox should be overriden when link is configured for image', async ( { + editor, + page, + } ) => { await page.getByRole( 'button', { name: 'Advanced' } ).click(); + const behaviorSelect = page.getByRole( 'combobox', { + name: 'Behaviors', + } ); + await behaviorSelect.selectOption( 'lightbox' ); + await page - .getByRole( 'combobox', { name: 'Behaviors' } ) - .selectOption( 'lightbox' ); + .getByLabel( 'Block tools' ) + .getByLabel( 'Insert link' ) + .click(); + + const form = page.locator( + '.block-editor-url-popover__link-editor' + ); + + const url = 'https://wordpress.org'; + + await form.getByLabel( 'URL' ).fill( url ); + + await form.getByRole( 'button', { name: 'Apply' } ).click(); + await expect( behaviorSelect ).toBeDisabled(); const postId = await editor.publishPost(); await page.goto( `/?p=${ postId }` ); - openLightboxButton = page.getByRole( 'button', { - name: 'Enlarge image', + // The lightbox markup should not appear in the DOM at all + await expect( + page.getByRole( 'button', { name: 'Enlarge image' } ) + ).not.toBeInViewport(); + } ); + + test.describe( 'keyboard navigation', () => { + let openLightboxButton; + let lightbox; + let closeButton; + + test.beforeEach( async ( { page, editor } ) => { + await page.getByRole( 'button', { name: 'Advanced' } ).click(); + await page + .getByRole( 'combobox', { name: 'Behaviors' } ) + .selectOption( 'lightbox' ); + + const postId = await editor.publishPost(); + await page.goto( `/?p=${ postId }` ); + + openLightboxButton = page.getByRole( 'button', { + name: 'Enlarge image', + } ); + lightbox = page.getByRole( 'dialog' ); + closeButton = lightbox.getByRole( 'button', { + name: 'Close', + } ); } ); - lightbox = page.getByRole( 'dialog' ); - closeButton = lightbox.getByRole( 'button', { - name: 'Close', + + test( 'should open and focus appropriately using enter key', async ( { + page, + } ) => { + // Open and close lightbox using the close button + await openLightboxButton.focus(); + await page.keyboard.press( 'Enter' ); + await expect( lightbox ).toBeVisible(); + await expect( closeButton ).toBeFocused(); } ); - } ); - test( 'should open and focus appropriately using enter key', async ( { - page, - } ) => { - // Open and close lightbox using the close button - await openLightboxButton.focus(); - await page.keyboard.press( 'Enter' ); - await expect( lightbox ).toBeVisible(); - await expect( closeButton ).toBeFocused(); - } ); + test( 'should close and focus appropriately using enter key on close button', async ( { + page, + } ) => { + // Open and close lightbox using the close button + await openLightboxButton.focus(); + await page.keyboard.press( 'Enter' ); + await expect( lightbox ).toBeVisible(); + await expect( closeButton ).toBeFocused(); + await page.keyboard.press( 'Enter' ); + await expect( lightbox ).toBeHidden(); + await expect( openLightboxButton ).toBeFocused(); + } ); - test( 'should close and focus appropriately using enter key on close button', async ( { - page, - } ) => { - // Open and close lightbox using the close button - await openLightboxButton.focus(); - await page.keyboard.press( 'Enter' ); - await expect( lightbox ).toBeVisible(); - await expect( closeButton ).toBeFocused(); - await page.keyboard.press( 'Enter' ); - await expect( lightbox ).toBeHidden(); - await expect( openLightboxButton ).toBeFocused(); - } ); + test( 'should close and focus appropriately using escape key', async ( { + page, + } ) => { + await openLightboxButton.focus(); + await page.keyboard.press( 'Enter' ); + await expect( lightbox ).toBeVisible(); + await expect( closeButton ).toBeFocused(); + await page.keyboard.press( 'Escape' ); + await expect( lightbox ).toBeHidden(); + await expect( openLightboxButton ).toBeFocused(); + } ); - test( 'should close and focus appropriately using escape key', async ( { - page, - } ) => { - await openLightboxButton.focus(); - await page.keyboard.press( 'Enter' ); - await expect( lightbox ).toBeVisible(); - await expect( closeButton ).toBeFocused(); - await page.keyboard.press( 'Escape' ); - await expect( lightbox ).toBeHidden(); - await expect( openLightboxButton ).toBeFocused(); + // TO DO: Add these tests, which will involve adding a caption + // to uploaded test images + // test( 'should trap focus appropriately when using tab', async ( { + // page, + // } ) => { + + // } ); + + // test( 'should trap focus appropriately using shift+tab', async ( { + // page, + // } ) => { + + // } ); } ); + } ); + + test( 'lightbox should work as expected when inserting image from URL', async ( { + editor, + page, + } ) => { + const imageBlockFromUrl = editor.canvas.locator( + 'role=document[name="Block: Image"i]' + ); + await expect( imageBlockFromUrl ).toBeVisible(); + + await imageBlockFromUrl + .getByRole( 'button' ) + .filter( { hasText: 'Insert from URL' } ) + .click(); + + const form = page.locator( + '.block-editor-media-placeholder__url-input-form' + ); + + const imgUrl = + 'https://wp20.wordpress.net/wp-content/themes/twentyseventeen-wp20/images/wp20-logo-white.svg'; + + await form.getByLabel( 'URL' ).fill( imgUrl ); - // TO DO: Add these tests, which will involve adding a caption - // to uploaded test images - // test( 'should trap focus appropriately when using tab', async ( { - // page, - // } ) => { + await form.getByRole( 'button', { name: 'Apply' } ).click(); - // } ); + const image = imageBlockFromUrl.locator( 'role=img' ); + await expect( image ).toBeVisible(); + await expect( image ).toHaveAttribute( 'src', imgUrl ); + + await page.getByRole( 'button', { name: 'Advanced' } ).click(); + await page + .getByRole( 'combobox', { name: 'Behaviors' } ) + .selectOption( 'lightbox' ); - // test( 'should trap focus appropriately using shift+tab', async ( { - // page, - // } ) => { + const postId = await editor.publishPost(); + await page.goto( `/?p=${ postId }` ); + + const lightbox = page.locator( '.wp-lightbox-overlay' ); + const imageDom = lightbox.locator( 'img' ); + await expect( imageDom ).toHaveAttribute( 'src', '' ); + + await page.getByRole( 'button', { name: 'Enlarge image' } ).click(); - // } ); + await expect( imageDom ).toHaveAttribute( 'src', imgUrl ); } ); } );