2022-06-23 01:54:38 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { render, screen } from '@testing-library/react';
|
|
|
|
import { useSelect } from '@wordpress/data';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import ShippingRecommendations from '../experimental-shipping-recommendations';
|
|
|
|
|
|
|
|
jest.mock( '@wordpress/data', () => ( {
|
|
|
|
...jest.requireActual( '@wordpress/data' ),
|
|
|
|
useSelect: jest.fn(),
|
|
|
|
} ) );
|
|
|
|
jest.mock( '../../settings-recommendations/dismissable-list', () => ( {
|
|
|
|
DismissableList: ( ( { children } ) => children ) as React.FC,
|
|
|
|
DismissableListHeading: ( ( { children } ) => children ) as React.FC,
|
|
|
|
} ) );
|
2023-04-25 22:17:33 +00:00
|
|
|
jest.mock( '@woocommerce/admin-layout', () => {
|
|
|
|
const mockContext = {
|
|
|
|
layoutPath: [ 'home' ],
|
|
|
|
layoutString: 'home',
|
|
|
|
extendLayout: () => {},
|
|
|
|
isDescendantOf: () => false,
|
|
|
|
};
|
|
|
|
return {
|
|
|
|
...jest.requireActual( '@woocommerce/admin-layout' ),
|
|
|
|
useLayoutContext: jest.fn().mockReturnValue( mockContext ),
|
|
|
|
useExtendLayout: jest.fn().mockReturnValue( mockContext ),
|
|
|
|
};
|
|
|
|
} );
|
2022-06-23 01:54:38 +00:00
|
|
|
|
|
|
|
const defaultSelectReturn = {
|
|
|
|
getActivePlugins: () => [],
|
|
|
|
getInstalledPlugins: () => [],
|
|
|
|
isJetpackConnected: () => false,
|
|
|
|
getSettings: () => ( {
|
|
|
|
general: {
|
|
|
|
woocommerce_default_country: 'US',
|
|
|
|
},
|
|
|
|
} ),
|
|
|
|
getProfileItems: () => ( {} ),
|
2022-08-29 09:25:24 +00:00
|
|
|
hasFinishedResolution: jest.fn(),
|
|
|
|
getOption: jest.fn(),
|
2022-06-23 01:54:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
describe( 'ShippingRecommendations', () => {
|
|
|
|
beforeEach( () => {
|
|
|
|
( useSelect as jest.Mock ).mockImplementation( ( fn ) =>
|
|
|
|
fn( () => ( { ...defaultSelectReturn } ) )
|
|
|
|
);
|
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'should not render when WCS is already installed and Jetpack is connected', () => {
|
|
|
|
( useSelect as jest.Mock ).mockImplementation( ( fn ) =>
|
|
|
|
fn( () => ( {
|
|
|
|
...defaultSelectReturn,
|
|
|
|
getActivePlugins: () => [ 'woocommerce-services' ],
|
|
|
|
isJetpackConnected: () => true,
|
|
|
|
} ) )
|
|
|
|
);
|
|
|
|
render( <ShippingRecommendations /> );
|
|
|
|
|
|
|
|
expect(
|
2022-07-13 15:41:22 +00:00
|
|
|
screen.queryByText( 'WooCommerce Shipping' )
|
2022-06-23 01:54:38 +00:00
|
|
|
).not.toBeInTheDocument();
|
|
|
|
} );
|
|
|
|
|
2024-06-27 13:36:00 +00:00
|
|
|
[
|
|
|
|
[ 'woocommerce-shipping' ],
|
|
|
|
[ 'woocommerce-tax' ],
|
|
|
|
[ 'woocommerce-shipping', 'woocommerce-tax' ],
|
|
|
|
].forEach( ( activePlugins ) => {
|
|
|
|
it( `should not render if the following plugins are active: ${ JSON.stringify(
|
|
|
|
activePlugins
|
|
|
|
) }`, () => {
|
|
|
|
( useSelect as jest.Mock ).mockImplementation( ( fn ) =>
|
|
|
|
fn( () => ( {
|
|
|
|
...defaultSelectReturn,
|
|
|
|
getActivePlugins: () => activePlugins,
|
|
|
|
} ) )
|
|
|
|
);
|
|
|
|
|
|
|
|
render( <ShippingRecommendations /> );
|
|
|
|
|
|
|
|
expect(
|
|
|
|
screen.queryByText( 'WooCommerce Shipping' )
|
|
|
|
).not.toBeInTheDocument();
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
2022-06-23 01:54:38 +00:00
|
|
|
it( 'should not render when store location is not US', () => {
|
|
|
|
( useSelect as jest.Mock ).mockImplementation( ( fn ) =>
|
|
|
|
fn( () => ( {
|
|
|
|
...defaultSelectReturn,
|
|
|
|
getSettings: () => ( {
|
|
|
|
general: {
|
|
|
|
woocommerce_default_country: 'JP',
|
|
|
|
},
|
|
|
|
} ),
|
|
|
|
} ) )
|
|
|
|
);
|
|
|
|
render( <ShippingRecommendations /> );
|
|
|
|
|
|
|
|
expect(
|
2022-07-13 15:41:22 +00:00
|
|
|
screen.queryByText( 'WooCommerce Shipping' )
|
2022-06-23 01:54:38 +00:00
|
|
|
).not.toBeInTheDocument();
|
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'should not render when store sells digital products only', () => {
|
|
|
|
( useSelect as jest.Mock ).mockImplementation( ( fn ) =>
|
|
|
|
fn( () => ( {
|
|
|
|
...defaultSelectReturn,
|
|
|
|
getProfileItems: () => ( {
|
|
|
|
product_types: [ 'downloads' ],
|
|
|
|
} ),
|
|
|
|
} ) )
|
|
|
|
);
|
|
|
|
render( <ShippingRecommendations /> );
|
|
|
|
|
|
|
|
expect(
|
2022-07-13 15:41:22 +00:00
|
|
|
screen.queryByText( 'WooCommerce Shipping' )
|
2022-06-23 01:54:38 +00:00
|
|
|
).not.toBeInTheDocument();
|
|
|
|
} );
|
|
|
|
|
|
|
|
it( 'should render WCS when not installed', () => {
|
|
|
|
render( <ShippingRecommendations /> );
|
|
|
|
|
|
|
|
expect(
|
2022-07-13 15:41:22 +00:00
|
|
|
screen.queryByText( 'WooCommerce Shipping' )
|
2022-06-23 01:54:38 +00:00
|
|
|
).toBeInTheDocument();
|
|
|
|
} );
|
|
|
|
} );
|