Skip to content

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 15 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/components/modal/Modal.driver.new.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import {ModalProps} from './index';
import {useComponentDriver, ComponentProps} from '../../testkit/new/Component.driver';
import {fireEvent} from '@testing-library/react-native';

export const ModalDriver = (props: ComponentProps) => {
const {renderTree, testID} = props;
const driver = useComponentDriver<ModalProps>(props);

const isVisible = () => {
return !!driver.getProps().visible;
};

const pressOnBackground = () => {
const overlay = renderTree.queryByTestId(`${testID}.TouchableOverlay`);
if (overlay) {
fireEvent.press(overlay);
}
};

return {...driver, isVisible, pressOnBackground};
};
18 changes: 18 additions & 0 deletions src/components/modal/__test__/index.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
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 visible', () => {
const renderTree = render(<TestCase visible/>);
const modal = ModalDriver({renderTree, testID});
expect(modal.isVisible()).toBeTruthy();
});
});
3 changes: 1 addition & 2 deletions src/components/modal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,10 @@ class Modal extends Component<ModalProps> {
<View
useSafeArea={isScreenReaderEnabled}
style={!isScreenReaderEnabled && [styles.touchableOverlay, {backgroundColor: overlayBackgroundColor}]}
testID={`${testID}.TouchableOverlay`}
>
{/*
// @ts-ignore */}
<TouchableWithoutFeedback {...accessibilityProps} onPress={onBackgroundPress}>
<TouchableWithoutFeedback {...accessibilityProps} onPress={onBackgroundPress} testID={`${testID}.TouchableOverlay`}>
<View style={isScreenReaderEnabled ? styles.accessibleOverlayView : styles.fill}/>
</TouchableWithoutFeedback>
</View>
Expand Down
10 changes: 10 additions & 0 deletions src/incubator/Dialog/Dialog.driver.new.ts
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 dialogModal = ModalDriver({renderTree, testID: `${testID}.modal`});
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would call it modalDriver

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ping

return {...dialogModal, ...driver};
};
44 changes: 44 additions & 0 deletions src/incubator/Dialog/__tests__/index.new.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import React, {useRef} from 'react';
import {render, act} from '@testing-library/react-native';
import Dialog, {DialogProps} from '../index';
import {DialogDriver} from '../Dialog.driver.new';

const testID = 'dialog';

const TestCase = (props: Omit<DialogProps, 'testID'>) => {
return <Dialog testID={testID} {...props}/>;
};

const getDriver = (Element: React.JSX.Element) => {
const renderTree = render(Element);
const dialogDriver = DialogDriver({renderTree, testID});
return {renderTree, dialogDriver};
};

describe('Sanity checks', () => {
it('Should show dialog', () => {
const {dialogDriver} = getDriver(<TestCase visible/>);
expect(dialogDriver.isVisible()).toBeTruthy();
});

it('Should dismiss dialog on background press', () => {
const {dialogDriver} = getDriver(<TestCase 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();
});
expect(dialogDriver.isVisible()).toBeFalsy();
});
});
2 changes: 2 additions & 0 deletions src/testkit/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ export {SortableListItemDriver} from '../components/sortableList/SortableListIte
export {SliderDriver} from '../components/slider/slider.driver';
export {SliderDriver as IncubatorSliderDriver} from '../incubator/Slider/Slider.driver';
export {ViewDriver} from '../components/view/View.driver.new';
export {ModalDriver} from '../components/modal/Modal.driver.new';
export {DialogDriver} from '../incubator/Dialog/Dialog.driver.new';