Skip to content

Commit

Permalink
Adding edit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ramonjd committed Mar 9, 2021
1 parent f1dae0d commit ab76525
Show file tree
Hide file tree
Showing 3 changed files with 168 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ const applyFallbackStyles = withFallbackStyles( ( node, ownProps ) => {
};
} );

function SubscriptionEdit( props ) {
export function SubscriptionEdit( props ) {
const {
className,
attributes,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
/**
* @jest-environment jsdom
*/

/**
* External dependencies
*/
Expand All @@ -13,6 +9,13 @@ import '@testing-library/jest-dom/extend-expect';
* Internal dependencies
*/
import SubscriptionsInspectorControls from '../controls';
import {
DEFAULT_BORDER_RADIUS_VALUE,
DEFAULT_BORDER_WEIGHT_VALUE,
DEFAULT_PADDING_VALUE,
DEFAULT_SPACING_VALUE,
DEFAULT_FONTSIZE_VALUE,
} from '../constants';

const setButtonBackgroundColor = jest.fn();
const setGradient = jest.fn();
Expand All @@ -26,20 +29,20 @@ const defaultProps = {
buttonGradient: 10,
setGradient,
},
borderRadius: 10,
borderWeight: 10,
borderRadius: DEFAULT_BORDER_RADIUS_VALUE,
borderWeight: DEFAULT_BORDER_WEIGHT_VALUE,
buttonOnNewLine: false,
emailFieldBackgroundColor: { color: '#000000' },
fallbackButtonBackgroundColor: '#000000',
fallbackTextColor: '#000000',
fontSize: 1,
fontSize: DEFAULT_FONTSIZE_VALUE,
isGradientAvailable: true,
padding: 3,
padding: DEFAULT_PADDING_VALUE,
setAttributes,
setButtonBackgroundColor,
setTextColor,
showSubscribersTotal: true,
spacing: 4,
spacing: DEFAULT_SPACING_VALUE,
subscriberCount: 100,
textColor: '#000000',
};
Expand Down Expand Up @@ -100,71 +103,72 @@ describe( 'Inspector controls', () => {

expect( screen.getByText( 'Text Settings' ) ).toBeInTheDocument();
} );
} );

