Skip to content
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

Test that the demo page loads without marking the post as dirty #10104

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
63 changes: 29 additions & 34 deletions test/e2e/specs/demo.test.js
@@ -1,42 +1,39 @@
/**
* External dependencies
*/
import fetch from 'node-fetch';

/**
* Internal dependencies
*/
import { visitAdmin } from '../support/utils';

// The response from Vimeo, but with the iframe taken out.
const MOCK_EMBED_RESPONSE = {
type: 'video',
version: '1.0',
provider_name: 'Vimeo',
provider_url: 'https://vimeo.com/',
title: 'The Mountain',
author_name: 'TSO Photography',
author_url: 'https://vimeo.com/terjes',
is_plus: '0',
account_type: 'basic',
html: '<p>Embedded content</p>',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your explanation around embeds @notnownikki 👍 I think I understand now the reason for this PR. There is just one thing I'm missing. Would the test be valid if we hardcoded the Vimeo response without the src as the HTML in this line?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, because it would always pass, even if Vimeo's response changed and caused the post to be dirty in real world use. If Vimeo decide to change their iframe dimensions for some reason, then in actual use the post might end up dirty, but in the test it would pass.

This seems like an unlikely thing to happen, but YouTube did it not too long ago with 16:9 and 4:3 videos getting different aspect ratios, so I don't want to run the risk of us having a test that says "Hey, everything is fine!" and causing us to miss actual errors.

width: 600,
height: 338,
duration: 189,
thumbnail_url: 'https://i.vimeocdn.com/video/145026168_295x166.jpg',
thumbnail_width: 295,
thumbnail_height: 166,
thumbnail_url_with_play_button: 'https://i.vimeocdn.com/filter/overlay?src0=https%3A%2F%2Fi.vimeocdn.com%2Fvideo%2F145026168_295x166.jpg&src1=http%3A%2F%2Ff.vimeocdn.com%2Fp%2Fimages%2Fcrawler_play.png',
upload_date: '2011-04-15 08:35:35',
video_id: 22439234,
uri: '/videos/22439234',
};

describe( 'new editor state', () => {
beforeAll( async () => {
// Intercept embed requests so that scripts loaded from third parties
// cannot leave errors in the console and cause the test to fail.
await page.setRequestInterception( true );
page.on( 'request', ( request ) => {
page.on( 'request', async ( request ) => {
if ( request.url().indexOf( 'oembed/1.0/proxy' ) !== -1 ) {
// Because we can't get the responses to requests and modify them on the fly,
// we have to make our own request, get the response, modify it, then use the
// modified values to respond to the request.
const response = await fetch(
request.url(),
{
headers: request.headers(),
method: request.method(),
body: request.postData(),
}
);
const preview = await response.json();
// Remove the first src attribute. This stops the Vimeo iframe loading the actual
// embedded content, but the height and width are preserved so layout related
// attributes, like aspect ratio CSS classes, remain the same.
preview.html = preview.html.replace( /src=[^\s]+/, '' );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we only apply this replace to vimeo?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's the only call we do to the embed API, and we want to do this to all embeds so we're not loading third party scripts in our tests.

request.respond( {
content: 'application/json',
body: JSON.stringify( MOCK_EMBED_RESPONSE ),
body: JSON.stringify( preview ),
} );
} else {
request.continue();
Expand All @@ -46,13 +43,11 @@ describe( 'new editor state', () => {
await visitAdmin( 'post-new.php', 'gutenberg-demo' );
} );

it( 'should not error', () => {
// This test case is intentionally empty. The `beforeAll` lifecycle of
// navigating to the Demo page is sufficient assertion in itself, as it
// will trigger the global console error capturing if an error occurs
// during this process.
//
// If any other test cases are added which verify expected behavior of
// the demo post, this empty test case can be removed.
it( 'content should load without making the post dirty', async () => {
const isDirty = await page.evaluate( () => {
const { select } = window.wp.data;
return select( 'core/editor' ).isEditedPostDirty();
} );
expect( isDirty ).toBeFalsy();
} );
} );