|
| 1 | +/** |
| 2 | + * External dependencies |
| 3 | + */ |
| 4 | +import { first, last } from 'lodash'; |
| 5 | + |
| 6 | +/** |
| 7 | + * WordPress dependencies |
| 8 | + */ |
| 9 | +import { clickButton, createNewPost, getAllBlocks } from '@wordpress/e2e-test-utils'; |
| 10 | + |
| 11 | +/** |
| 12 | + * Internal dependencies |
| 13 | + */ |
| 14 | +import { activateExperience, deactivateExperience, insertBlock } from '../../utils'; |
| 15 | + |
| 16 | +const TEXT_BLOCK_SELECTOR = '.wp-block[data-type="amp/amp-story-text"]'; |
| 17 | +const CODE_BLOCK_SELECTOR = '.wp-block[data-type="core/code"]'; |
| 18 | +const CODE_BLOCK_NAME = 'core/code'; |
| 19 | + |
| 20 | +describe( 'Block Position Controls', () => { |
| 21 | + beforeAll( async () => { |
| 22 | + await activateExperience( 'stories' ); |
| 23 | + } ); |
| 24 | + |
| 25 | + afterAll( async () => { |
| 26 | + await deactivateExperience( 'stories' ); |
| 27 | + } ); |
| 28 | + |
| 29 | + describe( 'Basic control functionality', () => { |
| 30 | + beforeEach( async () => { |
| 31 | + await createNewPost( { postType: 'amp_story' } ); |
| 32 | + await insertBlock( 'Preformatted' ); |
| 33 | + await insertBlock( 'Code' ); |
| 34 | + await insertBlock( 'Image' ); |
| 35 | + await insertBlock( 'Video' ); |
| 36 | + } ); |
| 37 | + |
| 38 | + it( 'should send the block forward on clicking that button', async () => { |
| 39 | + // The Video block was the last added, so it should be at the front. |
| 40 | + expect( last( ( await getAllBlocks() )[ 0 ].innerBlocks ).name ).toStrictEqual( 'core/video' ); |
| 41 | + |
| 42 | + // Send the code block forward, but not completely to the front. |
| 43 | + await page.click( CODE_BLOCK_SELECTOR ); |
| 44 | + await clickButton( 'Forward' ); |
| 45 | + |
| 46 | + // The code block should be have moved forward in the order, but is not yet at the front, that would be an index of 4. |
| 47 | + expect( ( await getAllBlocks() )[ 0 ].innerBlocks[ 3 ].name ).toStrictEqual( CODE_BLOCK_NAME ); |
| 48 | + } ); |
| 49 | + |
| 50 | + it( 'should send the block completely to the front on clicking that button', async () => { |
| 51 | + // Send the block completely to the front. |
| 52 | + await page.click( CODE_BLOCK_SELECTOR ); |
| 53 | + await clickButton( 'Front' ); |
| 54 | + |
| 55 | + // The code block should now be at the front, which means it'll be last in getAllBlocks(). |
| 56 | + expect( last( ( await getAllBlocks() )[ 0 ].innerBlocks ).name ).toStrictEqual( CODE_BLOCK_NAME ); |
| 57 | + } ); |
| 58 | + |
| 59 | + it( 'should send the block backward on clicking that button', async () => { |
| 60 | + // Send the code block forward, but not completely to the front. |
| 61 | + await page.click( CODE_BLOCK_SELECTOR ); |
| 62 | + await page.click( '.amp-story-controls-send-backwards' ); |
| 63 | + |
| 64 | + // The code block should be have moved backward in the order, but is not completely at the back, that would be an index of 0. |
| 65 | + expect( ( await getAllBlocks() )[ 0 ].innerBlocks[ 1 ].name ).toStrictEqual( CODE_BLOCK_NAME ); |
| 66 | + } ); |
| 67 | + |
| 68 | + it( 'should send the block completely to the back on clicking that button', async () => { |
| 69 | + // Send the block completely to the back. |
| 70 | + await page.click( CODE_BLOCK_SELECTOR ); |
| 71 | + await page.click( '.amp-story-controls-send-back' ); |
| 72 | + |
| 73 | + // The code block should now be at the back, which means it'll be first in getAllBlocks(). |
| 74 | + expect( first( ( await getAllBlocks() )[ 0 ].innerBlocks ).name ).toStrictEqual( CODE_BLOCK_NAME ); |
| 75 | + } ); |
| 76 | + } ); |
| 77 | + |
| 78 | + describe( 'Buttons are only present when appropriate', () => { |
| 79 | + beforeEach( async () => { |
| 80 | + await createNewPost( { postType: 'amp_story' } ); |
| 81 | + } ); |
| 82 | + |
| 83 | + it( 'should not have the Block Position controls when there is only one block on the page', async () => { |
| 84 | + await expect( page ).not.toMatch( 'Block Position' ); |
| 85 | + } ); |
| 86 | + |
| 87 | + it( 'should disable the Forward and Front buttons when the selected block is already at the front', async () => { |
| 88 | + await insertBlock( 'Image' ); |
| 89 | + |
| 90 | + // The 'Block Position' controls should be present. |
| 91 | + expect( page ).toMatch( 'Block Position' ); |
| 92 | + |
| 93 | + // Since the selected block is already at the front, the 'Front' and 'Forward' buttons should be disabled. |
| 94 | + expect( page ).toMatchElement( '.amp-story-controls-bring-front[aria-disabled="true"]' ); |
| 95 | + expect( page ).toMatchElement( '.amp-story-controls-bring-forward[aria-disabled="true"]' ); |
| 96 | + } ); |
| 97 | + |
| 98 | + it( 'should disable the Back and Backward buttons when the selected block is already at the front', async () => { |
| 99 | + await insertBlock( 'Image' ); |
| 100 | + await page.click( TEXT_BLOCK_SELECTOR ); |
| 101 | + |
| 102 | + // The 'Block Position' controls should again be present. |
| 103 | + expect( page ).toMatch( 'Block Position' ); |
| 104 | + |
| 105 | + // Since the selected block is already at the back, the 'Back' and 'Backward' buttons should be disabled. |
| 106 | + expect( page ).toMatchElement( '.amp-story-controls-send-back[aria-disabled="true"]' ); |
| 107 | + expect( page ).toMatchElement( '.amp-story-controls-send-backward[aria-disabled="true"]' ); |
| 108 | + } ); |
| 109 | + } ); |
| 110 | +} ); |
0 commit comments