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

Embed block integration tests part 2 #35533

Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
ae77e2e
added test for Embed block caption.
jd-alexander Oct 12, 2021
d7446d1
Merge remote-tracking branch 'origin/rnmobile/add/embed-block-integra…
jd-alexander Oct 14, 2021
afc1ddb
WIP
jd-alexander Oct 14, 2021
67ae243
fixed unneeded diff change.
jd-alexander Oct 14, 2021
f03d25d
WIP block settings
jd-alexander Oct 14, 2021
eab90f9
Merge remote-tracking branch 'origin/rnmobile/add/embed-block-integra…
jd-alexander Oct 15, 2021
e9f5c8b
Mocked RCTAztecView to utilize an underlying TextInput.
jd-alexander Oct 15, 2021
4b9d41f
Fixed Embed block caption test issues.
jd-alexander Oct 15, 2021
5000152
Created test - toggle resize for smaller devices media settings
jd-alexander Oct 15, 2021
0f4d623
Added cannot embed test.
jd-alexander Oct 15, 2021
f03c6ad
Removed unneeded test id.
jd-alexander Oct 15, 2021
9ee25d0
Merge branch 'rnmobile/add/embed-block-integration-tests' into rnmobi…
fluiddot Oct 19, 2021
b75b4e9
Merge branch 'rnmobile/add/embed-block-integration-tests' into rnmobi…
jd-alexander Oct 27, 2021
9b4287d
WIP insert embed from slash inserter.
jd-alexander Oct 27, 2021
b675261
Mock fetch request in cannot embed test case
fluiddot Nov 2, 2021
60e8cab
Trigger onSelectionChange event instead of onChange
fluiddot Nov 2, 2021
3b7faa9
Query slash inserter item by text
fluiddot Nov 2, 2021
9b29c49
Add expected HTML to slash inserter test case
fluiddot Nov 2, 2021
95a8a23
Mock autocomplete component styles
fluiddot Nov 2, 2021
1d3bce0
Set paragraph as default block
fluiddot Nov 2, 2021
afffdc1
Add empty paragraph HTML constant
fluiddot Nov 2, 2021
8d01d96
Add test suite for insert via slash inserter case
fluiddot Nov 2, 2021
231d832
Update toggle responsive test case
fluiddot Nov 2, 2021
3b1b260
Merge branch 'rnmobile/add/embed-block-integration-tests' into rnmobi…
fluiddot Nov 2, 2021
651c80e
Fix request mock for theme endpoint
fluiddot Nov 2, 2021
8e932aa
Add slash inserter cases for most used providers
fluiddot Nov 2, 2021
b56dc46
Expect for block settings button instead edit URL button
fluiddot Nov 2, 2021
520680a
Use snapshot testing instead of checking HTML
fluiddot Nov 2, 2021
70475b7
Add block settings test suite
fluiddot Nov 2, 2021
a13c5fd
Add embed test snapshots
fluiddot Nov 2, 2021
902ca9b
Use snapshot in insert generic embed block test
fluiddot Nov 2, 2021
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
223 changes: 218 additions & 5 deletions packages/block-library/src/embed/test/index.native.js
Expand Up @@ -13,7 +13,11 @@ import { Clipboard, Platform } from 'react-native';
/**
* WordPress dependencies
*/
import { getBlockTypes, unregisterBlockType } from '@wordpress/blocks';
import {
getBlockTypes,
setDefaultBlockName,
unregisterBlockType,
} from '@wordpress/blocks';
import fetchRequest from '@wordpress/api-fetch';
import { store as coreStore } from '@wordpress/core-data';
import { dispatch } from '@wordpress/data';
Expand Down Expand Up @@ -68,6 +72,7 @@ const MOCK_BAD_WORDPRESS_RESPONSE = {
},
html: false,
};
const EMBED_NULL_RESPONSE = null;

