Skip to content

Commit

Permalink
Add infrastructure for testing parsing + blocks list + serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
nylen committed May 20, 2017
1 parent c769615 commit f1c0511
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 1 deletion.
103 changes: 103 additions & 0 deletions blocks/test/full-content.js
Original file line number Diff line number Diff line change
@@ -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 );
} );
} );
} );
9 changes: 8 additions & 1 deletion webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,19 +96,26 @@ 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' ),
use: 'expose-loader?wp.' + entry,
} ) ),
...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 = {
Expand Down

0 comments on commit f1c0511

Please sign in to comment.