Skip to content

Commit 358d29f

Browse files
authored
Merge pull request #548 from Ikechukwu-Patrick/test-skeleton-loaders
Test: Implement Component: Skeleton Loading States for Dashboard
2 parents b581895 + d283e63 commit 358d29f

1 file changed

Lines changed: 67 additions & 0 deletions

File tree

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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(/total deliveries/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(/failed to fetch dashboard metrics/i)).toBeInTheDocument();
66+
});
67+
});

0 commit comments

Comments
 (0)