From 2e0bcab5f3792f1b1885b28345e40fe2686cb4fd Mon Sep 17 00:00:00 2001 From: ramonjd Date: Fri, 5 Mar 2021 19:48:43 +1100 Subject: [PATCH] Naming the anonymous control function to SubscriptionControls Adding mocks for apiFetch in the edit tests Using zero values for default input[type=number] fields to receive more reliable userEvent.type results --- .../blocks/subscriptions/controls.js | 7 +++-- .../extensions/blocks/subscriptions/edit.js | 4 +-- .../blocks/subscriptions/test/controls.js | 26 +++++++---------- .../blocks/subscriptions/test/edit.js | 28 +++++++++++++++++-- 4 files changed, 42 insertions(+), 23 deletions(-) diff --git a/projects/plugins/jetpack/extensions/blocks/subscriptions/controls.js b/projects/plugins/jetpack/extensions/blocks/subscriptions/controls.js index 10cd4e10d3581..787f35d3d5e08 100644 --- a/projects/plugins/jetpack/extensions/blocks/subscriptions/controls.js +++ b/projects/plugins/jetpack/extensions/blocks/subscriptions/controls.js @@ -33,7 +33,7 @@ import { DEFAULT_FONTSIZE_VALUE, } from './constants'; -export default ( { +export default function SubscriptionControls ( { buttonBackgroundColor, borderColor, buttonGradient, @@ -53,7 +53,7 @@ export default ( { spacing, subscriberCount, textColor, -} ) => { +} ) { return ( <> { isGradientAvailable && ( @@ -244,4 +244,5 @@ export default ( { ); -}; +} + diff --git a/projects/plugins/jetpack/extensions/blocks/subscriptions/edit.js b/projects/plugins/jetpack/extensions/blocks/subscriptions/edit.js index 7df97764f5f8f..5c521223415a0 100644 --- a/projects/plugins/jetpack/extensions/blocks/subscriptions/edit.js +++ b/projects/plugins/jetpack/extensions/blocks/subscriptions/edit.js @@ -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; @@ -204,7 +204,7 @@ export function SubscriptionEdit( props ) { return ( <> - { test( 'set border weight', () => { render( ); 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, } ); } ); } ); @@ -154,22 +150,20 @@ describe( 'Inspector controls', () => { test( 'set space inside', () => { render( ); 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( ); 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, } ); } ); } ); diff --git a/projects/plugins/jetpack/extensions/blocks/subscriptions/test/edit.js b/projects/plugins/jetpack/extensions/blocks/subscriptions/test/edit.js index eb208df93f2ca..8c15ed9da0869 100644 --- a/projects/plugins/jetpack/extensions/blocks/subscriptions/test/edit.js +++ b/projects/plugins/jetpack/extensions/blocks/subscriptions/test/edit.js @@ -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'; @@ -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 @@ -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( ); @@ -119,6 +144,5 @@ describe( 'SubscriptionEdit', () => { expect( container.querySelector( '.wp-block-jetpack-subscriptions__show-subs' ) ).toBeInTheDocument(); expect( container.querySelector( 'p' ) ).toBeInTheDocument(); - } ); } );