11/**
22 * External dependencies
33 */
4- import { Image } from 'react-native' ;
5- import { render , fireEvent , waitFor } from 'test/helpers' ;
4+ import { AccessibilityInfo , Image } from 'react-native' ;
5+ import {
6+ getEditorHtml ,
7+ initializeEditor ,
8+ render ,
9+ fireEvent ,
10+ waitFor ,
11+ within ,
12+ } from 'test/helpers' ;
613
714/**
815 * WordPress dependencies
916 */
1017import { BottomSheetSettings , BlockEdit } from '@wordpress/block-editor' ;
1118import { SlotFillProvider } from '@wordpress/components' ;
12- import { registerBlockType , unregisterBlockType } from '@wordpress/blocks' ;
19+ import { setDefaultBlockName , unregisterBlockType } from '@wordpress/blocks' ;
1320import {
1421 requestMediaPicker ,
1522 requestMediaEditor ,
@@ -19,7 +26,9 @@ import {
1926 * Internal dependencies
2027 */
2128import { IMAGE_BACKGROUND_TYPE } from '../shared' ;
22- import { metadata , settings , name } from '../index' ;
29+ import * as paragraph from '../../paragraph' ;
30+ import * as cover from '..' ;
31+ import { registerBlock } from '../..' ;
2332
2433// Avoid errors due to mocked stylesheet files missing required selectors
2534jest . mock ( '@wordpress/compose' , ( ) => ( {
@@ -33,10 +42,25 @@ jest.mock( '@wordpress/compose', () => ( {
3342 ) ) ,
3443} ) ) ;
3544
45+ const COVER_BLOCK_PLACEHOLDER_HTML = `<!-- wp:cover -->
46+ <div class="wp-block-cover"><span aria-hidden="true" class="has-background-dim-100 wp-block-cover__gradient-background has-background-dim"></span><div class="wp-block-cover__inner-container"></div></div>
47+ <!-- /wp:cover -->` ;
48+ const COVER_BLOCK_SOLID_COLOR_HTML = `<!-- wp:cover {"overlayColor":"cyan-bluish-gray"} -->
49+ <div class="wp-block-cover"><span aria-hidden="true" class="has-cyan-bluish-gray-background-color has-background-dim-100 wp-block-cover__gradient-background has-background-dim"></span><div class="wp-block-cover__inner-container"><!-- wp:paragraph {"align":"center","placeholder":"Write title…"} -->
50+ <p class="has-text-align-center"></p>
51+ <!-- /wp:paragraph --></div></div>
52+ <!-- /wp:cover -->` ;
53+
54+ const COLOR_PINK = '#f78da7' ;
55+ const COLOR_RED = '#cf2e2e' ;
56+ const COLOR_GRAY = '#abb8c3' ;
57+ const GRADIENT_GREEN =
58+ 'linear-gradient(135deg,rgb(122,220,180) 0%,rgb(0,208,130) 100%)' ;
59+
3660// Simplified tree to render Cover edit within slot
3761const CoverEdit = ( props ) => (
3862 < SlotFillProvider >
39- < BlockEdit isSelected name = { name } clientId = { 0 } { ...props } />
63+ < BlockEdit isSelected name = { cover . name } clientId = { 0 } { ...props } />
4064 < BottomSheetSettings isVisible />
4165 </ SlotFillProvider >
4266) ;
@@ -55,26 +79,24 @@ beforeAll( () => {
5579 const getSizeSpy = jest . spyOn ( Image , 'getSize' ) ;
5680 getSizeSpy . mockImplementation ( ( _url , callback ) => callback ( 300 , 200 ) ) ;
5781
82+ AccessibilityInfo . isScreenReaderEnabled . mockResolvedValue (
83+ Promise . resolve ( true )
84+ ) ;
85+
5886 // Register required blocks
59- registerBlockType ( name , {
60- ...metadata ,
61- ...settings ,
62- } ) ;
63- registerBlockType ( 'core/paragraph' , {
64- category : 'text' ,
65- title : 'Paragraph' ,
66- edit : ( ) => { } ,
67- save : ( ) => { } ,
68- } ) ;
87+ registerBlock ( paragraph ) ;
88+ registerBlock ( cover ) ;
89+ setDefaultBlockName ( paragraph . name ) ;
6990} ) ;
7091
7192afterAll ( ( ) => {
7293 // Restore mocks
7394 Image . getSize . mockRestore ( ) ;
95+ AccessibilityInfo . isScreenReaderEnabled . mockReset ( ) ;
7496
7597 // Clean up registered blocks
76- unregisterBlockType ( name ) ;
77- unregisterBlockType ( 'core/paragraph' ) ;
98+ unregisterBlockType ( paragraph . name ) ;
99+ unregisterBlockType ( cover . name ) ;
78100} ) ;
79101
80102describe ( 'when no media is attached' , ( ) => {
@@ -270,3 +292,159 @@ describe( 'when an image is attached', () => {
270292 ) ;
271293 } ) ;
272294} ) ;
295+
296+ describe ( 'color settings' , ( ) => {
297+ it ( 'sets a color for the overlay background when the placeholder is visible' , async ( ) => {
298+ const { getByTestId, getByA11yLabel } = await initializeEditor ( {
299+ initialHtml : COVER_BLOCK_PLACEHOLDER_HTML ,
300+ } ) ;
301+
302+ const block = await waitFor ( ( ) =>
303+ getByA11yLabel ( 'Cover block. Empty' )
304+ ) ;
305+ expect ( block ) . toBeDefined ( ) ;
306+
307+ // Select a color from the placeholder palette
308+ const colorPalette = await waitFor ( ( ) =>
309+ getByTestId ( 'color-palette' )
310+ ) ;
311+ const colorButton = within ( colorPalette ) . getByTestId ( COLOR_PINK ) ;
312+
313+ expect ( colorButton ) . toBeDefined ( ) ;
314+ fireEvent . press ( colorButton ) ;
315+
316+ // Wait for the block to be created
317+ const coverBlockWithOverlay = await waitFor ( ( ) =>
318+ getByA11yLabel ( / C o v e r B l o c k \. R o w 1 / )
319+ ) ;
320+ fireEvent . press ( coverBlockWithOverlay ) ;
321+
322+ // Open Block Settings
323+ const settingsButton = await waitFor ( ( ) =>
324+ getByA11yLabel ( 'Open Settings' )
325+ ) ;
326+ fireEvent . press ( settingsButton ) ;
327+
328+ // Wait for Block Settings to be visible
329+ const blockSettingsModal = getByTestId ( 'block-settings-modal' ) ;
330+ await waitFor ( ( ) => blockSettingsModal . props . isVisible ) ;
331+
332+ // Open the overlay color settings
333+ const colorOverlay = await waitFor ( ( ) =>
334+ getByA11yLabel ( 'Color. Empty' )
335+ ) ;
336+ expect ( colorOverlay ) . toBeDefined ( ) ;
337+ fireEvent . press ( colorOverlay ) ;
338+
339+ // Find the selected color
340+ const colorPaletteButton = await waitFor ( ( ) =>
341+ getByTestId ( COLOR_PINK )
342+ ) ;
343+ expect ( colorPaletteButton ) . toBeDefined ( ) ;
344+
345+ // Select another color
346+ const newColorButton = await waitFor ( ( ) => getByTestId ( COLOR_RED ) ) ;
347+ fireEvent . press ( newColorButton ) ;
348+
349+ expect ( getEditorHtml ( ) ) . toMatchSnapshot ( ) ;
350+ } ) ;
351+
352+ it ( 'sets a gradient overlay background when a solid background was already selected' , async ( ) => {
353+ const { getByTestId, getByA11yLabel } = await initializeEditor ( {
354+ initialHtml : COVER_BLOCK_SOLID_COLOR_HTML ,
355+ } ) ;
356+
357+ // Wait for the block to be created
358+ const coverBlock = await waitFor ( ( ) =>
359+ getByA11yLabel ( / C o v e r B l o c k \. R o w 1 / )
360+ ) ;
361+ expect ( coverBlock ) . toBeDefined ( ) ;
362+ fireEvent . press ( coverBlock ) ;
363+
364+ // Open Block Settings
365+ const settingsButton = await waitFor ( ( ) =>
366+ getByA11yLabel ( 'Open Settings' )
367+ ) ;
368+ fireEvent . press ( settingsButton ) ;
369+
370+ // Wait for Block Settings to be visible
371+ const blockSettingsModal = getByTestId ( 'block-settings-modal' ) ;
372+ await waitFor ( ( ) => blockSettingsModal . props . isVisible ) ;
373+
374+ // Open the overlay color settings
375+ const colorOverlay = await waitFor ( ( ) =>
376+ getByA11yLabel ( 'Color. Empty' )
377+ ) ;
378+ expect ( colorOverlay ) . toBeDefined ( ) ;
379+ fireEvent . press ( colorOverlay ) ;
380+
381+ // Find the selected color
382+ const colorButton = await waitFor ( ( ) => getByTestId ( COLOR_GRAY ) ) ;
383+ expect ( colorButton ) . toBeDefined ( ) ;
384+
385+ // Open the gradients
386+ const gradientsButton = await waitFor ( ( ) =>
387+ getByA11yLabel ( 'Gradient' )
388+ ) ;
389+ expect ( gradientsButton ) . toBeDefined ( ) ;
390+
391+ fireEvent ( gradientsButton , 'layout' , {
392+ nativeEvent : { layout : { width : 80 , height : 26 } } ,
393+ } ) ;
394+ fireEvent . press ( gradientsButton ) ;
395+
396+ // Find the gradient color
397+ const newGradientButton = await waitFor ( ( ) =>
398+ getByTestId ( GRADIENT_GREEN )
399+ ) ;
400+ expect ( newGradientButton ) . toBeDefined ( ) ;
401+ fireEvent . press ( newGradientButton ) ;
402+
403+ expect ( getEditorHtml ( ) ) . toMatchSnapshot ( ) ;
404+ } ) ;
405+
406+ it ( 'clears the selected overlay color and mantains the inner blocks' , async ( ) => {
407+ const {
408+ getByTestId,
409+ getByA11yLabel,
410+ getByText,
411+ } = await initializeEditor ( {
412+ initialHtml : COVER_BLOCK_SOLID_COLOR_HTML ,
413+ } ) ;
414+
415+ // Wait for the block to be created
416+ const coverBlock = await waitFor ( ( ) =>
417+ getByA11yLabel ( / C o v e r B l o c k \. R o w 1 / )
418+ ) ;
419+ expect ( coverBlock ) . toBeDefined ( ) ;
420+ fireEvent . press ( coverBlock ) ;
421+
422+ // Open Block Settings
423+ const settingsButton = await waitFor ( ( ) =>
424+ getByA11yLabel ( 'Open Settings' )
425+ ) ;
426+ fireEvent . press ( settingsButton ) ;
427+
428+ // Wait for Block Settings to be visible
429+ const blockSettingsModal = getByTestId ( 'block-settings-modal' ) ;
430+ await waitFor ( ( ) => blockSettingsModal . props . isVisible ) ;
431+
432+ // Open the overlay color settings
433+ const colorOverlay = await waitFor ( ( ) =>
434+ getByA11yLabel ( 'Color. Empty' )
435+ ) ;
436+ expect ( colorOverlay ) . toBeDefined ( ) ;
437+ fireEvent . press ( colorOverlay ) ;
438+
439+ // Find the selected color
440+ const colorButton = await waitFor ( ( ) => getByTestId ( COLOR_GRAY ) ) ;
441+ expect ( colorButton ) . toBeDefined ( ) ;
442+
443+ // Reset the selected color
444+ const resetButton = await waitFor ( ( ) => getByText ( 'Reset' ) ) ;
445+ expect ( resetButton ) . toBeDefined ( ) ;
446+ fireEvent . press ( resetButton ) ;
447+
448+ expect ( getEditorHtml ( ) ) . toMatchSnapshot ( ) ;
449+ } ) ;
450+ } ) ;
0 commit comments