Skip to content

Commit c03774d

Browse files
ft(626): testing Admin Dashboard
1 parent 39839d4 commit c03774d

File tree

4 files changed

+226
-0
lines changed

4 files changed

+226
-0
lines changed

Diff for: src/Chart/TeamChart.tsx

+3
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ function TeamChart({
8181
weeklyData[isoWeekNumber - 1].failed += failed;
8282
}
8383
// Monthly data
84+
if (!monthlyData[month]) {
85+
monthlyData[month] = { month: month + 1, success: 0, failed: 0 };
86+
}
8487
monthlyData[month].success += success;
8588
monthlyData[month].failed += failed;
8689
}

Diff for: tests/other-tests/AdminTeamDetails.test.tsx

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import React from 'react';
2+
import '@testing-library/jest-dom';
3+
import { render, screen, fireEvent } from '@testing-library/react';
4+
import TeamDetailsModal from '../../src/components/AdminTeamDetails';
5+
6+
const mockOnClose = jest.fn();
7+
8+
const mockTeams = [
9+
{
10+
name: 'Team A',
11+
avgRatings: {
12+
quality: '4.5',
13+
quantity: '3.5',
14+
professional_Skills: '5.0',
15+
},
16+
members: [
17+
{ status: { status: 'active' } },
18+
{ status: { status: 'suspended' } },
19+
],
20+
cohort: {
21+
name: 'Cohort 1',
22+
phase: { name: 'Phase 1' },
23+
program: { name: 'Program A' },
24+
},
25+
ttl: { profile: { name: 'TTL Name' } },
26+
},
27+
];
28+
29+
const mockSelectedTeam = {
30+
teams: 'Team A',
31+
organization: 'Org Name',
32+
};
33+
34+
beforeAll(() => {
35+
global.ResizeObserver = class {
36+
observe() {}
37+
unobserve() {}
38+
disconnect() {}
39+
};
40+
});
41+
42+
describe('TeamDetailsModal', () => {
43+
it('handles tab switching between Overview and Logins', () => {
44+
render(
45+
<TeamDetailsModal
46+
isOpen={true}
47+
onClose={mockOnClose}
48+
selectedteam={mockSelectedTeam}
49+
Teams={mockTeams}
50+
/>,
51+
);
52+
53+
fireEvent.click(screen.getByText('Logins'));
54+
expect(screen.getByText('Logins')).toHaveClass('text-primary');
55+
});
56+
57+
it('calls onClose when the Close button is clicked', () => {
58+
render(
59+
<TeamDetailsModal
60+
isOpen={true}
61+
onClose={mockOnClose}
62+
selectedteam={mockSelectedTeam}
63+
Teams={mockTeams}
64+
/>,
65+
);
66+
67+
fireEvent.click(screen.getByText('Close'));
68+
expect(mockOnClose).toHaveBeenCalled();
69+
});
70+
});

Diff for: tests/other-tests/LineChart.test.tsx

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { render, screen, fireEvent, waitFor } from '@testing-library/react';
2+
import { MockedProvider } from '@apollo/client/testing';
3+
import UserGrowth from '../../src/Chart/LineChart';
4+
import GET_ROLE_QUERY from '../../src/containers/admin-dashBoard/GetRolesQuery';
5+
import React from 'react';
6+
7+
const mockData = {
8+
getAllUsers: [
9+
{ createdAt: '1630454400000', updatedAt: '1630454400000' },
10+
{ createdAt: '1633146400000', updatedAt: '1633146400000' },
11+
],
12+
};
13+
14+
const mocks = [
15+
{
16+
request: {
17+
query: GET_ROLE_QUERY,
18+
variables: { orgToken: 'mockToken' },
19+
},
20+
result: {
21+
data: mockData,
22+
},
23+
},
24+
];
25+
26+
beforeAll(() => {
27+
global.ResizeObserver = class {
28+
observe() {}
29+
unobserve() {}
30+
disconnect() {}
31+
};
32+
});
33+
34+
describe('UserGrowth Component', () => {
35+
it('updates the selected year correctly on dropdown change', async () => {
36+
render(
37+
<MockedProvider mocks={mocks} addTypename={false}>
38+
<UserGrowth />
39+
</MockedProvider>,
40+
);
41+
42+
await waitFor(() => {
43+
const yearSelect = screen.getByLabelText(/Year:/i);
44+
expect(yearSelect).toBeInTheDocument();
45+
46+
fireEvent.change(yearSelect, { target: { value: '2023' } });
47+
48+
expect((yearSelect as HTMLSelectElement).value).toBe('2023');
49+
});
50+
});
51+
});

