/
cover.test.js
217 lines (193 loc) · 6.7 KB
/
cover.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
/**
* External dependencies
*/
import path from 'path';
import fs from 'fs';
import os from 'os';
import { v4 as uuid } from 'uuid';
/**
* WordPress dependencies
*/
import {
insertBlock,
createNewPost,
openDocumentSettingsSidebar,
transformBlockTo,
} from '@wordpress/e2e-test-utils';
async function upload( selector ) {
const inputElement = await page.waitForSelector(
`${ selector } input[type="file"]`
);
const testImagePath = path.join(
__dirname,
'..',
'..',
'..',
'assets',
'10x10_e2e_test_image_z9T8jK.png'
);
const filename = uuid();
const tmpFileName = path.join( os.tmpdir(), filename + '.png' );
fs.copyFileSync( testImagePath, tmpFileName );
await inputElement.uploadFile( tmpFileName );
await page.waitForSelector( `${ selector } img[src$="${ filename }.png"]` );
return filename;
}
describe( 'Cover', () => {
beforeEach( async () => {
await createNewPost();
} );
it( 'can set overlay color using color picker on block placeholder', async () => {
await insertBlock( 'Cover' );
// Get the first color option from the block placeholder's color picker
const colorPickerButton = await page.waitForSelector(
'.wp-block-cover__placeholder-background-options .components-circular-option-picker__option-wrapper:first-child button'
);
// Get the RGB value of the picked color
const pickedColor = await colorPickerButton.evaluate(
( node ) => node.style.backgroundColor
);
// Create the block by clicking selected color button
await colorPickerButton.click();
// Get the block's background dim element
const backgroundDim = await page.waitForSelector(
'.wp-block-cover .has-background-dim'
);
// Get the RGB value of the background dim
const dimColor = await backgroundDim.evaluate(
( node ) => node.style.backgroundColor
);
expect( pickedColor ).toEqual( dimColor );
} );
it( 'can set background image using image upload on block placeholder', async () => {
await insertBlock( 'Cover' );
// Create the block using uploaded image
const sourceImageFilename = await upload( '.wp-block-cover' );
// Get the block's background image URL
const blockImage = await page.waitForSelector( '.wp-block-cover img' );
const blockImageUrl = await blockImage.evaluate( ( el ) => el.src );
expect( blockImageUrl ).toContain( sourceImageFilename );
} );
it( 'dims background image down by 50% by default', async () => {
await insertBlock( 'Cover' );
// Create the block using uploaded image
await upload( '.wp-block-cover' );
// Get the block's background dim color and its opacity
const backgroundDim = await page.waitForSelector(
'.wp-block-cover .has-background-dim'
);
const [
backgroundDimColor,
backgroundDimOpacity,
] = await page.evaluate( ( el ) => {
const computedStyle = window.getComputedStyle( el );
return [ computedStyle.backgroundColor, computedStyle.opacity ];
}, backgroundDim );
expect( backgroundDimColor ).toBe( 'rgb(0, 0, 0)' );
expect( backgroundDimOpacity ).toBe( '0.5' );
} );
it( 'can have the title edited', async () => {
await insertBlock( 'Cover' );
// Click first color option from the block placeholder's color picker
const colorPickerButton = await page.waitForSelector(
'.wp-block-cover__placeholder-background-options .components-circular-option-picker__option-wrapper:first-child button'
);
await colorPickerButton.click();
// Click the title placeholder to put the cursor inside
const coverTitle = await page.waitForSelector(
'.wp-block-cover .wp-block-paragraph'
);
await coverTitle.click();
// Type the title
await page.keyboard.type( 'foo' );
const coverTitleText = await coverTitle.evaluate(
( el ) => el.innerText
);
expect( coverTitleText ).toEqual( 'foo' );
} );
it( 'can be resized using drag & drop', async () => {
await insertBlock( 'Cover' );
// Close the inserter
await page.click( '.edit-post-header-toolbar__inserter-toggle' );
// Open the sidebar
await openDocumentSettingsSidebar();
// Choose the first solid color as the background of the cover.
await page.click(
'.components-circular-option-picker__option-wrapper:first-child button'
);
// Select the cover block.By default the child paragraph gets selected.
await page.click( '.edit-post-header-toolbar__list-view-toggle' );
await page.click(
'.block-editor-list-view-block__contents-container a'
);
const heightInput = (
await page.$x(
'//div[./label[contains(text(),"Minimum height of cover")]]//input'
)
)[ 0 ];
// Verify the height of the cover is not defined
expect(
await page.evaluate( ( { value } ) => value, heightInput )
).toBeFalsy();
const resizeButton = await page.$(
'.components-resizable-box__handle-bottom'
);
const boundingBoxResizeButton = await resizeButton.boundingBox();
const coordinatesResizeButton = {
x: boundingBoxResizeButton.x + boundingBoxResizeButton.width / 2,
y: boundingBoxResizeButton.y + boundingBoxResizeButton.height / 2,
};
// Move the mouse to the position of the resize button.
await page.mouse.move(
coordinatesResizeButton.x,
coordinatesResizeButton.y
);
// Trigger a mousedown event against the resize button.
// Using page.mouse.down does not works because it triggers a global event,
// not an event for that element.
page.evaluate( ( { x, y } ) => {
const element = document.querySelector(
'.components-resizable-box__handle-bottom'
);
event = document.createEvent( 'CustomEvent' );
event.initCustomEvent( 'mousedown', true, true, null );
event.clientX = x;
event.clientY = y;
element.dispatchEvent( event );
}, coordinatesResizeButton );
// Move the mouse to resize the cover.
await page.mouse.move(
coordinatesResizeButton.x,
coordinatesResizeButton.y + 100,
{ steps: 10 }
);
// Release the mouse.
await page.mouse.up();
// Verify the height of the cover has changed.
expect(
await page.evaluate(
( { value } ) => Number.parseInt( value ),
heightInput
)
).toBeGreaterThan( 100 );
} );
it( 'dims the background image down by 50% when transformed from the Image block', async () => {
await insertBlock( 'Image' );
// Upload image and transform to the Cover block
await upload( '.wp-block-image' );
await transformBlockTo( 'Cover' );
// Get the block's background dim color and its opacity
const backgroundDim = await page.waitForSelector(
'.wp-block-cover .has-background-dim'
);
const [
backgroundDimColor,
backgroundDimOpacity,
] = await page.evaluate( ( el ) => {
const computedStyle = window.getComputedStyle( el );
return [ computedStyle.backgroundColor, computedStyle.opacity ];
}, backgroundDim );
expect( backgroundDimColor ).toBe( 'rgb(0, 0, 0)' );
expect( backgroundDimOpacity ).toBe( '0.5' );
} );
} );