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
This commit is contained in:
Albert Juhé Lluveras 2018-11-02 22:38:16 +01:00 committed by GitHub
parent aac90a8aae
commit 70e7e14e7a
3 changed files with 35 additions and 2 deletions

View File

@ -15,6 +15,7 @@ import PropTypes from 'prop-types';
import './style.scss'; import './style.scss';
import ActivityPanel from './activity-panel'; import ActivityPanel from './activity-panel';
import { Link } from '@woocommerce/components'; import { Link } from '@woocommerce/components';
import { getNewPath, getTimeRelatedQuery } from 'lib/nav-utils';
class Header extends Component { class Header extends Component {
constructor() { constructor() {
@ -83,7 +84,10 @@ class Header extends Component {
</span> </span>
{ _sections.map( ( section, i ) => { { _sections.map( ( section, i ) => {
const sectionPiece = Array.isArray( section ) ? ( const sectionPiece = Array.isArray( section ) ? (
<Link href={ section[ 0 ] } type={ isEmbedded ? 'wp-admin' : 'wc-admin' }> <Link
href={ getNewPath( getTimeRelatedQuery(), section[ 0 ], {} ) }
type={ isEmbedded ? 'wp-admin' : 'wc-admin' }
>
{ section[ 1 ] } { section[ 1 ] }
</Link> </Link>
) : ( ) : (

View File

@ -6,6 +6,12 @@ import history from 'lib/history';
import { parse, stringify } from 'qs'; import { parse, stringify } from 'qs';
import { isEmpty, pick, uniq } from 'lodash'; 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`. * 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. * @param {Object} query Query containing the parameters.
* @return {Object} Object containing the time related queries. * @return {Object} Object containing the time related queries.
*/ */
export const getTimeRelatedQuery = query => export const getTimeRelatedQuery = ( query = navUtils.getQuery() ) =>
pick( query, [ 'period', 'compare', 'before', 'after' ] ); pick( query, [ 'period', 'compare', 'before', 'after' ] );
/** /**

View File

@ -4,6 +4,18 @@
*/ */
import { getTimeRelatedQuery } from '../index'; 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', () => { describe( 'getTimeRelatedQuery', () => {
it( "should return an empty object it the query doesn't contain any time related parameters", () => { it( "should return an empty object it the query doesn't contain any time related parameters", () => {
const query = { const query = {
@ -33,4 +45,15 @@ describe( 'getTimeRelatedQuery', () => {
expect( getTimeRelatedQuery( query ) ).toEqual( timeRelatedQuery ); 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 );
} );
} ); } );