2022-05-23 12:05:30 +00:00
|
|
|
|
/**
|
|
|
|
|
* External dependencies
|
|
|
|
|
*/
|
2022-02-21 11:01:56 +00:00
|
|
|
|
import { URL } from 'url';
|
2022-05-23 12:05:30 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Internal dependencies
|
|
|
|
|
*/
|
2022-03-09 10:16:24 +00:00
|
|
|
|
import { BASE_URL, useTheme } from '../../utils';
|
2022-02-21 11:01:56 +00:00
|
|
|
|
const SELECTORS = {
|
|
|
|
|
productArchivePage: {
|
|
|
|
|
paginationUI: '.woocommerce-pagination',
|
|
|
|
|
productContainers: '.products .product',
|
|
|
|
|
productsList: '.products',
|
|
|
|
|
resultsCount: '.woocommerce-result-count',
|
2022-02-21 11:38:36 +00:00
|
|
|
|
title: '.page-title',
|
2022-02-21 11:01:56 +00:00
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
2022-02-21 11:38:36 +00:00
|
|
|
|
/**
|
|
|
|
|
* 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 x–y of z results'
|
|
|
|
|
// b) 'Showing all x results'
|
|
|
|
|
return {
|
|
|
|
|
displayedCount: Number( matches[ 1 ] ) || Number( matches[ 0 ] ),
|
|
|
|
|
shouldHavePaginationUI: !! matches[ 1 ],
|
|
|
|
|
};
|
|
|
|
|
} );
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-22 22:34:43 +00:00
|
|
|
|
describe( 'Classic Template blocks', () => {
|
2022-03-09 10:16:24 +00:00
|
|
|
|
useTheme( 'emptytheme' );
|
2022-02-21 11:01:56 +00:00
|
|
|
|
|
|
|
|
|
describe( 'Product Archive block', () => {
|
|
|
|
|
it( 'renders a list of products with their count and pagination', async () => {
|
|
|
|
|
const { productArchivePage } = SELECTORS;
|
|
|
|
|
|
2022-04-01 08:22:54 +00:00
|
|
|
|
await page.goto(
|
|
|
|
|
new URL( '/?post_type=product', BASE_URL ).toString()
|
|
|
|
|
);
|
2022-02-21 11:01:56 +00:00
|
|
|
|
|
|
|
|
|
await page.waitForSelector( productArchivePage.productsList );
|
|
|
|
|
await page.waitForSelector( productArchivePage.resultsCount );
|
|
|
|
|
|
2022-02-21 11:38:36 +00:00
|
|
|
|
const {
|
|
|
|
|
displayedCount,
|
|
|
|
|
shouldHavePaginationUI,
|
|
|
|
|
} = await extractPaginationData();
|
|
|
|
|
|
|
|
|
|
if ( shouldHavePaginationUI ) {
|
|
|
|
|
await expect( page ).toMatchElement(
|
|
|
|
|
productArchivePage.paginationUI
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const $productElements = await page.$$(
|
|
|
|
|
productArchivePage.productContainers
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
expect( $productElements ).toHaveLength( Number( displayedCount ) );
|
|
|
|
|
} );
|
|
|
|
|
} );
|
|
|
|
|
|
2022-02-28 07:00:01 +00:00
|
|
|
|
describe( 'Products by Category block', () => {
|
2022-02-21 11:38:36 +00:00
|
|
|
|
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
|
2022-04-01 08:22:54 +00:00
|
|
|
|
).toString()
|
2022-02-21 11:01:56 +00:00
|
|
|
|
);
|
|
|
|
|
|
2022-02-21 11:38:36 +00:00
|
|
|
|
await expect( page ).toMatchElement( productArchivePage.title, {
|
|
|
|
|
text: CATEGORY_NAME,
|
|
|
|
|
} );
|
|
|
|
|
|
|
|
|
|
await page.waitForSelector( productArchivePage.productsList );
|
|
|
|
|
await page.waitForSelector( productArchivePage.resultsCount );
|
|
|
|
|
|
|
|
|
|
const {
|
|
|
|
|
displayedCount,
|
|
|
|
|
shouldHavePaginationUI,
|
|
|
|
|
} = await extractPaginationData();
|
|
|
|
|
|
2022-02-21 11:01:56 +00:00
|
|
|
|
if ( shouldHavePaginationUI ) {
|
|
|
|
|
await expect( page ).toMatchElement(
|
|
|
|
|
productArchivePage.paginationUI
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const $productElements = await page.$$(
|
|
|
|
|
productArchivePage.productContainers
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
expect( $productElements ).toHaveLength( Number( displayedCount ) );
|
|
|
|
|
} );
|
|
|
|
|
} );
|
2022-02-21 12:26:22 +00:00
|
|
|
|
|
2022-02-28 07:00:01 +00:00
|
|
|
|
describe( 'Products by Tag block', () => {
|
2022-02-21 12:26:22 +00:00
|
|
|
|
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(
|
2022-04-01 08:22:54 +00:00
|
|
|
|
new URL(
|
|
|
|
|
`/product-tag/${ TAG_NAME.toLowerCase() }`,
|
|
|
|
|
BASE_URL
|
|
|
|
|
).toString()
|
2022-02-21 12:26:22 +00:00
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
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 );
|
|
|
|
|
} );
|
|
|
|
|
} );
|
2022-02-21 11:01:56 +00:00
|
|
|
|
} );
|