|
| 1 | +import React, {useCallback, useEffect, useState} from 'react'; |
| 2 | +import Button from '../../../components/button'; |
| 3 | +import View from '../../../components/view'; |
| 4 | +import Dialog from '../index'; |
| 5 | +import {ButtonDriver, ComponentDriver} from '../../../testkit'; |
| 6 | + |
| 7 | +const onDismiss = () => {}; |
| 8 | + |
| 9 | +const defaultProps = { |
| 10 | + testID: 'dialog', |
| 11 | + useSafeArea: true, |
| 12 | + onDismiss, |
| 13 | + bottom: true, |
| 14 | + centerH: true |
| 15 | +}; |
| 16 | + |
| 17 | +const TestCase = props => { |
| 18 | + const [visible, setVisible] = useState(props.visible); |
| 19 | + |
| 20 | + useEffect(() => { |
| 21 | + setVisible(props.visible); |
| 22 | + }, [props.visible]); |
| 23 | + |
| 24 | + const openDialog = useCallback(() => { |
| 25 | + setVisible(true); |
| 26 | + }, []); |
| 27 | + |
| 28 | + const closeDialog = useCallback(() => { |
| 29 | + setVisible(false); |
| 30 | + }, []); |
| 31 | + |
| 32 | + return ( |
| 33 | + <View> |
| 34 | + <Dialog {...defaultProps} {...props} visible={visible}> |
| 35 | + <View height={300}> |
| 36 | + <Button testID={'closeButton'} flex onPress={closeDialog}/> |
| 37 | + </View> |
| 38 | + </Dialog> |
| 39 | + <Button testID={'openButton'} flex onPress={openDialog}/> |
| 40 | + </View> |
| 41 | + ); |
| 42 | +}; |
| 43 | + |
| 44 | +const _TestCase = props => <TestCase {...props}/>; |
| 45 | + |
| 46 | +describe('Incubator.Dialog', () => { |
| 47 | + it('Incubator.Dialog should exist only if visible', async () => { |
| 48 | + const onDismiss = jest.fn(); |
| 49 | + const component = _TestCase({onDismiss}); |
| 50 | + const dialogDriver = new ComponentDriver({component, testID: 'dialog'}); |
| 51 | + expect(await dialogDriver.exists()).toBeFalsy(); |
| 52 | + const openButtonDriver = new ButtonDriver({component, testID: 'openButton'}); |
| 53 | + await openButtonDriver.press(); |
| 54 | + expect(await dialogDriver.exists()).toBeTruthy(); |
| 55 | + expect(onDismiss).toHaveBeenCalledTimes(0); |
| 56 | + const closeButtonDriver = new ButtonDriver({component, testID: 'closeButton'}); |
| 57 | + await closeButtonDriver.press(); |
| 58 | + expect(await dialogDriver.exists()).toBeFalsy(); |
| 59 | + expect(onDismiss).toHaveBeenCalledTimes(1); |
| 60 | + }); |
| 61 | +}); |
0 commit comments