Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Facet by post type #3473

Merged
merged 11 commits into from
Jun 5, 2023
Prev Previous commit
Next Next commit
Remove the facet parameter and add e2e tests
  • Loading branch information
felipeelia committed Jun 2, 2023
commit 1c14cd438447a116c81279aafbce8a0e3996e143
4 changes: 0 additions & 4 deletions assets/js/blocks/facets/post-type/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@
"type": "string",
"default": "Search"
},
"facet": {
"type": "string",
"default": ""
},
"displayCount": {
"type": "boolean",
"default": false
Expand Down
5 changes: 2 additions & 3 deletions assets/js/blocks/facets/post-type/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,14 @@ const FacetBlockEdit = (props) => {
const { attributes, setAttributes } = props;
const [preview, setPreview] = useState('');
const [loading, setLoading] = useState(false);
const { searchPlaceholder, facet, displayCount, orderby, order } = attributes;
const { searchPlaceholder, displayCount, orderby, order } = attributes;

const blockProps = useBlockProps();

useEffect(() => {
setLoading(true);
const params = new URLSearchParams({
searchPlaceholder,
facet,
displayCount,
orderby,
order,
Expand All @@ -33,7 +32,7 @@ const FacetBlockEdit = (props) => {
})
.then((preview) => setPreview(preview))
.finally(() => setLoading(false));
}, [searchPlaceholder, facet, displayCount, orderby, order]);
}, [searchPlaceholder, displayCount, orderby, order]);

return (
<Fragment>
Expand Down
15 changes: 1 addition & 14 deletions includes/classes/Feature/Facets/Types/PostType/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,6 @@ public function setup_endpoints() {
'displayCount' => [
'sanitize_callback' => 'rest_sanitize_boolean',
],
'facet' => [
'sanitize_callback' => 'sanitize_text_field',
],
'orderby' => [
'sanitize_callback' => 'sanitize_text_field',
],
Expand Down Expand Up @@ -137,7 +134,6 @@ public function render_block_preview( $request ) {
[
'searchPlaceholder' => $request->get_param( 'searchPlaceholder' ),
'displayCount' => $request->get_param( 'displayCount' ),
'facet' => $request->get_param( 'facet' ),
'orderby' => $request->get_param( 'orderby' ),
'order' => $request->get_param( 'order' ),
]
Expand All @@ -159,15 +155,7 @@ public function render_block_preview( $request ) {
$block_content = ob_get_clean();

if ( empty( $block_content ) ) {
if ( empty( $attributes['facet'] ) ) {
return esc_html__( 'Preview not available', 'elasticpress' );
}

return sprintf(
/* translators: Post type name */
esc_html__( 'Preview for %s not available', 'elasticpress' ),
esc_html( $request->get_param( 'facet' ) )
);
return esc_html__( 'Preview for post types not available', 'elasticpress' );
}

$block_content = preg_replace( '/href="(.*?)"/', 'href="#"', $block_content );
Expand All @@ -185,7 +173,6 @@ protected function parse_attributes( $attributes ) {
$attributes,
[
'searchPlaceholder' => esc_html_x( 'Search', 'Facet by post type search placeholder', 'elasticpress' ),
'facet' => '',
'displayCount' => false,
'orderby' => 'count',
'order' => 'desc',
Expand Down
94 changes: 94 additions & 0 deletions tests/cypress/integration/features/facets.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -700,4 +700,98 @@ describe('Facets Feature', { tags: '@slow' }, () => {
cy.url().should('include', 'ep_meta_filter_non_numeric_meta_field=Non-numeric');
});
});

describe('Facet by Post Type', () => {
/**
* Test that the Facet by Post Type block is functional.
*/
it('Can insert, configure, and use the Facet by Post Type block', () => {
/**
* Insert a Facet block.
*/
cy.openWidgetsPage();
cy.openBlockInserter();
cy.getBlocksList().should('contain.text', 'Facet by Post Type (ElasticPress)');
cy.insertBlock('Facet by Post Type (ElasticPress)');
cy.get('.wp-block-elasticpress-facet-post-type').last().as('block');

// Configure the block
cy.get('@block').click();
cy.openBlockSettingsSidebar();
cy.get('.block-editor-block-inspector input[type="text"]').clearThenType(
'Search Post Type',
true,
);

cy.intercept(
'/wp-json/elasticpress/v1/facets/post-type/block-preview*displayCount=true*',
).as('blockPreview');

/**
* Verify the display count setting on the editor.
*/
cy.get('@block')
.contains('.term', /\(\d*\)$/)
.should('not.exist');
cy.get('.block-editor-block-inspector .components-form-toggle__input').click();
cy.wait('@blockPreview');
cy.get('@block')
.contains('.term', /(^\(\d*\))$/)
.should('not.exist');

// Configure the block
cy.get('@block').click();
cy.get('.block-editor-block-inspector input[type="radio"][value="name"]').click();

cy.intercept(
'/wp-json/elasticpress/v1/facets/post-type/block-preview*orderby=name&order=asc*',
).as('blockPreview2');
cy.get('.block-editor-block-inspector input[type="radio"][value="asc"]').click();
cy.wait('@blockPreview2');

/**
* Verify the block has the expected output in the editor based on the
* block's settings.
*/
cy.get('@block').find('.term').should('be.elementsSortedAlphabetically');

/**
* Save widgets and visit the front page.
*/
cy.intercept('/wp-json/wp/v2/sidebars/*').as('sidebarsRest');
cy.get('.edit-widgets-header__actions button').contains('Update').click();
cy.wait('@sidebarsRest');
cy.visit('/');

/**
* Verify the blocks have the expected output on the front-end based on
* their settings.
*/
cy.get('.wp-block-elasticpress-facet').first().as('firstBlock');
cy.get('@firstBlock').find('.term').should('be.elementsSortedAlphabetically');
cy.get('@firstBlock')
.contains('.term', /(^\(\d*\))$/)
.should('not.exist');

cy.get('@firstBlock').contains('.term', 'Post').click();

/**
* Selecting that term should lead to the correct URL, mark the correct
* item as checked, and all articles being displayed should have the
* selected category.
*/
cy.url().should('include', 'ep_post_type_filter=post');
cy.get('@firstBlock')
.contains('.term', 'Post')
.find('.ep-checkbox')
.should('have.class', 'checked');

/**
* Clicking selected facet should remove it while keeping any other
* facets active.
*/
cy.get('@firstBlock').contains('.term', 'Post').click();
cy.url().should('not.include', 'ep_post_type_filter=post');
});
});
});