-
Notifications
You must be signed in to change notification settings - Fork 58
/
jetpack-editor-setup.js
301 lines (268 loc) · 8.54 KB
/
jetpack-editor-setup.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
296
297
298
299
300
301
/**
* WordPress dependencies
*/
import {
getBlockTypes,
getBlockVariations,
unregisterBlockType,
unregisterBlockVariation,
} from '@wordpress/blocks';
import { store as preferencesStore } from '@wordpress/preferences';
import { select } from '@wordpress/data';
import { registerCoreBlocks } from '@wordpress/block-library';
import { applyFilters, removeAllFilters } from '@wordpress/hooks';
import { initializeEditor } from 'test/helpers';
/**
* Internal dependencies
*/
import getJetpackData, {
JETPACK_DATA_PATH,
} from '../../jetpack/projects/js-packages/shared-extension-utils/src/get-jetpack-data';
import setupJetpackEditorHooks, {
registerJetpackBlocks,
registerJetpackEmbedVariations,
setupJetpackEditor,
} from '../jetpack-editor-setup';
const defaultJetpackData = { blogId: 1, isJetpackActive: true };
const defaultProps = {
capabilities: {
contactInfoBlock: true,
facebookEmbed: true,
instagramEmbed: true,
loomEmbed: true,
smartframeEmbed: true,
},
};
const jetpackBlocks = [
'jetpack/contact-info',
'jetpack/paywall',
'jetpack/tiled-gallery',
'videopress/video',
];
const jetpackEmbedVariations = [
'facebook',
'instagram',
'loom',
'smartframe',
];
/**
* Registers the Jetpack blocks using a sandbox registry for the modules via `jest.isolateModules`.
* Module isolation is required in this case because the Jetpack blocks are registered upon module importation.
*
* @param {Object} props
* @return {Object} Blocks API registered in the sandbox registry.
*/
const registerJetpackBlocksIsolated = ( props ) => {
let blocksAPI;
jest.isolateModules( () => {
registerJetpackBlocks( props );
blocksAPI = require( '@wordpress/blocks' );
} );
return blocksAPI;
};
describe( 'Jetpack blocks', () => {
afterEach( () => {
// Reset Jetpack data
delete global.window[ JETPACK_DATA_PATH ];
} );
it( 'should set up Jetpack data', () => {
const expectedJetpackData = {
available_blocks: {
'contact-info': { available: true },
paywall: { available: true },
'tiled-gallery': { available: true },
'videopress/video': { available: true },
},
jetpack: { is_active: true },
siteFragment: null,
tracksUserData: null,
wpcomBlogId: 1,
};
setupJetpackEditor( defaultJetpackData );
expect( getJetpackData() ).toEqual( expectedJetpackData );
} );
it( 'should register Jetpack blocks if Jetpack is active', () => {
setupJetpackEditor( defaultJetpackData );
const blocksAPI = registerJetpackBlocksIsolated( defaultProps );
expect( console ).toHaveLoggedWith(
'Block jetpack/contact-info registered.'
);
expect( console ).toHaveLoggedWith(
'Block jetpack/paywall registered.'
);
expect( console ).toHaveLoggedWith(
'Block jetpack/tiled-gallery registered.'
);
expect( console ).toHaveLoggedWith(
'Block videopress/video registered.'
);
const registeredBlocks = blocksAPI
.getBlockTypes()
.map( ( block ) => block.name );
expect( registeredBlocks ).toEqual(
expect.arrayContaining( jetpackBlocks )
);
} );
it( 'should not register Jetpack blocks if Jetpack is not active', () => {
setupJetpackEditor( { ...defaultJetpackData, isJetpackActive: false } );
const blocksAPI = registerJetpackBlocksIsolated( defaultProps );
const registeredBlocks = blocksAPI
.getBlockTypes()
.map( ( block ) => block.name );
expect( registeredBlocks ).toEqual( [] );
} );
it( 'should hide Jetpack blocks by capabilities', () => {
setupJetpackEditor( defaultJetpackData );
registerJetpackBlocksIsolated( {
capabilities: {
contactInfoBlock: false,
paywallBlock: true,
tiledGalleryBlock: true,
videoPressBlock: true,
},
} );
expect( console ).toHaveLoggedWith(
'Block jetpack/contact-info registered.'
);
expect( console ).toHaveLoggedWith(
'Block jetpack/paywall registered.'
);
expect( console ).toHaveLoggedWith(
'Block jetpack/tiled-gallery registered.'
);
expect( console ).toHaveLoggedWith(
'Block videopress/video registered.'
);
const hiddenBlockTypes = select( preferencesStore ).get(
'core',
'hiddenBlockTypes'
);
expect( hiddenBlockTypes ).toEqual( [
'jetpack/paywall',
'jetpack/contact-info',
] );
} );
it( "should not register Jetpack blocks if 'onlyCoreBlocks' capbility is on", () => {
setupJetpackEditor( defaultJetpackData );
const blocksAPI = registerJetpackBlocksIsolated( {
capabilities: {
...defaultProps.capabilities,
onlyCoreBlocks: true,
},
} );
const registeredBlocks = blocksAPI
.getBlockTypes()
.map( ( block ) => block.name );
expect( registeredBlocks ).toEqual( [] );
} );
it( 'sets `null` value in unsupported block details of Missing block when only core blocks are available', async () => {
setupJetpackEditorHooks();
await initializeEditor( {
capabilities: {
// Force only core blocks
onlyCoreBlocks: true,
},
} );
const defaultText = 'default-text';
const tryOverrideString = ( blockName ) =>
applyFilters(
'native.missing_block_detail',
defaultText,
blockName
);
// The following Jetpack blocks are not available, hence the string won't be overridden.
expect( tryOverrideString( 'contact-info' ) ).toBe( defaultText );
expect( tryOverrideString( 'video' ) ).toBe( defaultText );
expect( tryOverrideString( 'non-existing-jetpack-block' ) ).toBe(
defaultText
);
// The following blocks are available, hence the string will be overridden with `null` value.
expect( tryOverrideString( 'jetpack/contact-info' ) ).toBeNull();
expect( tryOverrideString( 'videopress/video' ) ).toBeNull();
} );
describe( 'Jetpack embed variations', () => {
afterEach( () => {
// Clean up embed variations
getBlockVariations( 'core/embed' ).forEach( ( variation ) => {
unregisterBlockVariation( 'core/embed', variation.name );
} );
// Embed variations can use the register block type WP hook to reactivate
// already registered variations, so we need to remove all hooks after the tests.
removeAllFilters( 'blocks.registerBlockType' );
// Clean up registered blocks
getBlockTypes().forEach( ( block ) => {
unregisterBlockType( block.name );
} );
} );
it( 'should not register Jetpack embed variations if Jetpack is not active', () => {
setupJetpackEditor( {
...defaultJetpackData,
isJetpackActive: false,
} );
registerJetpackEmbedVariations( defaultProps );
// Embed variations require the Embed block to be registered,
// so we need to register the core blocks to include it.
registerCoreBlocks();
const embedVariations = getBlockVariations(
'core/embed',
'inserter'
).map( ( block ) => block.name );
jetpackEmbedVariations.forEach( ( variation ) =>
expect( embedVariations ).not.toContain( variation )
);
} );
it( 'should not register Jetpack embed variations if capabilities are falsey', () => {
setupJetpackEditor( defaultJetpackData );
registerJetpackEmbedVariations( {
capabilities: {
facebookEmbed: false,
instagramEmbed: null,
loomEmbed: undefined,
},
} );
// Embed variations require the Embed block to be registered,
// so we need to register the core blocks to include it.
registerCoreBlocks();
const embedVariations = getBlockVariations(
'core/embed',
'inserter'
).map( ( block ) => block.name );
jetpackEmbedVariations.forEach( ( variation ) =>
expect( embedVariations ).not.toContain( variation )
);
} );
it( "should not register Jetpack embed variations if 'onlyCoreBlocks' capbility is on", () => {
setupJetpackEditor( defaultJetpackData );
registerJetpackEmbedVariations( {
capabilities: {
...defaultProps.capabilities,
onlyCoreBlocks: true,
},
} );
// Embed variations require the Embed block to be registered,
// so we need to register the core blocks to include it.
registerCoreBlocks();
const embedVariations = getBlockVariations(
'core/embed',
'inserter'
).map( ( block ) => block.name );
jetpackEmbedVariations.forEach( ( variation ) =>
expect( embedVariations ).not.toContain( variation )
);
} );
it( 'should register Jetpack embed variations if capabilities are true', () => {
setupJetpackEditor( defaultJetpackData );
registerJetpackEmbedVariations( defaultProps );
// Embed variations require the Embed block to be registered,
// so we need to register the core blocks to include it.
registerCoreBlocks();
const embedVariations = getBlockVariations(
'core/embed',
'inserter'
).map( ( block ) => block.name );
expect( embedVariations ).toEqual(
expect.arrayContaining( jetpackEmbedVariations )
);
} );
} );
} );