Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
.forEach( ( name ) => {
const nameToFilename = blockNameToFixtureBasename( name );
const foundFixtures = blockBasenames
.filter( ( basename ) => (
basename === nameToFilename ||
startsWith( basename, nameToFilename + '__' )
) )
.map( ( basename ) => {
const {
filename: htmlFixtureFileName,
} = getBlockFixtureHTML( basename );
const {
file: jsonFixtureContent,
} = getBlockFixtureJSON( basename );
// The parser output for this test. For missing files,
// JSON.parse( null ) === null.
const parserOutput = JSON.parse(
jsonFixtureContent,
} from '@wordpress/blocks';
import { parse as grammarParse } from '@wordpress/block-serialization-default-parser';
import { registerCoreBlocks } from '@wordpress/block-library';
import { //eslint-disable-line no-restricted-syntax
blockNameToFixtureBasename,
getAvailableBlockFixturesBasenames,
getBlockFixtureHTML,
getBlockFixtureJSON,
getBlockFixtureParsedJSON,
getBlockFixtureSerializedHTML,
writeBlockFixtureParsedJSON,
writeBlockFixtureJSON,
writeBlockFixtureSerializedHTML,
} from '@wordpress/e2e-tests/fixtures';
const blockBasenames = getAvailableBlockFixturesBasenames();
function normalizeParsedBlocks( blocks ) {
return blocks.map( ( block, index ) => {
// Clone and remove React-instance-specific stuff; also, attribute
// values that equal `undefined` will be removed
block = JSON.parse( JSON.stringify( block ) );
// Change client IDs to a predictable value
block.clientId = '_clientId_' + index;
// Recurse to normalize inner blocks
block.innerBlocks = normalizeParsedBlocks( block.innerBlocks );
return block;
} );
}
.map( ( basename ) => {
const {
filename: htmlFixtureFileName,
} = getBlockFixtureHTML( basename );
const {
file: jsonFixtureContent,
} = getBlockFixtureJSON( basename );
// The parser output for this test. For missing files,
// JSON.parse( null ) === null.
const parserOutput = JSON.parse(
jsonFixtureContent,
);
// The name of the first block that this fixture file
// contains (if any).
const firstBlock = get( parserOutput, [ '0', 'name' ], null );
return {
filename: htmlFixtureFileName,
parserOutput,
firstBlock,
};
.map( ( basename ) => {
const {
filename: htmlFixtureFileName,
} = getBlockFixtureHTML( basename );
const {
file: jsonFixtureContent,
} = getBlockFixtureJSON( basename );
// The parser output for this test. For missing files,
// JSON.parse( null ) === null.
const parserOutput = JSON.parse(
jsonFixtureContent,
);
// The name of the first block that this fixture file
// contains (if any).
const firstBlock = get( parserOutput, [ '0', 'name' ], null );
return {
filename: htmlFixtureFileName,
parserOutput,
firstBlock,
};
} )
.filter( ( fixture ) => fixture.parserOutput !== null );
).toEqual( blocksExpected );
} catch ( err ) {
throw new Error( format(
"File '%s' does not match expected value:\n\n%s",
jsonFixtureFileName,
err.message
) );
}
// `serialize` doesn't have a trailing newline, but the fixture
// files should.
const serializedActual = serialize( blocksActual ) + '\n';
const {
filename: serializedHTMLFileName,
file: serializedHTMLFixtureContent,
} = getBlockFixtureSerializedHTML( basename );
let serializedExpected;
if ( serializedHTMLFixtureContent ) {
serializedExpected = serializedHTMLFixtureContent;
} else if ( process.env.GENERATE_MISSING_FIXTURES ) {
serializedExpected = serializedActual;
writeBlockFixtureSerializedHTML( basename, serializedExpected );
} else {
throw new Error(
`Missing fixture file: ${ serializedHTMLFileName }`
);
}
try {
expect( serializedActual ).toEqual( serializedExpected );
} catch ( err ) {
}
// `serialize` doesn't have a trailing newline, but the fixture
// files should.
const serializedActual = serialize( blocksActual ) + '\n';
const {
filename: serializedHTMLFileName,
file: serializedHTMLFixtureContent,
} = getBlockFixtureSerializedHTML( basename );
let serializedExpected;
if ( serializedHTMLFixtureContent ) {
serializedExpected = serializedHTMLFixtureContent;
} else if ( process.env.GENERATE_MISSING_FIXTURES ) {
serializedExpected = serializedActual;
writeBlockFixtureSerializedHTML( basename, serializedExpected );
} else {
throw new Error(
`Missing fixture file: ${ serializedHTMLFileName }`
);
}
try {
expect( serializedActual ).toEqual( serializedExpected );
} catch ( err ) {
throw new Error( format(
"File '%s' does not match expected value:\n\n%s",
serializedHTMLFileName,
err.message
) );
}
} );
it( basename, () => {
const {
filename: htmlFixtureFileName,
file: htmlFixtureContent,
} = getBlockFixtureHTML( basename );
if ( htmlFixtureContent === null ) {
throw new Error(
`Missing fixture file: ${ htmlFixtureFileName }`
);
}
const {
filename: parsedJSONFixtureFileName,
file: parsedJSONFixtureContent,
} = getBlockFixtureParsedJSON( basename );
const parserOutputActual = grammarParse( htmlFixtureContent );
let parserOutputExpectedString;
if ( parsedJSONFixtureContent ) {
parserOutputExpectedString = parsedJSONFixtureContent;
} else if ( process.env.GENERATE_MISSING_FIXTURES ) {
parserOutputExpectedString = JSON.stringify(
parserOutputActual,
null,
4
) + '\n';
writeBlockFixtureParsedJSON( basename, parserOutputExpectedString );
} else {
throw new Error(
`Missing fixture file: ${ parsedJSONFixtureFileName }`
);
}
const {
filename: jsonFixtureFileName,
file: jsonFixtureContent,
} = getBlockFixtureJSON( basename );
let blocksExpectedString;
if ( jsonFixtureContent ) {
blocksExpectedString = jsonFixtureContent;
} else if ( process.env.GENERATE_MISSING_FIXTURES ) {
blocksExpectedString = JSON.stringify(
blocksActualNormalized,
null,
4
) + '\n';
writeBlockFixtureJSON( basename, blocksExpectedString );
} else {
throw new Error(
`Missing fixture file: ${ jsonFixtureFileName }`
);
}
const blocksExpected = JSON.parse( blocksExpectedString );
try {
expect(
blocksActualNormalized
).toEqual( blocksExpected );
} catch ( err ) {
throw new Error( format(
"File '%s' does not match expected value:\n\n%s",
jsonFixtureFileName,
err.message
const {
filename: parsedJSONFixtureFileName,
file: parsedJSONFixtureContent,
} = getBlockFixtureParsedJSON( basename );
const parserOutputActual = grammarParse( htmlFixtureContent );
let parserOutputExpectedString;
if ( parsedJSONFixtureContent ) {
parserOutputExpectedString = parsedJSONFixtureContent;
} else if ( process.env.GENERATE_MISSING_FIXTURES ) {
parserOutputExpectedString = JSON.stringify(
parserOutputActual,
null,
4
) + '\n';
writeBlockFixtureParsedJSON( basename, parserOutputExpectedString );
} else {
throw new Error(
`Missing fixture file: ${ parsedJSONFixtureFileName }`
);
}
const parserOutputExpected = JSON.parse( parserOutputExpectedString );
try {
expect(
parserOutputActual
).toEqual( parserOutputExpected );
} catch ( err ) {
throw new Error( format(
"File '%s' does not match expected value:\n\n%s",
parsedJSONFixtureFileName,
err.message