From db598dafc4c8e0667914f611b036c7b0191782b9 Mon Sep 17 00:00:00 2001 From: Ilyas Foo Date: Fri, 4 Dec 2020 14:45:16 +0800 Subject: [PATCH] Add page parameter to getNewPath to override default page wc-admin (https://github.com/woocommerce/woocommerce-admin/pull/5821) * Add page parameter to getNewPath to override default page wc-admin * Updated readme.txt --- .../packages/navigation/src/index.js | 12 +++++--- .../packages/navigation/src/test/index.js | 29 ++++++++++++++++++- plugins/woocommerce-admin/readme.txt | 1 + 3 files changed, 37 insertions(+), 5 deletions(-) diff --git a/plugins/woocommerce-admin/packages/navigation/src/index.js b/plugins/woocommerce-admin/packages/navigation/src/index.js index 410ba262431..6cd862a64e6 100644 --- a/plugins/woocommerce-admin/packages/navigation/src/index.js +++ b/plugins/woocommerce-admin/packages/navigation/src/index.js @@ -93,14 +93,16 @@ export function getSearchWords( query = navUtils.getQuery() ) { * @param {Object} query object of params to be updated. * @param {string} path Relative path (defaults to current path). * @param {Object} currentQuery object of current query params (defaults to current querystring). + * @param {string} page Page key (defaults to "wc-admin") * @return {string} Updated URL merging query params into existing params. */ export function getNewPath( query, path = getPath(), - currentQuery = getQuery() + currentQuery = getQuery(), + page = 'wc-admin' ) { - const args = { page: 'wc-admin', ...currentQuery, ...query }; + const args = { page, ...currentQuery, ...query }; if ( path !== '/' ) { args.path = path; } @@ -156,13 +158,15 @@ export function onQueryChange( param, path = getPath(), query = getQuery() ) { * @param {Object} query object of params to be updated. * @param {string} path Relative path (defaults to current path). * @param {Object} currentQuery object of current query params (defaults to current querystring). + * @param {string} page Page key (defaults to "wc-admin") */ export function updateQueryString( query, path = getPath(), - currentQuery = getQuery() + currentQuery = getQuery(), + page = 'wc-admin' ) { - const newPath = getNewPath( query, path, currentQuery ); + const newPath = getNewPath( query, path, currentQuery, page ); getHistory().push( newPath ); } diff --git a/plugins/woocommerce-admin/packages/navigation/src/test/index.js b/plugins/woocommerce-admin/packages/navigation/src/test/index.js index 983933062a7..a296220b849 100644 --- a/plugins/woocommerce-admin/packages/navigation/src/test/index.js +++ b/plugins/woocommerce-admin/packages/navigation/src/test/index.js @@ -1,7 +1,7 @@ /** * Internal dependencies */ -import { getPersistedQuery, getSearchWords } from '../index'; +import { getPersistedQuery, getSearchWords, getNewPath } from '../index'; jest.mock( '../index', () => ( { ...require.requireActual( '../index' ), @@ -108,3 +108,30 @@ describe( 'getSearchWords', () => { expect( () => getSearchWords( query ) ).toThrow( Error ); } ); } ); + +describe( 'getNewPath', () => { + it( 'should have default page as "wc-admin"', () => { + const path = getNewPath( {}, '', {} ); + + expect( path ).toEqual( 'admin.php?page=wc-admin&path=' ); + } ); + + it( 'should override default page when page parameter is specified', () => { + const path = getNewPath( {}, '', {}, 'custom-page' ); + + expect( path ).toEqual( 'admin.php?page=custom-page&path=' ); + } ); + + it( 'should override default page by query parameter over page parameter', () => { + const path = getNewPath( + { + page: 'custom-page', + }, + '', + {}, + 'default-page' + ); + + expect( path ).toEqual( 'admin.php?page=custom-page&path=' ); + } ); +} ); diff --git a/plugins/woocommerce-admin/readme.txt b/plugins/woocommerce-admin/readme.txt index ac87a7d2bde..804cb3e72fa 100644 --- a/plugins/woocommerce-admin/readme.txt +++ b/plugins/woocommerce-admin/readme.txt @@ -74,6 +74,7 @@ Release and roadmap notes are available on the [WooCommerce Developers Blog](htt == Unreleased == - Enhancement: Tasks extensibility in Home Screen. #5794 +- Enhancement: Add page parameter to override default wc-admin page in Navigation API. #5821 - Fix: Invalidate product count if the last product was updated in the list. #5790 == Changelog ==