// Embed block HTML examples
const EMPTY_EMBED_HTML = '<!-- wp:embed /-->';
Expand All @@ -76,11 +81,31 @@ const RICH_TEXT_EMBED_HTML = `<!-- wp:embed {"url":"https://twitter.com/notnowni
https://twitter.com/notnownikki
</div></figure>
<!-- /wp:embed -->`;
const RICH_TEXT_ERROR_EMBED_HTML = `<!-- wp:embed {"url":"https://twitter.com/testing","type":"rich","providerNameSlug":"twitter","responsive":true} -->
<figure class="wp-block-embed is-type-rich is-provider-twitter wp-block-embed-twitter"><div class="wp-block-embed__wrapper">
https://twitter.com/testing
</div></figure>
<!-- /wp:embed -->`;
const PHOTO_EMBED_HTML = `<!-- wp:embed {"url":"https://cloudup.com/cQFlxqtY4ob","type":"photo","providerNameSlug":"cloudup","responsive":true} -->
<figure class="wp-block-embed is-type-photo is-provider-cloudup wp-block-embed-cloudup"><div class="wp-block-embed__wrapper">
https://cloudup.com/cQFlxqtY4ob
</div></figure>
<!-- /wp:embed -->`;
const RICH_TEXT_EMBED_HTML_WITH_CAPTION = `<!-- wp:embed {"url":"https://twitter.com/notnownikki","type":"rich","providerNameSlug":"twitter","responsive":true} -->
<figure class="wp-block-embed is-type-rich is-provider-twitter wp-block-embed-twitter"><div class="wp-block-embed__wrapper">
https://twitter.com/notnownikki
</div><figcaption>Caption</figcaption></figure>
<!-- /wp:embed -->`;
const RICH_TEXT_EMBED_HTML_WITH_ALLOW_RESPONSIVE_SET_TO_FALSE = `<!-- wp:embed {"url":"https://twitter.com/notnownikki","type":"rich","providerNameSlug":"twitter","allowResponsive":false,"responsive":true} -->
<figure class="wp-block-embed is-type-rich is-provider-twitter wp-block-embed-twitter"><div class="wp-block-embed__wrapper">
https://twitter.com/notnownikki
</div></figure>
<!-- /wp:embed -->`;
const EMPTY_RICH_TEXT_EMBED_HTML =
'<!-- wp:embed {"providerNameSlug":"twitter","responsive":true} /-->';

const EMPTY_PARAGRAPH_HTML =
'<!-- wp:paragraph --><p></p><!-- /wp:paragraph -->';