describe( 'Border settings panel', () => {
test( 'displays correctly', () => {
test( 'set custom text ', () => {
render( <SubscriptionsInspectorControls { ...defaultProps } /> );
userEvent.click( screen.getByText( 'Text Settings' ), { selector: 'button' } );
userEvent.type( screen.getAllByLabelText( 'Custom Size' )[1], '18' );

expect( screen.getByText( 'Border Settings' ) ).toBeInTheDocument();
expect( setAttributes ).toHaveBeenLastCalledWith( {
fontSize: 18,
customFontSize: 18,
} );
} );
} );

describe( 'Spacing settings panel', () => {
describe( 'Border settings panel', () => {
test( 'displays correctly', () => {
render( <SubscriptionsInspectorControls { ...defaultProps } /> );

expect( screen.getByText( 'Spacing Settings' ) ).toBeInTheDocument();
expect( screen.getByText( 'Border Settings' ) ).toBeInTheDocument();
} );

test( 'set space inside', () => {
test( 'set border radius', () => {
render( <SubscriptionsInspectorControls { ...defaultProps } /> );
userEvent.click( screen.getByText( 'Spacing Settings' ), { selector: 'button' } );
// For some reason, userEvent.type is not triggering the onChange event
userEvent.clear( screen.getAllByLabelText( 'Space Inside' )[0] );
userEvent.click( screen.getByText( 'Border Settings' ), { selector: 'button' } );
userEvent.type( screen.getAllByLabelText( 'Border Radius' )[1], '5' );

expect( setAttributes ).toHaveBeenCalledWith( {
padding: undefined,
expect( setAttributes ).toHaveBeenLastCalledWith( {
borderRadius: 5,
} );
} );

test( 'set space between', () => {
test( 'set border weight', () => {
render( <SubscriptionsInspectorControls { ...defaultProps } /> );
userEvent.click( screen.getByText( 'Spacing Settings' ), { selector: 'button' } );
// For some reason, userEvent.type is not triggering the onChange event
userEvent.clear( screen.getAllByLabelText( 'Space Between' )[0] );
userEvent.click( screen.getByText( 'Border Settings' ), { selector: 'button' } );
userEvent.type( screen.getAllByLabelText( 'Border Weight' )[1], '10' );

expect( setAttributes ).toHaveBeenCalledWith( {
spacing: undefined,
expect( setAttributes ).toHaveBeenLastCalledWith( {
borderWeight: 10,
} );
} );
} );

describe( 'Border settings panel', () => {
describe( 'Spacing settings panel', () => {
test( 'displays correctly', () => {
render( <SubscriptionsInspectorControls { ...defaultProps } /> );

expect( screen.getByText( 'Border Settings' ) ).toBeInTheDocument();
expect( screen.getByText( 'Spacing Settings' ) ).toBeInTheDocument();
} );

test( 'set border radius', () => {
test( 'set space inside', () => {
render( <SubscriptionsInspectorControls { ...defaultProps } /> );
userEvent.click( screen.getByText( 'Border Settings' ), { selector: 'button' } );
// For some reason, userEvent.type is not triggering the onChange event
userEvent.clear( screen.getAllByLabelText( 'Border Radius' )[0] );
userEvent.click( screen.getByText( 'Spacing Settings' ), { selector: 'button' } );
// For some reason userEvent.type won't work on these inputs
userEvent.clear( screen.getAllByLabelText( 'Space Inside' )[1] );

expect( setAttributes ).toHaveBeenCalledWith( {
expect( setAttributes ).toHaveBeenLastCalledWith( {
padding: undefined,
} );
} );

test( 'set border weight', () => {
test( 'set space between', () => {
render( <SubscriptionsInspectorControls { ...defaultProps } /> );
userEvent.click( screen.getByText( 'Border Settings' ), { selector: 'button' } );
// For some reason, userEvent.type is not triggering the onChange event
userEvent.clear( screen.getAllByLabelText( 'Border Weight' )[0] );
userEvent.click( screen.getByText( 'Spacing Settings' ), { selector: 'button' } );
// For some reason userEvent.type won't work on these inputs
userEvent.clear( screen.getAllByLabelText( 'Space Between' )[1] );

expect( setAttributes ).toHaveBeenCalledWith( {
expect( setAttributes ).toHaveBeenLastCalledWith( {
spacing: undefined,
} );
} );
Expand Down
124 changes: 124 additions & 0 deletions projects/plugins/jetpack/extensions/blocks/subscriptions/test/edit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/**
* External dependencies
*/
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import '@testing-library/jest-dom/extend-expect';

/**
* Internal dependencies
*/
import { SubscriptionEdit } from '../edit';

const setAttributes = jest.fn();

const defaultAttributes = {
borderRadius: 0,
borderWeight: 0,
padding: 0,
spacing: 0,
submitButtonText: 'Submit',
subscribePlaceholder: 'Do it',
showSubscribersTotal: false,
buttonOnNewLine: false,
};

const defaultProps = {
attributes: defaultAttributes,
className: 'noodles',
setAttributes,
emailFieldBackgroundColor: '',
buttonBackgroundColor: '',
setButtonBackgroundColor: '',
fallbackButtonBackgroundColo: '',
textColor: '',
fallbackTextColor: '',
setTextColor: '',
borderColor: '',
fontSize: 12,
};


jest.mock( '../constants', () => ( {
IS_GRADIENT_AVAILABLE: true
} ) );

jest.mock( '@wordpress/block-editor', () => ( {
...jest.requireActual( '@wordpress/block-editor' ),
__experimentalUseGradient: jest.fn().mockReturnValue( {
gradientClass: undefined,
gradientValue: undefined,
setGradient: jest.fn()
} ),
} ) );

describe( 'SubscriptionEdit', () => {
test( 'adds correct classes to container', () => {
const { container, rerender } = render( <SubscriptionEdit { ...defaultProps } /> );

expect( container.querySelector( `.${ defaultProps.className }` ) ).toBeInTheDocument();
} );

test( 'adds correct classes when button on new line', () => {
const { container, rerender } = render( <SubscriptionEdit { ...defaultProps } /> );

expect( container.querySelector( '.wp-block-jetpack-subscriptions__use-newline' ) ).not.toBeInTheDocument();

const updatedProps = {
...defaultProps,
attributes: {
...defaultAttributes,
buttonOnNewLine: true,
},
};
rerender( <SubscriptionEdit { ...updatedProps } /> );

expect( container.querySelector( '.wp-block-jetpack-subscriptions__use-newline' ) ).toBeInTheDocument();
} );

test( 'renders text field with placeholder text', () => {
render( <SubscriptionEdit { ...defaultProps } /> );

expect( screen.getByPlaceholderText( defaultAttributes.subscribePlaceholder ) ).toBeInTheDocument();
} );

test( 'renders button with default text', () => {
render( <SubscriptionEdit { ...defaultProps } /> );

expect( screen.getByText( defaultAttributes.submitButtonText ) ).toBeInTheDocument();
} );

test( 'calls setAttributes handler on button when value changes', () => {
render( <SubscriptionEdit { ...defaultProps } /> );

expect( screen.getByText( defaultAttributes.submitButtonText ) ).toBeInTheDocument();
} );

test( 'displays subscriber total', () => {
render( <SubscriptionEdit { ...defaultProps } /> );
userEvent.type( screen.getByText( defaultAttributes.submitButtonText ), ' right now!' );

expect( setAttributes ).toHaveBeenCalledWith( {
submitButtonText: `${ defaultAttributes.submitButtonText} right now!`,
} );
} );

test( 'displays subscriber total', () => {
const { container, rerender } = render( <SubscriptionEdit { ...defaultProps } /> );
expect( container.querySelector( 'p' ) ).not.toBeInTheDocument();
expect( container.querySelector( '.wp-block-jetpack-subscriptions__show-subs' ) ).not.toBeInTheDocument();

const updatedProps = {
...defaultProps,
attributes: {
...defaultAttributes,
showSubscribersTotal: true,
},
};
rerender( <SubscriptionEdit { ...updatedProps } /> );

expect( container.querySelector( '.wp-block-jetpack-subscriptions__show-subs' ) ).toBeInTheDocument();
expect( container.querySelector( 'p' ) ).toBeInTheDocument();

} );
} );

0 comments on commit ab76525

Please sign in to comment.