2022-02-09 06:12:20 +00:00
/ * *
* External dependencies
* /
import { useSelect } from '@wordpress/data' ;
2022-02-15 02:30:46 +00:00
import { fireEvent , render , screen } from '@testing-library/react' ;
import { recordEvent } from '@woocommerce/tracks' ;
2022-02-09 06:12:20 +00:00
/ * *
* Internal dependencies
* /
import { PaymentGatewaySuggestions } from '../index' ;
jest . mock ( '@wordpress/data' , ( ) => ( {
... jest . requireActual ( '@wordpress/data' ) ,
useSelect : jest . fn ( ) ,
useDispatch : jest . fn ( ) . mockImplementation ( ( ) => ( {
updatePaymentGateway : jest . fn ( ) ,
} ) ) ,
} ) ) ;
2022-02-15 02:30:46 +00:00
jest . mock ( '@woocommerce/tracks' , ( ) => ( { recordEvent : jest . fn ( ) } ) ) ;
2022-02-09 06:12:20 +00:00
const paymentGatewaySuggestions = [
{
id : 'stripe' ,
title : 'Stripe' ,
content :
'Accept debit and credit cards in 135+ currencies, methods such as Alipay, and one-touch checkout with Apple Pay.' ,
2022-06-21 08:37:34 +00:00
image : 'http://localhost:8888/wp-content/plugins/woocommerce/assets/images/stripe.png' ,
2022-02-09 06:12:20 +00:00
plugins : [ 'woocommerce-gateway-stripe' ] ,
is _visible : true ,
recommendation _priority : 3 ,
2022-04-13 07:43:07 +00:00
category _other : [ 'US' ] ,
category _additional : [ ] ,
2022-02-09 06:12:20 +00:00
} ,
{
id : 'ppcp-gateway' ,
title : 'PayPal Payments' ,
content :
"Safe and secure payments using credit cards or your customer's PayPal account." ,
2022-06-21 08:37:34 +00:00
image : 'http://localhost:8888/wp-content/plugins/woocommerce/assets/images/paypal.png' ,
2022-02-09 06:12:20 +00:00
plugins : [ 'woocommerce-paypal-payments' ] ,
is _visible : true ,
2022-04-13 07:43:07 +00:00
category _other : [ 'US' ] ,
category _additional : [ 'US' ] ,
2022-02-09 06:12:20 +00:00
} ,
{
id : 'cod' ,
title : 'Cash on delivery' ,
content : 'Take payments in cash upon delivery.' ,
2022-06-21 08:37:34 +00:00
image : 'http://localhost:8888/wp-content/plugins/woocommerce-admin/images/onboarding/cod.svg' ,
2022-02-09 06:12:20 +00:00
is _visible : true ,
2022-03-31 04:23:26 +00:00
is _offline : true ,
2022-02-09 06:12:20 +00:00
} ,
{
id : 'bacs' ,
title : 'Direct bank transfer' ,
content : 'Take payments via bank transfer.' ,
2022-06-21 08:37:34 +00:00
image : 'http://localhost:8888/wp-content/plugins/woocommerce-admin/images/onboarding/bacs.svg' ,
2022-02-09 06:12:20 +00:00
is _visible : true ,
2022-03-31 04:23:26 +00:00
is _offline : true ,
2022-02-09 06:12:20 +00:00
} ,
{
id : 'woocommerce_payments:non-us' ,
title : 'WooCommerce Payments' ,
content :
'Manage transactions without leaving your WordPress Dashboard. Only with WooCommerce Payments.' ,
2022-06-21 08:37:34 +00:00
image : 'http://localhost:8888/wp-content/plugins/woocommerce-admin/images/onboarding/wcpay.svg' ,
2022-02-09 06:12:20 +00:00
plugins : [ 'woocommerce-payments' ] ,
description :
'With WooCommerce Payments, you can securely accept major cards, Apple Pay, and payments in over 100 currencies. Track cash flow and manage recurring revenue directly from your store’ s dashboard - with no setup costs or monthly fees.' ,
is _visible : true ,
recommendation _priority : 1 ,
} ,
{
id : 'eway' ,
title : 'Eway' ,
content :
'The Eway extension for WooCommerce allows you to take credit card payments directly on your store without redirecting your customers to a third party site to make payment.' ,
2022-06-21 08:37:34 +00:00
image : 'http://localhost:8888/wp-content/plugins/woocommerce-admin/images/onboarding/eway.png' ,
2022-02-09 06:12:20 +00:00
plugins : [ 'woocommerce-gateway-eway' ] ,
is _visible : true ,
2022-04-13 11:12:25 +00:00
category _other : [ 'US' ] ,
category _additional : [ 'US' ] ,
2022-02-09 06:12:20 +00:00
} ,
] ;
2022-04-05 03:01:03 +00:00
const paymentGatewaySuggestionsWithoutWCPay = paymentGatewaySuggestions . filter (
( p ) => p . title !== 'WooCommerce Payments'
) ;
2022-02-09 06:12:20 +00:00
describe ( 'PaymentGatewaySuggestions' , ( ) => {
2022-04-05 03:01:03 +00:00
test ( 'should render only WCPay if its suggested' , ( ) => {
2022-02-09 06:12:20 +00:00
const onComplete = jest . fn ( ) ;
const query = { } ;
useSelect . mockImplementation ( ( ) => ( {
isResolving : false ,
getPaymentGateway : jest . fn ( ) ,
paymentGatewaySuggestions ,
installedPaymentGateways : [ ] ,
} ) ) ;
const { container } = render (
< PaymentGatewaySuggestions
onComplete = { onComplete }
query = { query }
/ >
) ;
const paymentTitleElements = container . querySelectorAll (
'.woocommerce-task-payment__title'
) ;
const paymentTitles = Array . from ( paymentTitleElements ) . map (
( e ) => e . textContent
) ;
2022-04-05 03:01:03 +00:00
expect ( paymentTitles ) . toEqual ( [ ] ) ;
expect (
2022-04-11 03:51:55 +00:00
container . getElementsByTagName ( 'title' ) [ 0 ] . textContent
) . toBe ( 'WooCommerce Payments' ) ;
2022-04-05 03:01:03 +00:00
} ) ;
test ( 'should render all payment gateways if no WCPay' , ( ) => {
const onComplete = jest . fn ( ) ;
const query = { } ;
useSelect . mockImplementation ( ( ) => ( {
isResolving : false ,
getPaymentGateway : jest . fn ( ) ,
paymentGatewaySuggestions : paymentGatewaySuggestionsWithoutWCPay ,
installedPaymentGateways : [ ] ,
} ) ) ;
const { container } = render (
< PaymentGatewaySuggestions
onComplete = { onComplete }
query = { query }
/ >
) ;
const paymentTitleElements = container . querySelectorAll (
2022-04-14 02:52:12 +00:00
'.woocommerce-task-payment__title > span:first-child'
2022-04-05 03:01:03 +00:00
) ;
const paymentTitles = Array . from ( paymentTitleElements ) . map (
( e ) => e . textContent
) ;
2022-02-09 06:12:20 +00:00
expect ( paymentTitles ) . toEqual ( [
'Stripe' ,
'PayPal Payments' ,
'Eway' ,
'Cash on delivery' ,
'Direct bank transfer' ,
] ) ;
} ) ;
test ( 'should the payment gateway offline options at the bottom' , ( ) => {
const onComplete = jest . fn ( ) ;
const query = { } ;
useSelect . mockImplementation ( ( ) => ( {
isResolving : false ,
getPaymentGateway : jest . fn ( ) ,
2022-04-05 03:01:03 +00:00
paymentGatewaySuggestions : paymentGatewaySuggestionsWithoutWCPay ,
2022-02-09 06:12:20 +00:00
installedPaymentGateways : [ ] ,
} ) ) ;
const { container } = render (
< PaymentGatewaySuggestions
onComplete = { onComplete }
query = { query }
/ >
) ;
const paymentTitles = container . querySelectorAll (
'.woocommerce-task-payment__title'
) ;
expect ( paymentTitles [ paymentTitles . length - 1 ] . textContent ) . toBe (
'Direct bank transfer'
) ;
} ) ;
test ( 'should have finish setup button for installed payment gateways' , ( ) => {
const onComplete = jest . fn ( ) ;
const query = { } ;
useSelect . mockImplementation ( ( ) => ( {
isResolving : false ,
getPaymentGateway : jest . fn ( ) ,
2022-04-05 03:01:03 +00:00
paymentGatewaySuggestions : paymentGatewaySuggestionsWithoutWCPay ,
2022-02-09 06:12:20 +00:00
installedPaymentGateways : [
{
id : 'ppcp-gateway' ,
title : 'PayPal Payments' ,
content :
"Safe and secure payments using credit cards or your customer's PayPal account." ,
2022-06-21 08:37:34 +00:00
image : 'http://localhost:8888/wp-content/plugins/woocommerce/assets/images/paypal.png' ,
2022-02-09 06:12:20 +00:00
plugins : [ 'woocommerce-paypal-payments' ] ,
is _visible : true ,
} ,
] ,
} ) ) ;
const { getByText } = render (
< PaymentGatewaySuggestions
onComplete = { onComplete }
query = { query }
/ >
) ;
expect ( getByText ( 'Finish setup' ) ) . toBeInTheDocument ( ) ;
} ) ;
2022-04-13 07:43:07 +00:00
test ( 'should show "category_additional" gateways only after WCPay is set up' , ( ) => {
const onComplete = jest . fn ( ) ;
const query = { } ;
useSelect . mockImplementation ( ( ) => ( {
isResolving : false ,
getPaymentGateway : jest . fn ( ) ,
paymentGatewaySuggestions ,
installedPaymentGateways : [
{
id : 'woocommerce_payments' ,
title : 'WooCommerce Payments' ,
plugins : [ 'woocommerce-payments' ] ,
is _visible : true ,
needs _setup : false ,
} ,
] ,
countryCode : 'US' ,
} ) ) ;
const { container } = render (
< PaymentGatewaySuggestions
onComplete = { onComplete }
query = { query }
/ >
) ;
const paymentTitleElements = container . querySelectorAll (
'.woocommerce-task-payment__title'
) ;
const paymentTitles = Array . from ( paymentTitleElements ) . map (
( e ) => e . textContent
) ;
expect ( paymentTitles ) . toEqual ( [
'PayPal Payments' ,
2022-04-13 11:12:25 +00:00
'Eway' ,
2022-04-13 07:43:07 +00:00
'Cash on delivery' ,
'Direct bank transfer' ,
] ) ;
} ) ;
2022-02-15 02:30:46 +00:00
test ( 'should record event correctly when finish setup is clicked' , ( ) => {
const onComplete = jest . fn ( ) ;
const query = { } ;
useSelect . mockImplementation ( ( ) => ( {
isResolving : false ,
getPaymentGateway : jest . fn ( ) ,
2022-04-05 03:01:03 +00:00
paymentGatewaySuggestions : paymentGatewaySuggestionsWithoutWCPay ,
2022-02-15 02:30:46 +00:00
installedPaymentGateways : [
{
id : 'ppcp-gateway' ,
title : 'PayPal Payments' ,
content :
"Safe and secure payments using credit cards or your customer's PayPal account." ,
2022-06-21 08:37:34 +00:00
image : 'http://localhost:8888/wp-content/plugins/woocommerce/assets/images/paypal.png' ,
2022-02-15 02:30:46 +00:00
plugins : [ 'woocommerce-paypal-payments' ] ,
is _visible : true ,
} ,
] ,
} ) ) ;
render (
< PaymentGatewaySuggestions
onComplete = { onComplete }
query = { query }
/ >
) ;
fireEvent . click ( screen . getByText ( 'Finish setup' ) ) ;
expect ( recordEvent ) . toHaveBeenCalledWith ( 'tasklist_payment_setup' , {
selected : 'ppcp_gateway' ,
} ) ;
} ) ;
2022-04-05 03:01:03 +00:00
test ( 'should record event correctly when other payment methods is clicked' , ( ) => {
const onComplete = jest . fn ( ) ;
const query = { } ;
useSelect . mockImplementation ( ( ) => ( {
isResolving : false ,
getPaymentGateway : jest . fn ( ) ,
paymentGatewaySuggestions ,
installedPaymentGateways : [ ] ,
2022-04-13 11:12:25 +00:00
countryCode : 'US' ,
2022-04-05 03:01:03 +00:00
} ) ) ;
render (
< PaymentGatewaySuggestions
onComplete = { onComplete }
query = { query }
/ >
) ;
fireEvent . click ( screen . getByText ( 'Other payment methods' ) ) ;
// By default it's hidden, so when toggle it shows.
2022-04-13 11:12:25 +00:00
// Second call after "tasklist_payments_options".
expect (
recordEvent . mock . calls [ recordEvent . mock . calls . length - 1 ]
) . toEqual ( [
2022-04-05 03:01:03 +00:00
'tasklist_payment_show_toggle' ,
{
toggle : 'show' ,
2022-04-11 07:57:26 +00:00
payment _method _count : paymentGatewaySuggestions . length - 1 , // Minus one for WCPay since it's not counted in "other payment methods".
2022-04-13 11:12:25 +00:00
} ,
] ) ;
2022-04-05 03:01:03 +00:00
} ) ;
test ( 'should record event correctly when see more is clicked' , ( ) => {
const onComplete = jest . fn ( ) ;
const query = { } ;
useSelect . mockImplementation ( ( ) => ( {
isResolving : false ,
getPaymentGateway : jest . fn ( ) ,
paymentGatewaySuggestions ,
installedPaymentGateways : [ ] ,
2022-04-13 11:12:25 +00:00
countryCode : 'US' ,
2022-04-05 03:01:03 +00:00
} ) ) ;
render (
< PaymentGatewaySuggestions
onComplete = { onComplete }
query = { query }
/ >
) ;
fireEvent . click ( screen . getByText ( 'Other payment methods' ) ) ;
fireEvent . click ( screen . getByText ( 'See more' ) ) ;
2022-04-13 11:12:25 +00:00
expect (
recordEvent . mock . calls [ recordEvent . mock . calls . length - 1 ]
) . toEqual ( [ 'tasklist_payment_see_more' , { } ] ) ;
2022-04-05 03:01:03 +00:00
} ) ;
2022-02-09 06:12:20 +00:00
} ) ;