From 987b2bf5eb6151e419b207753e4a5da2c8082d15 Mon Sep 17 00:00:00 2001 From: Jason Conroy Date: Wed, 10 Jun 2020 11:00:25 +0930 Subject: [PATCH] Refactor and add tests for the marketing WelcomeCard component (https://github.com/woocommerce/woocommerce-admin/pull/4495) * Convert welcome card to functional component * Add tests for the WelcomeCard --- .../marketing/overview/welcome-card/index.js | 71 +++++++++-------- .../overview/welcome-card/test/index.js | 78 +++++++++++++++++++ 2 files changed, 118 insertions(+), 31 deletions(-) create mode 100644 plugins/woocommerce-admin/client/marketing/overview/welcome-card/test/index.js diff --git a/plugins/woocommerce-admin/client/marketing/overview/welcome-card/index.js b/plugins/woocommerce-admin/client/marketing/overview/welcome-card/index.js index ca7d9c19ad6..a4ce3f3d906 100644 --- a/plugins/woocommerce-admin/client/marketing/overview/welcome-card/index.js +++ b/plugins/woocommerce-admin/client/marketing/overview/welcome-card/index.js @@ -1,13 +1,13 @@ /** * External dependencies */ -import { Component } from '@wordpress/element'; import { __ } from '@wordpress/i18n'; import { get } from 'lodash'; import { Button } from '@wordpress/components'; import Gridicon from 'gridicons'; import { compose } from '@wordpress/compose'; import { withDispatch } from '@wordpress/data'; +import PropTypes from 'prop-types'; /** * WooCommerce dependencies @@ -22,44 +22,54 @@ import { recordEvent } from 'lib/tracks'; import withSelect from 'wc-api/with-select'; import WelcomeImage from './images/welcome.svg'; -class WelcomeCard extends Component { - constructor( props ) { - super( props ); +const WelcomeCard = ( { + isHidden, + updateOptions, +} ) => { - this.hide = this.hide.bind( this ); - } - - hide() { - this.props.updateOptions( { + const hide = () => { + updateOptions( { woocommerce_marketing_overview_welcome_hidden: 'yes', } ); - recordEvent( 'marketing_intro_close', {} ); } - render() { - if ( this.props.isHidden ) { - return null; - } - - return ( - - - -

{ __( 'Grow your customer base and increase your sales with marketing tools built for WooCommerce', 'woocommerce-admin' ) }

-
- ) + if ( isHidden ) { + return null; } + + return ( + + + +

{ __( 'Grow your customer base and increase your sales with marketing tools built for WooCommerce', 'woocommerce-admin' ) }

+
+ ) } +WelcomeCard.propTypes = { + /** + * Whether the card is hidden. + */ + isHidden: PropTypes.bool.isRequired, + /** + * updateOptions function. + */ + updateOptions: PropTypes.func.isRequired, +}; + +// named export +export { WelcomeCard } + +// default export export default compose( withSelect( ( select ) => { const { getOptions, isUpdateOptionsRequesting } = select( 'wc-api' ); @@ -67,7 +77,6 @@ export default compose( const options = getOptions( [ hideOptionName ] ); const isHidden = get( options, [ hideOptionName ], 'no' ) === 'yes'; const isUpdateRequesting = Boolean( isUpdateOptionsRequesting( [ hideOptionName ] ) ); - return { isHidden: isHidden || isUpdateRequesting, }; diff --git a/plugins/woocommerce-admin/client/marketing/overview/welcome-card/test/index.js b/plugins/woocommerce-admin/client/marketing/overview/welcome-card/test/index.js new file mode 100644 index 00000000000..3a71d8b3ad2 --- /dev/null +++ b/plugins/woocommerce-admin/client/marketing/overview/welcome-card/test/index.js @@ -0,0 +1,78 @@ +/** + * External dependencies + */ +import { shallow } from 'enzyme'; +import { recordEvent } from 'lib/tracks'; +import { Button } from '@wordpress/components'; + +/** + * Internal dependencies + */ +import { WelcomeCard } from '../index.js'; + +jest.mock( 'lib/tracks' ); +jest.mock( '@woocommerce/wc-admin-settings' ); + +describe( 'WelcomeCard hide button', () => { + let welcomeCardWrapper; + let mockUpdateOptions; + + beforeEach( () => { + mockUpdateOptions = jest.fn(); + welcomeCardWrapper = shallow( + + ); + } ); + + it( 'should record an event when clicked', () => { + const welcomeHideButton = welcomeCardWrapper.find( Button ); + expect( welcomeHideButton.length ).toBe( 1 ); + welcomeHideButton.simulate( 'click' ); + expect( recordEvent ).toHaveBeenCalledTimes( 1 ); + expect( recordEvent ).toHaveBeenCalledWith( + 'marketing_intro_close', + {} + ); + } ); + + it( 'should update option when clicked', () => { + const welcomeHideButton = welcomeCardWrapper.find( Button ); + expect( welcomeHideButton.length ).toBe( 1 ); + welcomeHideButton.simulate( 'click' ); + expect( mockUpdateOptions ).toHaveBeenCalledTimes( 1 ); + expect( mockUpdateOptions ).toHaveBeenCalledWith( { + woocommerce_marketing_overview_welcome_hidden: 'yes', + } ); + } ); +} ); + +describe( 'Component visibility can be toggled', () => { + const mockUpdateOptions = jest.fn(); + + it( 'WelcomeCard should be visible if isHidden is false', () => { + const welcomeCardWrapper = shallow( + + ); + const welcomeCardButton = welcomeCardWrapper.find( Button ); + expect( welcomeCardButton.length ).toBe( 1 ); + expect( mockUpdateOptions ).toHaveBeenCalledTimes( 0 ); + } ); + + it( 'WelcomeCard should be hidden if isHidden is true', () => { + const welcomeCardWrapper = shallow( + + ); + const welcomeCardButton = welcomeCardWrapper.find( Button ); + expect( welcomeCardButton.length ).toBe( 0 ); + expect( mockUpdateOptions ).toHaveBeenCalledTimes( 0 ); + } ); +} );