Skip to content

Commit

Permalink
Add test for dragging-and-dropping html
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin940726 committed Apr 21, 2023
1 parent 0c8c12b commit 6308224
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions test/e2e/specs/editor/blocks/image.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,66 @@ test.describe( 'Image', () => {
).toBe( new URL( page.url() ).host );
}, 'should replace the original image with the second image' ).toPass();
} );

test( 'should allow dragging and dropping HTML to media placeholder', async ( {
page,
editor,
} ) => {
await editor.insertBlock( { name: 'core/image' } );
const imageBlock = page.getByRole( 'document', {
name: 'Block: Image',
} );

const html = `
<figure>
<img src="https://live.staticflickr.com/3894/14962688165_04759a8b03_b.jpg" alt="Cat">
<figcaption>"Cat" by tomhouslay is licensed under <a href="https://creativecommons.org/licenses/by-nc/2.0/?ref=openverse">CC BY-NC 2.0</a>.</figcaption>
</figure>
`;

await page.evaluate( ( _html ) => {
const dummy = document.createElement( 'div' );
dummy.style.width = '10px';
dummy.style.height = '10px';
dummy.style.zIndex = 99999;
dummy.style.position = 'fixed';
dummy.style.top = 0;
dummy.style.left = 0;
dummy.draggable = 'true';
dummy.addEventListener( 'dragstart', ( event ) => {
event.dataTransfer.setData( 'text/html', _html );
setTimeout( () => {
dummy.remove();
}, 0 );
} );
document.body.appendChild( dummy );
}, html );

await page.mouse.move( 0, 0 );
await page.mouse.down();
await imageBlock.hover();
await page.mouse.up();

const host = new URL( page.url() ).host;

await expect.poll( editor.getBlocks ).toMatchObject( [
{
name: 'core/image',
attributes: {
link: expect.stringContaining( host ),
url: expect.stringContaining( host ),
id: expect.any( Number ),
alt: 'Cat',
caption: `"Cat" by tomhouslay is licensed under <a href="https://creativecommons.org/licenses/by-nc/2.0/?ref=openverse">CC BY-NC 2.0</a>.`,
},
},
] );
const url = ( await editor.getBlocks() )[ 0 ].attributes.url;
await expect( imageBlock.getByRole( 'img' ) ).toHaveAttribute(
'src',
url
);
} );
} );

class ImageBlockUtils {
Expand Down

0 comments on commit 6308224

Please sign in to comment.