1
1
import React from 'react' ;
2
- import { render , screen } from '@testing-library/react' ;
2
+ import { render , screen , fireEvent , waitFor } from '@testing-library/react' ;
3
3
import '@testing-library/jest-dom' ;
4
4
import Dashboard from '../components/dashboard/Dashboard' ;
5
5
6
6
// Mock subcomponents that are imported in Dashboard
7
7
jest . mock ( '../components/dashboard/Chart' , ( ) => ( ) => < div > Mocked Chart Component</ div > ) ;
8
8
jest . mock ( '../components/dashboard/Deposits' , ( ) => ( ) => < div > Mocked Deposits Component</ div > ) ;
9
- jest . mock ( '../components/defectConfig/DefectConfig' , ( ) => ( ) => < div > Mocked Orders Component</ div > ) ;
9
+
10
+ // Mock the DefectConfig component to include the Apply button and Snackbar
11
+ jest . mock ( '../components/defectConfig/DefectConfig' , ( ) => ( ) => (
12
+ < div >
13
+ Mocked DefectConfig Component
14
+ < button onClick = { ( ) => { } } > Apply</ button >
15
+ < div role = "alert" > Defect configuration updated successfully!</ div >
16
+ </ div >
17
+ ) ) ;
10
18
11
19
describe ( 'Dashboard Component' , ( ) => {
12
20
test ( 'renders the Dashboard component' , ( ) => {
@@ -24,4 +32,23 @@ describe('Dashboard Component', () => {
24
32
expect ( screen . getByText ( / C o p y r i g h t © / i) ) . toBeInTheDocument ( ) ;
25
33
expect ( screen . getByText ( 'Karevo' ) ) . toBeInTheDocument ( ) ;
26
34
} ) ;
35
+
36
+ test ( 'shows success message when Apply button is clicked' , async ( ) => {
37
+ render ( < Dashboard /> ) ;
38
+
39
+ // Check if the Apply button is present
40
+ const applyButton = screen . getByText ( 'Apply' ) ;
41
+ expect ( applyButton ) . toBeInTheDocument ( ) ;
42
+
43
+ // Mock the click event and verify Snackbar visibility
44
+ fireEvent . click ( applyButton ) ;
45
+
46
+ // Wait for the success message to appear
47
+ await waitFor ( ( ) => {
48
+ expect ( screen . getByRole ( 'alert' ) ) . toHaveTextContent ( 'Defect configuration updated successfully!' ) ;
49
+ } ) ;
50
+
51
+ // Optionally, you can test if the message disappears after 3 seconds
52
+ await new Promise ( ( resolve ) => setTimeout ( resolve , 3000 ) ) ;
53
+ } ) ;
27
54
} ) ;
0 commit comments