From 90189588e21499e63a6a998341f686e45073548b Mon Sep 17 00:00:00 2001 From: lukewalczak Date: Mon, 29 Aug 2022 23:26:09 +0200 Subject: [PATCH] fix: correct custom border radius in FAB --- src/components/FAB/FAB.tsx | 22 +- src/components/__tests__/FAB.test.js | 56 +- .../__tests__/__snapshots__/FAB.test.js.snap | 547 ++++++++++++------ 3 files changed, 431 insertions(+), 194 deletions(-) diff --git a/src/components/FAB/FAB.tsx b/src/components/FAB/FAB.tsx index c71c655d2c..166ed74512 100644 --- a/src/components/FAB/FAB.tsx +++ b/src/components/FAB/FAB.tsx @@ -164,7 +164,7 @@ const FAB = ({ visible = true, uppercase = !theme.isV3, loading, - testID, + testID = 'fab', size = 'medium', customSize, mode = 'elevated', @@ -214,13 +214,10 @@ const FAB = ({ color: foregroundColor, ...(isV3 ? theme.typescale.labelLarge : theme.fonts.medium), }; - const shapeStyle = { borderRadius: fabStyle.borderRadius }; - const containerStyles = [ - !isV3 && styles.elevated, - !isV3 && disabled && styles.disabled, - shapeStyle, - ]; + const { borderRadius = fabStyle.borderRadius } = (StyleSheet.flatten(style) || + {}) as ViewStyle; + const md3Elevation = isFlatMode || disabled ? 0 : 3; return ( @@ -229,6 +226,7 @@ const FAB = ({ style={ [ { + borderRadius, backgroundColor, opacity: visibility, transform: [ @@ -237,11 +235,14 @@ const FAB = ({ }, ], }, - containerStyles, + styles.container, + !isV3 && styles.elevated, + !isV3 && disabled && styles.disabled, style, ] as StyleProp } pointerEvents={visible ? 'auto' : 'none'} + testID={`${testID}-container`} {...(isV3 && { elevation: md3Elevation })} > {icon && loading !== true ? ( @@ -296,6 +297,9 @@ const styles = StyleSheet.create({ elevated: { elevation: 6, }, + container: { + overflow: 'hidden', + }, content: { flexDirection: 'row', alignItems: 'center', diff --git a/src/components/__tests__/FAB.test.js b/src/components/__tests__/FAB.test.js index 614091115e..29f48d6af2 100644 --- a/src/components/__tests__/FAB.test.js +++ b/src/components/__tests__/FAB.test.js @@ -1,13 +1,36 @@ import * as React from 'react'; +import { StyleSheet } from 'react-native'; import renderer from 'react-test-renderer'; import color from 'color'; +import { render } from '@testing-library/react-native'; import FAB from '../FAB'; import { getFABColors } from '../FAB/utils'; import getContrastingColor from '../../utils/getContrastingColor'; import { black, white } from '../../styles/themes/v2/colors'; import { getTheme } from '../../core/theming'; -it('renders normal FAB', () => { +const styles = StyleSheet.create({ + borderRadius: { + borderRadius: 0, + }, + small: { + height: 40, + width: 40, + borderRadius: 12, + }, + medium: { + height: 56, + width: 56, + borderRadius: 16, + }, + large: { + height: 96, + width: 96, + borderRadius: 28, + }, +}); + +it('renders default FAB', () => { const tree = renderer.create( {}} icon="plus" />).toJSON(); expect(tree).toMatchSnapshot(); @@ -21,6 +44,14 @@ it('renders small FAB', () => { expect(tree).toMatchSnapshot(); }); +it('renders large FAB', () => { + const tree = renderer + .create( {}} icon="plus" />) + .toJSON(); + + expect(tree).toMatchSnapshot(); +}); + it('renders FAB with custom size prop', () => { const tree = renderer .create( {}} icon="plus" />) @@ -100,6 +131,29 @@ it('renders visible FAB', () => { expect(toJSON()).toMatchSnapshot(); }); +it('renders FAB with custom border radius', () => { + const { getByTestId } = render( + {}} + icon="plus" + testID="fab" + style={styles.borderRadius} + /> + ); + + expect(getByTestId('fab-container')).toHaveStyle({ borderRadius: 0 }); +}); + +['small', 'medium', 'large'].forEach((size) => { + it(`renders ${size} FAB with correct size and border radius`, () => { + const { getByTestId } = render( + {}} size={size} icon="plus" testID={`${size}-fab`} /> + ); + + expect(getByTestId(`${size}-fab-content`)).toHaveStyle(styles[size]); + }); +}); + describe('getFABColors - background color', () => { it('should return color from styles', () => { expect( diff --git a/src/components/__tests__/__snapshots__/FAB.test.js.snap b/src/components/__tests__/__snapshots__/FAB.test.js.snap index 8e4d2d951e..c344467388 100644 --- a/src/components/__tests__/__snapshots__/FAB.test.js.snap +++ b/src/components/__tests__/__snapshots__/FAB.test.js.snap @@ -43,6 +43,7 @@ exports[`renders FAB with custom size prop 1`] = ` "backgroundColor": "rgba(234, 221, 255, 1)", "borderRadius": 25, "opacity": 1, + "overflow": "hidden", "transform": Array [ Object { "scale": 1, @@ -50,6 +51,7 @@ exports[`renders FAB with custom size prop 1`] = ` ], } } + testID="fab-container" > + + + + + □ + + + + + + + + +`; + +exports[`renders default FAB 1`] = ` + + + + `; -exports[`renders loading FAB 1`] = ` +exports[`renders large FAB 1`] = ` + + + + + □ + + + + + + + + +`; + +exports[`renders loading FAB 1`] = ` + + + + `; -exports[`renders normal FAB 1`] = ` - - - - - - - - - □ - - - - - - - - -`; - exports[`renders not visible FAB 1`] = `