2020-10-21 17:02:45 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { getAdminLink } from '@woocommerce/wc-admin-settings';
|
|
|
|
|
2020-10-13 01:40:53 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import {
|
|
|
|
addHistoryListener,
|
2020-12-09 00:45:30 +00:00
|
|
|
getDefaultMatchExpression,
|
2020-10-13 01:40:53 +00:00
|
|
|
getFullUrl,
|
|
|
|
getMatchingItem,
|
|
|
|
getMatchScore,
|
|
|
|
} from '../utils';
|
|
|
|
|
|
|
|
const originalLocation = window.location;
|
|
|
|
global.window = Object.create( window );
|
2020-10-21 17:02:45 +00:00
|
|
|
global.window.wcNavigation = {};
|
|
|
|
|
2020-10-13 01:40:53 +00:00
|
|
|
const sampleMenuItems = [
|
|
|
|
{
|
|
|
|
id: 'main',
|
|
|
|
title: 'Main page',
|
|
|
|
url: 'admin.php?page=wc-admin',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: 'path',
|
|
|
|
title: 'Page with Path',
|
|
|
|
url: 'admin.php?page=wc-admin&path=/test-path',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: 'hash',
|
|
|
|
title: 'Page with Hash',
|
|
|
|
url: 'admin.php?page=wc-admin&path=/test-path#anchor',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: 'multiple-args',
|
|
|
|
title: 'Page with multiple arguments',
|
|
|
|
url: 'admin.php?page=wc-admin&path=/test-path§ion=section-name',
|
|
|
|
},
|
2020-12-09 00:45:30 +00:00
|
|
|
{
|
|
|
|
id: 'multiple-args-plus-one',
|
|
|
|
title: 'Page with same multiple arguments plus an additional one',
|
|
|
|
url:
|
|
|
|
'admin.php?page=wc-admin&path=/test-path§ion=section-name&version=22',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: 'hash-and-multiple-args',
|
|
|
|
title: 'Page with multiple arguments and a hash',
|
|
|
|
url:
|
|
|
|
'admin.php?page=wc-admin&path=/test-path§ion=section-name#anchor',
|
|
|
|
},
|
2020-10-13 01:40:53 +00:00
|
|
|
];
|
|
|
|
|
2020-12-09 00:45:30 +00:00
|
|
|
const runGetMatchingItemTests = ( items ) => {
|
2020-10-13 01:40:53 +00:00
|
|
|
it( 'should get the closest matched item', () => {
|
2020-10-21 17:02:45 +00:00
|
|
|
window.location = new URL( getAdminLink( 'admin.php?page=wc-admin' ) );
|
2020-12-09 00:45:30 +00:00
|
|
|
const matchingItem = getMatchingItem( items );
|
2020-10-13 01:40:53 +00:00
|
|
|
expect( matchingItem.id ).toBe( 'main' );
|
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'should match the item without hash if a better match does not exist', () => {
|
|
|
|
window.location = new URL(
|
2020-10-21 17:02:45 +00:00
|
|
|
getAdminLink( 'admin.php?page=wc-admin#hash' )
|
2020-10-13 01:40:53 +00:00
|
|
|
);
|
2020-12-09 00:45:30 +00:00
|
|
|
const matchingItem = getMatchingItem( items );
|
2020-10-13 01:40:53 +00:00
|
|
|
expect( matchingItem.id ).toBe( 'main' );
|
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'should exactly match the item with a hash if it exists', () => {
|
|
|
|
window.location = new URL(
|
2020-10-21 17:02:45 +00:00
|
|
|
getAdminLink( 'admin.php?page=wc-admin&path=/test-path#anchor' )
|
2020-10-13 01:40:53 +00:00
|
|
|
);
|
2020-12-09 00:45:30 +00:00
|
|
|
const matchingItem = getMatchingItem( items );
|
2020-10-13 01:40:53 +00:00
|
|
|
expect( matchingItem.id ).toBe( 'hash' );
|
|
|
|
} );
|
|
|
|
|
2020-12-09 00:45:30 +00:00
|
|
|
it( 'should roughly match the item if all menu item arguments exist', () => {
|
2020-10-13 01:40:53 +00:00
|
|
|
window.location = new URL(
|
2020-10-21 17:02:45 +00:00
|
|
|
getAdminLink(
|
|
|
|
'admin.php?page=wc-admin&path=/test-path§ion=section-name'
|
|
|
|
)
|
2020-10-13 01:40:53 +00:00
|
|
|
);
|
2020-12-09 00:45:30 +00:00
|
|
|
const matchingItem = getMatchingItem( items );
|
2020-10-13 01:40:53 +00:00
|
|
|
expect( matchingItem.id ).toBe( 'multiple-args' );
|
|
|
|
} );
|
2020-12-09 00:45:30 +00:00
|
|
|
|
|
|
|
it( 'should match an item with irrelevant query parameters', () => {
|
|
|
|
window.location = new URL(
|
|
|
|
getAdminLink(
|
|
|
|
'admin.php?page=wc-admin&path=/test-path§ion=section-name&foo=bar'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
const matchingItem = getMatchingItem( items );
|
|
|
|
expect( matchingItem.id ).toBe( 'multiple-args' );
|
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'should match an item with similar query args plus one additional arg', () => {
|
|
|
|
window.location = new URL(
|
|
|
|
getAdminLink(
|
|
|
|
'admin.php?page=wc-admin&path=/test-path§ion=section-name&version=22'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
const matchingItem = getMatchingItem( items );
|
|
|
|
expect( matchingItem.id ).toBe( 'multiple-args-plus-one' );
|
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'should match an item with query parameters in mixed order', () => {
|
|
|
|
window.location = new URL(
|
|
|
|
getAdminLink(
|
|
|
|
'admin.php?foo=bar&page=wc-admin&path=/test-path§ion=section-name'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
const matchingItem = getMatchingItem( items );
|
|
|
|
expect( matchingItem.id ).toBe( 'multiple-args' );
|
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'should match an item with query parameters and a hash', () => {
|
|
|
|
window.location = new URL(
|
|
|
|
getAdminLink(
|
|
|
|
'admin.php?foo=bar&page=wc-admin&path=/test-path§ion=section-name#anchor'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
const matchingItem = getMatchingItem( items );
|
|
|
|
expect( matchingItem.id ).toBe( 'hash-and-multiple-args' );
|
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
|
|
|
describe( 'getMatchingItem', () => {
|
|
|
|
beforeAll( () => {
|
|
|
|
delete window.location;
|
|
|
|
} );
|
|
|
|
|
|
|
|
afterAll( () => {
|
|
|
|
window.location = originalLocation;
|
|
|
|
} );
|
|
|
|
|
|
|
|
runGetMatchingItemTests( sampleMenuItems );
|
|
|
|
// re-run the tests with sampleMenuItems in reverse order.
|
|
|
|
runGetMatchingItemTests( sampleMenuItems.reverse() );
|
|
|
|
} );
|
|
|
|
|
|
|
|
describe( 'getDefaultMatchExpression', () => {
|
|
|
|
it( 'should return the regex for the path without query args', () => {
|
|
|
|
expect( getDefaultMatchExpression( 'http://wordpress.org' ) ).toBe(
|
|
|
|
'^http:\\/\\/wordpress\\.org'
|
|
|
|
);
|
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'should return the regex for the path and query args', () => {
|
|
|
|
expect(
|
|
|
|
getDefaultMatchExpression(
|
|
|
|
'http://wordpress.org?param1=a¶m2=b'
|
|
|
|
)
|
|
|
|
).toBe(
|
|
|
|
'^http:\\/\\/wordpress\\.org(?=.*[?|&]param1=a(&|$|#))(?=.*[?|&]param2=b(&|$|#))'
|
|
|
|
);
|
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'should return the regex with hash if present', () => {
|
|
|
|
expect(
|
|
|
|
getDefaultMatchExpression(
|
|
|
|
'http://wordpress.org?param1=a¶m2=b#hash'
|
|
|
|
)
|
|
|
|
).toBe(
|
|
|
|
'^http:\\/\\/wordpress\\.org(?=.*[?|&]param1=a(&|$|#))(?=.*[?|&]param2=b(&|$|#))(.*#hash$)'
|
|
|
|
);
|
|
|
|
} );
|
2020-10-13 01:40:53 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
describe( 'getMatchScore', () => {
|
|
|
|
beforeAll( () => {
|
|
|
|
delete window.location;
|
2020-10-21 17:02:45 +00:00
|
|
|
window.location = new URL( getAdminLink( '/' ) );
|
2020-10-13 01:40:53 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
afterAll( () => {
|
|
|
|
window.location = originalLocation;
|
|
|
|
} );
|
|
|
|
|
2020-12-09 00:45:30 +00:00
|
|
|
it( 'should return max safe integer if the url is an exact match', () => {
|
2020-10-13 01:40:53 +00:00
|
|
|
expect(
|
|
|
|
getMatchScore(
|
2020-10-21 17:02:45 +00:00
|
|
|
new URL( getAdminLink( 'admin.php?page=testpage' ) ),
|
|
|
|
getAdminLink( 'admin.php?page=testpage' )
|
2020-10-13 01:40:53 +00:00
|
|
|
)
|
|
|
|
).toBe( Number.MAX_SAFE_INTEGER );
|
|
|
|
} );
|
|
|
|
|
2020-12-09 00:45:30 +00:00
|
|
|
it( 'should return matching path and parameter count', () => {
|
2020-10-13 01:40:53 +00:00
|
|
|
expect(
|
|
|
|
getMatchScore(
|
2020-12-09 00:45:30 +00:00
|
|
|
new URL(
|
|
|
|
getFullUrl(
|
|
|
|
'/wp-admin/admin.php?page=testpage&extra_param=a'
|
|
|
|
)
|
|
|
|
),
|
2020-10-13 01:40:53 +00:00
|
|
|
'/wp-admin/admin.php?page=testpage'
|
|
|
|
)
|
2020-12-09 00:45:30 +00:00
|
|
|
).toBe( 2 );
|
2020-10-13 01:40:53 +00:00
|
|
|
} );
|
|
|
|
|
2020-12-09 00:45:30 +00:00
|
|
|
it( 'should return 0 if the URL does not meet match criteria', () => {
|
2020-10-13 01:40:53 +00:00
|
|
|
expect(
|
|
|
|
getMatchScore(
|
2020-12-09 00:45:30 +00:00
|
|
|
new URL( getAdminLink( 'admin.php?page=different-page' ) ),
|
2020-10-21 17:02:45 +00:00
|
|
|
getAdminLink( 'admin.php?page=testpage' )
|
2020-10-13 01:40:53 +00:00
|
|
|
)
|
2020-12-09 00:45:30 +00:00
|
|
|
).toBe( 0 );
|
2020-10-13 01:40:53 +00:00
|
|
|
} );
|
|
|
|
|
2020-12-09 00:45:30 +00:00
|
|
|
it( 'should return match count for a custom match expression', () => {
|
2020-10-13 01:40:53 +00:00
|
|
|
expect(
|
|
|
|
getMatchScore(
|
|
|
|
new URL(
|
2020-12-09 00:45:30 +00:00
|
|
|
getAdminLink( 'admin.php?page=different-page¶m1=a' )
|
2020-10-13 01:40:53 +00:00
|
|
|
),
|
2020-12-09 00:45:30 +00:00
|
|
|
getAdminLink( 'admin.php?page=testpage' ),
|
|
|
|
'param1=a'
|
2020-10-13 01:40:53 +00:00
|
|
|
)
|
2020-12-09 00:45:30 +00:00
|
|
|
).toBe( 1 );
|
2020-10-13 01:40:53 +00:00
|
|
|
} );
|
|
|
|
|
2020-12-09 00:45:30 +00:00
|
|
|
it( 'should return 0 for custom match expression that does not match', () => {
|
2020-10-13 01:40:53 +00:00
|
|
|
expect(
|
|
|
|
getMatchScore(
|
|
|
|
new URL(
|
2020-12-09 00:45:30 +00:00
|
|
|
getAdminLink( 'admin.php?page=different-page¶m1=b' )
|
2020-10-13 01:40:53 +00:00
|
|
|
),
|
2020-12-09 00:45:30 +00:00
|
|
|
getAdminLink( 'admin.php?page=testpage' ),
|
|
|
|
'param1=a'
|
2020-10-13 01:40:53 +00:00
|
|
|
)
|
|
|
|
).toBe( 0 );
|
|
|
|
} );
|
2020-12-09 00:45:30 +00:00
|
|
|
|
|
|
|
it( 'should return match count if params match but are out of order', () => {
|
|
|
|
expect(
|
|
|
|
getMatchScore(
|
|
|
|
new URL( getAdminLink( 'admin.php?param1=a&page=testpage' ) ),
|
|
|
|
getAdminLink( 'admin.php?page=testpage' )
|
|
|
|
)
|
|
|
|
).toBe( 2 );
|
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'should return match count if multiple params match but are out of order', () => {
|
|
|
|
expect(
|
|
|
|
getMatchScore(
|
|
|
|
new URL(
|
|
|
|
getAdminLink( 'admin.php?param1=a&page=testpage¶m2=b' )
|
|
|
|
),
|
|
|
|
getAdminLink( 'admin.php?page=testpage¶m1=a' )
|
|
|
|
)
|
|
|
|
).toBe( 3 );
|
|
|
|
} );
|
2020-10-13 01:40:53 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
describe( 'getFullUrl', () => {
|
|
|
|
beforeAll( () => {
|
|
|
|
delete window.location;
|
2020-10-21 17:02:45 +00:00
|
|
|
window.location = new URL( getAdminLink( '/' ) );
|
2020-10-13 01:40:53 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
afterAll( () => {
|
|
|
|
window.location = originalLocation;
|
|
|
|
} );
|
|
|
|
|
2020-10-21 17:02:45 +00:00
|
|
|
const adminUrl = new URL( getAdminLink( '/' ) );
|
|
|
|
|
2020-10-13 01:40:53 +00:00
|
|
|
it( 'should get the full URL from a path', () => {
|
|
|
|
expect( getFullUrl( '/wp-admin/admin.php?page=testpage' ) ).toBe(
|
2020-10-21 17:02:45 +00:00
|
|
|
adminUrl.origin + '/wp-admin/admin.php?page=testpage'
|
2020-10-13 01:40:53 +00:00
|
|
|
);
|
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'should return the same URL from an already complete URL', () => {
|
2020-10-21 17:02:45 +00:00
|
|
|
expect( getFullUrl( getAdminLink( 'admin.php?page=testpage' ) ) ).toBe(
|
|
|
|
getAdminLink( 'admin.php?page=testpage' )
|
|
|
|
);
|
2020-10-13 01:40:53 +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 );
|
|
|
|
} );
|
|
|
|
} );
|