Commit 17a68fded49 for woocommerce
commit 17a68fded493923eee2ad6c05cfb79af0234866d
Author: Vlad Olaru <vlad.olaru@automattic.com>
Date: Mon Sep 14 11:36:31 2026 +0300
[tests] Add Jest coverage for Catalog Sorting block registration (#68661)
test(blocks): Add Jest coverage for Catalog Sorting block registration
The Catalog Sorting block's editor behaviour had no unit test. A new
Jest suite covers it: that the block registers its real edit
component, that its save callback is dynamic and returns null, and
that the editor placeholder renders the default sorting option
without a visual label.
The block's single browser test is kept exactly as it is. The
migration branch deletes it; this branch does not, because the block
would then have no browser coverage at all.
That test asserts on the editor canvas rather than a rendered page,
which looks like something worth improving. It is, but not here. The
block does render on the shop archive when the archive uses the
blockified Product Catalog template, which is the default. It renders
nothing when wc_blocks_use_blockified_product_grid_block_as_template
is false, because the archive then falls back to the classic template
and woocommerce_catalog_ordering() takes the legacy path instead.
Five specs in tests/e2e set that option to false and do not restore
it, so a front-end assertion here passes or fails according to what
ran before it. A front-end test for this block would need to pin the
option itself, which is a change to make deliberately rather than as a
side effect of moving unit coverage.
Nothing is removed: 104 lines of new test, no deletions.
Consolidates the mega-branch slices:
- Slice 091
Refs TESTOPS-234
Refs #68046
Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
diff --git a/plugins/woocommerce/changelog/testops-234-catalog-sorting b/plugins/woocommerce/changelog/testops-234-catalog-sorting
new file mode 100644
index 00000000000..053d8e5a33c
--- /dev/null
+++ b/plugins/woocommerce/changelog/testops-234-catalog-sorting
@@ -0,0 +1,3 @@
+Significance: patch
+Type: dev
+Comment: Add Jest coverage for the Catalog Sorting block's registration, dynamic save callback and editor placeholder. No E2E test is removed.
diff --git a/plugins/woocommerce/client/blocks/assets/js/blocks/catalog-sorting/test/index.tsx b/plugins/woocommerce/client/blocks/assets/js/blocks/catalog-sorting/test/index.tsx
new file mode 100644
index 00000000000..8a063b878b7
--- /dev/null
+++ b/plugins/woocommerce/client/blocks/assets/js/blocks/catalog-sorting/test/index.tsx
@@ -0,0 +1,104 @@
+/**
+ * External dependencies
+ */
+import { render, screen } from '@testing-library/react';
+import { registerBlockType } from '@wordpress/blocks';
+import type { ReactNode } from 'react';
+
+/**
+ * Internal dependencies
+ */
+import metadata from '../block.json';
+import Edit from '../edit';
+
+jest.mock( '@wordpress/blocks', () => ( {
+ registerBlockType: jest.fn(),
+} ) );
+
+jest.mock( '@wordpress/block-editor', () => ( {
+ InspectorControls: ( { children }: { children: ReactNode } ) => (
+ <div>{ children }</div>
+ ),
+ useBlockProps: ( props: Record< string, unknown > ) => props,
+} ) );
+
+jest.mock( '@wordpress/components', () => ( {
+ Disabled: ( { children }: { children: ReactNode } ) => <>{ children }</>,
+ ToggleControl: () => null,
+ __experimentalToolsPanel: ( { children }: { children: ReactNode } ) => (
+ <div>{ children }</div>
+ ),
+ __experimentalToolsPanelItem: ( { children }: { children: ReactNode } ) => (
+ <div>{ children }</div>
+ ),
+} ) );
+
+type RegisteredBlock = {
+ attributes: typeof metadata.attributes;
+ edit: unknown;
+ save: () => null;
+};
+
+const loadRegisteredBlock = () => {
+ let isolatedEdit: unknown;
+
+ jest.isolateModules( () => {
+ isolatedEdit = (
+ jest.requireActual( '../edit' ) as {
+ default: unknown;
+ }
+ ).default;
+ jest.requireActual( '../index' );
+ } );
+
+ expect( registerBlockType ).toHaveBeenCalledTimes( 1 );
+ const [ registeredMetadata, settings ] = ( registerBlockType as jest.Mock )
+ .mock.calls[ 0 ];
+
+ expect( registeredMetadata ).toEqual( metadata );
+ expect( registeredMetadata.name ).toBe( 'woocommerce/catalog-sorting' );
+ expect( settings.attributes ).toEqual( metadata.attributes );
+
+ return {
+ registeredMetadata,
+ settings: settings as RegisteredBlock,
+ isolatedEdit,
+ };
+};
+
+describe( 'Catalog Sorting block registration', () => {
+ beforeEach( () => {
+ jest.clearAllMocks();
+ } );
+
+ it( 'registers the real editor component', () => {
+ const { registeredMetadata, settings, isolatedEdit } =
+ loadRegisteredBlock();
+
+ expect( registeredMetadata ).toEqual( metadata );
+ expect( registeredMetadata.name ).toBe( 'woocommerce/catalog-sorting' );
+ expect( settings.attributes ).toEqual( metadata.attributes );
+ expect( settings.edit ).toBe( isolatedEdit );
+ } );
+
+ it( 'registers a dynamic save callback that returns null', () => {
+ const { settings } = loadRegisteredBlock();
+
+ expect( settings.save() ).toBeNull();
+ } );
+} );
+
+describe( 'Catalog Sorting editor placeholder', () => {
+ it( 'renders the default sorting option without a visual label', () => {
+ render(
+ <Edit
+ attributes={ { useLabel: false } }
+ setAttributes={ jest.fn() }
+ />
+ );
+
+ expect(
+ screen.getByRole( 'option', { name: 'Default sorting' } )
+ ).toBeInTheDocument();
+ } );
+} );
diff --git a/plugins/woocommerce/client/blocks/changelog/testops-234-catalog-sorting b/plugins/woocommerce/client/blocks/changelog/testops-234-catalog-sorting
new file mode 100644
index 00000000000..053d8e5a33c
--- /dev/null
+++ b/plugins/woocommerce/client/blocks/changelog/testops-234-catalog-sorting
@@ -0,0 +1,3 @@
+Significance: patch
+Type: dev
+Comment: Add Jest coverage for the Catalog Sorting block's registration, dynamic save callback and editor placeholder. No E2E test is removed.