Skip to content

Commit

Permalink
Naming the anonymous control function to SubscriptionControls
Browse files Browse the repository at this point in the history
Adding mocks for apiFetch in the edit tests
Using zero values for default input[type=number] fields to receive more reliable userEvent.type results
  • Loading branch information
ramonjd committed Mar 9, 2021
1 parent ab76525 commit 2e0bcab
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import {
DEFAULT_FONTSIZE_VALUE,
} from './constants';

export default ( {
export default function SubscriptionControls ( {
buttonBackgroundColor,
borderColor,
buttonGradient,
Expand All @@ -53,7 +53,7 @@ export default ( {
spacing,
subscriberCount,
textColor,
} ) => {
} ) {
return (
<>
{ isGradientAvailable && (
Expand Down Expand Up @@ -244,4 +244,5 @@ export default ( {
</PanelBody>
</>
);
};
}

Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import {
DEFAULT_SPACING_VALUE,
DEFAULT_FONTSIZE_VALUE,
} from './constants';
import Controls from './controls';
import SubscriptionControls from './controls';

const { getComputedStyle } = window;
const isGradientAvailable = !! useGradient;
Expand Down Expand Up @@ -204,7 +204,7 @@ export function SubscriptionEdit( props ) {
return (
<>
<InspectorControls>
<Controls
<SubscriptionControls
buttonBackgroundColor={ buttonBackgroundColor }
borderColor={ borderColor }
buttonGradient={ buttonGradient }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ import '@testing-library/jest-dom/extend-expect';
*/
import SubscriptionsInspectorControls from '../controls';
import {
DEFAULT_BORDER_RADIUS_VALUE,
DEFAULT_BORDER_WEIGHT_VALUE,
DEFAULT_PADDING_VALUE,
DEFAULT_SPACING_VALUE,
DEFAULT_FONTSIZE_VALUE,
} from '../constants';

Expand All @@ -29,20 +25,20 @@ const defaultProps = {
buttonGradient: 10,
setGradient,
},
borderRadius: DEFAULT_BORDER_RADIUS_VALUE,
borderWeight: DEFAULT_BORDER_WEIGHT_VALUE,
borderRadius: 0,
borderWeight: 0,
buttonOnNewLine: false,
emailFieldBackgroundColor: { color: '#000000' },
fallbackButtonBackgroundColor: '#000000',
fallbackTextColor: '#000000',
fontSize: DEFAULT_FONTSIZE_VALUE,
isGradientAvailable: true,
padding: DEFAULT_PADDING_VALUE,
padding: 0,
setAttributes,
setButtonBackgroundColor,
setTextColor,
showSubscribersTotal: true,
spacing: DEFAULT_SPACING_VALUE,
spacing: 0,
subscriberCount: 100,
textColor: '#000000',
};
Expand Down Expand Up @@ -136,10 +132,10 @@ describe( 'Inspector controls', () => {
test( 'set border weight', () => {
render( <SubscriptionsInspectorControls { ...defaultProps } /> );
userEvent.click( screen.getByText( 'Border Settings' ), { selector: 'button' } );
userEvent.type( screen.getAllByLabelText( 'Border Weight' )[1], '10' );
userEvent.type( screen.getAllByLabelText( 'Border Weight' )[1], '5' );

expect( setAttributes ).toHaveBeenLastCalledWith( {
borderWeight: 10,
borderWeight: 5,
} );
} );
} );
Expand All @@ -154,22 +150,20 @@ describe( 'Inspector controls', () => {
test( 'set space inside', () => {
render( <SubscriptionsInspectorControls { ...defaultProps } /> );
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] );
userEvent.type( screen.getAllByLabelText( 'Space Inside' )[1], '5' );

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

test( 'set space between', () => {
render( <SubscriptionsInspectorControls { ...defaultProps } /> );
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] );
userEvent.type( screen.getAllByLabelText( 'Space Between' )[1], '5' );

expect( setAttributes ).toHaveBeenLastCalledWith( {
spacing: undefined,
spacing: 5,
} );
} );
} );
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { render, screen } from '@testing-library/react';
import { render, screen, act } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import '@testing-library/jest-dom/extend-expect';

Expand Down Expand Up @@ -38,6 +38,18 @@ const defaultProps = {
fontSize: 12,
};

const originalFetch = window.fetch;

/**
* Mock return value for a successful fetch JSON return value.
*
* @return {Promise} Mock return value.
*/
const RESOLVED_FETCH_PROMISE = Promise.resolve( { count: 100 } );
const DEFAULT_FETCH_MOCK_RETURN = Promise.resolve( {
status: 200,
json: () => RESOLVED_FETCH_PROMISE,
} );

jest.mock( '../constants', () => ( {
IS_GRADIENT_AVAILABLE: true
Expand All @@ -53,6 +65,19 @@ jest.mock( '@wordpress/block-editor', () => ( {
} ) );

describe( 'SubscriptionEdit', () => {
beforeEach( () => {
window.fetch = jest.fn();
window.fetch.mockReturnValue( DEFAULT_FETCH_MOCK_RETURN );
} );

afterEach( async () => {
await act( () => RESOLVED_FETCH_PROMISE );
} );

afterAll( () => {
window.fetch = originalFetch;
} );

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

Expand Down Expand Up @@ -119,6 +144,5 @@ describe( 'SubscriptionEdit', () => {

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

} );
} );

0 comments on commit 2e0bcab

Please sign in to comment.