-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
block-visibility.spec.js
258 lines (230 loc) · 6.59 KB
/
block-visibility.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
251
252
253
254
255
256
257
258
/**
* WordPress dependencies
*/
const { test, expect } = require( '@wordpress/e2e-test-utils-playwright' );
test.use( {
BlockVisibilityUtils: async ( { page }, use ) => {
await use( new BlockVisibilityUtils( { page } ) );
},
} );
test.describe( 'Block Visibility', () => {
test.beforeEach( async ( { admin } ) => {
await admin.createNewPost();
} );
test.afterEach( async ( { page } ) => {
// Re-enable all blocks.
await page.evaluate( () => {
const blockTypes = window.wp.data
.select( 'core/blocks' )
.getBlockTypes();
window.wp.data
.dispatch( 'core/edit-post' )
.showBlockTypes( blockTypes );
} );
} );
test( 'should hide/show the block', async ( {
page,
BlockVisibilityUtils,
} ) => {
// Hide heading block.
await BlockVisibilityUtils.openBlockVisibilityManager();
await page
.getByRole( 'region', { name: 'Available block types' } )
.getByRole( 'checkbox', { name: 'Heading' } )
.uncheck();
await page
.getByRole( 'dialog', {
name: 'Preferences',
} )
.getByRole( 'button', { name: 'Close' } )
.click();
await page
.getByRole( 'button', { name: 'Block Inserter', exact: true } )
.click();
await page
.getByRole( 'region', { name: 'Block Library' } )
.getByRole( 'searchbox', {
name: 'Search',
} )
.fill( 'Heading' );
await expect(
page
.getByRole( 'tabpanel', { name: 'Blocks' } )
.getByRole( 'option', { name: 'Heading' } ),
'Heading block should not be visible'
).toBeHidden();
await page
.getByRole( 'button', { name: 'Block Inserter', exact: true } )
.click();
// Show heading block again.
await BlockVisibilityUtils.openBlockVisibilityManager();
await expect(
page
.getByRole( 'region', { name: 'Available block types' } )
.getByRole( 'checkbox', { name: 'Heading' } ),
'Heading block should be unchecked'
).not.toBeChecked();
await page
.getByRole( 'region', { name: 'Available block types' } )
.getByRole( 'checkbox', { name: 'Heading' } )
.check();
await page
.getByRole( 'dialog', {
name: 'Preferences',
} )
.getByRole( 'button', { name: 'Close' } )
.click();
await page
.getByRole( 'button', { name: 'Block Inserter', exact: true } )
.click();
await page
.getByRole( 'region', { name: 'Block Library' } )
.getByRole( 'searchbox', {
name: 'Search',
} )
.fill( 'Heading' );
await expect(
page
.getByRole( 'tabpanel', { name: 'Blocks' } )
.getByRole( 'option', { name: 'Heading' } ),
'Heading block should be visible'
).toBeVisible();
} );
test( 'should hide/show all blocks in a category at once', async ( {
page,
BlockVisibilityUtils,
} ) => {
// Hide Media category blocks.
await BlockVisibilityUtils.openBlockVisibilityManager();
await page
.getByRole( 'region', { name: 'Available block types' } )
.getByRole( 'checkbox', { name: 'Media', exact: true } )
.uncheck();
await page
.getByRole( 'dialog', {
name: 'Preferences',
} )
.getByRole( 'button', { name: 'Close' } )
.click();
await page
.getByRole( 'button', { name: 'Block Inserter', exact: true } )
.click();
await expect(
page
.getByRole( 'tabpanel', { name: 'Blocks' } )
.getByRole( 'heading', { name: 'Media' } ),
'Media category should not be visible'
).toBeHidden();
await page
.getByRole( 'button', { name: 'Block Inserter', exact: true } )
.click();
// Show Media category blocks again.
await BlockVisibilityUtils.openBlockVisibilityManager();
await expect(
page
.getByRole( 'region', { name: 'Available block types' } )
.getByRole( 'checkbox', { name: 'Media', exact: true } ),
'Media category should be unchecked'
).not.toBeChecked();
await page
.getByRole( 'region', { name: 'Available block types' } )
.getByRole( 'checkbox', { name: 'Media', exact: true } )
.check();
await page
.getByRole( 'dialog', {
name: 'Preferences',
} )
.getByRole( 'button', { name: 'Close' } )
.click();
await page
.getByRole( 'button', { name: 'Block Inserter', exact: true } )
.click();
await expect(
page
.getByRole( 'tabpanel', { name: 'Blocks' } )
.getByRole( 'heading', { name: 'Media' } ),
'Media category should be visible'
).toBeVisible();
} );
test( 'should search for blocks by keyword', async ( {
page,
BlockVisibilityUtils,
} ) => {
await BlockVisibilityUtils.openBlockVisibilityManager();
await page
.getByRole( 'searchbox', {
name: 'Search for a block',
} )
.fill( 'verse' );
const blockCategories = page
.getByRole( 'region', {
name: 'Available block types',
} )
.getByRole( 'group' );
await expect(
blockCategories,
'Only one category should be visible'
).toHaveCount( 1 );
await expect(
blockCategories.first().getByRole( 'checkbox', { name: 'Text' } ),
'Text category should be visible'
).toBeVisible();
const textCategoryBlocksList = blockCategories
.first()
.getByRole( 'list' );
await expect(
textCategoryBlocksList.getByRole( 'checkbox' ),
'Only one block should be visible'
).toHaveCount( 1 );
await expect(
textCategoryBlocksList.getByRole( 'checkbox', { name: 'Verse' } ),
'Verse block should be visible'
).toBeVisible();
} );
test( 'should reset hidden blocks', async ( {
page,
BlockVisibilityUtils,
} ) => {
await BlockVisibilityUtils.openBlockVisibilityManager();
// Hide Heading and List blocks.
await page
.getByRole( 'region', { name: 'Available block types' } )
.getByRole( 'checkbox', { name: 'Heading' } )
.uncheck();
await page
.getByRole( 'region', { name: 'Available block types' } )
.getByRole( 'checkbox', { name: 'List', exact: true } )
.uncheck();
// Reset hidden blocks.
await page.getByRole( 'button', { name: 'Reset' } ).click();
await expect(
page
.getByRole( 'region', { name: 'Available block types' } )
.getByRole( 'checkbox', { name: 'Heading' } ),
'Heading block should be checked'
).toBeChecked();
await expect(
page
.getByRole( 'region', { name: 'Available block types' } )
.getByRole( 'checkbox', { name: 'List', exact: true } ),
'List block should be checked'
).toBeChecked();
} );
} );
class BlockVisibilityUtils {
constructor( { page, admin, requestUtils } ) {
this.page = page;
this.admin = admin;
this.requestUtils = requestUtils;
}
async openBlockVisibilityManager() {
await this.page
.getByRole( 'region', { name: 'Editor top bar' } )
.getByRole( 'button', { name: 'Options' } )
.click();
await this.page
.getByRole( 'menuitem', { name: 'Preferences' } )
.click();
await this.page.getByRole( 'tab', { name: 'Blocks' } ).click();
}
}