-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
0e60717
Added drivers for modal and dialog
nitzanyiz 7efc19e
Added sanity checks with the new drivers
nitzanyiz 90daf4e
Tiny change
nitzanyiz 9091814
Added dialog dismiss test
nitzanyiz 6974988
Moved TouchableOverlay testID location
nitzanyiz 179d0a7
Changed driver deconstruct order
nitzanyiz f7caab1
Added query to handle the case the background doesn't exists
nitzanyiz ec85c42
Fixed imports. Added ref testing for dismiss
nitzanyiz 1b17b39
Merge branch 'master' into infra/DialogModalTestDriver
nitzanyiz 346acea
Name change and using pressable for the overlay
nitzanyiz 6b61202
Merged origin master
nitzanyiz c74690f
Copied old test with new button driver
nitzanyiz 3e7be33
Fixed modal test
nitzanyiz 88e0a69
Added test for modal dismiss
nitzanyiz 3a832dc
Check onDimiss callback being called
nitzanyiz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}; | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import React, {useState} from 'react'; | ||
import {render} from '@testing-library/react-native'; | ||
import Modal, {ModalProps} from '../'; | ||
import {ModalDriver} from '../Modal.driver.new'; | ||
import View from '../../view'; | ||
import Button from '../../button'; | ||
import {ButtonDriver} from '../../button/Button.driver.new'; | ||
|
||
const testID = 'modal'; | ||
|
||
const TestCase = (props: Omit<ModalProps, 'testID'>) => { | ||
return <Modal testID={testID} {...props}/>; | ||
}; | ||
|
||
describe('Sanity modal test', () => { | ||
it('Testing visibility', () => { | ||
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', () => { | ||
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); | ||
}); | ||
it('Should dismiss modal', () => { | ||
const onDismissHandler = jest.fn(); | ||
const TestCase = () => { | ||
const [showModal, setShowModal] = useState(true); | ||
return ( | ||
<View> | ||
<Modal testID={testID} onDismiss={onDismissHandler} visible={showModal}/> | ||
<Button testID={'button-dismiss'} onPress={() => setShowModal(false)}/> | ||
</View> | ||
); | ||
}; | ||
const renderTree = render(<TestCase/>); | ||
const modalDriver = ModalDriver({renderTree, testID}); | ||
const buttonDriver = ButtonDriver({renderTree, testID: 'button-dismiss'}); | ||
expect(modalDriver.isVisible()).toBeTruthy(); | ||
expect(onDismissHandler).not.toHaveBeenCalled(); | ||
buttonDriver.press(); | ||
expect(modalDriver.isVisible()).toBeFalsy(); | ||
expect(onDismissHandler).toHaveBeenCalledTimes(1); | ||
}); | ||
}); | ||
Comment on lines
+32
to
+52
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 added a test for the dismiss callback. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}; | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
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 dismissFn = jest.fn(); | ||
const {dialogDriver} = getDriver(<TestCase1 visible onDismiss={dismissFn}/>); | ||
expect(dismissFn).not.toHaveBeenCalled(); | ||
expect(dialogDriver.isVisible()).toBeTruthy(); | ||
dialogDriver.pressOnBackground(); | ||
expect(dialogDriver.isVisible()).toBeFalsy(); | ||
expect(dismissFn).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
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); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Test close when background is pressed
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.
Or Should dismiss modal on background press
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.
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.