2020-03-27 20:42:58 +00:00
/ * *
* External dependencies
* /
2024-09-02 12:23:52 +00:00
import { Fragment } from '@wordpress/element' ;
2020-08-20 04:59:52 +00:00
import { recordEvent } from '@woocommerce/tracks' ;
2020-10-15 12:41:39 +00:00
import { render , waitFor } from '@testing-library/react' ;
import userEvent from '@testing-library/user-event' ;
2020-08-20 04:59:52 +00:00
2020-03-27 20:42:58 +00:00
/ * *
* Internal dependencies
* /
2024-09-02 12:23:52 +00:00
import {
acceptWcsTos ,
getWcsAssets ,
getWcsLabelPurchaseConfigs ,
} from '../../wcs-api.js' ;
2020-10-15 12:41:39 +00:00
import { ShippingBanner } from '../index.js' ;
2020-03-27 20:42:58 +00:00
2020-08-13 02:05:22 +00:00
jest . mock ( '../../wcs-api.js' ) ;
2020-03-27 20:42:58 +00:00
acceptWcsTos . mockReturnValue ( Promise . resolve ( ) ) ;
2020-08-20 04:59:52 +00:00
jest . mock ( '@woocommerce/tracks' ) ;
2020-03-27 20:42:58 +00:00
2024-09-02 12:23:52 +00:00
const wcsPluginSlug = 'woocommerce-shipping' ;
const wcstPluginSlug = 'woocommerce-services' ;
2020-10-15 12:41:39 +00:00
2020-03-27 20:42:58 +00:00
describe ( 'Tracking impression in shippingBanner' , ( ) => {
const expectedTrackingData = {
banner _name : 'wcadmin_install_wcs_prompt' ,
jetpack _connected : true ,
jetpack _installed : true ,
2024-09-02 12:23:52 +00:00
wcs _installed : false ,
2020-03-27 20:42:58 +00:00
} ;
2020-10-15 12:41:39 +00:00
it ( 'should record an event when user sees banner loaded' , ( ) => {
render (
2020-03-27 20:42:58 +00:00
< ShippingBanner
isJetpackConnected = { true }
2024-09-02 12:23:52 +00:00
activePlugins = { [ wcstPluginSlug , 'jetpack' ] }
2020-03-27 20:42:58 +00:00
itemsCount = { 1 }
activatePlugins = { jest . fn ( ) }
2020-08-31 15:13:14 +00:00
installPlugins = { jest . fn ( ) }
2020-03-27 20:42:58 +00:00
isRequesting = { false }
2024-09-02 12:23:52 +00:00
isWcstCompatible = { true }
orderId = { 1 }
2020-03-27 20:42:58 +00:00
/ >
) ;
expect ( recordEvent ) . toHaveBeenCalledTimes ( 1 ) ;
expect ( recordEvent ) . toHaveBeenCalledWith (
'banner_impression' ,
expectedTrackingData
) ;
} ) ;
} ) ;
describe ( 'Tracking clicks in shippingBanner' , ( ) => {
2020-10-15 12:41:39 +00:00
const getExpectedTrackingData = ( element , wcsInstalled = true ) => {
2020-03-27 20:42:58 +00:00
return {
banner _name : 'wcadmin_install_wcs_prompt' ,
jetpack _connected : true ,
jetpack _installed : true ,
2020-10-15 12:41:39 +00:00
wcs _installed : wcsInstalled ,
2020-03-27 20:42:58 +00:00
element ,
} ;
} ;
2020-10-15 12:41:39 +00:00
it ( 'should record an event when user clicks "Create shipping label"' , async ( ) => {
2024-09-02 12:23:52 +00:00
const actionButtonLabel = 'Create shipping label' ;
2020-10-15 12:41:39 +00:00
const { getByRole } = render (
2020-03-27 20:42:58 +00:00
< ShippingBanner
2020-10-15 12:41:39 +00:00
isJetpackConnected = { true }
2024-09-02 12:23:52 +00:00
activePlugins = { [ 'jetpack' ] }
installPlugins = { jest
. fn ( )
. mockResolvedValue ( { success : true } ) }
activatePlugins = { jest
. fn ( )
. mockResolvedValue ( { success : true } ) }
2020-03-27 20:42:58 +00:00
isRequesting = { false }
itemsCount = { 1 }
2024-09-02 12:23:52 +00:00
orderId = { 1 }
isWcstCompatible = { true }
actionButtonLabel = { actionButtonLabel }
2020-03-27 20:42:58 +00:00
/ >
) ;
2024-09-02 12:23:52 +00:00
userEvent . click ( getByRole ( 'button' , { name : actionButtonLabel } ) ) ;
2020-10-15 12:41:39 +00:00
await waitFor ( ( ) =>
expect ( recordEvent ) . toHaveBeenCalledWith (
'banner_element_clicked' ,
2024-09-02 12:23:52 +00:00
getExpectedTrackingData ( 'shipping_banner_create_label' , false )
2020-10-15 12:41:39 +00:00
)
2020-03-27 20:42:58 +00:00
) ;
} ) ;
2020-10-15 12:41:39 +00:00
it ( 'should record an event when user clicks "WooCommerce Shipping"' , async ( ) => {
// Render the banner without WCS being active.
const { getByRole } = render (
< ShippingBanner
isJetpackConnected = { true }
activePlugins = { [ 'jetpack' ] }
installPlugins = { jest . fn ( ) }
activatePlugins = { jest . fn ( ) }
isRequesting = { false }
itemsCount = { 1 }
2024-09-02 12:23:52 +00:00
orderId = { 1 }
isWcstCompatible = { true }
2020-10-15 12:41:39 +00:00
/ >
) ;
userEvent . click (
getByRole ( 'link' , { name : /WooCommerce Shipping/ } )
) ;
await waitFor ( ( ) =>
expect ( recordEvent ) . toHaveBeenCalledWith (
'banner_element_clicked' ,
getExpectedTrackingData (
'shipping_banner_woocommerce_service_link' ,
false
)
)
2020-03-27 20:42:58 +00:00
) ;
} ) ;
2020-10-15 12:41:39 +00:00
it ( 'should record an event when user clicks "x" to dismiss the banner' , async ( ) => {
const { getByRole } = render (
< ShippingBanner
isJetpackConnected = { true }
2024-09-02 12:23:52 +00:00
activePlugins = { [ wcstPluginSlug , 'jetpack' ] }
2020-10-15 12:41:39 +00:00
installPlugins = { jest . fn ( ) }
activatePlugins = { jest . fn ( ) }
isRequesting = { false }
itemsCount = { 1 }
2024-09-02 12:23:52 +00:00
orderId = { 1 }
isWcstCompatible = { true }
2020-10-15 12:41:39 +00:00
/ >
2020-03-27 20:42:58 +00:00
) ;
2020-10-15 12:41:39 +00:00
userEvent . click (
getByRole ( 'button' , { name : 'Close Print Label Banner.' } )
) ;
await waitFor ( ( ) =>
expect ( recordEvent ) . toHaveBeenCalledWith (
'banner_element_clicked' ,
2024-09-02 12:23:52 +00:00
getExpectedTrackingData ( 'shipping_banner_dimiss' , false )
2020-10-15 12:41:39 +00:00
)
2020-03-27 20:42:58 +00:00
) ;
} ) ;
} ) ;
describe ( 'Create shipping label button' , ( ) => {
2020-08-31 15:13:14 +00:00
const installPlugins = jest . fn ( ) . mockReturnValue ( {
success : true ,
2020-04-16 23:58:36 +00:00
} ) ;
const activatePlugins = jest . fn ( ) . mockReturnValue ( {
2020-08-31 15:13:14 +00:00
success : true ,
2020-04-16 23:58:36 +00:00
} ) ;
2020-03-27 20:42:58 +00:00
delete window . location ; // jsdom won't allow to rewrite window.location unless deleted first
window . location = {
href : 'http://wcship.test/wp-admin/post.php?post=1000&action=edit' ,
} ;
2020-10-15 12:41:39 +00:00
it ( 'should install WooCommerce Shipping when button is clicked' , async ( ) => {
2024-09-02 12:23:52 +00:00
const actionButtonLabel = 'Create shipping label' ;
2020-10-15 12:41:39 +00:00
const { getByRole } = render (
2020-03-27 20:42:58 +00:00
< ShippingBanner
isJetpackConnected = { true }
activatePlugins = { activatePlugins }
activePlugins = { [ ] }
2020-08-31 15:13:14 +00:00
installPlugins = { installPlugins }
2020-03-27 20:42:58 +00:00
isRequesting = { false }
itemsCount = { 1 }
2024-09-02 12:23:52 +00:00
orderId = { 1 }
isWcstCompatible = { true }
actionButtonLabel = { actionButtonLabel }
2020-03-27 20:42:58 +00:00
/ >
) ;
2020-10-15 12:41:39 +00:00
userEvent . click (
getByRole ( 'button' , {
2024-09-02 12:23:52 +00:00
name : actionButtonLabel ,
2020-10-15 12:41:39 +00:00
} )
) ;
2020-03-27 20:42:58 +00:00
2020-10-15 12:41:39 +00:00
await waitFor ( ( ) =>
expect ( installPlugins ) . toHaveBeenCalledWith ( [
2024-09-02 12:23:52 +00:00
'woocommerce-shipping' ,
2020-10-15 12:41:39 +00:00
] )
) ;
2020-03-27 20:42:58 +00:00
} ) ;
2020-10-15 12:41:39 +00:00
it ( 'should activate WooCommerce Shipping when installation finishes' , async ( ) => {
2024-09-02 12:23:52 +00:00
const actionButtonLabel = 'Create shipping label' ;
2020-10-15 12:41:39 +00:00
const { getByRole } = render (
< ShippingBanner
isJetpackConnected = { true }
activatePlugins = { activatePlugins }
activePlugins = { [ ] }
installPlugins = { installPlugins }
isRequesting = { false }
itemsCount = { 1 }
2024-09-02 12:23:52 +00:00
orderId = { 1 }
isWcstCompatible = { true }
actionButtonLabel = { actionButtonLabel }
2020-10-15 12:41:39 +00:00
/ >
) ;
userEvent . click (
getByRole ( 'button' , {
2024-09-02 12:23:52 +00:00
name : actionButtonLabel ,
2020-10-15 12:41:39 +00:00
} )
) ;
2020-04-16 23:58:36 +00:00
2020-10-15 12:41:39 +00:00
await waitFor ( ( ) =>
expect ( activatePlugins ) . toHaveBeenCalledWith ( [
2024-09-02 12:23:52 +00:00
'woocommerce-shipping' ,
2020-10-15 12:41:39 +00:00
] )
) ;
2020-03-27 20:42:58 +00:00
} ) ;
it ( 'should perform a request to accept the TOS and get WCS assets to load' , async ( ) => {
2024-09-02 12:23:52 +00:00
getWcsLabelPurchaseConfigs . mockReturnValueOnce ( Promise . resolve ( { } ) ) ;
2020-10-15 12:41:39 +00:00
getWcsAssets . mockReturnValueOnce ( Promise . resolve ( { } ) ) ;
2024-09-02 12:23:52 +00:00
const actionButtonLabel = 'Create shipping label' ;
2020-03-27 20:42:58 +00:00
2020-10-15 12:41:39 +00:00
const { getByRole } = render (
< ShippingBanner
isJetpackConnected = { true }
activatePlugins = { activatePlugins }
2024-09-02 12:23:52 +00:00
activePlugins = { [ wcstPluginSlug ] }
2020-10-15 12:41:39 +00:00
installPlugins = { installPlugins }
isRequesting = { false }
itemsCount = { 1 }
2024-09-02 12:23:52 +00:00
orderId = { 1 }
isWcstCompatible = { true }
actionButtonLabel = { actionButtonLabel }
2020-10-15 12:41:39 +00:00
/ >
) ;
2020-03-27 20:42:58 +00:00
2020-10-15 12:41:39 +00:00
userEvent . click (
getByRole ( 'button' , {
2024-09-02 12:23:52 +00:00
name : actionButtonLabel ,
2020-10-15 12:41:39 +00:00
} )
) ;
2020-03-27 20:42:58 +00:00
2020-10-15 12:41:39 +00:00
await waitFor ( ( ) => expect ( acceptWcsTos ) . toHaveBeenCalled ( ) ) ;
2020-03-27 20:42:58 +00:00
expect ( getWcsAssets ) . toHaveBeenCalled ( ) ;
} ) ;
2020-10-15 12:41:39 +00:00
it ( 'should load WCS assets when a path is provided' , async ( ) => {
2024-09-02 12:23:52 +00:00
const actionButtonLabel = 'Create shipping label' ;
getWcsLabelPurchaseConfigs . mockReturnValueOnce ( Promise . resolve ( { } ) ) ;
2020-10-15 12:41:39 +00:00
const mockAssets = {
2024-09-02 12:23:52 +00:00
wcshipping _create _label _script : '/path/to/wcs.js' ,
wcshipping _create _label _style : '/path/to/wcs.css' ,
wcshipping _shipment _tracking _script :
'/wcshipping_shipment_tracking_script' ,
wcshipping _shipment _tracking _style :
'/wcshipping_shipment_tracking_style' ,
2020-03-27 20:42:58 +00:00
} ;
2020-10-15 12:41:39 +00:00
getWcsAssets . mockReturnValueOnce (
Promise . resolve ( {
assets : mockAssets ,
} )
) ;
2020-03-27 20:42:58 +00:00
2020-10-15 12:41:39 +00:00
const { getByRole } = render (
< Fragment >
< div id = "woocommerce-order-data" / >
< div id = "woocommerce-order-actions" / >
< ShippingBanner
isJetpackConnected = { true }
activatePlugins = { activatePlugins }
2024-09-02 12:23:52 +00:00
activePlugins = { [ wcstPluginSlug , 'jetpack' ] }
2020-10-15 12:41:39 +00:00
installPlugins = { installPlugins }
isRequesting = { false }
itemsCount = { 1 }
2024-09-02 12:23:52 +00:00
orderId = { 1 }
isWcstCompatible = { true }
actionButtonLabel = { actionButtonLabel }
2020-10-15 12:41:39 +00:00
/ >
< / F r a g m e n t >
2020-07-30 01:51:15 +00:00
) ;
2020-03-27 20:42:58 +00:00
2020-10-15 12:41:39 +00:00
userEvent . click (
getByRole ( 'button' , {
2024-09-02 12:23:52 +00:00
name : actionButtonLabel ,
2020-10-15 12:41:39 +00:00
} )
) ;
2020-03-27 20:42:58 +00:00
2020-10-15 12:41:39 +00:00
// Check that the metaboxes have been created.
await waitFor ( ( ) =>
expect (
getByRole ( 'heading' , { level : 2 , name : 'Shipping Label' } )
) . toBeInTheDocument ( )
) ;
2020-03-27 20:42:58 +00:00
2020-10-15 12:41:39 +00:00
expect (
getByRole ( 'heading' , { level : 2 , name : 'Shipment Tracking' } )
) . toBeInTheDocument ( ) ;
2020-03-27 20:42:58 +00:00
2020-10-15 12:41:39 +00:00
// Check that the script and style elements have been created.
2024-09-02 12:23:52 +00:00
const allScriptSrcs = Array . from (
document . querySelectorAll ( 'script' )
) . map ( ( script ) => script . src ) ;
const allLinkHrefs = Array . from (
document . querySelectorAll ( 'link' )
) . map ( ( link ) => link . href ) ;
expect ( allScriptSrcs ) . toContain (
'http://localhost' + mockAssets . wcshipping _create _label _script
2020-10-15 12:41:39 +00:00
) ;
2020-03-27 20:42:58 +00:00
2024-09-02 12:23:52 +00:00
expect ( allScriptSrcs ) . toContain (
'http://localhost' + mockAssets . wcshipping _shipment _tracking _script
2020-10-15 12:41:39 +00:00
) ;
2020-03-27 20:42:58 +00:00
2024-09-02 12:23:52 +00:00
expect ( allLinkHrefs ) . toContain (
'http://localhost' + mockAssets . wcshipping _create _label _style
2020-08-31 15:13:14 +00:00
) ;
2024-09-02 12:23:52 +00:00
expect ( allLinkHrefs ) . toContain (
'http://localhost' + mockAssets . wcshipping _shipment _tracking _style
) ;
} ) ;
2020-03-27 20:42:58 +00:00
2024-09-02 12:23:52 +00:00
it ( 'should open WCS modal' , async ( ) => {
const actionButtonLabel = 'Create shipping label' ;
getWcsLabelPurchaseConfigs . mockReturnValueOnce ( Promise . resolve ( { } ) ) ;
2020-10-15 12:41:39 +00:00
getWcsAssets . mockReturnValueOnce (
Promise . resolve ( {
assets : {
// Easy to identify string in our hijacked setter function.
2024-09-02 12:23:52 +00:00
wcshipping _create _label _script :
'wcshipping_create_label_script' ,
wcshipping _shipment _tracking _script :
'wcshipping_create_label_script' ,
2020-10-15 12:41:39 +00:00
// Empty string to avoid creating a script tag we also have to hijack.
2024-09-02 12:23:52 +00:00
wcshipping _create _label _style : '' ,
wcshipping _shipment _tracking _style : '' ,
2020-10-15 12:41:39 +00:00
} ,
} )
) ;
// Force the script tag to trigger its onload().
// Adapted from https://stackoverflow.com/a/49204336.
// const scriptSrcProperty = window.HTMLScriptElement.prototype.src;
Object . defineProperty ( window . HTMLScriptElement . prototype , 'src' , {
set ( src ) {
2024-09-02 12:23:52 +00:00
if (
[
'wcshipping_create_label_script' ,
'wcshipping_shipment_tracking_script' ,
] . includes ( src )
) {
setTimeout ( ( ) => {
this . onload ( ) ;
} , 1 ) ;
2020-10-15 12:41:39 +00:00
}
} ,
2020-03-27 20:42:58 +00:00
} ) ;
2020-10-15 12:41:39 +00:00
const { getByRole } = render (
< Fragment >
< div id = "woocommerce-order-data" / >
< div id = "woocommerce-order-actions" / >
< div id = "woocommerce-admin-print-label" / >
< ShippingBanner
isJetpackConnected = { true }
activatePlugins = { activatePlugins }
activePlugins = { [ wcsPluginSlug , 'jetpack' ] }
installPlugins = { installPlugins }
isRequesting = { false }
itemsCount = { 1 }
2024-09-02 12:23:52 +00:00
orderId = { 1 }
isWcstCompatible = { true }
actionButtonLabel = { actionButtonLabel }
2020-10-15 12:41:39 +00:00
/ >
< / F r a g m e n t >
2020-03-27 20:42:58 +00:00
) ;
2020-10-15 12:41:39 +00:00
2024-09-02 12:23:52 +00:00
const openWcsModalSpy = jest . spyOn (
ShippingBanner . prototype ,
'openWcsModal'
) ;
2020-10-15 12:41:39 +00:00
// Initiate the loading of WCS assets on first click.
userEvent . click (
getByRole ( 'button' , {
2024-09-02 12:23:52 +00:00
name : actionButtonLabel ,
2020-10-15 12:41:39 +00:00
} )
) ;
2024-09-02 12:23:52 +00:00
await waitFor ( ( ) => {
expect (
document . getElementById ( 'woocommerce-admin-print-label' )
) . not . toBeVisible ( ) ;
} ) ;
2020-03-27 20:42:58 +00:00
2024-09-02 12:23:52 +00:00
expect ( openWcsModalSpy ) . toHaveBeenCalledTimes ( 1 ) ;
2020-03-27 20:42:58 +00:00
} ) ;
} ) ;
describe ( 'In the process of installing, activating, loading assets for WooCommerce Service' , ( ) => {
2020-10-15 12:41:39 +00:00
it ( 'should show a busy loading state on "Create shipping label" and should disable "Close Print Label Banner"' , async ( ) => {
2024-09-02 12:23:52 +00:00
const actionButtonLabel = 'Create shipping label' ;
2020-10-15 12:41:39 +00:00
const { getByRole } = render (
2020-03-27 20:42:58 +00:00
< ShippingBanner
isJetpackConnected = { true }
activatePlugins = { jest . fn ( ) }
2020-10-15 12:41:39 +00:00
activePlugins = { [ 'jetpack' ] }
2020-08-31 15:13:14 +00:00
installPlugins = { jest . fn ( ) }
2020-03-27 20:42:58 +00:00
isRequesting = { true }
itemsCount = { 1 }
2024-09-02 12:23:52 +00:00
orderId = { 1 }
isWcstCompatible = { true }
actionButtonLabel = { actionButtonLabel }
2020-03-27 20:42:58 +00:00
/ >
) ;
2020-10-15 12:41:39 +00:00
expect (
2024-09-02 12:23:52 +00:00
getByRole ( 'button' , { name : actionButtonLabel } )
2020-10-15 12:41:39 +00:00
) . not . toHaveClass ( 'is-busy' ) ;
expect (
getByRole ( 'button' , { name : 'Close Print Label Banner.' } )
) . toBeEnabled ( ) ;
2024-09-02 12:23:52 +00:00
userEvent . click ( getByRole ( 'button' , { name : actionButtonLabel } ) ) ;
2020-10-15 12:41:39 +00:00
await waitFor ( ( ) =>
expect (
2024-09-02 12:23:52 +00:00
getByRole ( 'button' , { name : actionButtonLabel } )
2020-10-15 12:41:39 +00:00
) . toHaveClass ( 'is-busy' )
) ;
2020-03-27 20:42:58 +00:00
2020-10-15 12:41:39 +00:00
expect (
getByRole ( 'button' , { name : 'Close Print Label Banner.' } )
) . toBeDisabled ( ) ;
2020-03-27 20:42:58 +00:00
} ) ;
} ) ;
describe ( 'Setup error message' , ( ) => {
2020-10-15 12:41:39 +00:00
it ( 'should not show if there is no error (no interaction)' , ( ) => {
const { container } = render (
2020-03-27 20:42:58 +00:00
< ShippingBanner
isJetpackConnected = { true }
2020-10-15 12:41:39 +00:00
activatePlugins = { jest . fn ( ) }
2020-04-16 23:58:36 +00:00
activePlugins = { [ 'jetpack' ] }
2020-10-15 12:41:39 +00:00
installPlugins = { jest . fn ( ) }
2020-03-27 20:42:58 +00:00
itemsCount = { 1 }
isRequesting = { false }
2024-09-02 12:23:52 +00:00
orderId = { 1 }
isWcstCompatible = { true }
actionButtonLabel = "Create shipping label"
2020-03-27 20:42:58 +00:00
/ >
) ;
2020-10-15 12:41:39 +00:00
expect (
container . getElementsByClassName (
'wc-admin-shipping-banner-install-error'
)
) . toHaveLength ( 0 ) ;
2020-03-27 20:42:58 +00:00
} ) ;
2020-04-16 23:58:36 +00:00
it ( 'should show if there is installation error' , async ( ) => {
2024-09-02 12:23:52 +00:00
const actionButtonLabel = 'Create shipping label' ;
2020-10-15 12:41:39 +00:00
const { getByRole , getByText } = render (
< ShippingBanner
isJetpackConnected = { true }
activatePlugins = { jest . fn ( ) }
activePlugins = { [ 'jetpack' ] }
installPlugins = { jest . fn ( ) . mockReturnValue ( {
success : false ,
} ) }
itemsCount = { 1 }
isRequesting = { false }
2024-09-02 12:23:52 +00:00
orderId = { 1 }
isWcstCompatible = { true }
actionButtonLabel = { actionButtonLabel }
2020-10-15 12:41:39 +00:00
/ >
) ;
2020-04-16 23:58:36 +00:00
2024-09-02 12:23:52 +00:00
userEvent . click ( getByRole ( 'button' , { name : actionButtonLabel } ) ) ;
2020-10-15 12:41:39 +00:00
await waitFor ( ( ) =>
expect (
getByText (
'Unable to install the plugin. Refresh the page and try again.'
)
) . toBeInTheDocument ( )
2020-03-27 20:42:58 +00:00
) ;
} ) ;
2020-04-16 23:58:36 +00:00
it ( 'should show if there is activation error' , async ( ) => {
2024-09-02 12:23:52 +00:00
const actionButtonLabel = 'Create shipping label' ;
2020-10-15 12:41:39 +00:00
const { getByRole , getByText } = render (
2020-04-16 23:58:36 +00:00
< ShippingBanner
isJetpackConnected = { true }
2020-10-15 12:41:39 +00:00
activatePlugins = { jest . fn ( ) . mockReturnValue ( {
success : false ,
} ) }
2020-04-16 23:58:36 +00:00
activePlugins = { [ 'jetpack' ] }
2020-08-31 15:13:14 +00:00
installPlugins = { jest . fn ( ) . mockReturnValue ( {
success : true ,
2020-04-16 23:58:36 +00:00
} ) }
itemsCount = { 1 }
isRequesting = { false }
2024-09-02 12:23:52 +00:00
orderId = { 1 }
isWcstCompatible = { true }
actionButtonLabel = { actionButtonLabel }
2020-04-16 23:58:36 +00:00
/ >
2020-03-27 20:42:58 +00:00
) ;
2020-04-16 23:58:36 +00:00
2024-09-02 12:23:52 +00:00
userEvent . click ( getByRole ( 'button' , { name : actionButtonLabel } ) ) ;
2020-04-16 23:58:36 +00:00
2020-10-15 12:41:39 +00:00
await waitFor ( ( ) =>
expect (
getByText (
'Unable to activate the plugin. Refresh the page and try again.'
)
) . toBeInTheDocument ( )
2020-03-27 20:42:58 +00:00
) ;
} ) ;
} ) ;
describe ( 'The message in the banner' , ( ) => {
2020-04-16 23:58:36 +00:00
const createShippingBannerWrapper = ( { activePlugins } ) =>
2020-10-15 12:41:39 +00:00
render (
2020-03-27 20:42:58 +00:00
< ShippingBanner
isJetpackConnected = { true }
activatePlugins = { jest . fn ( ) }
activePlugins = { activePlugins }
2020-08-31 15:13:14 +00:00
installPlugins = { jest . fn ( ) }
2020-03-27 20:42:58 +00:00
isRequesting = { true }
itemsCount = { 1 }
2024-09-02 12:23:52 +00:00
orderId = { 1 }
isWcstCompatible = { true }
actionButtonLabel = "Create shipping label"
2020-03-27 20:42:58 +00:00
/ >
) ;
const notActivatedMessage =
2020-10-15 12:41:39 +00:00
'By clicking "Create shipping label", WooCommerce Shipping(opens in a new tab) will be installed and you agree to its Terms of Service(opens in a new tab).' ;
2020-03-27 20:42:58 +00:00
it ( 'should show install text "By clicking "Create shipping label"..." when first loaded.' , ( ) => {
2020-10-15 12:41:39 +00:00
const { container } = createShippingBannerWrapper ( {
2020-03-27 20:42:58 +00:00
activePlugins : [ ] ,
} ) ;
2020-10-15 12:41:39 +00:00
expect (
container . querySelector ( '.wc-admin-shipping-banner-blob p' )
. textContent
) . toBe ( notActivatedMessage ) ;
2020-03-27 20:42:58 +00:00
} ) ;
it ( 'should continue to show the initial message "By clicking "Create shipping label"..." after WooCommerce Service is installed successfully.' , ( ) => {
2020-10-15 12:41:39 +00:00
const { container , rerender } = createShippingBannerWrapper ( {
2020-03-27 20:42:58 +00:00
activePlugins : [ ] ,
} ) ;
2020-10-15 12:41:39 +00:00
rerender (
< ShippingBanner
isJetpackConnected = { true }
activatePlugins = { jest . fn ( ) }
2024-09-02 12:23:52 +00:00
activePlugins = { [ wcstPluginSlug ] }
2020-10-15 12:41:39 +00:00
installPlugins = { jest . fn ( ) }
isRequesting = { true }
itemsCount = { 1 }
2024-09-02 12:23:52 +00:00
orderId = { 1 }
isWcstCompatible = { true }
actionButtonLabel = "Create shipping label"
2020-10-15 12:41:39 +00:00
/ >
) ;
expect (
container . querySelector ( '.wc-admin-shipping-banner-blob p' )
. textContent
) . toBe ( notActivatedMessage ) ;
2020-03-27 20:42:58 +00:00
} ) ;
2024-09-02 12:23:52 +00:00
} ) ;
2020-03-27 20:42:58 +00:00
2024-09-02 12:23:52 +00:00
describe ( 'If incompatible WCS&T is active' , ( ) => {
const installPlugins = jest . fn ( ) . mockReturnValue ( {
success : true ,
} ) ;
const activatePlugins = jest . fn ( ) . mockReturnValue ( {
success : true ,
} ) ;
beforeEach ( ( ) => {
acceptWcsTos . mockClear ( ) ;
} ) ;
it ( 'should install and activate but show an error notice when an incompatible version of WCS&T is installed' , async ( ) => {
const actionButtonLabel = 'Install WooCommerce Shipping' ;
const { getByRole , getByText } = render (
< Fragment >
< div id = "woocommerce-order-data" / >
< div id = "woocommerce-order-actions" / >
< div id = "woocommerce-admin-print-label" / >
< ShippingBanner
isJetpackConnected = { true }
activatePlugins = { activatePlugins }
activePlugins = { [ wcstPluginSlug , 'jetpack' ] }
installPlugins = { installPlugins }
itemsCount = { 1 }
isRequesting = { false }
orderId = { 1 }
isWcstCompatible = { false }
actionButtonLabel = { actionButtonLabel }
/ >
< / F r a g m e n t >
) ;
userEvent . click ( getByRole ( 'button' , { name : actionButtonLabel } ) ) ;
await waitFor ( ( ) => {
expect ( installPlugins ) . toHaveBeenCalledWith ( [ wcsPluginSlug ] ) ;
} ) ;
await waitFor ( ( ) => {
expect ( activatePlugins ) . toHaveBeenCalledWith ( [ wcsPluginSlug ] ) ;
2020-03-27 20:42:58 +00:00
} ) ;
2020-10-15 12:41:39 +00:00
2024-09-02 12:23:52 +00:00
await waitFor ( ( ) => {
expect ( acceptWcsTos ) . not . toHaveBeenCalled ( ) ;
} ) ;
const notice = getByText ( ( _ , element ) => {
const hasText = ( node ) =>
node . textContent ===
'Please update the WooCommerce Shipping & Tax plugin to the latest version to ensure compatibility with WooCommerce Shipping.' ;
const nodeHasText = hasText ( element ) ;
const childrenDontHaveText = Array . from ( element . children ) . every (
( child ) => ! hasText ( child )
) ;
return nodeHasText && childrenDontHaveText ;
} ) ;
await waitFor ( ( ) => expect ( notice ) . toBeInTheDocument ( ) ) ;
// Assert that the "update" link is present
const updateLink = getByText ( /update/i ) ;
expect ( updateLink ) . toBeInTheDocument ( ) ;
expect ( updateLink . tagName ) . toBe ( 'A' ) ; // Ensures it's a link
2020-03-27 20:42:58 +00:00
} ) ;
} ) ;