-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathzoom-out.spec.js
294 lines (247 loc) · 10.9 KB
/
zoom-out.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
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
/**
* WordPress dependencies
*/
const { test, expect } = require( '@wordpress/e2e-test-utils-playwright' );
const EDITOR_ZOOM_OUT_CONTENT = `
<!-- wp:group {"tagName":"main","layout":{"type":"constrained"}} -->
<main class="wp-block-group"><!-- wp:group {"style":{"spacing":{"padding":{"top":"0","bottom":"0","left":"0","right":"0"}},"dimensions":{"minHeight":"100vh"}},"backgroundColor":"base-2","layout":{"type":"flex","orientation":"vertical","verticalAlignment":"space-between"}} -->
<div class="wp-block-group has-base-2-background-color has-background" style="min-height:100vh;padding-top:0;padding-right:0;padding-bottom:0;padding-left:0"><!-- wp:paragraph -->
<p>First Section Start</p>
<!-- /wp:paragraph -->
<!-- wp:paragraph {"style":{"layout":{"selfStretch":"fit","flexSize":null}}} -->
<p>First Section Center</p>
<!-- /wp:paragraph -->
<!-- wp:paragraph -->
<p>First Section End</p>
<!-- /wp:paragraph --></div>
<!-- /wp:group -->
<!-- wp:group {"style":{"spacing":{"padding":{"top":"0","bottom":"0","left":"0","right":"0"}},"dimensions":{"minHeight":"100vh"}},"backgroundColor":"base","layout":{"type":"flex","orientation":"vertical","verticalAlignment":"space-between"}} -->
<div class="wp-block-group has-base-background-color has-background" style="min-height:100vh;padding-top:0;padding-right:0;padding-bottom:0;padding-left:0"><!-- wp:paragraph -->
<p>Second Section Start</p>
<!-- /wp:paragraph -->
<!-- wp:paragraph {"style":{"layout":{"selfStretch":"fit","flexSize":null}}} -->
<p>Second Section Center</p>
<!-- /wp:paragraph -->
<!-- wp:paragraph -->
<p>Second Section End</p>
<!-- /wp:paragraph --></div>
<!-- /wp:group -->
<!-- wp:group {"style":{"spacing":{"padding":{"top":"0","bottom":"0","left":"0","right":"0"}},"dimensions":{"minHeight":"100vh"}},"backgroundColor":"base-2","layout":{"type":"flex","orientation":"vertical","verticalAlignment":"space-between"}} -->
<div class="wp-block-group has-base-2-background-color has-background" style="min-height:100vh;padding-top:0;padding-right:0;padding-bottom:0;padding-left:0"><!-- wp:paragraph -->
<p>Third Section Start</p>
<!-- /wp:paragraph -->
<!-- wp:paragraph {"style":{"layout":{"selfStretch":"fit","flexSize":null}}} -->
<p>Third Section Center</p>
<!-- /wp:paragraph -->
<!-- wp:paragraph -->
<p>Third Section End</p>
<!-- /wp:paragraph --></div>
<!-- /wp:group -->
<!-- wp:group {"style":{"spacing":{"padding":{"top":"0","bottom":"0","left":"0","right":"0"}},"dimensions":{"minHeight":"100vh"}},"backgroundColor":"base","layout":{"type":"flex","orientation":"vertical","verticalAlignment":"space-between"}} -->
<div class="wp-block-group has-base-background-color has-background" style="min-height:100vh;padding-top:0;padding-right:0;padding-bottom:0;padding-left:0"><!-- wp:paragraph -->
<p>Fourth Section Start</p>
<!-- /wp:paragraph -->
<!-- wp:paragraph {"style":{"layout":{"selfStretch":"fit","flexSize":null}}} -->
<p>Fourth Section Center</p>
<!-- /wp:paragraph -->
<!-- wp:paragraph -->
<p>Fourth Section End</p>
<!-- /wp:paragraph --></div>
<!-- /wp:group --></main>
<!-- /wp:group -->`;
const EDITOR_ZOOM_OUT_CONTENT_NO_SECTION_ROOT = `<!-- wp:group {"style":{"spacing":{"padding":{"top":"0","bottom":"0","left":"0","right":"0"}},"dimensions":{"minHeight":"100vh"}},"backgroundColor":"base-2","layout":{"type":"flex","orientation":"vertical","verticalAlignment":"space-between"}} -->
<div class="wp-block-group has-base-2-background-color has-background" style="min-height:100vh;padding-top:0;padding-right:0;padding-bottom:0;padding-left:0"><!-- wp:paragraph -->
<p>First Section Start</p>
<!-- /wp:paragraph -->
<!-- wp:paragraph {"style":{"layout":{"selfStretch":"fit","flexSize":null}}} -->
<p>First Section Center</p>
<!-- /wp:paragraph -->
<!-- wp:paragraph -->
<p>First Section End</p>
<!-- /wp:paragraph --></div>
<!-- /wp:group -->`;
test.describe( 'Zoom Out', () => {
test.beforeAll( async ( { requestUtils } ) => {
await requestUtils.activateTheme( 'twentytwentyfour' );
} );
test.afterAll( async ( { requestUtils } ) => {
await requestUtils.activateTheme( 'twentytwentyone' );
await requestUtils.deleteAllTemplates( 'wp_template' );
await requestUtils.deleteAllTemplates( 'wp_template_part' );
} );
test.beforeEach( async ( { admin } ) => {
await admin.visitSiteEditor( {
postId: 'twentytwentyfour//index',
postType: 'wp_template',
canvas: 'edit',
} );
} );
test( 'Entering zoomed out mode zooms the canvas', async ( {
page,
editor,
} ) => {
await page.getByLabel( 'Zoom Out' ).click();
const iframe = page.locator( 'iframe[name="editor-canvas"]' );
const html = editor.canvas.locator( 'html' );
// Check that the html is scaled.
await expect( html ).toHaveCSS(
'scale',
new RegExp( /0\.[5-8][0-9]*/, 'i' )
);
const iframeRect = await iframe.boundingBox();
const htmlRect = await html.boundingBox();
// Check that the iframe is larger than the html.
expect( iframeRect.width ).toBeGreaterThan( htmlRect.width );
// Check that the zoomed out content has a frame around it.
const paddingTop = await html.evaluate( ( element ) => {
const paddingValue = window.getComputedStyle( element ).paddingTop;
return parseFloat( paddingValue );
} );
expect( htmlRect.y + paddingTop ).toBeGreaterThan( iframeRect.y );
expect( htmlRect.x ).toBeGreaterThan( iframeRect.x );
} );
test( 'Toggling zoom state should keep content centered', async ( {
page,
editor,
} ) => {
// Add some patterns into the page.
await editor.setContent( EDITOR_ZOOM_OUT_CONTENT );
// Find the scroll container element
await page.evaluate( () => {
const { activeElement } =
document.activeElement?.contentDocument ?? document;
window.scrollContainer =
window.wp.dom.getScrollContainer( activeElement );
return window.scrollContainer;
} );
// Test: Test from top of page (scrollTop 0)
// Enter Zoom Out
await page.getByRole( 'button', { name: 'Zoom Out' } ).click();
const scrollTopZoomed = await page.evaluate( () => {
return window.scrollContainer.scrollTop;
} );
expect( scrollTopZoomed ).toBe( 0 );
// Exit Zoom Out
await page.getByRole( 'button', { name: 'Zoom Out' } ).click();
const scrollTopNoZoom = await page.evaluate( () => {
return window.scrollContainer.scrollTop;
} );
expect( scrollTopNoZoom ).toBe( 0 );
// Test: Should center the scroll position when zooming out/in
const firstSectionEnd = editor.canvas.locator(
'text=First Section End'
);
const secondSectionStart = editor.canvas.locator(
'text=Second Section Start'
);
const secondSectionCenter = editor.canvas.locator(
'text=Second Section Center'
);
const secondSectionEnd = editor.canvas.locator(
'text=Second Section End'
);
const thirdSectionStart = editor.canvas.locator(
'text=Third Section Start'
);
const thirdSectionCenter = editor.canvas.locator(
'text=Third Section Center'
);
const thirdSectionEnd = editor.canvas.locator(
'text=Third Section End'
);
const fourthSectionStart = editor.canvas.locator(
'text=Fourth Section Start'
);
// Test for second section
// Playwright scrolls it to the center of the viewport, so this is what we scroll to.
await secondSectionCenter.scrollIntoViewIfNeeded();
// Because the text is spread with a group height of 100vh, they should both be visible.
await expect( firstSectionEnd ).not.toBeInViewport();
await expect( secondSectionStart ).toBeInViewport();
await expect( secondSectionEnd ).toBeInViewport();
await expect( thirdSectionStart ).not.toBeInViewport();
// After zooming, if we zoomed out with the correct central point, they should both still be visible when toggling zoom out state
// Enter Zoom Out
await page.getByRole( 'button', { name: 'Zoom Out' } ).click();
await expect( firstSectionEnd ).toBeInViewport();
await expect( secondSectionStart ).toBeInViewport();
await expect( secondSectionEnd ).toBeInViewport();
await expect( thirdSectionStart ).toBeInViewport();
// Exit Zoom Out
await page.getByRole( 'button', { name: 'Zoom Out' } ).click();
await expect( firstSectionEnd ).not.toBeInViewport();
await expect( secondSectionStart ).toBeInViewport();
await expect( secondSectionEnd ).toBeInViewport();
await expect( thirdSectionStart ).not.toBeInViewport();
// Test for third section
// Playwright scrolls it to the center of the viewport, so this is what we scroll to.
await thirdSectionCenter.scrollIntoViewIfNeeded();
// Because the text is spread with a group height of 100vh, they should both be visible.
await expect( secondSectionEnd ).not.toBeInViewport();
await expect( thirdSectionStart ).toBeInViewport();
await expect( thirdSectionEnd ).toBeInViewport();
await expect( fourthSectionStart ).not.toBeInViewport();
// After zooming, if we zoomed out with the correct central point, they should both still be visible when toggling zoom out state
// Enter Zoom Out
await page.getByRole( 'button', { name: 'Zoom Out' } ).click();
await expect( secondSectionEnd ).toBeInViewport();
await expect( thirdSectionStart ).toBeInViewport();
await expect( thirdSectionEnd ).toBeInViewport();
await expect( fourthSectionStart ).toBeInViewport();
// Exit Zoom Out
await page.getByRole( 'button', { name: 'Zoom Out' } ).click();
await expect( secondSectionEnd ).not.toBeInViewport();
await expect( thirdSectionStart ).toBeInViewport();
await expect( thirdSectionEnd ).toBeInViewport();
await expect( fourthSectionStart ).not.toBeInViewport();
} );
test( 'Zoom out selected section has three items in options menu', async ( {
page,
} ) => {
// open the inserter
await page
.getByRole( 'button', {
name: 'Block Inserter',
exact: true,
} )
.click();
// switch to patterns tab
await page.getByRole( 'tab', { name: 'Patterns' } ).click();
// search for a pattern
await page
.getByRole( 'searchbox', { name: 'Search' } )
.fill( 'Footer' );
// click on Footer with colophon, 3 columns
await page
.getByRole( 'option', { name: 'Footer with colophon, 3 columns' } )
.click();
// open the block toolbar more settings menu
await page.getByLabel( 'Block tools' ).getByLabel( 'Options' ).click();
// get the length of the options menu
const optionsMenu = page
.getByRole( 'menu', { name: 'Options' } )
.getByRole( 'menuitem' );
// we expect 3 items in the options menu
await expect( optionsMenu ).toHaveCount( 3 );
} );
test( 'Zoom Out cannot be activated when the section root is missing', async ( {
page,
editor,
} ) => {
await editor.setContent( EDITOR_ZOOM_OUT_CONTENT_NO_SECTION_ROOT );
// Check that the Zoom Out toggle button is not visible.
await expect(
page.getByRole( 'button', { name: 'Zoom Out' } )
).toBeHidden();
// Check that activating the Patterns tab in the Inserter does not activate
// Zoom Out.
await page
.getByRole( 'button', {
name: 'Block Inserter',
exact: true,
} )
.click();
await page.getByRole( 'tab', { name: 'Patterns' } ).click();
await expect( page.locator( '.is-zoomed-out' ) ).toBeHidden();
} );
} );