2018-10-25 10:07:30 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2020-12-04 06:45:16 +00:00
|
|
|
import { getPersistedQuery, getSearchWords, getNewPath } from '../index';
|
2018-10-25 10:07:30 +00:00
|
|
|
|
2018-11-02 21:38:16 +00:00
|
|
|
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',
|
2018-11-14 01:45:05 +00:00
|
|
|
interval: 'day',
|
2019-02-26 10:06:37 +00:00
|
|
|
search: 'lorem',
|
2018-11-02 21:38:16 +00:00
|
|
|
} ),
|
|
|
|
} ) );
|
|
|
|
|
2018-11-14 01:45:05 +00:00
|
|
|
describe( 'getPersistedQuery', () => {
|
2018-10-25 10:07:30 +00:00
|
|
|
it( "should return an empty object it the query doesn't contain any time related parameters", () => {
|
|
|
|
const query = {
|
|
|
|
filter: 'advanced',
|
|
|
|
product_includes: 127,
|
|
|
|
};
|
2018-11-14 01:45:05 +00:00
|
|
|
const persistedQuery = {};
|
2018-10-25 10:07:30 +00:00
|
|
|
|
2018-11-14 01:45:05 +00:00
|
|
|
expect( getPersistedQuery( query ) ).toEqual( persistedQuery );
|
2018-10-25 10:07:30 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'should return time related parameters', () => {
|
|
|
|
const query = {
|
|
|
|
filter: 'advanced',
|
|
|
|
product_includes: 127,
|
|
|
|
period: 'year',
|
|
|
|
compare: 'previous_year',
|
|
|
|
after: '2018-02-01',
|
|
|
|
before: '2018-01-01',
|
2018-11-14 01:45:05 +00:00
|
|
|
type: 'bar',
|
|
|
|
interval: 'day',
|
2018-10-25 10:07:30 +00:00
|
|
|
};
|
2018-11-14 01:45:05 +00:00
|
|
|
const persistedQuery = {
|
2018-10-25 10:07:30 +00:00
|
|
|
period: 'year',
|
|
|
|
compare: 'previous_year',
|
|
|
|
after: '2018-02-01',
|
|
|
|
before: '2018-01-01',
|
2018-11-14 01:45:05 +00:00
|
|
|
type: 'bar',
|
|
|
|
interval: 'day',
|
2018-10-25 10:07:30 +00:00
|
|
|
};
|
|
|
|
|
2018-11-14 01:45:05 +00:00
|
|
|
expect( getPersistedQuery( query ) ).toEqual( persistedQuery );
|
2018-10-25 10:07:30 +00:00
|
|
|
} );
|
2018-11-02 21:38:16 +00:00
|
|
|
|
|
|
|
it( 'should get the query from getQuery() when none is provided in the params', () => {
|
2018-11-14 01:45:05 +00:00
|
|
|
const persistedQuery = {
|
2018-11-02 21:38:16 +00:00
|
|
|
period: 'year',
|
|
|
|
compare: 'previous_year',
|
|
|
|
after: '2018-02-01',
|
|
|
|
before: '2018-01-01',
|
2018-11-14 01:45:05 +00:00
|
|
|
interval: 'day',
|
2018-11-02 21:38:16 +00:00
|
|
|
};
|
|
|
|
|
2018-11-14 01:45:05 +00:00
|
|
|
expect( getPersistedQuery() ).toEqual( persistedQuery );
|
2018-11-02 21:38:16 +00:00
|
|
|
} );
|
2018-10-25 10:07:30 +00:00
|
|
|
} );
|
2019-02-26 10:06:37 +00:00
|
|
|
|
|
|
|
describe( 'getSearchWords', () => {
|
|
|
|
it( 'should get the search words from a query object', () => {
|
|
|
|
const query = {
|
|
|
|
search: 'lorem,dolor sit',
|
|
|
|
};
|
|
|
|
const searchWords = [ 'lorem', 'dolor sit' ];
|
|
|
|
|
|
|
|
expect( getSearchWords( query ) ).toEqual( searchWords );
|
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'should parse `%2C` as commas', () => {
|
|
|
|
const query = {
|
|
|
|
search: 'lorem%2Cipsum,dolor sit',
|
|
|
|
};
|
|
|
|
const searchWords = [ 'lorem,ipsum', 'dolor sit' ];
|
|
|
|
|
|
|
|
expect( getSearchWords( query ) ).toEqual( searchWords );
|
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'should return an empty array if the query has no `search` property', () => {
|
|
|
|
const query = {};
|
|
|
|
const searchWords = [];
|
|
|
|
|
|
|
|
expect( getSearchWords( query ) ).toEqual( searchWords );
|
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'should use the persisted query when it receives no params', () => {
|
|
|
|
const searchWords = [ 'lorem' ];
|
|
|
|
|
|
|
|
expect( getSearchWords() ).toEqual( searchWords );
|
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'should throw an error if the param is not an object', () => {
|
|
|
|
expect( () => getSearchWords( 'lorem' ) ).toThrow( Error );
|
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'should throw an error if the `search` property is not a string', () => {
|
|
|
|
const query = {
|
|
|
|
search: new Object(),
|
|
|
|
};
|
|
|
|
|
|
|
|
expect( () => getSearchWords( query ) ).toThrow( Error );
|
|
|
|
} );
|
|
|
|
} );
|
2020-12-04 06:45:16 +00:00
|
|
|
|
|
|
|
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=' );
|
|
|
|
} );
|
|
|
|
} );
|