From 4e79b475a73bd8d147deb45ff83f95d09e892fa3 Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Wed, 17 Feb 2021 15:16:54 +1300 Subject: [PATCH 01/10] Test --- .../send-a-message/whatsapp-button/edit.js | 204 +-------------- .../whatsapp-button/settings.js | 232 ++++++++++++++++++ .../whatsapp-button/test/edit.js | 44 ++++ .../whatsapp-button/test/settings.js | 45 ++++ projects/plugins/jetpack/jest.config.js | 3 +- projects/plugins/jetpack/package.json | 2 + .../plugins/jetpack/tests/jest-globals.js | 9 + projects/plugins/jetpack/yarn.lock | 46 +++- 8 files changed, 380 insertions(+), 205 deletions(-) create mode 100644 projects/plugins/jetpack/extensions/blocks/send-a-message/whatsapp-button/settings.js create mode 100644 projects/plugins/jetpack/extensions/blocks/send-a-message/whatsapp-button/test/edit.js create mode 100644 projects/plugins/jetpack/extensions/blocks/send-a-message/whatsapp-button/test/settings.js create mode 100644 projects/plugins/jetpack/tests/jest-globals.js diff --git a/projects/plugins/jetpack/extensions/blocks/send-a-message/whatsapp-button/edit.js b/projects/plugins/jetpack/extensions/blocks/send-a-message/whatsapp-button/edit.js index 2c1fd7511d5fa..65664f111bafd 100644 --- a/projects/plugins/jetpack/extensions/blocks/send-a-message/whatsapp-button/edit.js +++ b/projects/plugins/jetpack/extensions/blocks/send-a-message/whatsapp-button/edit.js @@ -2,53 +2,21 @@ * External dependencies */ import classnames from 'classnames'; -import { __, _x } from '@wordpress/i18n'; -import { useEffect, useState, useCallback } from '@wordpress/element'; +import { useEffect, useCallback } from '@wordpress/element'; import { useDispatch } from '@wordpress/data'; -import { - Button, - BaseControl, - TextControl, - TextareaControl, - SelectControl, - PanelBody, - ToolbarGroup, - Dropdown, - Path, - ToggleControl, -} from '@wordpress/components'; -import { - BlockControls, - InspectorControls, - RichText, - PanelColorSettings, -} from '@wordpress/block-editor'; -import { DOWN } from '@wordpress/keycodes'; +import { ToolbarGroup } from '@wordpress/components'; +import { BlockControls, InspectorControls, RichText } from '@wordpress/block-editor'; /** * Internal dependencies */ import { countryCodes } from '../shared/countrycodes.js'; -import renderMaterialIcon from '../../../shared/render-material-icon'; -import HelpMessage from '../../../shared/help-message'; +import WhatsAppButtonSettings from './settings'; import './view.scss'; -const WHATSAPP_GREEN = '#25D366'; -const WHATSAPP_DARK = '#465B64'; -const WHATSAPP_LIGHT = '#F4F4F4'; - export default function WhatsAppButtonEdit( { attributes, setAttributes, className, clientId } ) { - const { - countryCode, - phoneNumber, - buttonText, - firstMessage, - colorClass, - backgroundColor, - openInNewTab, - } = attributes; + const { countryCode, buttonText, colorClass, backgroundColor } = attributes; - const [ isValidPhoneNumber, setIsValidPhoneNumber ] = useState( true ); const { selectBlock } = useDispatch( 'core/block-editor' ); const getCountryCode = useCallback( async () => { @@ -84,119 +52,6 @@ export default function WhatsAppButtonEdit( { attributes, setAttributes, classNa } }, [ clientId, countryCode, getCountryCode, selectBlock ] ); - const validatePhoneNumber = newPhoneNumber => { - // No alphabetical characters but allow dots, dashes, and brackets. - // These will be stripped for the WhatsApp API (only numbers), but retain - // them in the UI for a more readable number for the user. - const phoneNumberRegEx = RegExp( /^[+]?[\s./0-9]*[(]?[0-9]{1,4}[)]?[-\s./0-9]*$/, 'g' ); - - if ( undefined === newPhoneNumber || newPhoneNumber.length < 1 ) { - return false; - } - - return phoneNumberRegEx.test( countryCode.replace( /\D/g, '' ) + newPhoneNumber ); - }; - - const setBackgroundColor = color => { - setAttributes( { backgroundColor: color } ); - - if ( color === undefined || color === WHATSAPP_GREEN || color === WHATSAPP_DARK ) { - return setAttributes( { colorClass: 'dark' } ); - } - - setAttributes( { colorClass: 'light' } ); - }; - - const renderSettingsToggle = ( isOpen, onToggle ) => { - const openOnArrowDown = event => { - if ( ! isOpen && event.keyCode === DOWN ) { - event.preventDefault(); - event.stopPropagation(); - onToggle(); - } - }; - - return ( -