2018-10-25 10:07:30 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2021-01-07 23:57:09 +00:00
|
|
|
import {
|
|
|
|
getHistory,
|
|
|
|
getPersistedQuery,
|
|
|
|
getSearchWords,
|
|
|
|
getNewPath,
|
2021-04-02 21:35:31 +00:00
|
|
|
addHistoryListener,
|
2021-01-07 23:57:09 +00:00
|
|
|
} from '../index';
|
2018-11-02 21:38:16 +00:00
|
|
|
|
2021-04-02 21:35:31 +00:00
|
|
|
global.window = Object.create( window );
|
|
|
|
global.window.wcNavigation = {};
|
|
|
|
|
2018-11-14 01:45:05 +00:00
|
|
|
describe( 'getPersistedQuery', () => {
|
2021-01-07 23:57:09 +00:00
|
|
|
beforeEach( () => {
|
|
|
|
getHistory().push(
|
|
|
|
getNewPath(
|
|
|
|
{
|
|
|
|
filter: 'advanced',
|
|
|
|
product_includes: 127,
|
|
|
|
period: 'year',
|
|
|
|
compare: 'previous_year',
|
|
|
|
after: '2018-02-01',
|
|
|
|
before: '2018-01-01',
|
|
|
|
interval: 'day',
|
|
|
|
search: 'lorem',
|
|
|
|
},
|
|
|
|
'/',
|
|
|
|
{}
|
|
|
|
)
|
|
|
|
);
|
|
|
|
} );
|
|
|
|
|
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=' );
|
|
|
|
} );
|
|
|
|
} );
|
2021-04-02 21:35:31 +00:00
|
|
|
|
|
|
|
describe( 'addHistoryListener', () => {
|
|
|
|
it( 'should add a custom event to the browser pushState', () => {
|
|
|
|
const mockCallback = jest.fn();
|
|
|
|
const removeListener = addHistoryListener( mockCallback );
|
|
|
|
window.history.pushState( {}, 'Test pushState' );
|
|
|
|
window.history.pushState( {}, 'Test pushState 2' );
|
|
|
|
|
|
|
|
expect( mockCallback.mock.calls.length ).toBe( 2 );
|
|
|
|
|
|
|
|
// Check that events are no longer called after removing the listener.
|
|
|
|
removeListener();
|
|
|
|
window.history.pushState( {}, 'Test pushState 3' );
|
|
|
|
expect( mockCallback.mock.calls.length ).toBe( 2 );
|
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'should add a custom event to the browser replaceState', () => {
|
|
|
|
const mockCallback = jest.fn();
|
|
|
|
const removeListener = addHistoryListener( mockCallback );
|
|
|
|
window.history.replaceState( {}, 'Test replaceState' );
|
|
|
|
window.history.replaceState( {}, 'Test replaceState 2' );
|
|
|
|
|
|
|
|
expect( mockCallback.mock.calls.length ).toBe( 2 );
|
|
|
|
|
|
|
|
// Check that events are no longer called after removing the listener.
|
|
|
|
removeListener();
|
|
|
|
window.history.replaceState( {}, 'Test replaceState 3' );
|
|
|
|
expect( mockCallback.mock.calls.length ).toBe( 2 );
|
|
|
|
} );
|
|
|
|
} );
|