Skip to content

Commit

Permalink
ft(626): testing Admin Dashboard
Browse files Browse the repository at this point in the history
  • Loading branch information
JacquelineTuyisenge committed Nov 30, 2024
1 parent 39839d4 commit e5f6641
Show file tree
Hide file tree
Showing 4 changed files with 226 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/Chart/TeamChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ function TeamChart({
weeklyData[isoWeekNumber - 1].failed += failed;
}
// Monthly data
if (!monthlyData[month]) {
monthlyData[month] = { month: month + 1, success: 0, failed: 0 };
}
monthlyData[month].success += success;
monthlyData[month].failed += failed;
}
Expand Down
70 changes: 70 additions & 0 deletions tests/other-tests/AdminTeamDetails.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import React from 'react';
import '@testing-library/jest-dom';
import { render, screen, fireEvent } from '@testing-library/react';
import TeamDetailsModal from '../../src/components/AdminTeamDetails';

const mockOnClose = jest.fn();

const mockTeams = [
{
name: 'Team A',
avgRatings: {
quality: '4.5',
quantity: '3.5',
professional_Skills: '5.0',
},
members: [
{ status: { status: 'active' } },
{ status: { status: 'suspended' } },
],
cohort: {
name: 'Cohort 1',
phase: { name: 'Phase 1' },
program: { name: 'Program A' },
},
ttl: { profile: { name: 'TTL Name' } },
},
];

const mockSelectedTeam = {
teams: 'Team A',
organization: 'Org Name',
};

beforeAll(() => {
global.ResizeObserver = class {
observe() {}
unobserve() {}
disconnect() {}
};
});

describe('TeamDetailsModal', () => {
it('handles tab switching between Overview and Logins', () => {
render(
<TeamDetailsModal
isOpen={true}
onClose={mockOnClose}
selectedteam={mockSelectedTeam}
Teams={mockTeams}
/>,
);

fireEvent.click(screen.getByText('Logins'));
expect(screen.getByText('Logins')).toHaveClass('text-primary');
});

it('calls onClose when the Close button is clicked', () => {
render(
<TeamDetailsModal
isOpen={true}
onClose={mockOnClose}
selectedteam={mockSelectedTeam}
Teams={mockTeams}
/>,
);

fireEvent.click(screen.getByText('Close'));
expect(mockOnClose).toHaveBeenCalled();
});
});
51 changes: 51 additions & 0 deletions tests/other-tests/LineChart.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { render, screen, fireEvent, waitFor } from '@testing-library/react';
import { MockedProvider } from '@apollo/client/testing';
import UserGrowth from '../../src/Chart/LineChart';
import GET_ROLE_QUERY from '../../src/containers/admin-dashBoard/GetRolesQuery';
import React from 'react';

const mockData = {
getAllUsers: [
{ createdAt: '1630454400000', updatedAt: '1630454400000' },
{ createdAt: '1633146400000', updatedAt: '1633146400000' },
],
};

const mocks = [
{
request: {
query: GET_ROLE_QUERY,
variables: { orgToken: 'mockToken' },
},
result: {
data: mockData,
},
},
];

beforeAll(() => {
global.ResizeObserver = class {
observe() {}
unobserve() {}
disconnect() {}
};
});

describe('UserGrowth Component', () => {
it('updates the selected year correctly on dropdown change', async () => {
render(
<MockedProvider mocks={mocks} addTypename={false}>
<UserGrowth />
</MockedProvider>,
);

await waitFor(() => {
const yearSelect = screen.getByLabelText(/Year:/i);
expect(yearSelect).toBeInTheDocument();

fireEvent.change(yearSelect, { target: { value: '2023' } });

expect((yearSelect as HTMLSelectElement).value).toBe('2023');
});
});
});
102 changes: 102 additions & 0 deletions tests/other-tests/TeamChart.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import React from 'react';
import TeamChart from '../../src/Chart/TeamChart';
import '@testing-library/jest-dom';
import { render, screen } from '@testing-library/react';
import { Line } from 'react-chartjs-2';

jest.mock('react-chartjs-2', () => ({
Line: jest.fn(() => <div data-testid="mock-chart" />),
}));

describe('TeamChart Component', () => {
const mockData: any = {
daily: { '2024-01-01': { success: 10, failed: 2 } },
weekly: { '2024-W01': { success: 50, failed: 5 } },
monthly: { '2024-01': { success: 100, failed: 20 } },
};

const mockCurrentTeam = [{ name: 'Team A' }];

it('renders the TeamChart component', () => {
render(
<TeamChart
timeframe="daily"
CurrentTeam={mockCurrentTeam}
loginsbyDate={mockData}
/>,
);
expect(screen.getByTestId('mock-chart')).toBeInTheDocument();
});

it('organizes login data correctly for daily timeframe', () => {
const { container } = render(
<TeamChart
timeframe="daily"
CurrentTeam={mockCurrentTeam}
loginsbyDate={mockData}
/>,
);

expect(Line).toHaveBeenCalledWith(
expect.objectContaining({
data: expect.objectContaining({
labels: ['Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa', 'Su'],
}),
}),
{},
);
});

it('renders weekly chart correctly', () => {
render(
<TeamChart
timeframe="weekly"
CurrentTeam={mockCurrentTeam}
loginsbyDate={mockData}
/>,
);

expect(Line).toHaveBeenCalledWith(
expect.objectContaining({
data: expect.objectContaining({
labels: expect.arrayContaining(['03', '06', '09']),
}),
}),
{},
);
});

it('organizes login data correctly for monthly timeframe', () => {
render(
<TeamChart
timeframe="monthly"
CurrentTeam={mockCurrentTeam}
loginsbyDate={mockData}
/>,
);

expect(Line).toHaveBeenCalledWith(
expect.objectContaining({
data: expect.objectContaining({
labels: Array.from({ length: 12 }, (_, i) =>
String(i + 1).padStart(2, '0'),
),
}),
}),
{},
);
});

it('uses the default timeframe of daily when not specified', () => {
render(<TeamChart CurrentTeam={mockCurrentTeam} loginsbyDate={mockData} />);

expect(Line).toHaveBeenCalledWith(
expect.objectContaining({
data: expect.objectContaining({
labels: ['Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa', 'Su'],
}),
}),
{},
);
});
});

0 comments on commit e5f6641

Please sign in to comment.