From f1c05116b0c742e14fe7456b5d2f5167c1002a28 Mon Sep 17 00:00:00 2001 From: James Nylen Date: Fri, 19 May 2017 23:02:45 -0400 Subject: [PATCH] Add infrastructure for testing parsing + blocks list + serialization --- blocks/test/full-content.js | 103 ++++++++++++++++++++++++++++++++++++ webpack.config.js | 9 +++- 2 files changed, 111 insertions(+), 1 deletion(-) create mode 100644 blocks/test/full-content.js diff --git a/blocks/test/full-content.js b/blocks/test/full-content.js new file mode 100644 index 00000000000000..277ec6e4b9e480 --- /dev/null +++ b/blocks/test/full-content.js @@ -0,0 +1,103 @@ +/** + * External dependencies + */ +import fs from 'fs'; +import path from 'path'; +import { uniq } from 'lodash'; +import { expect } from 'chai'; + +/** + * Internal dependencies + */ +import { + // parseWithGrammar, + parseWithTinyMCE, +} from '../api/parser'; +import serialize from '../api/serializer'; + +const fixturesDir = path.join( __dirname, 'fixtures' ); + +// We expect 3 different types of files for each fixture: +// - fixture.html - original content +// - fixture.json - blocks structure +// - fixture.serialized.html - re-serialized content +// Get the "base" name for each fixture first. +const fileBasenames = uniq( + fs.readdirSync( fixturesDir ) + .map( f => f.replace( /\..+$/, '' ) ) +); + +function readFixtureFile( filename ) { + try { + return fs.readFileSync( + path.join( fixturesDir, filename ), + 'utf8' + ); + } catch ( err ) { + return null; + } +} + +function writeFixtureFile( filename, content ) { + fs.writeFileSync( + path.join( fixturesDir, filename ), + content + ); +} + +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 unique UIDs to a predictable value + block.uid = '_uid_' + index; + return block; + } ); +} + +describe( 'full post content fixture', () => { + fileBasenames.forEach( f => { + it( f, () => { + const content = readFixtureFile( f + '.html' ); + + const blocksActual = parseWithTinyMCE( content ); + const blocksActualNormalized = normalizeParsedBlocks( blocksActual ); + let blocksExpectedString = readFixtureFile( f + '.json' ); + + if ( ! blocksExpectedString ) { + if ( process.env.GENERATE_MISSING_FIXTURES ) { + blocksExpectedString = JSON.stringify( + blocksActualNormalized, + null, + 4 + ); + writeFixtureFile( f + '.json', blocksExpectedString ); + } else { + throw new Error( + 'Missing fixture file: ' + f + '.json' + ); + } + } + + const blocksExpected = JSON.parse( blocksExpectedString ); + expect( blocksActualNormalized ).to.eql( blocksExpected ); + + const serializedActual = serialize( blocksActual ); + let serializedExpected = readFixtureFile( f + '.serialized.html' ); + + if ( ! serializedExpected ) { + if ( process.env.GENERATE_MISSING_FIXTURES ) { + serializedExpected = serializedActual; + writeFixtureFile( f + '.serialized.html', serializedExpected ); + } else { + throw new Error( + 'Missing fixture file: ' + f + '.serialized.html' + ); + } + } + + expect( serializedActual ).to.eql( serializedExpected ); + } ); + } ); +} ); diff --git a/webpack.config.js b/webpack.config.js index fd9feb954f5f9d..2847c01cb131a3 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -96,6 +96,9 @@ switch ( process.env.NODE_ENV ) { case 'test': config.target = 'node'; + config.node = { + __dirname: true, + }; config.module.rules = [ ...[ 'i18n', 'element', 'blocks', 'editor' ].map( ( entry ) => ( { test: require.resolve( './' + entry + '/index.js' ), @@ -103,12 +106,16 @@ switch ( process.env.NODE_ENV ) { } ) ), ...config.module.rules, ]; + const testFiles = glob.sync( + './{' + Object.keys( config.entry ).sort() + '}/**/test/*.js' + ); config.entry = [ './i18n/index.js', './element/index.js', './blocks/index.js', './editor/index.js', - ...glob.sync( `./{${ Object.keys( config.entry ).join() }}/**/test/*.js` ), + ...testFiles.filter( f => /full-content\.js$/.test( f ) ), + ...testFiles.filter( f => ! /full-content\.js$/.test( f ) ), ]; config.externals = [ require( 'webpack-node-externals' )() ]; config.output = {