From 7c32ac9c153b886e5491562365a9c9a0199a0253 Mon Sep 17 00:00:00 2001 From: Jorge Costa Date: Wed, 29 Jan 2020 20:32:35 +0000 Subject: [PATCH] =?UTF-8?q?Fix:=20Color=20Gradients=20component=20was=20no?= =?UTF-8?q?t=20able=20to=20handle=20only=20gra=E2=80=A6=20(#19925)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../components/colors-gradients/control.js | 4 +- .../colors-gradients/test/control.js | 146 ++++++++++++++++++ 2 files changed, 148 insertions(+), 2 deletions(-) create mode 100644 packages/block-editor/src/components/colors-gradients/test/control.js diff --git a/packages/block-editor/src/components/colors-gradients/control.js b/packages/block-editor/src/components/colors-gradients/control.js index be3ec94f50bc93..14fce71c662ea6 100644 --- a/packages/block-editor/src/components/colors-gradients/control.js +++ b/packages/block-editor/src/components/colors-gradients/control.js @@ -109,7 +109,7 @@ function ColorGradientControlInner( { ) } - { currentTab === 'color' && ( + { ( currentTab === 'color' || ! canChooseAGradient ) && ( ) } - { currentTab === 'gradient' && ( + { ( currentTab === 'gradient' || ! canChooseAColor ) && ( ( element ) => { + return ( + element.type === 'button' && + element.children[ 0 ] === text && + element.children.length === 1 + ); +}; + +const getButtonWithAriaLabelStartPredicate = ( ariaLabelStart ) => ( element ) => { + return ( + element.type === 'button' && + element.props[ 'aria-label' ] && + element.props[ 'aria-label' ].startsWith( ariaLabelStart ) + ); +}; + +const colorTabButtonPredicate = getButtonWithTestPredicate( 'Solid Color' ); +const gradientTabButtonPredicate = getButtonWithTestPredicate( 'Gradient' ); + +describe( 'ColorPaletteControl', () => { + it( 'renders tabs if it is possible to select a color and a gradient rendering a color picker at the start', async () => { + let wrapper; + + await act( async () => { + wrapper = create( + + ); + } ); + + // Is showing the two tab buttons. + expect( wrapper.root.findAll( colorTabButtonPredicate ) ).toHaveLength( 1 ); + expect( wrapper.root.findAll( gradientTabButtonPredicate ) ).toHaveLength( 1 ); + + // Is showing the two predefined Colors. + expect( wrapper.root.findAll( + ( element ) => ( element.type === 'button' && element.props && element.props[ 'aria-label' ] && element.props[ 'aria-label' ].startsWith( 'Color:' ) ) + ) ).toHaveLength( 2 ); + + // Is showing the custom color picker. + expect( wrapper.root.findAll( + getButtonWithTestPredicate( 'Custom Color' ) + ) ).toHaveLength( 1 ); + } ); + + it( 'renders the color picker and does not render tabs if it is only possible to select a color', async () => { + let wrapper; + + await act( async () => { + wrapper = create( + + ); + } ); + + // Is not showing the two tab buttons. + expect( wrapper.root.findAll( colorTabButtonPredicate ) ).toHaveLength( 0 ); + expect( wrapper.root.findAll( gradientTabButtonPredicate ) ).toHaveLength( 0 ); + + // Is showing the two predefined Colors. + expect( wrapper.root.findAll( + getButtonWithAriaLabelStartPredicate( 'Color:' ) + ) ).toHaveLength( 2 ); + + // Is showing the custom color picker. + expect( wrapper.root.findAll( + getButtonWithTestPredicate( 'Custom Color' ) + ) ).toHaveLength( 1 ); + } ); + + it( 'renders the gradient picker and does not render tabs if it is only possible to select a gradient', async () => { + let wrapper; + + await act( async () => { + wrapper = create( + + ); + } ); + + // Is not showing the two tab buttons. + expect( wrapper.root.findAll( colorTabButtonPredicate ) ).toHaveLength( 0 ); + expect( wrapper.root.findAll( gradientTabButtonPredicate ) ).toHaveLength( 0 ); + + // Is showing the two predefined Gradients. + expect( wrapper.root.findAll( + getButtonWithAriaLabelStartPredicate( 'Gradient:' ) + ) ).toHaveLength( 2 ); + + // Is showing the custom gradient picker. + expect( wrapper.root.findAll( + ( element ) => ( element.props && element.props.className && element.props.className.includes( 'components-custom-gradient-picker' ) ) + ).length ).toBeGreaterThanOrEqual( 1 ); + } ); +} );