1+ // __tests__/components/DashboardOverview.test.tsx
2+
3+ import React from 'react' ;
4+ import { render , screen } from '@testing-library/react' ;
5+ import DashboardOverview from '@/components/dashboard/DashboardOverview' ; // Adjust path as needed
6+ import { useDashboardData } from '@/hooks/useDashboardData' ; // Adjust path to your data hook
7+
8+ // Mock the data fetching hook to control loading states
9+ jest . mock ( '@/hooks/useDashboardData' ) ;
10+
11+ describe ( 'Component: Skeleton Loading States for Dashboard' , ( ) => {
12+ beforeEach ( ( ) => {
13+ jest . clearAllMocks ( ) ;
14+ } ) ;
15+
16+ it ( 'renders skeleton UI placeholders when data is loading' , ( ) => {
17+ // Force the loading state to true
18+ ( useDashboardData as jest . Mock ) . mockReturnValue ( {
19+ data : null ,
20+ isLoading : true ,
21+ error : null ,
22+ } ) ;
23+
24+ render ( < DashboardOverview /> ) ;
25+
26+ // Assert that skeleton elements are rendered
27+ const skeletons = screen . getAllByTestId ( 'skeleton-loader' ) ;
28+ expect ( skeletons . length ) . toBeGreaterThan ( 0 ) ;
29+
30+ // Assert that actual data content is not present
31+ expect ( screen . queryByText ( / t o t a l d e l i v e r i e s / i) ) . not . toBeInTheDocument ( ) ;
32+ } ) ;
33+
34+ it ( 'removes skeleton loaders and renders actual data when loading completes' , ( ) => {
35+ // Simulate a successful data fetch
36+ ( useDashboardData as jest . Mock ) . mockReturnValue ( {
37+ data : {
38+ totalDeliveries : 1450 ,
39+ activeDrivers : 32 ,
40+ } ,
41+ isLoading : false ,
42+ error : null ,
43+ } ) ;
44+
45+ render ( < DashboardOverview /> ) ;
46+
47+ // Assert that skeletons are removed
48+ expect ( screen . queryByTestId ( 'skeleton-loader' ) ) . not . toBeInTheDocument ( ) ;
49+
50+ // Assert that real data is rendered
51+ expect ( screen . getByText ( '1450' ) ) . toBeInTheDocument ( ) ;
52+ expect ( screen . getByText ( '32' ) ) . toBeInTheDocument ( ) ;
53+ } ) ;
54+
55+ it ( 'renders an error boundary or state if data fetching fails' , ( ) => {
56+ ( useDashboardData as jest . Mock ) . mockReturnValue ( {
57+ data : null ,
58+ isLoading : false ,
59+ error : new Error ( 'Failed to fetch dashboard metrics' ) ,
60+ } ) ;
61+
62+ render ( < DashboardOverview /> ) ;
63+
64+ expect ( screen . queryByTestId ( 'skeleton-loader' ) ) . not . toBeInTheDocument ( ) ;
65+ expect ( screen . getByText ( / f a i l e d t o f e t c h d a s h b o a r d m e t r i c s / i) ) . toBeInTheDocument ( ) ;
66+ } ) ;
67+ } ) ;
0 commit comments