11import React from 'react' ;
2- import { render } from 'react-native-testing-library' ;
2+ import { render , fireEvent } from 'react-native-testing-library' ;
3+ import { renderHook } from '@testing-library/react-hooks' ;
4+
5+ import ToastProvider from '../../../providers/ToastProvider' ;
6+
7+ import ToastContext from '../../../contexts/ToastContext' ;
8+
9+ import useToast from '../../../hooks/useToast' ;
310
411import ToastContainer from '..' ;
512
@@ -33,29 +40,6 @@ describe('ToastContainer', () => {
3340 expect ( getByText ( 'Toast success message' ) ) . toBeTruthy ( ) ;
3441 } ) ;
3542
36- // it('should be able to automatically dismiss toast with setTimeout fn', () => {
37- // const messages = [
38- // {
39- // id: '1',
40- // message: 'Testing toast timeout',
41- // },
42- // ];
43-
44- // const { getByText } = render(
45- // <ToastContainer messages={messages} toastConfig={defaultConfig} />,
46- // {
47- // wrapper: ToastProvider,
48- // },
49- // );
50-
51- // expect(setTimeout).toHaveBeenCalledTimes(1);
52- // expect(setTimeout).toHaveBeenLastCalledWith(expect.any(Function), 3000);
53- // setTimeout(() => {
54- // expect(getByText('Testing toast timeout')).toBeFalsy();
55- // }, 3000);
56- // jest.runAllTimers();
57- // });
58-
5943 it ( 'should be able to render a toast on screen with title' , ( ) => {
6044 const messages = [
6145 {
@@ -72,4 +56,75 @@ describe('ToastContainer', () => {
7256 expect ( getByText ( 'My Title' ) ) . toBeTruthy ( ) ;
7357 expect ( getByText ( 'Toast success message' ) ) . toBeTruthy ( ) ;
7458 } ) ;
59+
60+ it ( 'should be able to automatically dismiss toast with setTimeout fn' , ( ) => {
61+ const messages = [
62+ {
63+ id : '1' ,
64+ message : 'Testing toast timeout' ,
65+ } ,
66+ ] ;
67+
68+ const { result } = renderHook ( ( ) => useToast ( ) , {
69+ wrapper : ToastProvider ,
70+ } ) ;
71+
72+ const CustomProvider : React . FC = ( { children } ) => (
73+ < ToastContext . Provider value = { result . current } >
74+ { children }
75+ </ ToastContext . Provider >
76+ ) ;
77+
78+ const { getByText } = render (
79+ < ToastContainer messages = { messages } toastConfig = { defaultConfig } /> ,
80+ {
81+ wrapper : CustomProvider ,
82+ } ,
83+ ) ;
84+
85+ expect ( setTimeout ) . toHaveBeenCalledTimes ( 1 ) ;
86+ setTimeout ( ( ) => {
87+ expect ( getByText ( 'Testing toast timeout' ) ) . toBeFalsy ( ) ;
88+ } , 3000 ) ;
89+
90+ jest . useFakeTimers ( ) ;
91+ } ) ;
92+
93+ it ( 'should be able to render a toast on screen and close by clicking on it' , async ( ) => {
94+ const messages = [
95+ {
96+ id : '1' ,
97+ message : 'An error ocurred' ,
98+ } ,
99+ ] ;
100+
101+ const { result } = renderHook ( ( ) => useToast ( ) , {
102+ wrapper : ToastProvider ,
103+ } ) ;
104+
105+ const removeToastHookSpy = jest
106+ . spyOn ( result . current , 'removeToast' )
107+ . mockImplementationOnce ( ( ) => jest . fn ( ) ) ;
108+
109+ const CustomProvider : React . FC = ( { children } ) => (
110+ < ToastContext . Provider value = { result . current } >
111+ { children }
112+ </ ToastContext . Provider >
113+ ) ;
114+
115+ const { getByText } = render (
116+ < ToastContainer messages = { messages } toastConfig = { defaultConfig } /> ,
117+ {
118+ wrapper : CustomProvider ,
119+ } ,
120+ ) ;
121+
122+ const toast = getByText ( 'An error ocurred' ) ;
123+
124+ expect ( toast ) . toBeTruthy ( ) ;
125+
126+ fireEvent . press ( toast ) ;
127+
128+ expect ( removeToastHookSpy ) . toHaveBeenCalledTimes ( 1 ) ;
129+ } ) ;
75130} ) ;
0 commit comments