1- import { screen , waitFor , render as rtlRender } from '@testing-library/react' ;
1+ import { screen , waitFor } from '@testing-library/react' ;
22import userEvent from '@testing-library/user-event' ;
3- import { IntlProvider } from '@edx/frontend-platform/i18n ' ;
3+ import { renderWrapper } from '@src/setupTest ' ;
44import { ToastManagerProvider , useToastManager } from './ToastManagerContext' ;
55
6- const render = ( ui : React . ReactElement ) => rtlRender (
7- < IntlProvider locale = "en" >
8- { ui }
9- </ IntlProvider > ,
10- ) ;
11-
126const TestComponent = ( ) => {
13- const { handleShowToast, handleDiscardToast } = useToastManager ( ) ;
7+ const { showToast } = useToastManager ( ) ;
8+
9+ const handleShowToast = ( ) => showToast ( { message : 'Test toast message' , type : 'error' } ) ;
10+ const handleShowAnotherToast = ( ) => showToast ( { message : 'Another message' , type : 'success' } ) ;
1411
1512 return (
1613 < div >
17- < button type = "button" onClick = { ( ) => handleShowToast ( 'Test toast message' ) } >
18- Show Toast
19- </ button >
20- < button type = "button" onClick = { ( ) => handleShowToast ( 'Another message' ) } >
21- Show Another Toast
22- </ button >
23- < button type = "button" onClick = { handleDiscardToast } >
24- Discard Toast
25- </ button >
14+ < button type = "button" onClick = { handleShowToast } > Show Toast</ button >
15+ < button type = "button" onClick = { handleShowAnotherToast } > Show Another Toast</ button >
2616 </ div >
2717 ) ;
2818} ;
2919
3020describe ( 'ToastManagerContext' , ( ) => {
3121 describe ( 'ToastManagerProvider' , ( ) => {
3222 it ( 'does not show toast initially' , ( ) => {
33- render (
23+ renderWrapper (
3424 < ToastManagerProvider >
3525 < TestComponent />
3626 </ ToastManagerProvider > ,
@@ -39,15 +29,14 @@ describe('ToastManagerContext', () => {
3929 expect ( screen . queryByRole ( 'alert' ) ) . not . toBeInTheDocument ( ) ;
4030 } ) ;
4131
42- it ( 'shows toast when handleShowToast is called' , async ( ) => {
32+ it ( 'shows toast when showToast is called' , async ( ) => {
4333 const user = userEvent . setup ( ) ;
44- render (
34+ renderWrapper (
4535 < ToastManagerProvider >
4636 < TestComponent />
4737 </ ToastManagerProvider > ,
4838 ) ;
4939
50- // handleShowToast is called on button click
5140 const showButton = screen . getByText ( 'Show Toast' ) ;
5241 await user . click ( showButton ) ;
5342
@@ -57,59 +46,29 @@ describe('ToastManagerContext', () => {
5746 } ) ;
5847 } ) ;
5948
60- it ( 'updates toast message when handleShowToast is called with different message ' , async ( ) => {
49+ it ( 'adds multiple toasts when showToast is called multiple times ' , async ( ) => {
6150 const user = userEvent . setup ( ) ;
62- render (
51+ renderWrapper (
6352 < ToastManagerProvider >
6453 < TestComponent />
6554 </ ToastManagerProvider > ,
6655 ) ;
6756
68- // Show first toast
6957 const showButton = screen . getByText ( 'Show Toast' ) ;
70- await user . click ( showButton ) ;
71-
72- await waitFor ( ( ) => {
73- expect ( screen . getByText ( 'Test toast message' ) ) . toBeInTheDocument ( ) ;
74- } ) ;
75-
76- // Show another toast
7758 const showAnotherButton = screen . getByText ( 'Show Another Toast' ) ;
78- await user . click ( showAnotherButton ) ;
7959
80- await waitFor ( ( ) => {
81- expect ( screen . getByText ( 'Another message' ) ) . toBeInTheDocument ( ) ;
82- expect ( screen . queryByText ( 'Test toast message' ) ) . not . toBeInTheDocument ( ) ;
83- } ) ;
84- } ) ;
85-
86- it ( 'hides toast when handleDiscardToast is called' , async ( ) => {
87- const user = userEvent . setup ( ) ;
88- render (
89- < ToastManagerProvider >
90- < TestComponent />
91- </ ToastManagerProvider > ,
92- ) ;
93-
94- const showButton = screen . getByText ( 'Show Toast' ) ;
9560 await user . click ( showButton ) ;
61+ await user . click ( showAnotherButton ) ;
9662
9763 await waitFor ( ( ) => {
9864 expect ( screen . getByText ( 'Test toast message' ) ) . toBeInTheDocument ( ) ;
99- } ) ;
100-
101- // handleDiscardToast is called on button click
102- const discardButton = screen . getByText ( 'Discard Toast' ) ;
103- await user . click ( discardButton ) ;
104-
105- await waitFor ( ( ) => {
106- expect ( screen . queryByRole ( 'alert' ) ) . not . toBeInTheDocument ( ) ;
65+ expect ( screen . getByText ( 'Another message' ) ) . toBeInTheDocument ( ) ;
10766 } ) ;
10867 } ) ;
10968
11069 it ( 'hides toast when close button is clicked' , async ( ) => {
11170 const user = userEvent . setup ( ) ;
112- render (
71+ renderWrapper (
11372 < ToastManagerProvider >
11473 < TestComponent />
11574 </ ToastManagerProvider > ,
@@ -127,48 +86,22 @@ describe('ToastManagerContext', () => {
12786
12887 await waitFor ( ( ) => {
12988 expect ( screen . queryByRole ( 'alert' ) ) . not . toBeInTheDocument ( ) ;
130- } ) ;
131- } ) ;
132-
133- it ( 'calls handleClose callback when toast is closed' , async ( ) => {
134- const user = userEvent . setup ( ) ;
135- const mockHandleClose = jest . fn ( ) ;
136-
137- render (
138- < ToastManagerProvider handleClose = { mockHandleClose } >
139- < TestComponent />
140- </ ToastManagerProvider > ,
141- ) ;
142-
143- const showButton = screen . getByText ( 'Show Toast' ) ;
144- await user . click ( showButton ) ;
145-
146- await waitFor ( ( ) => {
147- expect ( screen . getByText ( 'Test toast message' ) ) . toBeInTheDocument ( ) ;
148- } ) ;
149-
150- const closeButton = screen . getByLabelText ( 'Close' ) ;
151- await user . click ( closeButton ) ;
152-
153- await waitFor ( ( ) => {
154- expect ( mockHandleClose ) . toHaveBeenCalledTimes ( 1 ) ;
155- } ) ;
89+ } , { timeout : 500 } ) ;
15690 } ) ;
15791 } ) ;
15892
15993 describe ( 'useToastManager hook' , ( ) => {
16094 it ( 'throws error when used outside ToastManagerProvider' , ( ) => {
161- // Suppress console.error for this test
162- const consoleSpy = jest . spyOn ( console , 'error' ) . mockImplementation ( ( ) => { } ) ;
95+ const consoleSpy = jest . spyOn ( console , 'error' ) . mockImplementation ( ( ) => { } ) ;
16396
16497 const TestComponentWithoutProvider = ( ) => {
16598 useToastManager ( ) ;
16699 return < div > Test</ div > ;
167100 } ;
168101
169102 expect ( ( ) => {
170- render ( < TestComponentWithoutProvider /> ) ;
171- } ) . toThrow ( 'useToastManager must be used within an ToastManagerProvider' ) ;
103+ renderWrapper ( < TestComponentWithoutProvider /> ) ;
104+ } ) . toThrow ( 'useToastManager must be used within a ToastManagerProvider' ) ;
172105
173106 consoleSpy . mockRestore ( ) ;
174107 } ) ;
0 commit comments