Skip to content

Commit b382ab2

Browse files
authored
FloatingButton - add style tests (#2976)
1 parent 8bbe17c commit b382ab2

File tree

2 files changed

+97
-5
lines changed

2 files changed

+97
-5
lines changed

src/components/floatingButton/__tests__/index.spec.tsx

Lines changed: 95 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import React from 'react';
2+
import {ViewStyle} from 'react-native';
23
import {render} from '@testing-library/react-native';
3-
import FloatingButton/* , {FloatingButtonLayouts} */ from '../index';
4+
import {Spacings} from '../../../style';
5+
import FloatingButton, {FloatingButtonLayouts} from '../index';
46
import {ButtonDriver} from '../../button/Button.driver.new';
7+
import {useComponentDriver, ComponentProps} from '../../../testkit/new/Component.driver';
58

69
const TEST_ID = 'floating_button';
710
const button = {
@@ -10,12 +13,15 @@ const button = {
1013
const secondaryButton = {
1114
label: 'Second'
1215
};
13-
// buttonLayout={showVertical ? FloatingButtonLayouts.VERTICAL : FloatingButtonLayouts.HORIZONTAL}
14-
1516

1617
const TestCase = (props) => {
1718
return <FloatingButton {...props} testID={TEST_ID}/>;
1819
};
20+
export const FloatingButtonDriver = (props: ComponentProps) => {
21+
const driver = useComponentDriver(props);
22+
const getStyle = () => driver.getProps().style as ViewStyle;
23+
return {...driver, getStyle};
24+
};
1925

2026
describe('FloatingButton', () => {
2127
describe('visible', () => {
@@ -59,4 +65,90 @@ describe('FloatingButton', () => {
5965
expect(await buttonDriver.getLabel().getText()).toEqual(secondaryButton.label);
6066
});
6167
});
68+
69+
describe('bottomMargin', () => {
70+
it('should have default bottom margin', () => {
71+
const props = {visible: true};
72+
const renderTree = render(<TestCase {...props}/>);
73+
const buttonDriver = ButtonDriver({renderTree, testID: `${TEST_ID}.button`});
74+
75+
expect(buttonDriver.getProps()?.style?.marginBottom).toBe(Spacings.s8);
76+
});
77+
78+
it('should have default bottom margin for both buttons', () => {
79+
const props = {visible: true, secondaryButton};
80+
const renderTree = render(<TestCase {...props}/>);
81+
const buttonDriver = ButtonDriver({renderTree, testID: `${TEST_ID}.button`});
82+
const buttonDriver2 = ButtonDriver({renderTree, testID: `${TEST_ID}.secondaryButton`});
83+
84+
expect(buttonDriver.getProps()?.style?.marginBottom).toBe(Spacings.s4);
85+
expect(buttonDriver2.getProps()?.style?.marginBottom).toBe(Spacings.s7);
86+
});
87+
88+
it('should have bottom margin that match bottomMargin prop', () => {
89+
const props = {visible: true, bottomMargin: 10};
90+
const renderTree = render(<TestCase {...props}/>);
91+
const buttonDriver = ButtonDriver({renderTree, testID: `${TEST_ID}.button`});
92+
93+
expect(buttonDriver.getProps()?.style?.marginBottom).toBe(10);
94+
});
95+
96+
it('should have bottom margin for secondary button that match bottomMarginProp', () => {
97+
const props = {visible: true, secondaryButton, bottomMargin: 10};
98+
const renderTree = render(<TestCase {...props}/>);
99+
const buttonDriver = ButtonDriver({renderTree, testID: `${TEST_ID}.button`});
100+
const buttonDriver2 = ButtonDriver({renderTree, testID: `${TEST_ID}.secondaryButton`});
101+
102+
expect(buttonDriver.getProps()?.style?.marginBottom).toBe(Spacings.s4);
103+
expect(buttonDriver2.getProps()?.style?.marginBottom).toBe(10);
104+
});
105+
});
106+
107+
describe('buttonLayout', () => {
108+
it('should style vertical layout (default)', () => {
109+
const props = {visible: true, secondaryButton};
110+
const renderTree = render(<TestCase {...props}/>);
111+
const driver = FloatingButtonDriver({renderTree, testID: TEST_ID});
112+
113+
expect(driver.getStyle()?.flexDirection).toBe(undefined);
114+
expect(driver.getStyle()?.alignItems).toBe('center');
115+
expect(driver.getStyle()?.justifyContent).toBe('center');
116+
expect(driver.getStyle()?.paddingHorizontal).toBe(undefined);
117+
});
118+
119+
it('should style horizontal layout', () => {
120+
const props = {visible: true, secondaryButton, buttonLayout: FloatingButtonLayouts.HORIZONTAL};
121+
const renderTree = render(<TestCase {...props}/>);
122+
const driver = FloatingButtonDriver({renderTree, testID: TEST_ID});
123+
124+
expect(driver.getStyle()?.flexDirection).toBe('row');
125+
expect(driver.getStyle()?.alignItems).toBe('center');
126+
expect(driver.getStyle()?.justifyContent).toBe('center');
127+
expect(driver.getStyle()?.paddingHorizontal).toBe(undefined);
128+
});
129+
});
130+
131+
describe('fullWidth', () => {
132+
it('should style vertical layout (default) when fullWidth is true', () => {
133+
const props = {visible: true, secondaryButton, fullWidth: true};
134+
const renderTree = render(<TestCase {...props}/>);
135+
const driver = FloatingButtonDriver({renderTree, testID: TEST_ID});
136+
137+
expect(driver.getStyle()?.flexDirection).toBe(undefined);
138+
expect(driver.getStyle()?.alignItems).toBe(undefined);
139+
expect(driver.getStyle()?.justifyContent).toBe(undefined);
140+
expect(driver.getStyle()?.paddingHorizontal).toBe(16);
141+
});
142+
143+
it('should style horizontal layout when fullWidth is true', () => {
144+
const props = {visible: true, secondaryButton, buttonLayout: FloatingButtonLayouts.HORIZONTAL, fullWidth: true};
145+
const renderTree = render(<TestCase {...props}/>);
146+
const driver = FloatingButtonDriver({renderTree, testID: TEST_ID});
147+
148+
expect(driver.getStyle()?.flexDirection).toBe('row');
149+
expect(driver.getStyle()?.alignItems).toBe('center');
150+
expect(driver.getStyle()?.justifyContent).toBe('center');
151+
expect(driver.getStyle()?.paddingHorizontal).toBe(undefined);
152+
});
153+
});
62154
});

src/components/floatingButton/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,8 @@ class FloatingButton extends PureComponent<FloatingButtonProps> {
193193

194194
return (
195195
<View
196-
row={!!this.isSecondaryHorizontal}
197-
center={!!this.isSecondaryHorizontal || !fullWidth}
196+
row={this.isSecondaryHorizontal}
197+
center={this.isSecondaryHorizontal || !fullWidth}
198198
paddingH-16={!this.isSecondaryHorizontal && fullWidth}
199199
pointerEvents="box-none"
200200
animated

0 commit comments

Comments
 (0)