Diff for: tests/other-tests/TeamChart.test.tsx

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
import React from 'react';
2+
import TeamChart from '../../src/Chart/TeamChart';
3+
import '@testing-library/jest-dom';
4+
import { render, screen } from '@testing-library/react';
5+
import { Line } from 'react-chartjs-2';
6+
7+
jest.mock('react-chartjs-2', () => ({
8+
Line: jest.fn(() => <div data-testid="mock-chart" />),
9+
}));
10+
11+
describe('TeamChart Component', () => {
12+
const mockData: any = {
13+
daily: { '2024-01-01': { success: 10, failed: 2 } },
14+
weekly: { '2024-W01': { success: 50, failed: 5 } },
15+
monthly: { '2024-01': { success: 100, failed: 20 } },
16+
};
17+
18+
const mockCurrentTeam = [{ name: 'Team A' }];
19+
20+
it('renders the TeamChart component', () => {
21+
render(
22+
<TeamChart
23+
timeframe="daily"
24+
CurrentTeam={mockCurrentTeam}
25+
loginsbyDate={mockData}
26+
/>,
27+
);
28+
expect(screen.getByTestId('mock-chart')).toBeInTheDocument();
29+
});
30+
31+
it('organizes login data correctly for daily timeframe', () => {
32+
const { container } = render(
33+
<TeamChart
34+
timeframe="daily"
35+
CurrentTeam={mockCurrentTeam}
36+
loginsbyDate={mockData}
37+
/>,
38+
);
39+
40+
expect(Line).toHaveBeenCalledWith(
41+
expect.objectContaining({
42+
data: expect.objectContaining({
43+
labels: ['Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa', 'Su'],
44+
}),
45+
}),
46+
{},
47+
);
48+
});
49+
50+
it('renders weekly chart correctly', () => {
51+
render(
52+
<TeamChart
53+
timeframe="weekly"
54+
CurrentTeam={mockCurrentTeam}
55+
loginsbyDate={mockData}
56+
/>,
57+
);
58+
59+
expect(Line).toHaveBeenCalledWith(
60+
expect.objectContaining({
61+
data: expect.objectContaining({
62+
labels: expect.arrayContaining(['03', '06', '09']),
63+
}),
64+
}),
65+
{},
66+
);
67+
});
68+
69+
it('organizes login data correctly for monthly timeframe', () => {
70+
render(
71+
<TeamChart
72+
timeframe="monthly"
73+
CurrentTeam={mockCurrentTeam}
74+
loginsbyDate={mockData}
75+
/>,
76+
);
77+
78+
expect(Line).toHaveBeenCalledWith(
79+
expect.objectContaining({
80+
data: expect.objectContaining({
81+
labels: Array.from({ length: 12 }, (_, i) =>
82+
String(i + 1).padStart(2, '0'),
83+
),
84+
}),
85+
}),
86+
{},
87+
);
88+
});
89+
90+
it('uses the default timeframe of daily when not specified', () => {
91+
render(<TeamChart CurrentTeam={mockCurrentTeam} loginsbyDate={mockData} />);
92+
93+
expect(Line).toHaveBeenCalledWith(
94+
expect.objectContaining({
95+
data: expect.objectContaining({
96+
labels: ['Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa', 'Su'],
97+
}),
98+
}),
99+
{},
100+
);
101+
});
102+
});

0 commit comments

Comments
 (0)