-
Notifications
You must be signed in to change notification settings - Fork 797
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Pull controls out of ButtonEdit into separate component * [not verified] First pass at adding tests * Add tests for ButtonEdit * [not verified] Add gradient color test * [not verified] Refactor tests and add fixtures * [not verified] Add fixtures * [not verified] Linting * [not verified] Add changelog * [not verified] Update changelog, no public message required * Clean up: move all panels to the controls * Temporarily mock out the width controls * Mock button width panel in Controls tests
- Loading branch information
Showing
11 changed files
with
416 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Significance: patch | ||
Type: compat | ||
Comment: Button Block: add tests (no public changelog required). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
projects/plugins/jetpack/extensions/blocks/button/controls.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import ButtonBorderPanel from './button-border-panel'; | ||
import ButtonColorsPanel from './button-colors-panel'; | ||
import ButtonWidthPanel from './button-width-panel'; | ||
|
||
export default function ButtonControls( { | ||
attributes, | ||
backgroundColor, | ||
fallbackBackgroundColor, | ||
fallbackTextColor, | ||
setAttributes, | ||
setBackgroundColor, | ||
setTextColor, | ||
textColor, | ||
gradientValue, | ||
setGradient, | ||
isGradientAvailable, | ||
} ) { | ||
const { align, borderRadius, width } = attributes; | ||
|
||
return ( | ||
<> | ||
<ButtonColorsPanel | ||
{ ...{ | ||
backgroundColor, | ||
fallbackBackgroundColor, | ||
fallbackTextColor, | ||
gradientValue, | ||
setBackgroundColor, | ||
setGradient, | ||
setTextColor, | ||
textColor, | ||
isGradientAvailable, | ||
} } | ||
/> | ||
<ButtonBorderPanel borderRadius={ borderRadius } setAttributes={ setAttributes } /> | ||
<ButtonWidthPanel | ||
align={ align } | ||
width={ width } | ||
onChange={ newWidth => setAttributes( { width: newWidth } ) } | ||
/> | ||
</> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
160 changes: 160 additions & 0 deletions
160
projects/plugins/jetpack/extensions/blocks/button/test/controls.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { render, screen, within } from '@testing-library/react'; | ||
import userEvent from '@testing-library/user-event'; | ||
import '@testing-library/jest-dom/extend-expect'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import ButtonControls from '../controls'; | ||
|
||
// Temporarily mock out the ButtonWidthControl, which is causing errors due to missing | ||
// dependencies in the jest test runner. | ||
jest.mock( '../button-width-panel', () => ( { | ||
__esModule: true, | ||
default: () => <div>Mocked Width Settings</div>, | ||
} ) ); | ||
|
||
const defaultAttributes = { | ||
align: undefined, | ||
width: undefined, | ||
}; | ||
|
||
const setAttributes = jest.fn(); | ||
const setBackgroundColor = jest.fn(); | ||
const setTextColor = jest.fn(); | ||
const setGradient = jest.fn(); | ||
|
||
const defaultProps = { | ||
attributes: defaultAttributes, | ||
backgroundColor: { | ||
class: undefined, | ||
color: undefined, | ||
}, | ||
fallbackBackgroundColor: 'rgba(0, 0, 0, 0)', | ||
fallbackTextColor: undefined, | ||
setAttributes: setAttributes, | ||
setBackgroundColor: setBackgroundColor, | ||
setTextColor: setTextColor, | ||
textColor: { | ||
class: undefined, | ||
color: undefined, | ||
}, | ||
gradientValue: undefined, | ||
setGradient: setGradient, | ||
isGradientAvailable: false, | ||
}; | ||
|
||
beforeEach( () => { | ||
setAttributes.mockClear(); | ||
setBackgroundColor.mockClear(); | ||
setTextColor.mockClear(); | ||
setGradient.mockClear(); | ||
} ); | ||
|
||
describe( 'Inspector settings', () => { | ||
describe( 'Color settings when gradients are not available', () => { | ||
test( 'loads and displays Color Settings panel', () => { | ||
render( <ButtonControls { ...defaultProps } /> ); | ||
|
||
expect( screen.getByText( 'Background & Text Color' ) ).toBeInTheDocument(); | ||
expect( screen.getByText( 'Text Color' ) ).toBeInTheDocument(); | ||
expect( screen.getByText( 'Background' ) ).toBeInTheDocument(); | ||
} ); | ||
|
||
test( 'loads and displays only default background color options', () => { | ||
render( <ButtonControls { ...defaultProps } /> ); | ||
const backgroundColorPanel = screen.getByText( 'Background' ).closest( 'fieldset' ); | ||
|
||
expect( within( backgroundColorPanel ).queryByText( 'Solid' ) ).not.toBeInTheDocument(); | ||
expect( within( backgroundColorPanel ).queryByText( 'Gradient' ) ).not.toBeInTheDocument(); | ||
} ); | ||
|
||
test( 'sets text color attribute', () => { | ||
render( <ButtonControls { ...defaultProps } /> ); | ||
|
||
const textColors = screen.getByText( 'Text Color' ).closest( 'fieldset' ); | ||
userEvent.click( within( textColors ).getAllByLabelText( 'Color: ', { exact: false } )[ 0 ] ); | ||
|
||
expect( setTextColor.mock.calls[ 0 ] [ 0 ] ).toMatch( /#[a-z0-9]{6,6}/ ); | ||
} ); | ||
|
||
test( 'sets background color attribute', () => { | ||
render( <ButtonControls { ...defaultProps } /> ); | ||
|
||
const backgroundSection = screen.getByText( 'Background' ).closest( 'fieldset' ); | ||
const backgroundColorOption = within( backgroundSection ).getAllByLabelText( 'Color: ', { exact: false } )[ 0 ]; | ||
userEvent.click( backgroundColorOption ); | ||
|
||
expect( setBackgroundColor.mock.calls[ 0 ] [ 0 ] ).toMatch( /#[a-z0-9]{6,6}/ ); | ||
} ); | ||
} ); | ||
|
||
describe( 'Color settings when gradients are available', () => { | ||
test( 'loads and displays Gradient Color Settings panel', () => { | ||
render( <ButtonControls { ...defaultProps } isGradientAvailable={ true } /> ); | ||
|
||
expect( screen.getByText( 'Background & Text Color' ) ).toBeInTheDocument(); | ||
expect( screen.getByText( 'Text Color' ) ).toBeInTheDocument(); | ||
expect( screen.getByText( 'Background' ) ).toBeInTheDocument(); | ||
} ); | ||
|
||
test( 'loads and displays solid and gradient background color options', () => { | ||
render( <ButtonControls { ...defaultProps } isGradientAvailable={ true } /> ); | ||
const backgroundSection = screen.getByText( 'Background' ).closest( 'fieldset' ); | ||
|
||
expect( within( backgroundSection ).getByText( 'Solid' ) ).toBeInTheDocument(); | ||
expect( within( backgroundSection ).getByText( 'Gradient' ) ).toBeInTheDocument(); | ||
} ); | ||
|
||
test( 'sets text color attribute', () => { | ||
render( <ButtonControls { ...defaultProps } isGradientAvailable={ true } /> ); | ||
|
||
const textColors = screen.getByText( 'Text Color' ).closest( 'fieldset' ); | ||
userEvent.click( within( textColors ).getAllByLabelText( 'Color: ', { exact: false } )[ 0 ] ); | ||
|
||
expect( setTextColor.mock.calls[ 0 ] [ 0 ] ).toMatch( /#[a-z0-9]{6,6}/ ); | ||
} ); | ||
|
||
test( 'sets solid background color attribute', () => { | ||
render( <ButtonControls { ...defaultProps } isGradientAvailable={ true } /> ); | ||
|
||
const backgroundSection = screen.getByText( 'Background' ).closest( 'fieldset' ); | ||
|
||
userEvent.click( within( backgroundSection ).getByText( 'Solid' ) ); | ||
userEvent.click( within( backgroundSection ).getAllByLabelText( 'Color: ', { exact: false } )[ 0 ] ); | ||
|
||
expect( setBackgroundColor.mock.calls[ 0 ] [ 0 ] ).toMatch( /#[a-z0-9]{6,6}/ ); | ||
} ); | ||
|
||
test( 'sets gradient background color attribute', () => { | ||
render( <ButtonControls { ...defaultProps } isGradientAvailable={ true } /> ); | ||
|
||
const backgroundSection = screen.getByText( 'Background' ).closest( 'fieldset' ); | ||
userEvent.click( within( backgroundSection ).getByText( 'Gradient' ) ); | ||
userEvent.click( within( backgroundSection ).getAllByLabelText( 'Gradient: ', { exact: false } )[ 0 ] ); | ||
|
||
expect( setGradient.mock.calls[ 0 ][ 0 ] ).toMatch( /linear\-gradient\((.+)\)/ ); | ||
} ); | ||
} ); | ||
|
||
describe( 'Border settings', () => { | ||
test( 'loads and displays border radius', () => { | ||
render( <ButtonControls { ...defaultProps } /> ); | ||
|
||
expect( screen.getByText( 'Border Settings' ) ).toBeInTheDocument(); | ||
} ); | ||
|
||
test( 'sets the border radius attribute', () => { | ||
render( <ButtonControls { ...defaultProps } /> ); | ||
|
||
const borderPanel = screen.getByText( 'Border Settings' ).closest( 'div' ); | ||
const input = within( borderPanel ).getAllByLabelText( 'Border radius' )[ 1 ]; | ||
|
||
userEvent.type( input, '5' ); | ||
expect( setAttributes ).toHaveBeenCalledWith( { borderRadius: 5 } ); | ||
} ); | ||
} ); | ||
} ); |
Oops, something went wrong.