2020-03-04 14:32:31 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import {
|
2021-10-25 14:46:34 +00:00
|
|
|
clickBlockToolbarButton,
|
2020-07-29 13:39:15 +00:00
|
|
|
openDocumentSettingsSidebar,
|
2020-03-04 14:32:31 +00:00
|
|
|
switchUserToAdmin,
|
2023-01-25 15:41:22 +00:00
|
|
|
searchForBlock,
|
|
|
|
openGlobalBlockInserter,
|
2023-03-28 12:33:28 +00:00
|
|
|
insertBlock,
|
2020-03-04 14:32:31 +00:00
|
|
|
} from '@wordpress/e2e-test-utils';
|
2020-07-29 13:39:15 +00:00
|
|
|
import {
|
2020-09-14 07:46:58 +00:00
|
|
|
findLabelWithText,
|
2020-07-29 13:39:15 +00:00
|
|
|
visitBlockPage,
|
2021-10-25 14:46:34 +00:00
|
|
|
selectBlockByName,
|
2023-01-25 15:41:22 +00:00
|
|
|
switchBlockInspectorTabWhenGutenbergIsInstalled,
|
2020-07-29 13:39:15 +00:00
|
|
|
} from '@woocommerce/blocks-test-utils';
|
2021-10-18 06:48:03 +00:00
|
|
|
import { merchant } from '@woocommerce/e2e-utils';
|
2020-03-04 14:32:31 +00:00
|
|
|
|
2022-05-23 12:05:30 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2023-02-20 09:10:41 +00:00
|
|
|
import { openWidgetEditor, closeModalIfExists } from '../../utils.js';
|
2020-10-27 12:24:53 +00:00
|
|
|
|
2020-06-15 14:59:18 +00:00
|
|
|
const block = {
|
|
|
|
name: 'Cart',
|
|
|
|
slug: 'woocommerce/cart',
|
2021-11-09 13:18:55 +00:00
|
|
|
class: '.wp-block-woocommerce-cart',
|
2023-01-25 15:41:22 +00:00
|
|
|
selectors: {
|
|
|
|
insertButton: "//button//span[text()='Cart']",
|
|
|
|
},
|
2021-11-09 13:18:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const filledCartBlock = {
|
|
|
|
name: 'Filled Cart',
|
|
|
|
slug: 'woocommerce/filled-cart-block',
|
|
|
|
class: '.wp-block-woocommerce-filled-cart-block',
|
|
|
|
};
|
|
|
|
|
|
|
|
const emptyCartBlock = {
|
|
|
|
name: 'Empty Cart',
|
|
|
|
slug: 'woocommerce/empty-cart-block',
|
|
|
|
class: '.wp-block-woocommerce-empty-cart-block',
|
2020-06-15 14:59:18 +00:00
|
|
|
};
|
|
|
|
|
2022-09-21 06:29:16 +00:00
|
|
|
const crossSellsBlock = {
|
|
|
|
name: 'Cart Cross-Sells block',
|
|
|
|
slug: 'woocommerce/cart-cross-sells-products-block',
|
|
|
|
class: '.wp-block-woocommerce-cart-cross-sells-products-block',
|
|
|
|
};
|
|
|
|
|
2021-05-05 11:52:27 +00:00
|
|
|
if ( process.env.WOOCOMMERCE_BLOCKS_PHASE < 2 ) {
|
2022-05-23 12:05:30 +00:00
|
|
|
// eslint-disable-next-line jest/no-focused-tests, jest/expect-expect
|
2020-06-16 12:18:34 +00:00
|
|
|
test.only( `skipping ${ block.name } tests`, () => {} );
|
2021-05-05 11:52:27 +00:00
|
|
|
}
|
2020-06-16 12:18:34 +00:00
|
|
|
|
2020-06-15 14:59:18 +00:00
|
|
|
describe( `${ block.name } Block`, () => {
|
2021-10-18 06:48:03 +00:00
|
|
|
describe( 'in page editor', () => {
|
2022-08-12 14:23:08 +00:00
|
|
|
beforeAll( async () => {
|
|
|
|
await switchUserToAdmin();
|
|
|
|
await visitBlockPage( `${ block.name } Block` );
|
2020-09-14 07:46:58 +00:00
|
|
|
} );
|
2020-07-29 13:39:15 +00:00
|
|
|
|
2022-08-12 14:23:08 +00:00
|
|
|
it( 'can only be inserted once', async () => {
|
2023-01-25 15:41:22 +00:00
|
|
|
await openGlobalBlockInserter();
|
|
|
|
await page.keyboard.type( block.name );
|
|
|
|
const button = await page.$x( block.selectors.insertButton );
|
|
|
|
expect( button ).toHaveLength( 0 );
|
2022-08-12 14:23:08 +00:00
|
|
|
} );
|
2020-07-29 13:39:15 +00:00
|
|
|
|
2023-03-28 12:33:28 +00:00
|
|
|
it( 'inner blocks can be added/removed by filters', async () => {
|
|
|
|
// Begin by removing the block.
|
|
|
|
await selectBlockByName( block.slug );
|
|
|
|
const options = await page.$x(
|
|
|
|
'//div[@class="block-editor-block-toolbar"]//button[@aria-label="Options"]'
|
|
|
|
);
|
|
|
|
await options[ 0 ].click();
|
|
|
|
const removeButton = await page.$x(
|
|
|
|
'//button[contains(., "Remove Cart")]'
|
|
|
|
);
|
|
|
|
await removeButton[ 0 ].click();
|
|
|
|
// Expect block to have been removed.
|
|
|
|
await expect( page ).not.toMatchElement( block.class );
|
|
|
|
|
|
|
|
// Register a checkout filter to allow `core/table` block in the Checkout block's inner blocks, add
|
|
|
|
// core/audio into the woocommerce/cart-order-summary-block and remove core/paragraph from all Cart inner
|
|
|
|
// blocks.
|
|
|
|
await page.evaluate(
|
|
|
|
"wc.blocksCheckout.registerCheckoutFilters( 'woo-test-namespace'," +
|
|
|
|
'{ additionalCartCheckoutInnerBlockTypes: ( value, extensions, { block } ) => {' +
|
|
|
|
" value.push('core/table');" +
|
|
|
|
" if ( block === 'woocommerce/cart-order-summary-block' ) {" +
|
|
|
|
" value.push( 'core/audio' );" +
|
|
|
|
' }' +
|
|
|
|
' return value;' +
|
|
|
|
'}' +
|
|
|
|
'}' +
|
|
|
|
');'
|
|
|
|
);
|
|
|
|
|
|
|
|
await insertBlock( block.name );
|
|
|
|
|
|
|
|
// Select the shipping address block and try to insert a block. Check the Table block is available.
|
|
|
|
await selectBlockByName( 'woocommerce/cart-order-summary-block' );
|
|
|
|
await page.waitForTimeout( 1000 );
|
|
|
|
const addBlockButton = await page.waitForXPath(
|
|
|
|
'//div[@data-type="woocommerce/cart-order-summary-block"]//button[@aria-label="Add block"]'
|
|
|
|
);
|
|
|
|
await addBlockButton.click();
|
|
|
|
const tableButton = await page.waitForXPath(
|
|
|
|
'//*[@role="option" and contains(., "Table")]'
|
|
|
|
);
|
|
|
|
const audioButton = await page.waitForXPath(
|
|
|
|
'//*[@role="option" and contains(., "Audio")]'
|
|
|
|
);
|
|
|
|
expect( tableButton ).not.toBeNull();
|
|
|
|
expect( audioButton ).not.toBeNull();
|
|
|
|
|
|
|
|
// // Now check the filled cart block and expect only the Table block to be available there.
|
|
|
|
await selectBlockByName( 'woocommerce/filled-cart-block' );
|
|
|
|
const filledCartAddBlockButton = await page.waitForXPath(
|
|
|
|
'//div[@data-type="woocommerce/filled-cart-block"]//button[@aria-label="Add block"]'
|
|
|
|
);
|
|
|
|
await filledCartAddBlockButton.click();
|
|
|
|
const filledCartTableButton = await page.waitForXPath(
|
|
|
|
'//*[@role="option" and contains(., "Table")]'
|
|
|
|
);
|
|
|
|
const filledCartAudioButton = await page.$x(
|
|
|
|
'//*[@role="option" and contains(., "Audio")]'
|
|
|
|
);
|
|
|
|
expect( filledCartTableButton ).not.toBeNull();
|
|
|
|
expect( filledCartAudioButton ).toHaveLength( 0 );
|
|
|
|
} );
|
|
|
|
|
2022-08-12 14:23:08 +00:00
|
|
|
it( 'renders without crashing', async () => {
|
|
|
|
await expect( page ).toRenderBlock( block );
|
|
|
|
await expect( page ).toRenderBlock( filledCartBlock );
|
2022-09-21 06:29:16 +00:00
|
|
|
await expect( page ).toRenderBlock( crossSellsBlock );
|
2022-08-12 14:23:08 +00:00
|
|
|
await expect( page ).toRenderBlock( emptyCartBlock );
|
|
|
|
} );
|
2021-10-18 06:48:03 +00:00
|
|
|
|
2022-08-12 14:23:08 +00:00
|
|
|
it( 'shows empty cart when changing the view', async () => {
|
|
|
|
await page.waitForSelector( block.class ).catch( () => {
|
|
|
|
throw new Error(
|
|
|
|
`Could not find an element with class ${ block.class } - the block probably did not load correctly.`
|
|
|
|
);
|
2021-03-19 10:05:42 +00:00
|
|
|
} );
|
2022-08-12 14:23:08 +00:00
|
|
|
await selectBlockByName( block.slug );
|
|
|
|
await clickBlockToolbarButton( 'Switch view', 'ariaLabel' );
|
|
|
|
const emptyCartButton = await page.waitForXPath(
|
|
|
|
`//button[contains(@class,'components-dropdown-menu__menu-item')]//span[contains(text(), 'Empty Cart')]`
|
|
|
|
);
|
|
|
|
// Clicks the element by running the JavaScript HTMLElement.click() method on the given element in the
|
|
|
|
// browser context, which fires a click event. It doesn't scroll the page or move the mouse and works
|
|
|
|
// even if the element is off-screen.
|
|
|
|
await emptyCartButton.evaluate( ( b ) => b.click() );
|
2021-05-05 11:52:27 +00:00
|
|
|
|
2022-08-12 14:23:08 +00:00
|
|
|
await expect( page ).not.toMatchElement(
|
|
|
|
`${ emptyCartBlock.class }[hidden]`
|
|
|
|
);
|
|
|
|
await expect( page ).toMatchElement(
|
|
|
|
`${ filledCartBlock.class }[hidden]`
|
|
|
|
);
|
2021-10-25 14:46:34 +00:00
|
|
|
|
2022-08-12 14:23:08 +00:00
|
|
|
await selectBlockByName( block.slug );
|
|
|
|
await clickBlockToolbarButton( 'Switch view', 'ariaLabel' );
|
|
|
|
const filledCartButton = await page.waitForXPath(
|
|
|
|
`//button[contains(@class,'components-dropdown-menu__menu-item')]//span[contains(text(), 'Filled Cart')]`
|
|
|
|
);
|
|
|
|
await filledCartButton.evaluate( ( b ) => b.click() );
|
2021-10-25 14:46:34 +00:00
|
|
|
|
2022-08-12 14:23:08 +00:00
|
|
|
await expect( page ).toMatchElement(
|
|
|
|
`${ emptyCartBlock.class }[hidden]`
|
|
|
|
);
|
|
|
|
await expect( page ).not.toMatchElement(
|
|
|
|
`${ filledCartBlock.class }[hidden]`
|
|
|
|
);
|
|
|
|
} );
|
2021-10-25 14:46:34 +00:00
|
|
|
|
2022-08-12 14:23:08 +00:00
|
|
|
describe( 'attributes', () => {
|
|
|
|
beforeEach( async () => {
|
|
|
|
await openDocumentSettingsSidebar();
|
2023-01-25 15:41:22 +00:00
|
|
|
await switchBlockInspectorTabWhenGutenbergIsInstalled(
|
|
|
|
'Settings'
|
|
|
|
);
|
2022-08-12 14:23:08 +00:00
|
|
|
await selectBlockByName(
|
|
|
|
'woocommerce/cart-order-summary-shipping-block'
|
2021-10-18 06:48:03 +00:00
|
|
|
);
|
2021-05-05 11:52:27 +00:00
|
|
|
} );
|
|
|
|
|
2022-08-12 14:23:08 +00:00
|
|
|
it( 'can toggle Shipping calculator', async () => {
|
2023-03-22 11:24:45 +00:00
|
|
|
const selector = ` .wc-block-components-totals-shipping__change-address__link`;
|
2022-08-12 14:23:08 +00:00
|
|
|
const toggleLabel = await findLabelWithText(
|
|
|
|
'Shipping calculator'
|
|
|
|
);
|
|
|
|
await expect( toggleLabel ).toToggleElement( selector );
|
2021-05-05 11:52:27 +00:00
|
|
|
} );
|
|
|
|
} );
|
2020-07-29 13:39:15 +00:00
|
|
|
} );
|
2021-10-18 06:48:03 +00:00
|
|
|
|
|
|
|
describe( 'in widget editor', () => {
|
|
|
|
it( "can't be inserted in a widget area", async () => {
|
|
|
|
await merchant.login();
|
|
|
|
await openWidgetEditor();
|
|
|
|
await closeModalIfExists();
|
|
|
|
await searchForBlock( block.name );
|
2022-12-30 11:47:33 +00:00
|
|
|
await page.waitForXPath(
|
|
|
|
`//button//span[text()='${ block.name }']`
|
|
|
|
);
|
2021-10-18 06:48:03 +00:00
|
|
|
const cartButton = await page.$x(
|
|
|
|
`//button//span[text()='${ block.name }']`
|
|
|
|
);
|
|
|
|
|
|
|
|
// This one match is the Cart widget.
|
|
|
|
expect( cartButton ).toHaveLength( 1 );
|
|
|
|
} );
|
|
|
|
} );
|
2020-03-04 14:32:31 +00:00
|
|
|
} );
|