-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathfull-content.spec.js
250 lines (222 loc) · 6.87 KB
/
full-content.spec.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/**
* External dependencies
*/
import fs from 'fs';
import path from 'path';
import { uniq, startsWith, get } from 'lodash';
import { format } from 'util';
/**
* WordPress dependencies
*/
import {
getBlockTypes,
parse,
serialize,
unstable__bootstrapServerSideBlockDefinitions, // eslint-disable-line camelcase
} from '@wordpress/blocks';
import { parse as grammarParse } from '@wordpress/block-serialization-default-parser';
import { registerCoreBlocks } from '@wordpress/block-library';
const fixturesDir = path.join( __dirname, 'fixtures' );
// We expect 4 different types of files for each fixture:
// - fixture.html : original content
// - fixture.parsed.json : parser output
// - 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 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;
} );
}
describe( 'full post content fixture', () => {
beforeAll( () => {
unstable__bootstrapServerSideBlockDefinitions( require( './server-registered.json' ) );
// Load all hooks that modify blocks
require( '../../../packages/editor/src/hooks' );
registerCoreBlocks();
} );
fileBasenames.forEach( ( f ) => {
it( f, () => {
const content = readFixtureFile( f + '.html' );
if ( content === null ) {
throw new Error(
'Missing fixture file: ' + f + '.html'
);
}
const parserOutputActual = grammarParse( content );
let parserOutputExpectedString = readFixtureFile( f + '.parsed.json' );
if ( ! parserOutputExpectedString ) {
if ( process.env.GENERATE_MISSING_FIXTURES ) {
parserOutputExpectedString = JSON.stringify(
parserOutputActual,
null,
4
) + '\n';
writeFixtureFile( f + '.parsed.json', parserOutputExpectedString );
} else {
throw new Error(
'Missing fixture file: ' + f + '.parsed.json'
);
}
}
const parserOutputExpected = JSON.parse( parserOutputExpectedString );
try {
expect(
parserOutputActual
).toEqual( parserOutputExpected );
} catch ( err ) {
throw new Error( format(
"File '%s.parsed.json' does not match expected value:\n\n%s",
f,
err.message
) );
}
const blocksActual = parse( content );
// Block validation may log errors during deprecation migration,
// unless explicitly handled from a valid block via isEligible.
// Match on filename for deprecated blocks fixtures to allow.
const isDeprecated = /__deprecated([-_]|$)/.test( f );
if ( isDeprecated ) {
/* eslint-disable no-console */
console.warn.mockReset();
console.error.mockReset();
/* eslint-enable no-console */
}
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 );
try {
expect(
blocksActualNormalized
).toEqual( blocksExpected );
} catch ( err ) {
throw new Error( format(
"File '%s.json' does not match expected value:\n\n%s",
f,
err.message
) );
}
// `serialize` doesn't have a trailing newline, but the fixture
// files should.
const serializedActual = serialize( blocksActual ) + '\n';
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'
);
}
}
try {
expect( serializedActual ).toEqual( serializedExpected );
} catch ( err ) {
throw new Error( format(
"File '%s.serialized.html' does not match expected value:\n\n%s",
f,
err.message
) );
}
} );
} );
it( 'should be present for each block', () => {
const errors = [];
getBlockTypes()
.map( ( block ) => block.name )
// We don't want tests for each oembed provider, which all have the same
// `save` functions and attributes.
// The `core/template` is not worth testing here because it's never saved, it's covered better in e2e tests.
.filter( ( name ) => name.indexOf( 'core-embed' ) !== 0 && name !== 'core/template' )
.forEach( ( name ) => {
const nameToFilename = name.replace( /\//g, '__' );
const foundFixtures = fileBasenames
.filter( ( basename ) => (
basename === nameToFilename ||
startsWith( basename, nameToFilename + '__' )
) )
.map( ( basename ) => {
// The file that contains the input HTML for this test.
const inputFilename = basename + '.html';
// The parser output for this test. For missing files,
// JSON.parse( null ) === null.
const parserOutput = JSON.parse(
readFixtureFile( basename + '.json' )
);
// The name of the first block that this fixture file
// contains (if any).
const firstBlock = get( parserOutput, [ '0', 'name' ], null );
return {
filename: inputFilename,
parserOutput,
firstBlock,
};
} )
.filter( ( fixture ) => fixture.parserOutput !== null );
if ( ! foundFixtures.length ) {
errors.push( format(
"Expected a fixture file called '%s.html' or '%s__*.html'.",
nameToFilename,
nameToFilename
) );
}
foundFixtures.forEach( ( fixture ) => {
if ( name !== fixture.firstBlock ) {
errors.push( format(
"Expected fixture file '%s' to test the '%s' block.",
fixture.filename,
name
) );
}
} );
} );
if ( errors.length ) {
throw new Error(
'Problem(s) with fixture files:\n\n' + errors.join( '\n' )
);
}
} );
} );