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

Commit 4ef263e

Browse files
authored
Merge branch 'trunk' into dev/refactor-prod-control-ts
2 parents de3c8a8 + 0ade961 commit 4ef263e

File tree

11 files changed

+118
-132
lines changed

11 files changed

+118
-132
lines changed

assets/js/base/context/hooks/collections/use-collection.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ export interface useCollectionOptions {
4949
isEditor?: boolean;
5050
}
5151

52-
export const useCollection = (
52+
export const useCollection = < T >(
5353
options: useCollectionOptions
5454
): {
55-
results: unknown;
55+
results: T[];
5656
isLoading: boolean;
5757
} => {
5858
const {
@@ -68,7 +68,7 @@ export const useCollection = (
6868
'the resource properties.'
6969
);
7070
}
71-
const currentResults = useRef< { results: unknown; isLoading: boolean } >( {
71+
const currentResults = useRef< { results: T[]; isLoading: boolean } >( {
7272
results: [],
7373
isLoading: true,
7474
} );
@@ -102,7 +102,7 @@ export const useCollection = (
102102
}
103103

104104
return {
105-
results: store.getCollection< T >( ...args ),
105+
results: store.getCollection< T[] >( ...args ),
106106
isLoading: ! store.hasFinishedResolution(
107107
'getCollection',
108108
args

assets/js/base/context/hooks/use-store-products.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,17 @@ export const useStoreProducts = (
3535
namespace: '/wc/store/v1',
3636
resourceName: 'products',
3737
};
38-
const { results: products, isLoading: productsLoading } = useCollection( {
39-
...collectionOptions,
40-
query,
41-
} );
38+
const { results: products, isLoading: productsLoading } =
39+
useCollection< ProductResponseItem >( {
40+
...collectionOptions,
41+
query,
42+
} );
4243
const { value: totalProducts } = useCollectionHeader( 'x-wp-total', {
4344
...collectionOptions,
4445
query,
4546
} );
4647
return {
47-
products: products as ProductResponseItem[], // TODO: Remove this once getCollection selector and resolver is converted to TS.
48+
products,
4849
totalProducts: parseInt( totalProducts as string, 10 ),
4950
productsLoading,
5051
};

assets/js/blocks/active-filters/active-attribute-filters.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { __ } from '@wordpress/i18n';
1111
import { getSettingWithCoercion } from '@woocommerce/settings';
1212
import {
1313
AttributeObject,
14+
AttributeTerm,
1415
isAttributeQueryCollection,
1516
isAttributeTermCollection,
1617
isBoolean,
@@ -47,7 +48,7 @@ const ActiveAttributeFilters = ( {
4748
displayStyle,
4849
isLoadingCallback,
4950
}: ActiveAttributeFiltersProps ) => {
50-
const { results, isLoading } = useCollection( {
51+
const { results, isLoading } = useCollection< AttributeTerm >( {
5152
namespace: '/wc/store/v1',
5253
resourceName: 'products/attributes/terms',
5354
resourceValues: [ attributeObject.id ],

assets/js/blocks/attribute-filter/block.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { getSettingWithCoercion } from '@woocommerce/settings';
1919
import { getQueryArgs, removeQueryArgs } from '@wordpress/url';
2020
import {
2121
AttributeQuery,
22+
AttributeTerm,
2223
isAttributeQueryCollection,
2324
isBoolean,
2425
isString,
@@ -124,7 +125,7 @@ const AttributeFilterBlock = ( {
124125
useQueryStateByKey( 'attributes', [] );
125126

126127
const { results: attributeTerms, isLoading: attributeTermsLoading } =
127-
useCollection( {
128+
useCollection< AttributeTerm >( {
128129
namespace: '/wc/store/v1',
129130
resourceName: 'products/attributes/terms',
130131
resourceValues: [ attributeObject?.id || 0 ],

assets/js/blocks/cart/inner-blocks/empty-cart-block/frontend.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,15 @@ const FrontendBlock = ( {
1919
} ): JSX.Element | null => {
2020
const { cartItems, cartIsLoading } = useStoreCart();
2121
useEffect( () => {
22+
if ( cartItems.length !== 0 || cartIsLoading ) {
23+
return;
24+
}
2225
dispatchEvent( 'wc-blocks_render_blocks_frontend', {
2326
element: document.body.querySelector(
2427
'.wp-block-woocommerce-cart'
2528
),
2629
} );
27-
}, [] );
30+
}, [ cartIsLoading, cartItems ] );
2831
if ( ! cartIsLoading && cartItems.length === 0 ) {
2932
return <div className={ className }>{ children }</div>;
3033
}

assets/js/interactivity/index.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,4 @@ document.addEventListener( 'DOMContentLoaded', async () => {
1212
registerDirectives();
1313
await init();
1414
afterLoads.forEach( ( afterLoad ) => afterLoad( rawStore ) );
15-
// eslint-disable-next-line no-console
16-
console.log( 'Interactivity API started' );
1715
} );
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
/**
3+
* Title: Product Collection Grid
4+
* Slug: woocommerce-blocks/product-collection-grid
5+
* Categories: WooCommerce
6+
* Inserter: false
7+
*/
8+
?>
9+
10+
<!-- wp:woocommerce/product-collection {"queryId":0,"query":{"perPage":4,"pages":0,"offset":0,"postType":"product","order":"asc","orderBy":"title","author":"","search":"","exclude":[],"sticky":"","inherit":false,"taxQuery":{},"parents":[],"isProductCollectionBlock":true,"woocommerceOnSale":false,"woocommerceStockStatus":["instock","outofstock","onbackorder"],"woocommerceAttributes":[],"woocommerceHandPickedProducts":[]},"tagName":"div","displayLayout":{"type":"flex","columns":4}} -->
11+
<div class="wp-block-woocommerce-product-collection"><!-- wp:woocommerce/product-template -->
12+
<!-- wp:woocommerce/product-image {"imageSizing":"thumbnail","isDescendentOfQueryLoop":true} /-->
13+
14+
<!-- wp:post-title {"textAlign":"left","level":3,"isLink":true,"style":{"spacing":{"margin":{"bottom":"0.75rem","top":"0"}},"typography":{"lineHeight":"1"}},"fontSize":"small","__woocommerceNamespace":"woocommerce/product-collection/product-title"} /-->
15+
16+
<!-- wp:woocommerce/product-price {"isDescendentOfQueryLoop":true,"textAlign":"left","style":{"typography":{"fontSize":"0.8rem","lineHeight":"1","fontStyle":"normal","fontWeight":"600"}}} /-->
17+
<!-- /wp:woocommerce/product-template -->
18+
19+
<!-- wp:query-pagination {"layout":{"type":"flex","justifyContent":"center"}} -->
20+
<!-- wp:query-pagination-previous /-->
21+
22+
<!-- wp:query-pagination-numbers /-->
23+
24+
<!-- wp:query-pagination-next /-->
25+
<!-- /wp:query-pagination -->
26+
27+
<!-- wp:query-no-results -->
28+
<!-- wp:paragraph {"placeholder":"Add text or blocks that will display when a query returns no results."} -->
29+
<p></p>
30+
<!-- /wp:paragraph -->
31+
<!-- /wp:query-no-results --></div>
32+
<!-- /wp:woocommerce/product-collection -->

src/BlockTypes/ProductButton.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ protected function enqueue_assets( array $attributes ) {
6161
*/
6262
public function dequeue_add_to_cart_scripts() {
6363
wp_dequeue_script( 'wc-add-to-cart' );
64-
wp_dequeue_script( 'wc-add-to-cart-variation' );
6564
}
6665

6766
/**

tests/e2e/bin/posts/mini-cart.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
11
<!-- wp:woocommerce/mini-cart /-->
2+
3+
<!-- wp:woocommerce/all-products {"columns":3,"rows":3,"alignButtons":false,"contentVisibility":{"orderBy":true},"orderby":"date","layoutConfig":[["woocommerce/product-image",{"imageSizing":"thumbnail"}],["woocommerce/product-title"],["woocommerce/product-price"],["woocommerce/product-rating"],["woocommerce/product-button"]]} -->
4+
<div class="wp-block-woocommerce-all-products wc-block-all-products" data-attributes="{&quot;alignButtons&quot;:false,&quot;columns&quot;:3,&quot;contentVisibility&quot;:{&quot;orderBy&quot;:true},&quot;isPreview&quot;:false,&quot;layoutConfig&quot;:[[&quot;woocommerce/product-image&quot;,{&quot;imageSizing&quot;:&quot;thumbnail&quot;}],[&quot;woocommerce/product-title&quot;],[&quot;woocommerce/product-price&quot;],[&quot;woocommerce/product-rating&quot;],[&quot;woocommerce/product-button&quot;]],&quot;orderby&quot;:&quot;date&quot;,&quot;rows&quot;:3}"></div>
5+
<!-- /wp:woocommerce/all-products -->
Lines changed: 63 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -1,132 +1,79 @@
11
/**
22
* External dependencies
33
*/
4-
import { BlockData } from '@woocommerce/e2e-types';
54
import { test, expect } from '@woocommerce/e2e-playwright-utils';
5+
import { Page } from '@playwright/test';
66

7-
const blockData: BlockData = {
8-
name: 'woocommerce/mini-cart',
9-
mainClass: '.wc-block-mini-cart',
10-
selectors: {
11-
frontend: {
12-
drawer: '.wc-block-mini-cart__drawer',
13-
drawerCloseButton: 'button[aria-label="Close"]',
14-
},
15-
editor: {},
16-
},
7+
const openMiniCart = async ( page: Page ) => {
8+
await page.getByLabel( 'items in cart,' ).hover();
9+
await page.getByLabel( 'items in cart,' ).click();
1710
};
1811

19-
const getMiniCartButton = async ( { page } ) => {
20-
return page.getByLabel( '0 items in cart, total price of $0.00' );
21-
};
12+
test.describe( `Mini Cart Block`, () => {
13+
/**
14+
* This is a workaround to run tests in isolation.
15+
* Ideally, the test should be run in isolation by default. But we're
16+
* overriding the storageState in config which make all tests run with admin
17+
* user.
18+
*/
19+
test.use( {
20+
storageState: {
21+
origins: [],
22+
cookies: [],
23+
},
24+
} );
25+
26+
test.beforeEach( async ( { page } ) => {
27+
await page.goto( `/mini-cart-block`, { waitUntil: 'commit' } );
28+
} );
29+
30+
test( 'should open the empty cart drawer', async ( { page } ) => {
31+
await openMiniCart( page );
32+
33+
await expect( page.getByRole( 'dialog' ) ).toContainText(
34+
'Your cart is currently empty!'
35+
);
36+
} );
37+
38+
test( 'should close the drawer when clicking on the close button', async ( {
39+
page,
40+
} ) => {
41+
await openMiniCart( page );
42+
43+
await expect( page.getByRole( 'dialog' ) ).toContainText(
44+
'Your cart is currently empty!'
45+
);
46+
47+
await page.getByRole( 'button', { name: 'Close' } ).click();
2248

23-
test.describe( `${ blockData.name } Block`, () => {
24-
test.describe( `standalone`, () => {
25-
test.beforeEach( async ( { admin, page, editor } ) => {
26-
await admin.createNewPost( { legacyCanvas: true } );
27-
await editor.insertBlock( { name: blockData.name } );
28-
await editor.publishPost();
29-
const url = new URL( page.url() );
30-
const postId = url.searchParams.get( 'post' );
31-
await page.goto( `/?p=${ postId }`, { waitUntil: 'commit' } );
32-
} );
33-
34-
test( 'should open the empty cart drawer', async ( { page } ) => {
35-
const miniCartButton = await getMiniCartButton( { page } );
36-
37-
await miniCartButton.click();
38-
39-
await expect(
40-
page.locator( blockData.selectors.frontend.drawer ).first()
41-
).toHaveText( 'Your cart is currently empty!' );
42-
} );
43-
44-
test( 'should close the drawer when clicking on the close button', async ( {
45-
page,
46-
} ) => {
47-
const miniCartButton = await getMiniCartButton( { page } );
48-
49-
await miniCartButton.click();
50-
51-
await expect(
52-
page.locator( blockData.selectors.frontend.drawer ).first()
53-
).toHaveText( 'Your cart is currently empty!' );
54-
55-
// Wait for the drawer to fully open.
56-
await page.waitForSelector(
57-
blockData.selectors.frontend.drawerCloseButton
58-
);
59-
60-
const closeButton = page.locator(
61-
blockData.selectors.frontend.drawerCloseButton
62-
);
63-
64-
await closeButton?.click();
65-
66-
// Wait for the drawer to fully close.
67-
await page.waitForSelector( blockData.selectors.frontend.drawer, {
68-
state: 'detached',
69-
} );
70-
71-
expect(
72-
await page
73-
.locator( blockData.selectors.frontend.drawer )
74-
.count()
75-
).toEqual( 0 );
76-
} );
77-
78-
test( 'should close the drawer when clicking outside the drawer', async ( {
79-
page,
80-
} ) => {
81-
const miniCartButton = await getMiniCartButton( { page } );
82-
83-
await miniCartButton.click();
84-
85-
await expect(
86-
page.locator( blockData.selectors.frontend.drawer ).first()
87-
).toHaveText( 'Your cart is currently empty!' );
88-
89-
// Wait for the drawer to fully open.
90-
await page.waitForSelector(
91-
blockData.selectors.frontend.drawerCloseButton
92-
);
93-
94-
await page.mouse.click( 50, 200 );
95-
96-
// Wait for the drawer to fully close.
97-
await page.waitForSelector( blockData.selectors.frontend.drawer, {
98-
state: 'detached',
99-
} );
100-
101-
expect(
102-
await page
103-
.locator( blockData.selectors.frontend.drawer )
104-
.count()
105-
).toEqual( 0 );
106-
} );
49+
await expect( page.getByRole( 'dialog' ) ).toHaveCount( 0 );
10750
} );
10851

109-
test.describe( `with All products Block`, () => {
110-
test.beforeEach( async ( { admin, page, editor } ) => {
111-
await admin.createNewPost( { legacyCanvas: true } );
112-
await editor.insertBlock( { name: blockData.name } );
113-
await editor.insertBlock( { name: 'woocommerce/all-products' } );
114-
await editor.publishPost();
115-
const url = new URL( page.url() );
116-
const postId = url.searchParams.get( 'post' );
117-
await page.goto( `/?p=${ postId }`, { waitUntil: 'commit' } );
118-
} );
52+
test( 'should close the drawer when clicking outside the drawer', async ( {
53+
page,
54+
} ) => {
55+
await openMiniCart( page );
11956

120-
test( 'should open the filled cart drawer', async ( { page } ) => {
121-
const miniCartButton = await getMiniCartButton( { page } );
57+
await expect( page.getByRole( 'dialog' ) ).toContainText(
58+
'Your cart is currently empty!'
59+
);
60+
61+
await expect(
62+
page.getByRole( 'button', { name: 'Close' } )
63+
).toBeInViewport();
64+
65+
await page.mouse.click( 50, 200 );
66+
67+
await expect( page.getByRole( 'dialog' ) ).toHaveCount( 0 );
68+
} );
12269

123-
await page.click( 'text=Add to cart' );
70+
test( 'should open the filled cart drawer', async ( { page } ) => {
71+
await page.click( 'text=Add to cart' );
12472

125-
await miniCartButton.click();
73+
await openMiniCart( page );
12674

127-
await expect(
128-
page.locator( '.wc-block-mini-cart__title' ).first()
129-
).toHaveText( 'Your cart (1 item)' );
130-
} );
75+
await expect( page.getByRole( 'dialog' ) ).toContainText(
76+
'Your cart (1 item)'
77+
);
13178
} );
13279
} );

0 commit comments

Comments
 (0)