-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1282 from openedx/ammar/add-leaderboard-datatable
add leaderboard datatable
- Loading branch information
Showing
6 changed files
with
288 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,69 @@ | ||
import { render } from '@testing-library/react'; | ||
import { | ||
render, screen, waitFor, within, | ||
} from '@testing-library/react'; | ||
import '@testing-library/jest-dom'; | ||
import { IntlProvider } from '@edx/frontend-platform/i18n'; | ||
import { QueryClientProvider } from '@tanstack/react-query'; | ||
import MockAdapter from 'axios-mock-adapter'; | ||
import axios from 'axios'; | ||
Check failure on line 8 in src/components/AdvanceAnalyticsV2/tabs/Leaderboard.test.jsx GitHub Actions / tests (18)
|
||
import { getAuthenticatedHttpClient } from '@edx/frontend-platform/auth'; | ||
import Leaderboard from './Leaderboard'; | ||
import { queryClient } from '../../test/testUtils'; | ||
import EnterpriseDataApiService from '../../../data/services/EnterpriseDataApiService'; | ||
|
||
jest.spyOn(EnterpriseDataApiService, 'fetchAdminAnalyticsTableData'); | ||
|
||
const axiosMock = new MockAdapter(axios); | ||
getAuthenticatedHttpClient.mockReturnValue(axios); | ||
|
||
const mockLeaderboardData = { | ||
next: null, | ||
previous: null, | ||
count: 3, | ||
num_pages: 1, | ||
current_page: 1, | ||
results: [ | ||
{ | ||
email: '[email protected]', | ||
daily_sessions: 74, | ||
learning_time_hours: 13.1, | ||
average_session_length: 1.8, | ||
course_completions: 3, | ||
}, | ||
{ | ||
email: '[email protected]', | ||
daily_sessions: 48, | ||
learning_time_hours: 131.9, | ||
average_session_length: 2.7, | ||
course_completions: 1, | ||
}, | ||
{ | ||
email: '[email protected]', | ||
daily_sessions: 92, | ||
learning_time_hours: 130, | ||
average_session_length: 1.4, | ||
course_completions: 3, | ||
}, | ||
], | ||
}; | ||
|
||
axiosMock.onAny().reply(200); | ||
axios.get = jest.fn(() => Promise.resolve({ data: mockLeaderboardData })); | ||
|
||
const TEST_ENTERPRISE_ID = '33ce6562-95e0-4ecf-a2a7-7d407eb96f69'; | ||
|
||
describe('Leaderboard Component', () => { | ||
test('renders all sections with correct classes and content', () => { | ||
const { container } = render( | ||
<IntlProvider locale="en"> | ||
<Leaderboard /> | ||
</IntlProvider>, | ||
<QueryClientProvider client={queryClient()}> | ||
<IntlProvider locale="en"> | ||
<Leaderboard | ||
enterpriseId={TEST_ENTERPRISE_ID} | ||
startDate="2021-01-01" | ||
endDate="2021-12-31" | ||
/> | ||
</IntlProvider>, | ||
</QueryClientProvider>, | ||
); | ||
|
||
const sections = [ | ||
|
@@ -25,4 +80,44 @@ describe('Leaderboard Component', () => { | |
expect(section).toHaveTextContent(subtitle); | ||
}); | ||
}); | ||
test('renders the table rows with correct values', async () => { | ||
render( | ||
<QueryClientProvider client={queryClient()}> | ||
<IntlProvider locale="en"> | ||
<Leaderboard | ||
enterpriseId={TEST_ENTERPRISE_ID} | ||
startDate="2021-01-01" | ||
endDate="2021-12-31" | ||
/> | ||
</IntlProvider>, | ||
</QueryClientProvider>, | ||
); | ||
|
||
// validate the header row | ||
const headers = screen.getAllByRole('columnheader'); | ||
expect(headers).toHaveLength(5); | ||
expect(headers[0]).toHaveTextContent('Email'); | ||
expect(headers[1]).toHaveTextContent('Learning Hours'); | ||
expect(headers[2]).toHaveTextContent('Daily Sessions'); | ||
expect(headers[3]).toHaveTextContent('Average Session Length (Hours)'); | ||
expect(headers[4]).toHaveTextContent('Course Completions'); | ||
|
||
await waitFor(() => { | ||
expect(EnterpriseDataApiService.fetchAdminAnalyticsTableData).toHaveBeenCalled(); | ||
|
||
// ensure the correct number of rows are rendered (including header row) | ||
const rows = screen.getAllByRole('row'); | ||
expect(rows).toHaveLength(3 + 1); // +1 for header row | ||
|
||
// validate content of each data row | ||
mockLeaderboardData.results.forEach((user, index) => { | ||
const rowCells = within(rows[index + 1]).getAllByRole('cell'); // Skip header row | ||
expect(rowCells[0]).toHaveTextContent(user.email); | ||
expect(rowCells[1]).toHaveTextContent(user.learning_time_hours); | ||
expect(rowCells[2]).toHaveTextContent(user.daily_sessions); | ||
expect(rowCells[3]).toHaveTextContent(user.average_session_length); | ||
expect(rowCells[4]).toHaveTextContent(user.course_completions); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters