|
| 1 | +import React from 'react'; |
| 2 | +import {render} from '@testing-library/react-native'; |
| 3 | +import Toast, {ToastProps} from '../index'; |
| 4 | +import {ToastDriver} from '../Toast.driver.new'; |
| 5 | + |
| 6 | +const testID = 'toast'; |
| 7 | + |
| 8 | +const TestCase = (props: ToastProps) => { |
| 9 | + return <Toast visible testID={testID} {...props}/>; |
| 10 | +}; |
| 11 | + |
| 12 | +const getDriver = (props?: ToastProps) => { |
| 13 | + const renderTree = render(<TestCase {...props}/>); |
| 14 | + const toastDriver = ToastDriver({renderTree, testID}); |
| 15 | + return {render, toastDriver}; |
| 16 | +}; |
| 17 | + |
| 18 | +describe('Sanity checks', () => { |
| 19 | + it('Should show toast', () => { |
| 20 | + const {toastDriver} = getDriver(); |
| 21 | + expect(toastDriver.exists()).toBeTruthy(); |
| 22 | + }); |
| 23 | + |
| 24 | + it('Should dismiss after one second', async () => { |
| 25 | + jest.useFakeTimers(); |
| 26 | + const TIME = 1000; |
| 27 | + const dismissFn = jest.fn(); |
| 28 | + const {toastDriver} = getDriver({autoDismiss: TIME, onDismiss: dismissFn}); |
| 29 | + expect(toastDriver.exists()).toBeTruthy(); |
| 30 | + expect(dismissFn).not.toHaveBeenCalled(); |
| 31 | + jest.advanceTimersByTime(TIME); |
| 32 | + expect(dismissFn).toHaveBeenCalledTimes(1); |
| 33 | + }); |
| 34 | + |
| 35 | + it('Should show an Hello World message', () => { |
| 36 | + const MESSAGE = 'Hello World'; |
| 37 | + const {toastDriver} = getDriver({message: MESSAGE}); |
| 38 | + expect(toastDriver.getMessage().getText()).toEqual(MESSAGE); |
| 39 | + }); |
| 40 | + |
| 41 | + it('Should press on action button', () => { |
| 42 | + const actionFn = jest.fn(); |
| 43 | + const {toastDriver} = getDriver({action: {onPress: actionFn}}); |
| 44 | + expect(actionFn).not.toHaveBeenCalled(); |
| 45 | + toastDriver.getAction().press(); |
| 46 | + expect(actionFn).toHaveBeenCalled(); |
| 47 | + }); |
| 48 | +}); |
0 commit comments