-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathmulti-entity-editing.test.js
309 lines (273 loc) · 9.47 KB
/
multi-entity-editing.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/**
* WordPress dependencies
*/
import {
insertBlock,
createNewPost,
publishPost,
deleteAllTemplates,
activateTheme,
canvas,
openDocumentSettingsSidebar,
pressKeyWithModifier,
selectBlockByClientId,
visitSiteEditor,
clickSiteEditorMenuItem,
navigateSiteEditorBackToRoot,
openSiteEditorNavigationPanel,
siteEditorNavigateSequence,
} from '@wordpress/e2e-test-utils';
const clickTemplateItem = async ( menus, itemName ) => {
await openSiteEditorNavigationPanel();
await navigateSiteEditorBackToRoot();
await siteEditorNavigateSequence( menus );
await clickSiteEditorMenuItem( itemName );
};
const createTemplatePart = async (
templatePartName = 'test-template-part',
isNested = false
) => {
// Create new template part.
await insertBlock( 'Template Part' );
const [ createNewButton ] = await page.$x(
'//button[contains(text(), "New template part")]'
);
await createNewButton.click();
await page.waitForSelector(
isNested
? '.wp-block-template-part .wp-block-template-part.block-editor-block-list__layout'
: '.wp-block-template-part.block-editor-block-list__layout'
);
await openDocumentSettingsSidebar();
const advancedPanelXPath = `//div[contains(@class,"interface-interface-skeleton__sidebar")]//button[@class="components-button components-panel__body-toggle"][contains(text(),"Advanced")]`;
const advancedPanel = await page.waitForXPath( advancedPanelXPath );
await advancedPanel.click();
const nameInputXPath = `${ advancedPanelXPath }/ancestor::div[contains(@class, "components-panel__body")]//div[contains(@class,"components-base-control__field")]//label[contains(text(), "Title")]/following-sibling::input`;
const nameInput = await page.waitForXPath( nameInputXPath );
await nameInput.click();
// Select all of the text in the title field.
await pressKeyWithModifier( 'primary', 'a' );
// Give the reusable block a title
await page.keyboard.type( templatePartName );
};
const editTemplatePart = async ( textToAdd, isNested = false ) => {
await page.click(
`${
isNested
? '.wp-block-template-part .wp-block-template-part'
: '.wp-block-template-part'
} .block-editor-button-block-appender`
);
await page.click( '.editor-block-list-item-paragraph' );
for ( const text of textToAdd ) {
await page.keyboard.type( text );
await page.keyboard.press( 'Enter' );
}
};
const saveAllEntities = async () => {
if ( await openEntitySavePanel() ) {
await page.click( 'button.editor-entities-saved-states__save-button' );
}
};
const openEntitySavePanel = async () => {
// Open the entity save panel if it is not already open.
try {
await page.waitForSelector( '.entities-saved-states__panel', {
timeout: 500,
} );
} catch {
try {
await page.click(
'.edit-site-save-button__button[aria-disabled=false]',
{ timeout: 500 }
);
} catch {
try {
await page.click(
'.editor-post-publish-button__button.has-changes-dot',
{ timeout: 500 }
);
} catch {
return false; // Not dirty because the button is disabled.
}
}
await page.waitForSelector( '.entities-saved-states__panel' );
}
// If we made it this far, the panel is opened.
return true;
};
const isEntityDirty = async ( name ) => {
const isOpen = await openEntitySavePanel();
if ( ! isOpen ) {
return false;
}
try {
await page.waitForXPath(
`//label[@class="components-checkbox-control__label"]//strong[contains(text(),"${ name }")]`,
{ timeout: 1000 }
);
return true;
} catch {}
return false;
};
const removeErrorMocks = () => {
// TODO: Add back console mocks when
// https://github.com/WordPress/gutenberg/issues/17355 is fixed.
/* eslint-disable no-console */
console.warn.mockReset();
console.error.mockReset();
console.info.mockReset();
/* eslint-enable no-console */
};
describe( 'Multi-entity editor states', () => {
beforeAll( async () => {
await activateTheme( 'emptytheme' );
await deleteAllTemplates( 'wp_template' );
await deleteAllTemplates( 'wp_template_part' );
} );
afterAll( async () => {
await activateTheme( 'twentytwentyone' );
} );
it( 'should not display any dirty entities when loading the site editor', async () => {
await visitSiteEditor();
expect( await openEntitySavePanel() ).toBe( false );
} );
// Skip reason: This should be rewritten to use other methods to switching to different templates.
it.skip( 'should not dirty an entity by switching to it in the template dropdown', async () => {
await visitSiteEditor( {
postId: 'emptytheme//header',
postType: 'wp_template_part',
} );
await page.waitForFunction( () =>
Array.from( window.frames ).find(
( { name } ) => name === 'editor-canvas'
)
);
// Wait for blocks to load.
await canvas().waitForSelector( '.wp-block' );
expect( await isEntityDirty( 'header' ) ).toBe( false );
expect( await isEntityDirty( 'Index' ) ).toBe( false );
// Switch back and make sure it is still clean.
await clickTemplateItem( 'Templates', 'Index' );
await page.waitForFunction( () =>
Array.from( window.frames ).find(
( { name } ) => name === 'editor-canvas'
)
);
await canvas().waitForSelector( '.wp-block' );
expect( await isEntityDirty( 'header' ) ).toBe( false );
expect( await isEntityDirty( 'Index' ) ).toBe( false );
removeErrorMocks();
} );
describe( 'Multi-entity edit', () => {
const templatePartName = 'Test Template Part Name Edit';
const nestedTPName = 'Test Nested Template Part Name Edit';
const templateName = 'Custom Template';
beforeAll( async () => {
await deleteAllTemplates( 'wp_template' );
await deleteAllTemplates( 'wp_template_part' );
await createNewPost( {
postType: 'wp_template',
title: templateName,
} );
await publishPost();
await createTemplatePart( templatePartName );
await editTemplatePart( [
'Default template part test text.',
'Second paragraph test.',
] );
await createTemplatePart( nestedTPName, true );
await editTemplatePart(
[ 'Nested Template Part Text.', 'Second Nested test.' ],
true
);
await saveAllEntities();
await visitSiteEditor();
// Wait for site editor to load.
await canvas().waitForSelector(
'.wp-block-template-part.block-editor-block-list__layout'
);
// Our custom template shows up in the "Templates > General" menu; let's use it.
await clickTemplateItem(
[ 'Templates', 'General templates' ],
templateName
);
await page.waitForXPath(
`//h1[contains(@class, "edit-site-document-actions__title") and contains(text(), '${ templateName }')]`
);
removeErrorMocks();
} );
const saveAndWaitResponse = async () => {
await Promise.all( [
saveAllEntities(),
// Wait for the save request and the subsequent query to be
// fulfilled - both are requests made to /index.php route.
// Without that, clicked elements can lose focus sometimes
// when the response is received.
page.waitForResponse( ( response ) => {
return (
response.url().includes( 'index.php' ) &&
response.request().method() === 'POST'
);
} ),
page.waitForResponse( ( response ) => {
return (
response.url().includes( 'index.php' ) &&
response.request().method() === 'GET'
);
} ),
] );
removeErrorMocks();
};
it.skip( 'should only dirty the parent entity when editing the parent', async () => {
// Clear selection so that the block is not added to the template part.
await insertBlock( 'Paragraph' );
// Add changes to the main parent entity.
await page.keyboard.type( 'Test.' );
expect( await isEntityDirty( templateName ) ).toBe( true );
expect( await isEntityDirty( templatePartName ) ).toBe( false );
expect( await isEntityDirty( nestedTPName ) ).toBe( false );
await saveAndWaitResponse();
} );
it.skip( 'should only dirty the child when editing the child', async () => {
// Select parent TP to unlock selecting content.
await canvas().click( '.wp-block-template-part' );
await canvas().click(
'.wp-block-template-part .wp-block[data-type="core/paragraph"]'
);
await page.keyboard.type( 'Some more test words!' );
expect( await isEntityDirty( templateName ) ).toBe( false );
expect( await isEntityDirty( templatePartName ) ).toBe( true );
expect( await isEntityDirty( nestedTPName ) ).toBe( false );
await saveAndWaitResponse();
} );
it.skip( 'should only dirty the nested entity when editing the nested entity', async () => {
// Select parent TP to unlock selecting child.
await canvas().click( '.wp-block-template-part' );
// Select child TP to unlock selecting content.
await canvas().click(
'.wp-block-template-part .wp-block-template-part'
);
await canvas().click(
'.wp-block-template-part .wp-block-template-part .wp-block[data-type="core/paragraph"]'
);
await page.keyboard.type( 'Nested test words!' );
expect( await isEntityDirty( templateName ) ).toBe( false );
expect( await isEntityDirty( templatePartName ) ).toBe( false );
expect( await isEntityDirty( nestedTPName ) ).toBe( true );
await saveAndWaitResponse();
} );
it.skip( 'should not allow selecting template part content without parent selected', async () => {
// Unselect blocks.
await selectBlockByClientId();
// Try to select a child block of a template part.
await canvas().click(
'.wp-block-template-part .wp-block-template-part .wp-block[data-type="core/paragraph"]'
);
const selectedBlock = await page.evaluate( () => {
return wp.data.select( 'core/block-editor' ).getSelectedBlock();
} );
expect( selectedBlock?.name ).toBe( 'core/template-part' );
} );
} );
} );