woocommerce/plugins/woocommerce-blocks/tests/e2e/specs/frontend/cart.test.js

144 lines
4.1 KiB
JavaScript
Raw Normal View History

Ensure cart changes remain after using back button to get back to the cart (https://github.com/woocommerce/woocommerce-blocks/pull/3960) * Add cartUpdate middleware * Include timestamp for when cart data was generated * Add getCartFromApi action * Check whether cart can be hydrated or needs to be fetched from API * Add cartDataIsStale action and remove getCartFromApi action * Add isCartDataStale selector * Don't load cart until staleness check is complete * Add comment to ease worry about locaStorage execution * Correct doc block and fix typographical error * Cater for lastCartUpdate or the timestamp being undefined * Include @woocommerce/api and @woocommerce/e2e-utils * Add getNormalPagePermalink test utility This will allow us to get the permalink of a "normal" page, i.e. one that is not using the block editor. * Add getBlockPagePermalink test utility This will get the permalink for a page using the Block editor. * Emit action to update cart staleness in all execution paths * Add visitPostOfType function This will allow us to visit the editor page for a post based on its name and post type. * Add functions to get permalinks from editor pages * Add front-end tests for cart * Update package-lock.json * Create local function to ensure the page permalink is visible * Change functions for getting permalinks They were often failing for unknown reasons, these _somehow_ make them more stable. * Add logging for GitHub actions * Add more logging for tests * Add more logging for tests * Add more logging for tests * Wait for networkidle on back * Remove logging except timestamp * Wait for timestamp to be added to localStorage * Split tests to make runs slightly shorter * Wait for add to cart request before continuing test * Fix formatting in cart.test.js * Rename cartDataStale to setIsCartDataStale * Create constant for localStorage timestamp name * Rename cartDataIsStale to isCartDataStale This is for consistency, the action to change this is called setIsCartDataStale - I think this reads better. * Use cleaner logic when determining if the cart should render or fetch * Change docblock for isCartDataStale selector * Remove boolean cast from SET_IS_CART_DATA_STALE reducer * Set longer timeouts for frontend cart tests * Enclose cart staleness checks in condition/prevent unnecessary execution * Remove unnecessary boolean cast from selector * Use constant for local storage key in tests * Use new localstorage key in tests. cant access constants in page context
2021-03-24 13:28:11 +00:00
/**
* External dependencies
*/
import {
switchUserToAdmin,
ensureSidebarOpened,
openPublishPanel,
findSidebarPanelWithTitle,
findSidebarPanelToggleButtonWithTitle,
} from '@wordpress/e2e-test-utils';
import { shopper } from '@woocommerce/e2e-utils';
/**
* Internal dependencies
*/
import { visitPostOfType } from '../../../utils/visit-block-page';
import { getBlockPagePermalink, getNormalPagePermalink } from '../../../utils';
const block = {
name: 'Cart',
slug: 'woocommerce/cart',
class: '.wc-block-cart',
};
if ( process.env.WOOCOMMERCE_BLOCKS_PHASE < 2 )
// eslint-disable-next-line jest/no-focused-tests
test.only( `skipping ${ block.name } tests`, () => {} );
describe( `${ block.name } Block`, () => {
let cartBlockPermalink;
let productPermalink;
beforeAll( async () => {
await page.evaluate( () => window.localStorage.clear() );
await switchUserToAdmin();
cartBlockPermalink = await getBlockPagePermalink(
`${ block.name } Block`
);
await visitPostOfType( 'Woo Single #1', 'product' );
productPermalink = await getNormalPagePermalink();
await page.goto( productPermalink );
await shopper.addToCart();
} );
it( 'Adds a timestamp to localstorage when the cart is updated', async () => {
await jest.setTimeout( 60000 );
await page.goto( cartBlockPermalink );
await page.waitForFunction( () => {
const wcCartStore = wp.data.select( 'wc/store/cart' );
return (
! wcCartStore.isResolving( 'getCartData' ) &&
wcCartStore.hasFinishedResolution( 'getCartData', [] )
);
} );
await page.click(
'.wc-block-cart__main .wc-block-components-quantity-selector__button--plus'
);
await page.waitForFunction( () => {
const timeStamp = window.localStorage.getItem(
'wc-blocks_cart_update_timestamp'
);
return typeof timeStamp === 'string' && timeStamp;
} );
const timestamp = await page.evaluate( () => {
return window.localStorage.getItem(
'wc-blocks_cart_update_timestamp'
);
} );
expect( timestamp ).not.toBeNull();
} );
it( 'Shows the freshest cart data when using browser navigation buttons', async () => {
await jest.setTimeout( 60000 );
await page.goto( cartBlockPermalink );
await page.waitForFunction( () => {
const wcCartStore = wp.data.select( 'wc/store/cart' );
return (
! wcCartStore.isResolving( 'getCartData' ) &&
wcCartStore.hasFinishedResolution( 'getCartData', [] )
);
} );
await page.click(
'.wc-block-cart__main .wc-block-components-quantity-selector__button--plus'
);
await page.waitForResponse(
( response ) =>
response.url().includes( '/wc/store/cart/update-item' ) &&
response.status() === 200
);
const selectedValue = parseInt(
await page.$eval(
'.wc-block-cart__main .wc-block-components-quantity-selector__input',
( el ) => el.value
)
);
// This is to ensure we've clicked the right cart button.
expect( selectedValue ).toBeGreaterThan( 1 );
await page.click( '.wc-block-cart__submit-button' );
await page.waitForSelector( '.wc-block-checkout' );
await page.goBack( { waitUntil: 'networkidle0' } );
await page.waitForFunction( () => {
const timeStamp = window.localStorage.getItem(
'wc-blocks_cart_update_timestamp'
);
return typeof timeStamp === 'string' && timeStamp;
} );
const timestamp = await page.evaluate( () => {
return window.localStorage.getItem(
'wc-blocks_cart_update_timestamp'
);
} );
expect( timestamp ).not.toBeNull();
// We need this to check if the block is done loading.
await page.waitForFunction( () => {
const wcCartStore = wp.data.select( 'wc/store/cart' );
return (
! wcCartStore.isResolving( 'getCartData' ) &&
wcCartStore.hasFinishedResolution( 'getCartData', [] )
);
} );
// Then we check to ensure the stale cart action has been emitted, so it'll fetch the cart from the API.
await page.waitForFunction( () => {
const wcCartStore = wp.data.select( 'wc/store/cart' );
return wcCartStore.isCartDataStale() === true;
} );
const value = parseInt(
await page.$eval(
'.wc-block-components-quantity-selector__input',
( el ) => el.value
)
);
expect( value ).toBe( selectedValue );
} );
} );