@@ -24,7 +24,7 @@ import {
2424 useTheme ,
2525 waitForAllProductsBlockLoaded ,
2626} from '../../utils' ;
27- import { clickLink } from '../../../utils' ;
27+ import { clickLink , saveOrPublish } from '../../../utils' ;
2828
2929const 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} ) ;
0 commit comments