2023-02-26 18:23:11 +00:00
/ * *
* External dependencies
* /
import { render , screen } from '@testing-library/react' ;
import userEvent from '@testing-library/user-event' ;
/ * *
* Internal dependencies
* /
2023-03-19 14:54:55 +00:00
import { useCampaignTypes , useCampaigns } from '~/marketing/hooks' ;
2023-02-26 18:23:11 +00:00
import { Campaigns } from './Campaigns' ;
2023-03-02 19:25:00 +00:00
jest . mock ( '~/marketing/hooks' , ( ) = > ( {
2023-03-19 14:54:55 +00:00
useCampaigns : jest.fn ( ) ,
2023-03-09 13:52:11 +00:00
useCampaignTypes : jest.fn ( ) ,
2023-03-02 19:25:00 +00:00
} ) ) ;
2023-03-08 17:08:28 +00:00
jest . mock ( '~/marketing/components' , ( ) = > {
const originalModule = jest . requireActual ( '~/marketing/components' ) ;
return {
__esModule : true ,
. . . originalModule ,
2023-03-12 17:02:12 +00:00
CreateNewCampaignModal : ( ) = > < div > Mocked CreateNewCampaignModal < / div > ,
2023-03-08 17:08:28 +00:00
} ;
} ) ;
2023-03-02 19:25:00 +00:00
2023-02-28 15:51:20 +00:00
/ * *
* Create a test campaign data object .
* /
const createTestCampaign = ( programId : string ) = > {
return {
id : ` google-listings-and-ads| ${ programId } ` ,
title : ` Campaign ${ programId } ` ,
description : '' ,
cost : ` USD 30 ` ,
manageUrl : ` https://wc1.test/wp-admin/admin.php?page=wc-admin&path=/google/dashboard&subpath=/campaigns/edit&programId= ${ programId } ` ,
2024-04-09 08:50:15 +00:00
icon : 'https://woocommerce.com/wp-content/uploads/2021/06/woo-GoogleListingsAds-jworee.png' ,
2024-07-22 14:59:43 +00:00
channelName : 'Google for WooCommerce' ,
2023-02-28 15:51:20 +00:00
channelSlug : 'google-listings-and-ads' ,
} ;
} ;
2023-02-26 18:23:11 +00:00
describe ( 'Campaigns component' , ( ) = > {
it ( 'renders a TablePlaceholder when loading is in progress' , ( ) = > {
( useCampaigns as jest . Mock ) . mockReturnValue ( {
loading : true ,
error : undefined ,
data : undefined ,
meta : undefined ,
} ) ;
2023-03-09 13:52:11 +00:00
( useCampaignTypes as jest . Mock ) . mockReturnValue ( {
2023-03-02 19:25:00 +00:00
loading : true ,
} ) ;
2023-02-26 18:23:11 +00:00
const { container } = render ( < Campaigns / > ) ;
const tablePlaceholder = container . querySelector (
'div.woocommerce-table__table.is-loading'
) ;
expect ( tablePlaceholder ) . toBeInTheDocument ( ) ;
} ) ;
it ( 'renders an error message when loading is done and data is undefined' , ( ) = > {
( useCampaigns as jest . Mock ) . mockReturnValue ( {
loading : false ,
error : { } ,
data : undefined ,
meta : undefined ,
} ) ;
2023-03-09 13:52:11 +00:00
( useCampaignTypes as jest . Mock ) . mockReturnValue ( {
2023-03-02 19:25:00 +00:00
loading : false ,
} ) ;
2023-02-26 18:23:11 +00:00
render ( < Campaigns / > ) ;
expect (
screen . getByText ( 'An unexpected error occurred.' )
) . toBeInTheDocument ( ) ;
} ) ;
it ( 'renders an info message when loading is done and data is an empty array' , ( ) = > {
( useCampaigns as jest . Mock ) . mockReturnValue ( {
loading : false ,
error : undefined ,
data : [ ] ,
meta : {
total : 0 ,
} ,
} ) ;
2023-03-09 13:52:11 +00:00
( useCampaignTypes as jest . Mock ) . mockReturnValue ( {
2023-03-02 19:25:00 +00:00
loading : false ,
} ) ;
2023-02-26 18:23:11 +00:00
render ( < Campaigns / > ) ;
expect (
screen . getByText ( 'Advertise with marketing campaigns' )
) . toBeInTheDocument ( ) ;
} ) ;
it ( 'renders a table with campaign info and without pagination when loading is done and data is an array with length no more than page size' , ( ) = > {
( useCampaigns as jest . Mock ) . mockReturnValue ( {
loading : false ,
error : undefined ,
2023-02-28 15:51:20 +00:00
data : [ createTestCampaign ( '1' ) ] ,
2023-02-26 18:23:11 +00:00
meta : {
total : 1 ,
} ,
} ) ;
2023-03-09 13:52:11 +00:00
( useCampaignTypes as jest . Mock ) . mockReturnValue ( {
2023-03-02 19:25:00 +00:00
loading : false ,
} ) ;
2023-02-26 18:23:11 +00:00
const { container } = render ( < Campaigns / > ) ;
2023-02-28 15:51:20 +00:00
expect ( screen . getByText ( 'Campaign 1' ) ) . toBeInTheDocument ( ) ;
expect ( screen . getByText ( 'USD 30' ) ) . toBeInTheDocument ( ) ;
2023-02-26 18:23:11 +00:00
const pagination = container . querySelector ( '.woocommerce-pagination' ) ;
expect ( pagination ) . not . toBeInTheDocument ( ) ;
} ) ;
it ( 'renders a table with campaign info and with pagination when loading is done and data is an array with length more than page size' , async ( ) = > {
( useCampaigns as jest . Mock ) . mockReturnValue ( {
loading : false ,
error : undefined ,
data : [
2023-02-28 15:51:20 +00:00
createTestCampaign ( '1' ) ,
createTestCampaign ( '2' ) ,
createTestCampaign ( '3' ) ,
createTestCampaign ( '4' ) ,
createTestCampaign ( '5' ) ,
createTestCampaign ( '6' ) ,
2023-02-26 18:23:11 +00:00
] ,
meta : {
total : 6 ,
} ,
} ) ;
2023-03-09 13:52:11 +00:00
( useCampaignTypes as jest . Mock ) . mockReturnValue ( {
2023-03-02 19:25:00 +00:00
loading : false ,
} ) ;
2023-02-26 18:23:11 +00:00
render ( < Campaigns / > ) ;
// Campaign info.
2023-02-28 15:51:20 +00:00
expect ( screen . getByText ( 'Campaign 1' ) ) . toBeInTheDocument ( ) ;
2023-02-26 18:23:11 +00:00
// Pagination.
expect ( screen . getByText ( 'Page 1 of 2' ) ) . toBeInTheDocument ( ) ;
// Click on the next button to go to next page.
await userEvent . click (
screen . getByRole ( 'button' , { name : 'Next Page' } )
) ;
// Campaign info in the second page.
2023-02-28 15:51:20 +00:00
expect ( screen . getByText ( 'Campaign 6' ) ) . toBeInTheDocument ( ) ;
2023-02-26 18:23:11 +00:00
} ) ;
2023-03-02 19:25:00 +00:00
2023-06-20 15:03:56 +00:00
it ( 'does not render a "Create new campaign" button in the card header when there are no campaign types' , async ( ) = > {
2023-03-02 19:25:00 +00:00
( useCampaigns as jest . Mock ) . mockReturnValue ( {
loading : false ,
error : undefined ,
data : [ createTestCampaign ( '1' ) ] ,
meta : {
total : 1 ,
} ,
} ) ;
2023-03-09 13:52:11 +00:00
( useCampaignTypes as jest . Mock ) . mockReturnValue ( {
2023-03-02 19:25:00 +00:00
loading : false ,
2023-06-20 15:03:56 +00:00
data : [ ] ,
} ) ;
render ( < Campaigns / > ) ;
expect (
screen . queryByRole ( 'button' , { name : 'Create new campaign' } )
) . not . toBeInTheDocument ( ) ;
} ) ;
it ( 'renders a "Create new campaign" button in the card header when there are campaign types, and upon clicking, displays the "Create a new campaign" modal' , async ( ) = > {
( useCampaigns as jest . Mock ) . mockReturnValue ( {
loading : false ,
error : undefined ,
data : [ createTestCampaign ( '1' ) ] ,
meta : {
total : 1 ,
} ,
} ) ;
( useCampaignTypes as jest . Mock ) . mockReturnValue ( {
loading : false ,
data : [
{
id : 'google-ads' ,
name : 'Google Ads' ,
description :
'Boost your product listings with a campaign that is automatically optimized to meet your goals.' ,
channel : {
slug : 'google-listings-and-ads' ,
2024-07-22 14:59:43 +00:00
name : 'Google for WooCommerce' ,
2023-06-20 15:03:56 +00:00
} ,
create_url :
'https://wc1.test/wp-admin/admin.php?page=wc-admin&path=/google/dashboard&subpath=/campaigns/create' ,
icon_url :
2024-04-09 08:50:15 +00:00
'https://woocommerce.com/wp-content/uploads/2021/06/woo-GoogleListingsAds-jworee.png' ,
2023-06-20 15:03:56 +00:00
} ,
] ,
2023-03-02 19:25:00 +00:00
} ) ;
render ( < Campaigns / > ) ;
await userEvent . click (
screen . getByRole ( 'button' , { name : 'Create new campaign' } )
) ;
2023-03-09 14:09:24 +00:00
// Mocked CreateNewCampaignModal should be displayed.
2023-03-02 19:25:00 +00:00
expect (
2023-03-09 14:09:24 +00:00
screen . getByText ( 'Mocked CreateNewCampaignModal' )
2023-03-02 19:25:00 +00:00
) . toBeInTheDocument ( ) ;
} ) ;
2023-02-26 18:23:11 +00:00
} ) ;