|
| 1 | +import React from 'react'; |
| 2 | +import {ViewStyle} from 'react-native'; |
| 3 | +import {render} from '@testing-library/react-native'; |
| 4 | +import {Spacings} from '../../../style'; |
| 5 | +import FloatingButton, {FloatingButtonLayouts} from '../index'; |
| 6 | +import {ButtonDriver} from '../../button/Button.driver.new'; |
| 7 | +import {useComponentDriver, ComponentProps} from '../../../testkit/new/Component.driver'; |
| 8 | + |
| 9 | +const TEST_ID = 'floating_button'; |
| 10 | +const button = { |
| 11 | + label: 'First' |
| 12 | +}; |
| 13 | +const secondaryButton = { |
| 14 | + label: 'Second' |
| 15 | +}; |
| 16 | + |
| 17 | +const TestCase = (props) => { |
| 18 | + return <FloatingButton {...props} testID={TEST_ID}/>; |
| 19 | +}; |
| 20 | +export const FloatingButtonDriver = (props: ComponentProps) => { |
| 21 | + const driver = useComponentDriver(props); |
| 22 | + const getStyle = () => driver.getProps().style as ViewStyle; |
| 23 | + return {...driver, getStyle}; |
| 24 | +}; |
| 25 | + |
| 26 | +describe('FloatingButton', () => { |
| 27 | + describe('visible', () => { |
| 28 | + it('should render a button according to visibility', async () => { |
| 29 | + const props = {}; |
| 30 | + const renderTree = render(<TestCase {...props}/>); |
| 31 | + const buttonDriver = ButtonDriver({renderTree, testID: `${TEST_ID}.button`}); |
| 32 | + expect(await buttonDriver.exists()).not.toBeTruthy(); |
| 33 | + |
| 34 | + renderTree.rerender(<TestCase visible/>); |
| 35 | + expect(await buttonDriver.exists()).toBeTruthy(); |
| 36 | + }); |
| 37 | + }); |
| 38 | + |
| 39 | + describe('buttons', () => { |
| 40 | + it('should render a button', async () => { |
| 41 | + const props = {visible: true}; |
| 42 | + const renderTree = render(<TestCase {...props}/>); |
| 43 | + const buttonDriver = ButtonDriver({renderTree, testID: `${TEST_ID}.button`}); |
| 44 | + expect(await buttonDriver.exists()).toBeTruthy(); |
| 45 | + }); |
| 46 | + |
| 47 | + it('should not render a secondary button', async () => { |
| 48 | + const props = {visible: true}; |
| 49 | + const renderTree = render(<TestCase {...props}/>); |
| 50 | + const buttonDriver = ButtonDriver({renderTree, testID: `${TEST_ID}.secondaryButton`}); |
| 51 | + expect(await buttonDriver.exists()).not.toBeTruthy(); |
| 52 | + }); |
| 53 | + |
| 54 | + it('should render a button with a label', async () => { |
| 55 | + const props = {visible: true, button}; |
| 56 | + const renderTree = render(<TestCase {...props}/>); |
| 57 | + const buttonDriver = ButtonDriver({renderTree, testID: `${TEST_ID}.button`}); |
| 58 | + expect(await buttonDriver.getLabel().getText()).toEqual(button.label); |
| 59 | + }); |
| 60 | + |
| 61 | + it('should render secondary button with label', async () => { |
| 62 | + const props = {visible: true, secondaryButton}; |
| 63 | + const renderTree = render(<TestCase {...props}/>); |
| 64 | + const buttonDriver = ButtonDriver({renderTree, testID: `${TEST_ID}.secondaryButton`}); |
| 65 | + expect(await buttonDriver.getLabel().getText()).toEqual(secondaryButton.label); |
| 66 | + }); |
| 67 | + }); |
| 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 | + }); |
| 154 | +}); |
0 commit comments