const MOST_USED_PROVIDERS = embed.settings.variations.filter( ( { name } ) =>
[ 'youtube', 'twitter', 'wordpress', 'vimeo' ].includes( name )
Expand All @@ -89,6 +114,12 @@ const MOST_USED_PROVIDERS = embed.settings.variations.filter( ( { name } ) =>
// Return specified mocked responses for the oembed endpoint.
const mockEmbedResponses = ( mockedResponses ) => {
fetchRequest.mockImplementation( ( { path } ) => {
if ( path === '/wp/v2/themes?status=active' ) {
return Promise.resolve( [
{ theme_supports: { 'responsive-embeds': true } },
] );
}

const matchedEmbedResponse = mockedResponses.find(
( mockedResponse ) =>
path ===
Expand Down Expand Up @@ -143,6 +174,7 @@ beforeAll( () => {
// block is added to empty posts.
registerBlock( paragraph );
registerBlock( embed );
setDefaultBlockName( paragraph.name );
jd-alexander marked this conversation as resolved.
Show resolved Hide resolved
} );

beforeEach( () => {
Expand Down Expand Up @@ -619,8 +651,7 @@ describe( 'Embed block', () => {
getByTestId,
getByText,
} = await initializeEditor( {
initialHtml:
'<!-- wp:paragraph --><p></p><!-- /wp:paragraph -->',
initialHtml: EMPTY_PARAGRAPH_HTML,
} );

// Paste URL in paragraph block
Expand Down Expand Up @@ -663,8 +694,7 @@ describe( 'Embed block', () => {
getByTestId,
getByText,
} = await initializeEditor( {
initialHtml:
'<!-- wp:paragraph --><p></p><!-- /wp:paragraph -->',
initialHtml: EMPTY_PARAGRAPH_HTML,
} );

// Paste URL in paragraph block
Expand Down Expand Up @@ -700,4 +730,187 @@ describe( 'Embed block', () => {
expect( getEditorHtml() ).toMatchSnapshot();
} );
} );

describe( 'insert via slash inserter', () => {
it( 'insert generic embed block', async () => {
const expectedHtml = EMPTY_EMBED_HTML;
const embedBlockSlashInserter = '/Embed';
const {
getByPlaceholderText,
getByA11yLabel,
getByText,
} = await initializeEditor( { initialHtml: EMPTY_PARAGRAPH_HTML } );

const paragraphText = getByPlaceholderText( 'Start writing…' );
fireEvent( paragraphText, 'focus' );
// Trigger onSelectionChange to update both the current text and text selection.
// This event is required by the autocompleter, as it only displays the slash inserter
// if the text selection is located at the end of the text, for this reason,
// the start and end arguments match the text length.
fireEvent(
paragraphText,
'onSelectionChange',
embedBlockSlashInserter.length,
embedBlockSlashInserter.length,
jd-alexander marked this conversation as resolved.
Show resolved Hide resolved
embedBlockSlashInserter,
{
nativeEvent: {
eventCount: 1,
target: undefined,
text: embedBlockSlashInserter,
},
}
);

fireEvent.press( await waitFor( () => getByText( 'Embed' ) ) );

const block = await waitFor( () =>
getByA11yLabel( /Embed Block\. Row 1/ )
);

const blockName = within( block ).getByText( 'Embed' );

expect( blockName ).toBeDefined();
expect( getEditorHtml() ).toBe( expectedHtml );
} );

it( 'insert Twitter embed block', async () => {
const expectedHtml = EMPTY_RICH_TEXT_EMBED_HTML;
const embedBlockSlashInserter = '/Twitter';
const {
getByPlaceholderText,
getByA11yLabel,
getByText,
} = await initializeEditor( { initialHtml: EMPTY_PARAGRAPH_HTML } );

const paragraphText = getByPlaceholderText( 'Start writing…' );
fireEvent( paragraphText, 'focus' );
// Trigger onSelectionChange to update both the current text and text selection.
// This event is required by the autocompleter, as it only displays the slash inserter
// if the text selection is located at the end of the text, for this reason,
// the start and end arguments match the text length.
fireEvent(
paragraphText,
'onSelectionChange',
embedBlockSlashInserter.length,
embedBlockSlashInserter.length,
embedBlockSlashInserter,
{
nativeEvent: {
eventCount: 1,
target: undefined,
text: embedBlockSlashInserter,
},
}
);

fireEvent.press( await waitFor( () => getByText( 'Twitter' ) ) );

const block = await waitFor( () =>
getByA11yLabel( /Embed Block\. Row 1/ )
);

const blockName = within( block ).getByText( 'Twitter' );

expect( blockName ).toBeDefined();
expect( getEditorHtml() ).toBe( expectedHtml );
} );
} );

it( 'sets an Embed block caption', async () => {
jd-alexander marked this conversation as resolved.
Show resolved Hide resolved
const initialHtml = RICH_TEXT_EMBED_HTML;
const expectedHtml = RICH_TEXT_EMBED_HTML_WITH_CAPTION;
const expectedCaption = 'Caption';

const waitForElement = ( { getByA11yLabel } ) =>
getByA11yLabel( /Embed Block\. Row 1/ );
const {
element,
getByPlaceholderText,
getByDisplayValue,
} = await initializeEditor(
{
initialHtml,
},
{ waitForElement }
);

// Select block
fireEvent.press( element );

// Set a caption
const captionField = getByPlaceholderText( 'Add caption' );
fireEvent( captionField, 'focus' );
fireEvent( captionField, 'onChange', {
nativeEvent: {
eventCount: 1,
target: undefined,
text: expectedCaption,
},
} );

// Get current caption
const caption = await waitFor( () =>
getByDisplayValue( `<p>${ expectedCaption }</p>` )
);

expect( caption ).toBeDefined();
expect( getEditorHtml() ).toBe( expectedHtml );
} );

it( 'Toggle resize for smaller devices media settings', async () => {
const initialHtml = RICH_TEXT_EMBED_HTML;
const expectedHtml = RICH_TEXT_EMBED_HTML_WITH_ALLOW_RESPONSIVE_SET_TO_FALSE;

const waitForElement = ( { getByA11yLabel } ) =>
getByA11yLabel( /Embed Block\. Row 1/ );
const { element, getByA11yLabel } = await initializeEditor(
{
initialHtml,
},
{ waitForElement }
);

// Select block
fireEvent.press( element );

const settingsButton = await waitFor( () =>
getByA11yLabel( 'Open Settings' )
);
fireEvent.press( settingsButton );

const resizeToggleControl = await waitFor( () =>
getByA11yLabel( /Resize for smaller devices/ )
);

fireEvent.press( resizeToggleControl );

expect( getEditorHtml() ).toBe( expectedHtml );
} );

it( 'Cannot embed should be shown on the placeholder if EmbedPreview data is null', async () => {
jd-alexander marked this conversation as resolved.
Show resolved Hide resolved
// Return null response for requests to oembed endpoint.
fetchRequest.mockImplementation( ( { path } ) => {
const isEmbedRequest = path.startsWith( '/oembed/1.0/proxy' );
return Promise.resolve( isEmbedRequest ? EMBED_NULL_RESPONSE : {} );
} );

const initialHtml = RICH_TEXT_ERROR_EMBED_HTML;

const waitForElement = ( { getByA11yLabel } ) =>
getByA11yLabel( /Embed Block\. Row 1/ );
const { element, getByText } = await initializeEditor(
{
initialHtml,
},
{ waitForElement }
);

// Select block
fireEvent.press( element );

const cannotEmbedText = getByText( 'Unable to embed media' );

expect( cannotEmbedText ).toBeDefined();
} );
} );
3 changes: 3 additions & 0 deletions test/native/__mocks__/styleMock.js
Expand Up @@ -150,4 +150,7 @@ module.exports = {
'embed__icon--error': {
fill: 'red',
},
'components-autocomplete': {
height: 100,
},
};