Skip to content

Commit fee51ca

Browse files
committed
Blocks: Isolate registration state in tests
1 parent 3558a0d commit fee51ca

2 files changed

Lines changed: 92 additions & 87 deletions

File tree

‎packages/blocks/src/api/test/registration.jsx‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ describe( 'blocks', () => {
5353
unlock( select( blocksStore ) ).getUnprocessedBlockTypes()
5454
);
5555
dispatch( blocksStore ).removeBlockTypes( registeredNames );
56+
Object.keys( select( blocksStore ).getCollections() ).forEach(
57+
unregisterBlockCollection
58+
);
5659
setFreeformContentHandlerName( undefined );
5760
setUnregisteredTypeHandlerName( undefined );
5861
setDefaultBlockName( undefined );

‎test/integration/blocks-raw-handling.jsdom.test.js‎

Lines changed: 89 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
import fs from 'node:fs';
22
import path from 'node:path';
33
import { fileURLToPath } from 'node:url';
4-
import { beforeAll, describe, expect, it, vi } from 'vitest';
4+
import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest';
55
import {
66
createBlock,
7+
getBlockTypes,
78
getBlockContent,
89
pasteHandler,
910
rawHandler,
1011
registerBlockType,
1112
serialize,
13+
unregisterBlockType,
1214
} from '@wordpress/blocks';
1315
import { registerCoreBlocks } from '@wordpress/block-library';
1416
import '../../packages/editor/src/hooks';
@@ -23,99 +25,99 @@ function readFile( filePath ) {
2325
: '';
2426
}
2527

26-
describe( 'Blocks raw handling', () => {
27-
beforeAll( () => {
28-
registerCoreBlocks();
29-
registerBlockType( 'test/gallery', {
30-
apiVersion: 3,
31-
title: 'Test Gallery',
32-
category: 'text',
33-
attributes: {
34-
ids: {
35-
type: 'array',
36-
default: [],
37-
},
28+
beforeAll( () => {
29+
registerCoreBlocks();
30+
registerBlockType( 'test/gallery', {
31+
apiVersion: 3,
32+
title: 'Test Gallery',
33+
category: 'text',
34+
attributes: {
35+
ids: {
36+
type: 'array',
37+
default: [],
3838
},
39-
transforms: {
40-
from: [
41-
{
42-
type: 'shortcode',
43-
tag: 'gallery',
44-
isMatch( { named: { ids } } ) {
45-
return ids.indexOf( 42 ) > -1;
46-
},
47-
attributes: {
48-
ids: {
49-
type: 'array',
50-
shortcode: ( { named: { ids } } ) =>
51-
ids
52-
.split( ',' )
53-
.map( ( id ) => parseInt( id, 10 ) ),
54-
},
55-
},
56-
priority: 9,
39+
},
40+
transforms: {
41+
from: [
42+
{
43+
type: 'shortcode',
44+
tag: 'gallery',
45+
isMatch( { named: { ids } } ) {
46+
return ids.indexOf( 42 ) > -1;
5747
},
58-
],
59-
},
60-
save: () => null,
61-
} );
62-
63-
registerBlockType( 'test/non-inline-block', {
64-
apiVersion: 3,
65-
title: 'Test Non Inline Block',
66-
category: 'text',
67-
supports: {
68-
pasteTextInline: false,
69-
},
70-
transforms: {
71-
from: [
72-
{
73-
type: 'raw',
74-
isMatch: ( node ) => {
75-
return (
76-
'words to live by' === node.textContent.trim()
77-
);
78-
},
79-
transform: () => {
80-
return createBlock( 'core/embed', {
81-
url: 'https://www.youtube.com/watch?v=dQw4w9WgXcQ',
82-
} );
48+
attributes: {
49+
ids: {
50+
type: 'array',
51+
shortcode: ( { named: { ids } } ) =>
52+
ids
53+
.split( ',' )
54+
.map( ( id ) => parseInt( id, 10 ) ),
8355
},
8456
},
85-
],
86-
},
87-
save: () => null,
88-
} );
89-
90-
registerBlockType( 'test/transform-to-multiple-blocks', {
91-
apiVersion: 3,
92-
title: 'Test Transform to Multiple Blocks',
93-
category: 'text',
94-
transforms: {
95-
from: [
96-
{
97-
type: 'raw',
98-
isMatch: ( node ) => {
99-
return node.textContent
100-
.split( ' ' )
101-
.every( ( chunk ) => /^P\S+?/.test( chunk ) );
102-
},
103-
transform: ( node ) => {
104-
return node.textContent
105-
.split( ' ' )
106-
.map( ( chunk ) =>
107-
createBlock( 'core/paragraph', {
108-
content: chunk.substring( 1 ),
109-
} )
110-
);
111-
},
57+
priority: 9,
58+
},
59+
],
60+
},
61+
save: () => null,
62+
} );
63+
64+
registerBlockType( 'test/non-inline-block', {
65+
apiVersion: 3,
66+
title: 'Test Non Inline Block',
67+
category: 'text',
68+
supports: {
69+
pasteTextInline: false,
70+
},
71+
transforms: {
72+
from: [
73+
{
74+
type: 'raw',
75+
isMatch: ( node ) => {
76+
return 'words to live by' === node.textContent.trim();
11277
},
113-
],
114-
},
115-
save: () => null,
116-
} );
78+
transform: () => {
79+
return createBlock( 'core/embed', {
80+
url: 'https://www.youtube.com/watch?v=dQw4w9WgXcQ',
81+
} );
82+
},
83+
},
84+
],
85+
},
86+
save: () => null,
87+
} );
88+
89+
registerBlockType( 'test/transform-to-multiple-blocks', {
90+
apiVersion: 3,
91+
title: 'Test Transform to Multiple Blocks',
92+
category: 'text',
93+
transforms: {
94+
from: [
95+
{
96+
type: 'raw',
97+
isMatch: ( node ) => {
98+
return node.textContent
99+
.split( ' ' )
100+
.every( ( chunk ) => /^P\S+?/.test( chunk ) );
101+
},
102+
transform: ( node ) => {
103+
return node.textContent.split( ' ' ).map( ( chunk ) =>
104+
createBlock( 'core/paragraph', {
105+
content: chunk.substring( 1 ),
106+
} )
107+
);
108+
},
109+
},
110+
],
111+
},
112+
save: () => null,
117113
} );
114+
} );
118115

116+
afterAll( () => {
117+
getBlockTypes().forEach( ( { name } ) => unregisterBlockType( name ) );
118+
} );
119+
120+
describe( 'Blocks raw handling', () => {
119121
it( 'should filter inline content', () => {
120122
const filtered = pasteHandler( {
121123
HTML: '<h2><em>test</em></h2>',

0 commit comments

Comments
 (0)