-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
full-content.js
184 lines (163 loc) · 4.81 KB
/
full-content.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/**
* External dependencies
*/
import fs from 'fs';
import path from 'path';
import { uniq, isObject, omit, startsWith } from 'lodash';
import { expect } from 'chai';
import { format } from 'util';
/**
* Internal dependencies
*/
import {
// parseWithGrammar,
parseWithTinyMCE,
} from '../api/parser';
import serialize from '../api/serializer';
import { getBlockTypes } from '../api/registration';
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 )
.filter( f => /(\.html|\.json)$/.test( f ) )
.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 normalizeReactTree( element ) {
if ( Array.isArray( element ) ) {
return element.map( child => normalizeReactTree( child ) );
}
// Check if we got an object first, then if it actually has a `type` like a
// React component. Sometimes we get other stuff here, which probably
// indicates a bug.
if ( isObject( element ) && element.type ) {
const toReturn = {
type: element.type,
};
const attributes = omit( element.props, 'children' );
if ( Object.keys( attributes ).length ) {
toReturn.attributes = attributes;
}
if ( element.props.children ) {
toReturn.children = normalizeReactTree( element.props.children );
}
return toReturn;
}
return element;
}
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;
// Walk each attribute and get a more concise representation of any
// React elements
for ( const k in block.attributes ) {
block.attributes[ k ] = normalizeReactTree( block.attributes[ k ] );
}
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
) + '\n';
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 );
} );
} );
it( 'should be present for each block', () => {
const errors = [];
getBlockTypes().map( block => block.slug ).forEach( slug => {
const slugToFilename = slug.replace( /\//g, '-' );
const foundFixtures = fileBasenames
.filter( basename => (
basename === slugToFilename ||
startsWith( basename, slugToFilename + '-' )
) )
.map( basename => {
const filename = basename + '.html';
return {
filename,
contents: readFixtureFile( filename ),
};
} )
.filter( fixture => fixture.contents !== null );
if ( ! foundFixtures.length ) {
errors.push( format(
'Expected a fixture file called \'%s.html\' or \'%s-*.html\'.',
slugToFilename,
slugToFilename
) );
}
foundFixtures.forEach( fixture => {
const delimiter = new RegExp(
'<!--\\s*wp:' + slug + '(\\s+|\\s*-->)'
);
if ( ! delimiter.test( fixture.contents ) ) {
errors.push( format(
'Expected fixture file \'%s\' to test the \'%s\' block.',
fixture.filename,
slug
) );
}
} );
} );
if ( errors.length ) {
throw new Error(
'Problem(s) with fixture files:\n\n' + errors.join( '\n' )
);
}
} );
} );