-
Notifications
You must be signed in to change notification settings - Fork 734
Dialog and Modal test drivers. #2893
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 13 commits
0e60717
7efc19e
90daf4e
9091814
6974988
179d0a7
f7caab1
ec85c42
1b17b39
346acea
6b61202
c74690f
3e7be33
88e0a69
3a832dc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import {ModalProps} from './index'; | ||
import {useComponentDriver, ComponentProps} from '../../testkit/new/Component.driver'; | ||
import {usePressableDriver} from '../../testkit/new/usePressable.driver'; | ||
|
||
export const ModalDriver = (props: ComponentProps) => { | ||
const {renderTree, testID} = props; | ||
const driver = useComponentDriver<ModalProps>(props); | ||
const overlayDriver = usePressableDriver<{}>(useComponentDriver<{}>({renderTree, testID: `${testID}.TouchableOverlay`})); | ||
|
||
const isVisible = () => { | ||
return !!driver.getProps().visible; | ||
}; | ||
|
||
return {...driver, isVisible, pressOnBackground: overlayDriver.press}; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import React from 'react'; | ||
import {render} from '@testing-library/react-native'; | ||
import Modal, {ModalProps} from '../'; | ||
import {ModalDriver} from '../Modal.driver.new'; | ||
|
||
const testID = 'modal'; | ||
|
||
const TestCase = (props: Omit<ModalProps, 'testID'>) => { | ||
return <Modal testID={testID} {...props}/>; | ||
}; | ||
|
||
describe('Sanity modal test', () => { | ||
it('Should be not visible at first and visible at second render', () => { | ||
const renderTree = render(<TestCase/>); | ||
const modalDriver = ModalDriver({renderTree, testID}); | ||
expect(modalDriver.isVisible()).toBeFalsy(); | ||
renderTree.rerender(<TestCase visible/>); | ||
expect(modalDriver.isVisible()).toBeTruthy(); | ||
}); | ||
|
||
it('Should press on background', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Test close when background is pressed There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Or Should dismiss modal on background press There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I actually don't think this causes the modal to dismiss it just lets you press on the background. On the dialog pressing on the background of the modal makes it dismiss. |
||
const backgroundPressHandler = jest.fn(); | ||
const renderTree = render(<TestCase visible onBackgroundPress={backgroundPressHandler}/>); | ||
const modalDriver = ModalDriver({renderTree, testID}); | ||
expect(backgroundPressHandler).not.toHaveBeenCalled(); | ||
modalDriver.pressOnBackground(); | ||
expect(backgroundPressHandler).toHaveBeenCalledTimes(1); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import {DialogProps} from './index'; | ||
import {useComponentDriver, ComponentProps} from '../../testkit/new/Component.driver'; | ||
import {ModalDriver} from '../../testkit/'; | ||
|
||
export const DialogDriver = (props: ComponentProps) => { | ||
const {renderTree, testID} = props; | ||
const driver = useComponentDriver<DialogProps>(props); | ||
const modalDriver = ModalDriver({renderTree, testID: `${testID}.modal`}); | ||
return {...modalDriver, ...driver}; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import React, {useRef, useState, useEffect, useCallback} from 'react'; | ||
import {render, act} from '@testing-library/react-native'; | ||
import Dialog, {DialogProps} from '../index'; | ||
import {DialogDriver} from '../Dialog.driver.new'; | ||
import View from '../../../components/view'; | ||
import Button from '../../../components/button'; | ||
import {ButtonDriver} from '../../../components/button/Button.driver.new'; | ||
|
||
const testID = 'dialog'; | ||
|
||
const TestCase1 = (props: Omit<DialogProps, 'testID'>) => { | ||
return <Dialog testID={testID} {...props}/>; | ||
}; | ||
|
||
const onDismiss = () => {}; | ||
|
||
const defaultProps = { | ||
testID: 'dialog', | ||
useSafeArea: true, | ||
onDismiss, | ||
bottom: true, | ||
centerH: true | ||
}; | ||
|
||
const TestCase2 = props => { | ||
const [visible, setVisible] = useState(props.visible); | ||
|
||
useEffect(() => { | ||
setVisible(props.visible); | ||
}, [props.visible]); | ||
|
||
const openDialog = useCallback(() => { | ||
setVisible(true); | ||
}, []); | ||
|
||
const closeDialog = useCallback(() => { | ||
setVisible(false); | ||
}, []); | ||
|
||
return ( | ||
<View> | ||
<Dialog {...defaultProps} {...props} visible={visible}> | ||
<View height={300}> | ||
<Button testID={'closeButton'} flex onPress={closeDialog}/> | ||
</View> | ||
</Dialog> | ||
<Button testID={'openButton'} flex onPress={openDialog}/> | ||
</View> | ||
); | ||
}; | ||
|
||
const getDriver = (Element: React.JSX.Element) => { | ||
const renderTree = render(Element); | ||
const dialogDriver = DialogDriver({renderTree, testID}); | ||
return {renderTree, dialogDriver}; | ||
}; | ||
|
||
describe('Incubator.Dialog sanity checks', () => { | ||
it('Should show dialog', () => { | ||
const {dialogDriver} = getDriver(<TestCase1 visible/>); | ||
expect(dialogDriver.isVisible()).toBeTruthy(); | ||
}); | ||
|
||
it('Should dismiss dialog on background press', () => { | ||
const {dialogDriver} = getDriver(<TestCase1 visible/>); | ||
expect(dialogDriver.isVisible()).toBeTruthy(); | ||
dialogDriver.pressOnBackground(); | ||
expect(dialogDriver.isVisible()).toBeFalsy(); | ||
}); | ||
|
||
it('Should dismiss dialog on dismiss call', () => { | ||
let dialogRef: React.RefObject<{dismiss: () => void}>; | ||
const RefTestCase = () => { | ||
dialogRef = useRef<{dismiss:() => void}>(null); | ||
return <Dialog testID={testID} visible ref={dialogRef}/>; | ||
}; | ||
const {dialogDriver} = getDriver(<RefTestCase/>); | ||
expect(dialogDriver.isVisible()).toBeTruthy(); | ||
act(() => { | ||
dialogRef.current?.dismiss(); | ||
}); | ||
M-i-k-e-l marked this conversation as resolved.
Show resolved
Hide resolved
|
||
expect(dialogDriver.isVisible()).toBeFalsy(); | ||
}); | ||
it('Should exist only if visible', async () => { | ||
const onDismiss = jest.fn(); | ||
const component = <TestCase2 onDismiss={onDismiss}/>; | ||
const {dialogDriver, renderTree} = getDriver(component); | ||
expect(dialogDriver.exists()).toBeFalsy(); | ||
const openButtonDriver = ButtonDriver({renderTree, testID: 'openButton'}); | ||
await openButtonDriver.press(); | ||
expect(await dialogDriver.exists()).toBeTruthy(); | ||
expect(onDismiss).toHaveBeenCalledTimes(0); | ||
const closeButtonDriver = ButtonDriver({renderTree, testID: 'closeButton'}); | ||
await closeButtonDriver.press(); | ||
expect(await dialogDriver.exists()).toBeFalsy(); | ||
expect(onDismiss).toHaveBeenCalledTimes(1); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.