woocommerce/plugins/woocommerce-admin/packages/navigation/src/test/index.js

111 lines
2.7 KiB
JavaScript
Raw Normal View History

/**
* Internal dependencies
*/
import { getPersistedQuery, getSearchWords } 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',
2018-11-14 01:45:05 +00:00
interval: 'day',
search: 'lorem',
} ),
} ) );
2018-11-14 01:45:05 +00:00
describe( 'getPersistedQuery', () => {
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-11-14 01:45:05 +00:00
expect( getPersistedQuery( query ) ).toEqual( persistedQuery );
} );
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-11-14 01:45:05 +00:00
const persistedQuery = {
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-11-14 01:45:05 +00:00
expect( getPersistedQuery( query ) ).toEqual( persistedQuery );
} );
it( 'should get the query from getQuery() when none is provided in the params', () => {
2018-11-14 01:45:05 +00:00
const persistedQuery = {
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-14 01:45:05 +00:00
expect( getPersistedQuery() ).toEqual( persistedQuery );
} );
} );
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 );
} );
} );