-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathedit.js
324 lines (277 loc) · 8.03 KB
/
edit.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/**
* External dependencies
*/
import { screen, fireEvent, act, within } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
/**
* Internal dependencies
*/
import {
initializeEditor,
selectBlock,
} from 'test/integration/helpers/integration-test-editor';
async function setup( attributes ) {
const testBlock = { name: 'core/cover', attributes };
return initializeEditor( testBlock );
}
async function createAndSelectBlock() {
await userEvent.click(
screen.getByRole( 'button', {
name: 'Color: Black',
} )
);
await userEvent.click(
screen.getByRole( 'button', {
name: 'Select Cover',
} )
);
}
describe( 'Cover block', () => {
describe( 'Editor canvas', () => {
test( 'shows placeholder if background image and color not set', async () => {
await setup();
expect(
screen.getByRole( 'group', {
name: 'To edit this block, you need permission to upload media.',
} )
).toBeInTheDocument();
} );
test( 'can set overlay color using color picker on block placeholder', async () => {
const { container } = await setup();
const colorPicker = screen.getByRole( 'button', {
name: 'Color: Black',
} );
await userEvent.click( colorPicker );
const color = colorPicker.style.backgroundColor;
expect(
screen.queryByRole( 'group', {
name: 'To edit this block, you need permission to upload media.',
} )
).not.toBeInTheDocument();
// eslint-disable-next-line testing-library/no-node-access
const overlay = container.getElementsByClassName(
'wp-block-cover__background'
);
expect( overlay[ 0 ] ).toHaveStyle(
`background-color: ${ color }`
);
} );
test( 'can have the title edited', async () => {
await setup();
await userEvent.click(
screen.getByRole( 'button', {
name: 'Color: Black',
} )
);
const title = screen.getByLabelText( 'Empty block;', {
exact: false,
} );
await userEvent.click( title );
await userEvent.keyboard( 'abc' );
expect( title ).toHaveTextContent( 'abc' );
} );
} );
describe( 'Block toolbar', () => {
test( 'full height toggle sets minHeight style attribute to 100vh when clicked', async () => {
await setup();
await createAndSelectBlock();
expect( screen.getByLabelText( 'Block: Cover' ) ).not.toHaveStyle(
'min-height: 100vh;'
);
await userEvent.click(
screen.getByLabelText( 'Toggle full height' )
);
expect( screen.getByLabelText( 'Block: Cover' ) ).toHaveStyle(
'min-height: 100vh;'
);
} );
test( 'content position button sets content position', async () => {
await setup();
await createAndSelectBlock();
await userEvent.click(
screen.getByLabelText( 'Change content position' )
);
expect( screen.getByLabelText( 'Block: Cover' ) ).not.toHaveClass(
'has-custom-content-position'
);
await act( async () =>
within( screen.getByRole( 'grid' ) )
.getByRole( 'gridcell', {
name: 'top left',
} )
.focus()
);
expect( screen.getByLabelText( 'Block: Cover' ) ).toHaveClass(
'has-custom-content-position'
);
expect( screen.getByLabelText( 'Block: Cover' ) ).toHaveClass(
'is-position-top-left'
);
} );
} );
describe( 'Inspector controls', () => {
describe( 'Media settings', () => {
test( 'does not display media settings panel if url is not set', async () => {
await setup();
expect(
screen.queryByRole( 'button', {
name: 'Media settings',
} )
).not.toBeInTheDocument();
} );
test( 'displays media settings panel if url is set', async () => {
await setup( {
url: 'http://localhost/my-image.jpg',
} );
await selectBlock( 'Block: Cover' );
expect(
screen.getByRole( 'button', {
name: 'Media settings',
} )
).toBeInTheDocument();
} );
} );
test( 'sets hasParallax attribute to true if fixed background toggled', async () => {
await setup( {
url: 'http://localhost/my-image.jpg',
} );
expect( screen.getByLabelText( 'Block: Cover' ) ).not.toHaveClass(
'has-parallax'
);
await selectBlock( 'Block: Cover' );
await userEvent.click(
screen.getByLabelText( 'Fixed background' )
);
expect( screen.getByLabelText( 'Block: Cover' ) ).toHaveClass(
'has-parallax'
);
} );
test( 'sets isRepeated attribute to true if repeated background toggled', async () => {
await setup( {
url: 'http://localhost/my-image.jpg',
} );
expect( screen.getByLabelText( 'Block: Cover' ) ).not.toHaveClass(
'is-repeated'
);
await selectBlock( 'Block: Cover' );
await userEvent.click(
screen.getByLabelText( 'Repeated background' )
);
expect( screen.getByLabelText( 'Block: Cover' ) ).toHaveClass(
'is-repeated'
);
} );
test( 'sets left focalPoint attribute when focal point values changed', async () => {
await setup( {
url: 'http://localhost/my-image.jpg',
} );
await selectBlock( 'Block: Cover' );
await userEvent.clear( screen.getByLabelText( 'Left' ) );
await userEvent.type( screen.getByLabelText( 'Left' ), '100' );
expect(
within( screen.getByLabelText( 'Block: Cover' ) ).getByRole(
'img'
)
).toHaveStyle( 'object-position: 100% 50%;' );
} );
test( 'sets alt attribute if text entered in alt text box', async () => {
await setup( {
url: 'http://localhost/my-image.jpg',
} );
await selectBlock( 'Block: Cover' );
await userEvent.type(
screen.getByLabelText( 'Alternative text' ),
'Me'
);
expect( screen.getByAltText( 'Me' ) ).toBeInTheDocument();
} );
test( 'clears media when clear media button clicked', async () => {
await setup( {
url: 'http://localhost/my-image.jpg',
} );
await selectBlock( 'Block: Cover' );
expect(
within( screen.getByLabelText( 'Block: Cover' ) ).getByRole(
'img'
)
).toBeInTheDocument();
await userEvent.click(
screen.getByRole( 'button', {
name: 'Clear Media',
} )
);
expect(
within( screen.queryByLabelText( 'Block: Cover' ) ).queryByRole(
'img'
)
).not.toBeInTheDocument();
} );
describe( 'Color panel', () => {
test( 'applies selected opacity to block when number control value changed', async () => {
const { container } = await setup();
await createAndSelectBlock();
// eslint-disable-next-line testing-library/no-node-access
const overlay = container.getElementsByClassName(
'wp-block-cover__background'
);
expect( overlay[ 0 ] ).toHaveClass( 'has-background-dim-100' );
await userEvent.click(
screen.getByRole( 'tab', {
name: 'Styles',
} )
);
fireEvent.change(
screen.getByRole( 'spinbutton', {
name: 'Overlay opacity',
} ),
{
target: { value: '40' },
}
);
expect( overlay[ 0 ] ).toHaveClass( 'has-background-dim-40' );
} );
test( 'applies selected opacity to block when slider moved', async () => {
const { container } = await setup();
await createAndSelectBlock();
// eslint-disable-next-line testing-library/no-node-access
const overlay = container.getElementsByClassName(
'wp-block-cover__background'
);
expect( overlay[ 0 ] ).toHaveClass( 'has-background-dim-100' );
await userEvent.click(
screen.getByRole( 'tab', {
name: 'Styles',
} )
);
fireEvent.change(
screen.getByRole( 'slider', {
name: 'Overlay opacity',
} ),
{ target: { value: 30 } }
);
expect( overlay[ 0 ] ).toHaveClass( 'has-background-dim-30' );
} );
} );
describe( 'Dimensions panel', () => {
test( 'sets minHeight attribute when number control value changed', async () => {
await setup();
await createAndSelectBlock();
await userEvent.click(
screen.getByRole( 'tab', {
name: 'Styles',
} )
);
await userEvent.clear(
screen.getByLabelText( 'Minimum height of cover' )
);
await userEvent.type(
screen.getByLabelText( 'Minimum height of cover' ),
'300'
);
expect( screen.getByLabelText( 'Block: Cover' ) ).toHaveStyle(
'min-height: 300px;'
);
} );
} );
} );
} );