Skip to content

Toast testkit driver #2901

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 9 commits into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from all 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/incubator/toast/Toast.driver.new.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import {ToastProps} from '../index';
import {useComponentDriver, ComponentProps} from '../../testkit/new/Component.driver';
import {TextDriver} from '../../components/text/Text.driver.new';
import {ButtonDriver} from '../../components/button/Button.driver.new';

export const ToastDriver = (props: ComponentProps) => {
const {renderTree, testID} = props;
const driver = useComponentDriver<ToastProps>(props);
const actionButtonDriver = ButtonDriver({renderTree, testID: `${testID}-action`});
const messageDriver = TextDriver({renderTree, testID: `${testID}-message`});

const getMessage = () => {
return messageDriver;
};

const getAction = () => {
return actionButtonDriver;
};

return {...driver, getMessage, getAction};
};
48 changes: 48 additions & 0 deletions src/incubator/toast/__test__/index.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import React from 'react';
import {render} from '@testing-library/react-native';
import Toast, {ToastProps} from '../index';
import {ToastDriver} from '../Toast.driver.new';

const testID = 'toast';

const TestCase = (props: ToastProps) => {
return <Toast visible testID={testID} {...props}/>;
};

const getDriver = (props?: ToastProps) => {
const renderTree = render(<TestCase {...props}/>);
const toastDriver = ToastDriver({renderTree, testID});
return {render, toastDriver};
};

describe('Sanity checks', () => {
it('Should show toast', () => {
const {toastDriver} = getDriver();
expect(toastDriver.exists()).toBeTruthy();
});

it('Should dismiss after one second', async () => {
jest.useFakeTimers();
const TIME = 1000;
const dismissFn = jest.fn();
const {toastDriver} = getDriver({autoDismiss: TIME, onDismiss: dismissFn});
expect(toastDriver.exists()).toBeTruthy();
expect(dismissFn).not.toHaveBeenCalled();
jest.advanceTimersByTime(TIME);
expect(dismissFn).toHaveBeenCalledTimes(1);
});

it('Should show an Hello World message', () => {
const MESSAGE = 'Hello World';
const {toastDriver} = getDriver({message: MESSAGE});
expect(toastDriver.getMessage().getText()).toEqual(MESSAGE);
});

it('Should press on action button', () => {
const actionFn = jest.fn();
const {toastDriver} = getDriver({action: {onPress: actionFn}});
expect(actionFn).not.toHaveBeenCalled();
toastDriver.getAction().press();
expect(actionFn).toHaveBeenCalled();
});
});