Skip to content
This repository was archived by the owner on Feb 23, 2024. It is now read-only.

Commit bad1af8

Browse files
authored
Product Query - Add e2e tests for the filter by price block (#7351)
* Product Query - Add e2e tests for the Filter By Price block * fix comment * remove not used import * update description * update functions after merge * improve check
1 parent be5ce32 commit bad1af8

File tree

3 files changed

+119
-6
lines changed

3 files changed

+119
-6
lines changed

src/BlockTypes/ProductQuery.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ private function get_filter_by_price_query() {
327327
$max_price_query = empty( $max_price ) ? array() : [
328328
'key' => '_price',
329329
'value' => $max_price,
330-
'compare' => '<=',
330+
'compare' => '<',
331331
'type' => 'numeric',
332332
];
333333

tests/e2e/specs/shopper/filter-products-by-price.test.ts

Lines changed: 117 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import {
2424
useTheme,
2525
waitForAllProductsBlockLoaded,
2626
} from '../../utils';
27-
import { clickLink } from '../../../utils';
27+
import { clickLink, saveOrPublish } from '../../../utils';
2828

2929
const block = {
3030
name: 'Filter by Price',
@@ -38,6 +38,7 @@ const block = {
3838
frontend: {
3939
priceMaxAmount: '.wc-block-price-filter__amount--max',
4040
productsList: '.wc-block-grid__products > li',
41+
queryProductsList: '.wp-block-post-template > li',
4142
classicProductsList: '.products.columns-3 > li',
4243
submitButton: '.wc-block-components-filter-submit-button',
4344
},
@@ -83,7 +84,7 @@ describe( `${ block.name } Block`, () => {
8384
await page.goto( link );
8485
} );
8586

86-
it( 'should render', async () => {
87+
it( 'should render products', async () => {
8788
await waitForAllProductsBlockLoaded();
8889
const products = await page.$$( selectors.frontend.productsList );
8990

@@ -139,7 +140,7 @@ describe( `${ block.name } Block`, () => {
139140
await deleteAllTemplates( 'wp_template_part' );
140141
} );
141142

142-
it( 'should render', async () => {
143+
it( 'should render products', async () => {
143144
const products = await page.$$(
144145
selectors.frontend.classicProductsList
145146
);
@@ -179,7 +180,7 @@ describe( `${ block.name } Block`, () => {
179180
await expect( page ).toMatch( block.foundProduct );
180181
} );
181182

182-
it( 'should refresh the page only if the user click on button', async () => {
183+
it( 'should refresh the page only if the user clicks on button', async () => {
183184
await goToTemplateEditor( {
184185
postId: productCatalogTemplateId,
185186
} );
@@ -201,10 +202,13 @@ describe( `${ block.name } Block`, () => {
201202
await page.waitForSelector( block.class + '.is-loading', {
202203
hidden: true,
203204
} );
205+
204206
expect( isRefreshed ).not.toBeCalled();
205207

206208
await setMaxPrice();
207209

210+
expect( isRefreshed ).not.toBeCalled();
211+
208212
await clickLink( selectors.frontend.submitButton );
209213

210214
await page.waitForSelector(
@@ -226,4 +230,113 @@ describe( `${ block.name } Block`, () => {
226230
);
227231
} );
228232
} );
233+
234+
describe( 'with Product Query Block', () => {
235+
let editorPageUrl = '';
236+
let frontedPageUrl = '';
237+
beforeAll( async () => {
238+
await switchUserToAdmin();
239+
await createNewPost( {
240+
postType: 'post',
241+
title: block.name,
242+
} );
243+
244+
await insertBlock( 'Product Query' );
245+
await insertBlock( block.name );
246+
await insertBlock( 'Active Product Filters' );
247+
await page.waitForNetworkIdle();
248+
await publishPost();
249+
250+
editorPageUrl = page.url();
251+
frontedPageUrl = await page.evaluate( () =>
252+
wp.data.select( 'core/editor' ).getPermalink()
253+
);
254+
await page.goto( frontedPageUrl );
255+
} );
256+
257+
it( 'should render products', async () => {
258+
const products = await page.$$(
259+
selectors.frontend.queryProductsList
260+
);
261+
262+
expect( products ).toHaveLength( 5 );
263+
} );
264+
265+
it( 'should show only products that match the filter', async () => {
266+
const isRefreshed = jest.fn( () => void 0 );
267+
page.on( 'load', isRefreshed );
268+
269+
await page.waitForSelector( block.class + '.is-loading', {
270+
hidden: true,
271+
} );
272+
273+
await expect( page ).toMatch( block.foundProduct );
274+
expect( isRefreshed ).not.toBeCalled();
275+
276+
await Promise.all( [ setMaxPrice(), page.waitForNavigation() ] );
277+
278+
await page.waitForSelector( selectors.frontend.queryProductsList );
279+
const products = await page.$$(
280+
selectors.frontend.queryProductsList
281+
);
282+
283+
const pageURL = page.url();
284+
const parsedURL = new URL( pageURL );
285+
286+
expect( isRefreshed ).toBeCalledTimes( 1 );
287+
expect( products ).toHaveLength( 1 );
288+
289+
expect( parsedURL.search ).toEqual(
290+
block.urlSearchParamWhenFilterIsApplied
291+
);
292+
await expect( page ).toMatch( block.foundProduct );
293+
} );
294+
295+
it( 'should refresh the page only if the user click on button', async () => {
296+
await page.goto( editorPageUrl );
297+
298+
await openBlockEditorSettings();
299+
await selectBlockByName( block.slug );
300+
await page.waitForXPath(
301+
block.selectors.editor.filterButtonToggle
302+
);
303+
const [ filterButtonToggle ] = await page.$x(
304+
block.selectors.editor.filterButtonToggle
305+
);
306+
await filterButtonToggle.click();
307+
308+
await saveOrPublish();
309+
await page.goto( frontedPageUrl );
310+
311+
const isRefreshed = jest.fn( () => void 0 );
312+
page.on( 'load', isRefreshed );
313+
await page.waitForSelector( block.class + '.is-loading', {
314+
hidden: true,
315+
} );
316+
317+
expect( isRefreshed ).not.toBeCalled();
318+
319+
await setMaxPrice();
320+
321+
expect( isRefreshed ).not.toBeCalled();
322+
323+
await clickLink( selectors.frontend.submitButton );
324+
325+
await page.waitForSelector( selectors.frontend.queryProductsList );
326+
327+
const products = await page.$$(
328+
selectors.frontend.queryProductsList
329+
);
330+
331+
const pageURL = page.url();
332+
const parsedURL = new URL( pageURL );
333+
334+
expect( isRefreshed ).toBeCalledTimes( 1 );
335+
expect( products ).toHaveLength( 1 );
336+
await expect( page ).toMatch( block.foundProduct );
337+
expect( parsedURL.search ).toEqual(
338+
block.urlSearchParamWhenFilterIsApplied
339+
);
340+
} );
341+
} );
229342
} );

tests/e2e/specs/shopper/filter-products-by-rating.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ describe( `${ block.name } Block`, () => {
157157

158158
await waitForCanvas();
159159
await selectBlockByName( block.slug );
160-
await openBlockEditorSettings( { isFSEEditor: true } );
160+
await openBlockEditorSettings();
161161
await page.waitForXPath(
162162
block.selectors.editor.filterButtonToggle
163163
);

0 commit comments

Comments
 (0)