-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add: Generic end 2 end test to the block transforms.
- Loading branch information
1 parent
f97617f
commit b7c0a55
Showing
8 changed files
with
302 additions
and
39 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
test/e2e/specs/__snapshots__/block-transforms.test.js.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`test transforms individual transforms work as expected Code block should transform to Preformatted block. fixture: core__code 1`] = ` | ||
"<!-- wp:preformatted --> | ||
<pre class=\\"wp-block-preformatted\\">export default function MyButton() { | ||
return <Button>Click Me!</Button>; | ||
}</pre> | ||
<!-- /wp:preformatted -->" | ||
`; | ||
exports[`test transforms individual transforms work as expected Paragraph block should transform to Heading block. fixture: core__paragraph__align-right 1`] = ` | ||
"<!-- wp:heading --> | ||
<h2>... like this one, which is separate from the above and right aligned.</h2> | ||
<!-- /wp:heading -->" | ||
`; | ||
exports[`test transforms individual transforms work as expected Paragraph block should transform to List block. fixture: core__paragraph__align-right 1`] = ` | ||
"<!-- wp:list --> | ||
<ul><li>... like this one, which is separate from the above and right aligned.</li></ul> | ||
<!-- /wp:list -->" | ||
`; | ||
exports[`test transforms individual transforms work as expected Paragraph block should transform to Preformatted block. fixture: core__paragraph__align-right 1`] = ` | ||
"<!-- wp:preformatted --> | ||
<pre class=\\"wp-block-preformatted\\">... like this one, which is separate from the above and right aligned.</pre> | ||
<!-- /wp:preformatted -->" | ||
`; | ||
exports[`test transforms individual transforms work as expected Paragraph block should transform to Quote block. fixture: core__paragraph__align-right 1`] = ` | ||
"<!-- wp:quote --> | ||
<blockquote class=\\"wp-block-quote\\"><p>... like this one, which is separate from the above and right aligned.</p></blockquote> | ||
<!-- /wp:quote -->" | ||
`; | ||
exports[`test transforms individual transforms work as expected Paragraph block should transform to Verse block. fixture: core__paragraph__align-right 1`] = ` | ||
"<!-- wp:verse --> | ||
<pre class=\\"wp-block-verse\\">... like this one, which is separate from the above and right aligned.</pre> | ||
<!-- /wp:verse -->" | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import path from 'path'; | ||
import { | ||
intersection, | ||
mapValues, | ||
pickBy, | ||
some, | ||
} from 'lodash'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { | ||
getAllBlocks, | ||
getAvailableBlockTransforms, | ||
getBlockTitle, | ||
getEditedPostContent, | ||
hasBlockSwitcher, | ||
newPost, | ||
setPostContent, | ||
selectBlockByClientId, | ||
transformBlock, | ||
} from '../support/utils'; | ||
import { | ||
getFileBaseNames, | ||
readFixtureFile, | ||
} from '../../support/utils'; | ||
import { EXPECTED_TRANSFORMS } from './fixtures/block-transforms'; | ||
|
||
/* | ||
* Returns true if the fileBase refers to a fixture of a block | ||
* that should not be handled e.g: because of being deprecated, | ||
* or because of being a block that tests an error state. | ||
*/ | ||
const isAnExpectedUnhandledBlock = ( fixturesDir, fileBase ) => { | ||
if ( fileBase.includes( 'deprecated' ) ) { | ||
return true; | ||
} | ||
const parsedBlockObject = JSON.parse( | ||
readFixtureFile( fixturesDir, fileBase + '.parsed.json' ) | ||
)[ 0 ]; | ||
return some( | ||
[ | ||
null, | ||
'core/block', | ||
'core/column', | ||
'core/freeform', | ||
'core/text-columns', | ||
'core/subhead', | ||
'core/text', | ||
'unregistered/example', | ||
], | ||
( blockName ) => parsedBlockObject.blockName === blockName | ||
); | ||
}; | ||
|
||
const setPostContentAndSelectBlock = async ( content ) => { | ||
await setPostContent( content ); | ||
const blocks = await getAllBlocks(); | ||
const clientId = blocks[ 0 ].clientId; | ||
await page.click( '.editor-post-title .editor-post-title__block' ); | ||
await selectBlockByClientId( clientId ); | ||
}; | ||
|
||
const getTransformStructureFromFile = async ( fixturesDir, fileBase ) => { | ||
if ( isAnExpectedUnhandledBlock( fixturesDir, fileBase ) ) { | ||
return null; | ||
} | ||
const content = readFixtureFile( fixturesDir, fileBase + '.html' ); | ||
await setPostContentAndSelectBlock( content ); | ||
const block = ( await getAllBlocks() )[ 0 ]; | ||
const availableTransforms = await getAvailableBlockTransforms(); | ||
const originalBlock = await getBlockTitle( block.name ); | ||
|
||
return { | ||
availableTransforms, | ||
originalBlock, | ||
content, | ||
}; | ||
}; | ||
|
||
const getTransformResult = async ( blockContent, transformName ) => { | ||
await setPostContentAndSelectBlock( blockContent ); | ||
expect( await hasBlockSwitcher() ).toBe( true ); | ||
await transformBlock( transformName ); | ||
return getEditedPostContent(); | ||
}; | ||
|
||
describe( 'test transforms', () => { | ||
const fixturesDir = path.join( | ||
__dirname, '..', '..', 'integration', 'full-content', 'fixtures' | ||
); | ||
|
||
// Todo: Remove the intersect as soon as all fixtures are corrected, | ||
// and its direct usage on the editor does not trigger errors. | ||
// Currently some fixtures trigger errors (mainly media related) | ||
// because when loaded in the editor, | ||
// some requests are triggered that have a 404 response. | ||
const fileBasenames = intersection( | ||
getFileBaseNames( fixturesDir ), | ||
[ | ||
'core__code', | ||
'core__paragraph__align-right', | ||
] | ||
); | ||
|
||
const transformStructure = {}; | ||
beforeAll( async () => { | ||
await newPost(); | ||
|
||
for ( const fileBase of fileBasenames ) { | ||
const structure = await getTransformStructureFromFile( | ||
fixturesDir, | ||
fileBase | ||
); | ||
if ( ! structure ) { | ||
continue; | ||
} | ||
transformStructure[ fileBase ] = structure; | ||
await setPostContent( '' ); | ||
await page.click( '.editor-post-title .editor-post-title__block' ); | ||
} | ||
} ); | ||
|
||
it( 'should contain the expected transforms', async () => { | ||
expect( | ||
mapValues( | ||
pickBy( | ||
transformStructure, | ||
( { availableTransforms } ) => availableTransforms, | ||
), | ||
( { availableTransforms, originalBlock } ) => { | ||
return { originalBlock, availableTransforms }; | ||
} | ||
) | ||
).toEqual( EXPECTED_TRANSFORMS ); | ||
} ); | ||
|
||
describe( 'individual transforms work as expected', () => { | ||
beforeAll( async () => { | ||
await newPost(); | ||
} ); | ||
|
||
beforeEach( async () => { | ||
await setPostContent( '' ); | ||
await page.click( '.editor-post-title .editor-post-title__block' ); | ||
} ); | ||
|
||
for ( const [ fixture, { originalBlock, availableTransforms } ] of Object.entries( EXPECTED_TRANSFORMS ) ) { | ||
for ( const transform of availableTransforms ) { | ||
it( `${ originalBlock } block should transform to ${ transform } block. fixture: ${ fixture }`, | ||
async () => { | ||
const { content } = transformStructure[ fixture ]; | ||
expect( | ||
await getTransformResult( content, transform ) | ||
).toMatchSnapshot(); | ||
} | ||
); | ||
} | ||
} | ||
} ); | ||
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
export const EXPECTED_TRANSFORMS = { | ||
core__code: { | ||
originalBlock: 'Code', | ||
availableTransforms: [ | ||
'Preformatted', | ||
], | ||
}, | ||
'core__paragraph__align-right': { | ||
originalBlock: 'Paragraph', | ||
availableTransforms: [ | ||
'Heading', | ||
'List', | ||
'Quote', | ||
'Preformatted', | ||
'Verse', | ||
], | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/** | ||
* Returns a string containing the block title associated with the provided block name. | ||
* @param {string} blockName Block name. | ||
* | ||
* @return {Promise} Promise resolving with a string containing the block title. | ||
*/ | ||
export async function getBlockTitle( blockName ) { | ||
return page.evaluate( ( _blockName ) => { | ||
return wp.data.select( 'core/blocks' ).getBlockType( _blockName ).title; | ||
}, blockName ); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/** | ||
* Transforms the current selected block in the block whose title is blockTitle. | ||
* | ||
* @param {string} blockTitle Title of the destination block of the transformation. | ||
*/ | ||
export const transformBlock = async ( blockTitle ) => { | ||
await page.click( '.editor-block-toolbar .editor-block-switcher' ); | ||
await page.click( | ||
`.editor-block-types-list .editor-block-types-list__list-item button[aria-label="${ blockTitle }"]` | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.