-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
block-directory-add.test.js
221 lines (194 loc) · 5.34 KB
/
block-directory-add.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
/**
* WordPress dependencies
*/
import {
createNewPost,
searchForBlock,
setUpResponseMocking,
getEditedPostContent,
createJSONResponse,
} from '@wordpress/e2e-test-utils';
/**
* Internal dependencies
*/
import { useExperimentalFeatures } from '../../experimental-features';
// Urls to mock
const SEARCH_URLS = [
'/__experimental/block-directory/search',
`rest_route=${ encodeURIComponent(
'/__experimental/block-directory/search'
) }`,
];
const INSTALL_URLS = [
'/__experimental/plugins',
`rest_route=${ encodeURIComponent( '/__experimental/plugins' ) }`,
];
// Example Blocks
const MOCK_BLOCK1 = {
name: 'block-directory-test-block/main-block',
title: 'Block Directory Test Block',
description: 'This plugin is useful for the block.',
id: 'block-directory-test-block',
rating: 0,
rating_count: 0,
active_installs: 0,
author_block_rating: 0,
author_block_count: 1,
author: 'No Author',
icon: 'block-default',
assets: [
'fake_url.com/block.js', // we will mock this
],
humanized_updated: '5 months ago',
};
const MOCK_INSTALLED_BLOCK_PLUGIN_DETAILS = {
plugin: 'block-directory-test-block',
status: 'active',
name: 'Block Directory',
plugin_uri: '',
author: 'No Author',
author_uri: '',
description: {
raw: 'This plugin is useful for the block.',
rendered: 'This plugin is useful for the block.',
},
version: '1.0',
network_only: false,
requires_wp: '',
requires_php: '',
text_domain: 'block-directory-test-block',
};
const MOCK_BLOCK2 = {
...MOCK_BLOCK1,
name: 'block-directory-test-block/secondary-block',
title: 'Block Directory Test Block - Pt Deux',
id: 'block-directory-test-secondary-block',
};
// Block that will be registered
const block = `( function() {
var registerBlockType = wp.blocks.registerBlockType;
var el = wp.element.createElement;
registerBlockType( '${ MOCK_BLOCK1.name }', {
title: 'Test Block for Block Directory',
icon: 'hammer',
category: 'text',
attributes: {},
edit: function( props ) {
return el( 'p', null, 'Test Copy' );
},
save: function() {
return null;
},
} );
} )();`;
const MOCK_OPTIONS = {
namespace: '__experimental',
methods: [ 'GET' ],
endpoints: [
{
methods: [ 'GET' ],
args: {},
},
],
schema: {
$schema: 'http://json-schema.org/draft-04/schema#',
title: 'block-directory-item',
type: 'object',
properties: {},
},
};
const MOCK_OPTIONS_RESPONSE = {
match: ( request ) =>
matchUrl( request.url(), SEARCH_URLS ) &&
request.method() === 'OPTIONS',
onRequestMatch: async ( request ) => {
const response = {
content: 'application/json',
body: JSON.stringify( MOCK_OPTIONS ),
headers: {
Allow: 'GET',
},
};
return request.respond( response );
},
};
const MOCK_EMPTY_RESPONSES = [
MOCK_OPTIONS_RESPONSE,
{
match: ( request ) => matchUrl( request.url(), SEARCH_URLS ),
onRequestMatch: createJSONResponse( [] ),
},
];
const MOCK_BLOCKS_RESPONSES = [
MOCK_OPTIONS_RESPONSE,
{
// Mock response for search with the block
match: ( request ) => matchUrl( request.url(), SEARCH_URLS ),
onRequestMatch: createJSONResponse( [ MOCK_BLOCK1, MOCK_BLOCK2 ] ),
},
{
// Mock response for install
match: ( request ) => matchUrl( request.url(), INSTALL_URLS ),
onRequestMatch: createJSONResponse(
MOCK_INSTALLED_BLOCK_PLUGIN_DETAILS
),
},
{
// Mock the response for the js asset once it gets injected
match: ( request ) => request.url().includes( MOCK_BLOCK1.assets[ 0 ] ),
onRequestMatch: createResponse(
Buffer.from( block, 'utf8' ),
'application/javascript; charset=utf-8'
),
},
];
function getResponseObject( obj, contentType ) {
return {
status: 200,
contentType,
body: obj,
};
}
function createResponse( mockResponse, contentType ) {
return async ( request ) =>
request.respond( getResponseObject( mockResponse, contentType ) );
}
const matchUrl = ( reqUrl, urls ) => {
return urls.some( ( el ) => reqUrl.indexOf( el ) >= 0 );
};
describe( 'adding blocks from block directory', () => {
useExperimentalFeatures( [ '#gutenberg-block-directory' ] );
beforeEach( async () => {
await createNewPost();
} );
it( 'Should show an empty state when no plugin is found.', async () => {
// Be super weird so there won't be a matching block installed
const impossibleBlockName = '@#$@@Dsdsdfw2#$@';
// Return an empty list of plugins
await setUpResponseMocking( MOCK_EMPTY_RESPONSES );
// Search for the block via the inserter
await searchForBlock( impossibleBlockName );
const selectorContent = await page.evaluate(
() =>
document.querySelector( '.block-editor-inserter__block-list' )
.innerHTML
);
expect( selectorContent ).toContain( 'has-no-results' );
} );
it( 'Should be able to add (the first) block.', async () => {
// Setup our mocks
await setUpResponseMocking( MOCK_BLOCKS_RESPONSES );
// Search for the block via the inserter
await searchForBlock( MOCK_BLOCK1.title );
// Grab the first block in the list -> Needs to be the first one, the mock response expects it.
const addBtn = await page.waitForSelector(
'.block-directory-downloadable-blocks-list li:first-child button'
);
// Add the block
await addBtn.click();
// Delay to let block script load
await new Promise( ( resolve ) => setTimeout( resolve, 100 ) );
// The block will auto select and get added, make sure we see it in the content
expect( await getEditedPostContent() ).toMatchSnapshot();
} );
} );