2023-05-10 12:36:42 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { render } from '@testing-library/react';
|
|
|
|
import { recordPageView } from '@woocommerce/tracks';
|
|
|
|
|
2019-05-14 03:58:37 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { updateLinkHref } from '../controller';
|
2023-05-10 12:36:42 +00:00
|
|
|
import { EmbedLayout } from '../index';
|
2019-05-14 03:58:37 +00:00
|
|
|
|
2023-12-05 08:36:30 +00:00
|
|
|
jest.mock( '@woocommerce/admin-layout', () => {
|
|
|
|
return {
|
|
|
|
LayoutContextProvider: () => null,
|
|
|
|
getLayoutContextValue: () => null,
|
|
|
|
};
|
|
|
|
} );
|
|
|
|
|
2019-05-14 03:58:37 +00:00
|
|
|
describe( 'updateLinkHref', () => {
|
2020-10-15 01:55:55 +00:00
|
|
|
const timeExcludedScreens = [ 'stock', 'settings', 'customers' ];
|
2019-05-14 03:58:37 +00:00
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
const REPORT_URL =
|
|
|
|
'http://example.com/wp-admin/admin.php?page=wc-admin&path=/analytics/orders';
|
2019-06-13 19:58:21 +00:00
|
|
|
const DASHBOARD_URL = 'http://example.com/wp-admin/admin.php?page=wc-admin';
|
|
|
|
const REPORT_URL_TIME_EXCLUDED =
|
|
|
|
'http://example.com/wp-admin/admin.php?page=wc-admin&path=/analytics/settings';
|
2020-02-14 02:23:21 +00:00
|
|
|
const WOO_URL =
|
|
|
|
'http://example.com/wp-admin/edit.php?post_type=shop_coupon';
|
2019-05-14 03:58:37 +00:00
|
|
|
const WP_ADMIN_URL = 'http://example.com/wp-admin/edit-comments.php';
|
|
|
|
|
2019-06-13 19:58:21 +00:00
|
|
|
const nextQuery = {
|
2019-05-14 03:58:37 +00:00
|
|
|
fruit: 'apple',
|
|
|
|
dish: 'cobbler',
|
2019-06-13 19:58:21 +00:00
|
|
|
};
|
2019-05-14 03:58:37 +00:00
|
|
|
|
|
|
|
it( 'should update report urls', () => {
|
|
|
|
const item = { href: REPORT_URL };
|
|
|
|
updateLinkHref( item, nextQuery, timeExcludedScreens );
|
2019-06-13 19:58:21 +00:00
|
|
|
const encodedPath = encodeURIComponent( '/analytics/orders' );
|
2019-05-14 03:58:37 +00:00
|
|
|
|
|
|
|
expect( item.href ).toBe(
|
2019-06-13 19:58:21 +00:00
|
|
|
`admin.php?page=wc-admin&path=${ encodedPath }&fruit=apple&dish=cobbler`
|
2019-05-14 03:58:37 +00:00
|
|
|
);
|
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'should update dashboard urls', () => {
|
|
|
|
const item = { href: DASHBOARD_URL };
|
|
|
|
updateLinkHref( item, nextQuery, timeExcludedScreens );
|
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
expect( item.href ).toBe(
|
|
|
|
'admin.php?page=wc-admin&fruit=apple&dish=cobbler'
|
|
|
|
);
|
2019-05-14 03:58:37 +00:00
|
|
|
} );
|
|
|
|
|
2019-06-13 19:58:21 +00:00
|
|
|
it( 'should not add the nextQuery to a time excluded screen', () => {
|
|
|
|
const item = { href: REPORT_URL_TIME_EXCLUDED };
|
2019-05-14 03:58:37 +00:00
|
|
|
updateLinkHref( item, nextQuery, timeExcludedScreens );
|
2019-06-13 19:58:21 +00:00
|
|
|
const encodedPath = encodeURIComponent( '/analytics/settings' );
|
2019-05-14 03:58:37 +00:00
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
expect( item.href ).toBe(
|
|
|
|
`admin.php?page=wc-admin&path=${ encodedPath }`
|
|
|
|
);
|
2019-05-14 03:58:37 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'should not update WooCommerce urls', () => {
|
|
|
|
const item = { href: WOO_URL };
|
|
|
|
updateLinkHref( item, nextQuery, timeExcludedScreens );
|
|
|
|
|
|
|
|
expect( item.href ).toBe( WOO_URL );
|
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'should not update wp-admin urls', () => {
|
|
|
|
const item = { href: WP_ADMIN_URL };
|
|
|
|
updateLinkHref( item, nextQuery, timeExcludedScreens );
|
|
|
|
|
|
|
|
expect( item.href ).toBe( WP_ADMIN_URL );
|
|
|
|
} );
|
2023-05-31 12:35:54 +00:00
|
|
|
|
|
|
|
it( 'should filter out undefined query values', () => {
|
|
|
|
const item = { href: REPORT_URL };
|
|
|
|
updateLinkHref(
|
|
|
|
item,
|
|
|
|
{ ...nextQuery, test: undefined, anotherParam: undefined },
|
|
|
|
timeExcludedScreens
|
|
|
|
);
|
|
|
|
const encodedPath = encodeURIComponent( '/analytics/orders' );
|
|
|
|
|
|
|
|
expect( item.href ).toBe(
|
|
|
|
`admin.php?page=wc-admin&path=${ encodedPath }&fruit=apple&dish=cobbler`
|
|
|
|
);
|
|
|
|
} );
|
2019-05-14 03:58:37 +00:00
|
|
|
} );
|
2023-05-10 12:36:42 +00:00
|
|
|
|
|
|
|
describe( 'Layout', () => {
|
|
|
|
it( 'should call recordPageView with correct parameters', () => {
|
|
|
|
window.history.pushState( {}, 'Page Title', '/url?search' );
|
|
|
|
render( <EmbedLayout /> );
|
|
|
|
expect( recordPageView ).toHaveBeenCalledWith( '/url?search', {
|
|
|
|
has_navigation: true,
|
|
|
|
is_embedded: true,
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
} );
|