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

150 lines
4.0 KiB
JavaScript
Raw Normal View History

import { URL } from 'url';
import { activateTheme } from '@wordpress/e2e-test-utils';
import { BASE_URL } from '../../utils';
const SELECTORS = {
productArchivePage: {
paginationUI: '.woocommerce-pagination',
productContainers: '.products .product',
productsList: '.products',
resultsCount: '.woocommerce-result-count',
title: '.page-title',
},
};
/**
* Extracts data regarding pagination from the page
*
* @return {Promise<{ displayedCount: number, shouldHavePaginationUI: boolean }>} How many products are displayed
* and whether there should be a pagination UI (i.e. the displayed products are lesser than the total
* number of products).
*/
function extractPaginationData() {
const { productArchivePage } = SELECTORS;
return page.$eval( productArchivePage.resultsCount, ( $el ) => {
const resultsCountRegEx = /1(\d+)|\d+/;
const matches = $el.textContent.match( resultsCountRegEx );
// Depending on pagination, string can be either:
// a) 'Showing xy of z results'
// b) 'Showing all x results'
return {
displayedCount: Number( matches[ 1 ] ) || Number( matches[ 0 ] ),
shouldHavePaginationUI: !! matches[ 1 ],
};
} );
}
describe( 'Legacy Template blocks', () => {
beforeAll( async () => {
await activateTheme( 'emptytheme' );
} );
afterAll( async () => {
Critical flow: Can see correct tax (https://github.com/woocommerce/woocommerce-blocks/pull/5900) * Tax is displayed correctly e2e test * Add extra utils to shopper * add single e2e test script oin package.json * Feedback changes from Niels * Test tax is correct on summary page * Check if CI test passes * Longer timeout for tax test * change timeout in jest config * increate jest timeout * Test * Test * Test * setTimeout outside of tests to 120000 * Address raluca's feedback and timeout everywheregaa * Increase timeout on for search box * set jest timeout to 120000 * Raluca's suggestions * Debug * Increase search timeout to 5000 * Only run Tax e2e tests in CI for quicker debug - REMOVE THIS * use waitforSelector * 30s delay for toMatchElement * Let'shope this works * Remove toMatchElement * waitForSelector * Run debug test only * Debug tax test * debug taxes * shopper using toMatchElement again * Remove the 30000 timeout delay from toMatchElement * Run all tests but only once * Remove tax test * Revert "Remove tax test" This reverts commit 7db34120e1e91f4fd26514fdb9525cac2a6066bb. * Delete the minicart test * Revert "Revert "Remove tax test"" This reverts commit 31dd654e52f37fcd02ccd25336958248bef495c6. * Revert "Delete the minicart test" This reverts commit 5595f0834a975d097a300bc0c7cbf643caa8b764. * tax test * block addToCart * Put back block functions * Refactor block.addToCart * mini-cart e2e test uses shopper.block.emptyCart() * only run shopper tests in CI * Run frontend and shopper tests and change waitFor -> waitForTimeout * Run frontend tests in CI * Run CI e2e tests again * Skip legacy template blocks * Skip legacy template test but run all others * Switch to storefront instead of twentytwentyone after legacy-template-blocks tests finish * Tidy up * fix typo * Fix ESLint issue Co-authored-by: Saad Tarhi <saad.trh@gmail.com> Co-authored-by: Saad Tarhi <saad.tarhi@automattic.com>
2022-03-08 14:50:16 +00:00
await activateTheme( 'storefront' );
} );
describe( 'Product Archive block', () => {
it( 'renders a list of products with their count and pagination', async () => {
const { productArchivePage } = SELECTORS;
await page.goto( new URL( '/?post_type=product', BASE_URL ) );
await page.waitForSelector( productArchivePage.productsList );
await page.waitForSelector( productArchivePage.resultsCount );
const {
displayedCount,
shouldHavePaginationUI,
} = await extractPaginationData();
if ( shouldHavePaginationUI ) {
await expect( page ).toMatchElement(
productArchivePage.paginationUI
);
}
const $productElements = await page.$$(
productArchivePage.productContainers
);
expect( $productElements ).toHaveLength( Number( displayedCount ) );
} );
} );
describe( 'Products by Category block', () => {
it( 'renders a list of products with their count, pagination and the category title', async () => {
const CATEGORY_NAME = 'Uncategorized';
const { productArchivePage } = SELECTORS;
await page.goto(
new URL(
`/product-category/${ CATEGORY_NAME.toLowerCase() }`,
BASE_URL
)
);
await expect( page ).toMatchElement( productArchivePage.title, {
text: CATEGORY_NAME,
} );
await page.waitForSelector( productArchivePage.productsList );
await page.waitForSelector( productArchivePage.resultsCount );
const {
displayedCount,
shouldHavePaginationUI,
} = await extractPaginationData();
if ( shouldHavePaginationUI ) {
await expect( page ).toMatchElement(
productArchivePage.paginationUI
);
}
const $productElements = await page.$$(
productArchivePage.productContainers
);
expect( $productElements ).toHaveLength( Number( displayedCount ) );
} );
} );
describe( 'Products by Tag block', () => {
it( 'renders a list of products with their count, pagination and the tag title', async () => {
const TAG_NAME = 'Newest';
const { productArchivePage } = SELECTORS;
await page.goto(
new URL( `/product-tag/${ TAG_NAME.toLowerCase() }`, BASE_URL )
);
await expect( page ).toMatchElement( productArchivePage.title, {
text: TAG_NAME,
} );
await page.waitForSelector( productArchivePage.productsList );
await page.waitForSelector( productArchivePage.resultsCount );
const {
displayedCount,
shouldHavePaginationUI,
} = await extractPaginationData();
if ( shouldHavePaginationUI ) {
await expect( page ).toMatchElement(
productArchivePage.paginationUI
);
}
const $productElements = await page.$$(
productArchivePage.productContainers
);
expect( $productElements ).toHaveLength( displayedCount );
} );
} );
} );