From 70e7e14e7a5e84e6db6ed145d2f58a994ae9250a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Albert=20Juh=C3=A9=20Lluveras?= Date: Fri, 2 Nov 2018 22:38:16 +0100 Subject: [PATCH] Persist time queries when clicking on breadcrumbs links (https://github.com/woocommerce/woocommerce-admin/pull/759) * Make breadcrumbs links persist date queries * Add tests to getTimeRelatedQuery() when query parameter not set * Add comment explanation about navUtils self-import --- .../woocommerce-admin/client/header/index.js | 6 ++++- .../client/lib/nav-utils/index.js | 8 ++++++- .../client/lib/nav-utils/test/index.js | 23 +++++++++++++++++++ 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/plugins/woocommerce-admin/client/header/index.js b/plugins/woocommerce-admin/client/header/index.js index 1ace603f9e0..0de78215dbc 100644 --- a/plugins/woocommerce-admin/client/header/index.js +++ b/plugins/woocommerce-admin/client/header/index.js @@ -15,6 +15,7 @@ import PropTypes from 'prop-types'; import './style.scss'; import ActivityPanel from './activity-panel'; import { Link } from '@woocommerce/components'; +import { getNewPath, getTimeRelatedQuery } from 'lib/nav-utils'; class Header extends Component { constructor() { @@ -83,7 +84,10 @@ class Header extends Component { { _sections.map( ( section, i ) => { const sectionPiece = Array.isArray( section ) ? ( - + { section[ 1 ] } ) : ( diff --git a/plugins/woocommerce-admin/client/lib/nav-utils/index.js b/plugins/woocommerce-admin/client/lib/nav-utils/index.js index 27971819982..bf25d23696d 100644 --- a/plugins/woocommerce-admin/client/lib/nav-utils/index.js +++ b/plugins/woocommerce-admin/client/lib/nav-utils/index.js @@ -6,6 +6,12 @@ import history from 'lib/history'; import { parse, stringify } from 'qs'; import { isEmpty, pick, uniq } from 'lodash'; +/** + * Internal dependencies + */ +// Import the module into itself. Functions consumed from this import can be mocked in tests. +import * as navUtils from './index'; + /** * Returns a string with the site's wp-admin URL appended. JS version of `admin_url`. * @@ -35,7 +41,7 @@ export const stringifyQuery = query => ( isEmpty( query ) ? '' : '?' + stringify * @param {Object} query Query containing the parameters. * @return {Object} Object containing the time related queries. */ -export const getTimeRelatedQuery = query => +export const getTimeRelatedQuery = ( query = navUtils.getQuery() ) => pick( query, [ 'period', 'compare', 'before', 'after' ] ); /** diff --git a/plugins/woocommerce-admin/client/lib/nav-utils/test/index.js b/plugins/woocommerce-admin/client/lib/nav-utils/test/index.js index 4df6494bec6..deaf5ff647a 100644 --- a/plugins/woocommerce-admin/client/lib/nav-utils/test/index.js +++ b/plugins/woocommerce-admin/client/lib/nav-utils/test/index.js @@ -4,6 +4,18 @@ */ import { getTimeRelatedQuery } from '../index'; +jest.mock( '../index', () => ( { + ...require.requireActual( '../index' ), + getQuery: jest.fn().mockReturnValue( { + filter: 'advanced', + product_includes: 127, + period: 'year', + compare: 'previous_year', + after: '2018-02-01', + before: '2018-01-01', + } ), +} ) ); + describe( 'getTimeRelatedQuery', () => { it( "should return an empty object it the query doesn't contain any time related parameters", () => { const query = { @@ -33,4 +45,15 @@ describe( 'getTimeRelatedQuery', () => { expect( getTimeRelatedQuery( query ) ).toEqual( timeRelatedQuery ); } ); + + it( 'should get the query from getQuery() when none is provided in the params', () => { + const timeRelatedQuery = { + period: 'year', + compare: 'previous_year', + after: '2018-02-01', + before: '2018-01-01', + }; + + expect( getTimeRelatedQuery() ).toEqual( timeRelatedQuery ); + } ); } );