-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
full-content.test.js
295 lines (269 loc) · 7.98 KB
/
full-content.test.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/**
* External dependencies
*/
import glob from 'fast-glob';
import { fromPairs, omit, 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,
__experimentalRegisterExperimentalCoreBlocks,
} from '@wordpress/block-library';
import prettierConfig from '@wordpress/prettier-config';
/**
* Internal dependencies
*/
import {
blockNameToFixtureBasename,
getAvailableBlockFixturesBasenames,
getBlockFixtureHTML,
getBlockFixtureJSON,
getBlockFixtureParsedJSON,
getBlockFixtureSerializedHTML,
writeBlockFixtureParsedJSON,
writeBlockFixtureJSON,
writeBlockFixtureSerializedHTML,
} from '../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. Validation issues
// add too much noise so they get removed as well.
block = JSON.parse(
JSON.stringify( omit( block, 'validationIssues' ) )
);
// 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( async () => {
const blockMetadataFiles = await glob(
'packages/block-library/src/*/block.json'
);
const blockDefinitions = fromPairs(
blockMetadataFiles.map( ( file ) => {
const { name, ...metadata } = require( file );
return [ name, metadata ];
} )
);
unstable__bootstrapServerSideBlockDefinitions( blockDefinitions );
// Load all hooks that modify blocks
require( '../../../packages/editor/src/hooks' );
registerCoreBlocks();
if ( process.env.GUTENBERG_PHASE === 2 ) {
__experimentalRegisterExperimentalCoreBlocks( {
enableFSEBlocks: true,
} );
}
} );
let spacer = 4;
if ( prettierConfig?.useTabs ) {
spacer = '\t';
} else if ( prettierConfig?.tabWidth ) {
spacer = prettierConfig?.tabWidth;
}
blockBasenames.forEach( ( basename ) => {
// eslint-disable-next-line jest/valid-title
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, spacer ) + '\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
)
);
}
const blocksActual = parse( htmlFixtureContent );
// Block validation may log errors during deprecation migration,
// unless explicitly handled from a valid block via isEligible.
// Match on basename for deprecated blocks fixtures to allow.
const isDeprecated = /__deprecated([-_]|$)/.test( basename );
if ( isDeprecated ) {
/* eslint-disable no-console */
console.warn.mockReset();
console.error.mockReset();
console.info.mockReset();
/* eslint-enable no-console */
}
const blocksActualNormalized = normalizeParsedBlocks(
blocksActual
);
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, spacer ) +
'\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
)
);
}
// `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( '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 ) => ! [ 'core/embed', 'core/template' ].includes( name )
)
.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 );
// 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 );
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' )
);
}
} );
} );