generated from openedx/frontend-template-application
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add license table in home page (#28)
- Loading branch information
Showing
14 changed files
with
357 additions
and
5 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
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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
@import "assets/colors.scss"; | ||
|
||
.license-section { | ||
background-color: $color-white; | ||
padding: 2rem; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import MockAdapter from 'axios-mock-adapter'; | ||
import { getAuthenticatedHttpClient } from '@edx/frontend-platform/auth'; | ||
import { initializeMockApp } from '@edx/frontend-platform/testing'; | ||
import { fetchLicensesData } from 'features/Dashboard/data'; | ||
import { executeThunk } from 'test-utils'; | ||
import { initializeStore } from 'store'; | ||
|
||
let axiosMock; | ||
let store; | ||
|
||
describe('Dashboard redux tests', () => { | ||
beforeEach(() => { | ||
initializeMockApp({ | ||
authenticatedUser: { | ||
userId: 1, | ||
username: 'testuser', | ||
administrator: true, | ||
roles: [], | ||
}, | ||
}); | ||
axiosMock = new MockAdapter(getAuthenticatedHttpClient()); | ||
|
||
store = initializeStore(); | ||
}); | ||
|
||
afterEach(() => { | ||
axiosMock.reset(); | ||
}); | ||
|
||
test('successful fetch licenses data', async () => { | ||
const licensesApiUrl = `${process.env.COURSE_OPERATIONS_API_V2_BASE_URL}/license-pool/?limit=false&institution_id=1`; | ||
const mockResponse = [ | ||
{ | ||
licenseName: 'License Name 1', | ||
purchasedSeats: 20, | ||
numberOfStudents: 6, | ||
numberOfPendingStudents: 11, | ||
}, | ||
{ | ||
licenseName: 'License Name 2', | ||
purchasedSeats: 10, | ||
numberOfStudents: 1, | ||
numberOfPendingStudents: 5, | ||
}, | ||
]; | ||
axiosMock.onGet(licensesApiUrl) | ||
.reply(200, mockResponse); | ||
|
||
expect(store.getState().dashboard.tableLicense.status) | ||
.toEqual('loading'); | ||
|
||
await executeThunk(fetchLicensesData(1), store.dispatch, store.getState); | ||
|
||
expect(store.getState().dashboard.tableLicense.data) | ||
.toEqual(mockResponse); | ||
|
||
expect(store.getState().dashboard.tableLicense.status) | ||
.toEqual('success'); | ||
}); | ||
|
||
test('failed fetch licenses data', async () => { | ||
const licensesApiUrl = `${process.env.COURSE_OPERATIONS_API_V2_BASE_URL}/license-pool/?limit=false&institution_id=1`; | ||
axiosMock.onGet(licensesApiUrl) | ||
.reply(500); | ||
|
||
expect(store.getState().dashboard.tableLicense.status) | ||
.toEqual('loading'); | ||
|
||
await executeThunk(fetchLicensesData(1), store.dispatch, store.getState); | ||
|
||
expect(store.getState().dashboard.tableLicense.data) | ||
.toEqual([]); | ||
|
||
expect(store.getState().dashboard.tableLicense.status) | ||
.toEqual('error'); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { reducer } from 'features/Dashboard/data/slice'; | ||
export { fetchLicensesData } from 'features/Dashboard/data/thunks'; |
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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* eslint-disable no-param-reassign */ | ||
import { createSlice } from '@reduxjs/toolkit'; | ||
import { RequestStatus, initialStateService } from 'features/constants'; | ||
|
||
const initialState = { | ||
tableLicense: { | ||
...initialStateService, | ||
}, | ||
}; | ||
|
||
export const dashboardSlice = createSlice({ | ||
name: 'dashboard', | ||
initialState, | ||
reducers: { | ||
fetchLicensesDataRequest: (state) => { | ||
state.tableLicense.status = RequestStatus.LOADING; | ||
}, | ||
fetchLicensesDataSuccess: (state, { payload }) => { | ||
state.tableLicense.status = RequestStatus.SUCCESS; | ||
state.tableLicense.data = payload; | ||
}, | ||
fetchLicensesDataFailed: (state) => { | ||
state.tableLicense.status = RequestStatus.ERROR; | ||
}, | ||
}, | ||
}); | ||
|
||
export const { | ||
fetchLicensesDataRequest, | ||
fetchLicensesDataSuccess, | ||
fetchLicensesDataFailed, | ||
} = dashboardSlice.actions; | ||
|
||
export const { reducer } = dashboardSlice; |
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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { logError } from '@edx/frontend-platform/logging'; | ||
import { camelCaseObject } from '@edx/frontend-platform'; | ||
|
||
import { getLicensesByInstitution } from 'features/Common/data/api'; | ||
import { | ||
fetchLicensesDataRequest, | ||
fetchLicensesDataSuccess, | ||
fetchLicensesDataFailed, | ||
} from 'features/Dashboard/data/slice'; | ||
|
||
function fetchLicensesData(id) { | ||
return async (dispatch) => { | ||
dispatch(fetchLicensesDataRequest()); | ||
try { | ||
const response = camelCaseObject(await getLicensesByInstitution(id, false)); | ||
dispatch(fetchLicensesDataSuccess(response.data)); | ||
} catch (error) { | ||
dispatch(fetchLicensesDataFailed()); | ||
logError(error); | ||
} | ||
}; | ||
} | ||
|
||
export { | ||
fetchLicensesData, | ||
}; |
27 changes: 27 additions & 0 deletions
27
src/features/Licenses/LicensesTable/_test_/columns.test.jsx
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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { columns } from 'features/Licenses/LicensesTable/columns'; | ||
|
||
describe('columns in license table', () => { | ||
test('returns an array of columns with correct properties', () => { | ||
expect(columns).toBeInstanceOf(Array); | ||
expect(columns).toHaveLength(4); | ||
|
||
const [ | ||
nameColumn, | ||
purchasedColumn, | ||
enrolledColumn, | ||
remainingColumn, | ||
] = columns; | ||
|
||
expect(nameColumn).toHaveProperty('Header', 'License Pool'); | ||
expect(nameColumn).toHaveProperty('accessor', 'licenseName'); | ||
|
||
expect(purchasedColumn).toHaveProperty('Header', 'Purchased'); | ||
expect(purchasedColumn).toHaveProperty('accessor', 'purchasedSeats'); | ||
|
||
expect(enrolledColumn).toHaveProperty('Header', 'Enrolled'); | ||
expect(enrolledColumn).toHaveProperty('accessor', 'numberOfStudents'); | ||
|
||
expect(remainingColumn).toHaveProperty('Header', 'Remaining'); | ||
expect(remainingColumn).toHaveProperty('accessor', 'numberOfPendingStudents'); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import React from 'react'; | ||
import '@testing-library/jest-dom'; | ||
import { render, screen } from '@testing-library/react'; | ||
|
||
import LicensesTable from 'features/Licenses/LicensesTable'; | ||
import { columns } from 'features/Licenses/LicensesTable/columns'; | ||
|
||
describe('Licenses Table', () => { | ||
test('renders Licenses table without data', () => { | ||
render(<LicensesTable data={[]} count={0} columns={[]} />); | ||
const emptyTableText = screen.getByText('No licenses found.'); | ||
expect(emptyTableText).toBeInTheDocument(); | ||
}); | ||
|
||
test('renders Licenses table with data', () => { | ||
const data = [ | ||
{ | ||
licenseName: 'License Name 1', | ||
purchasedSeats: 20, | ||
numberOfStudents: 6, | ||
numberOfPendingStudents: 11, | ||
}, | ||
{ | ||
licenseName: 'License Name 2', | ||
purchasedSeats: 10, | ||
numberOfStudents: 1, | ||
numberOfPendingStudents: 5, | ||
}, | ||
]; | ||
|
||
const component = render( | ||
<LicensesTable data={data} count={data.length} columns={columns} />, | ||
); | ||
|
||
// Check if the table rows are present | ||
const tableRows = screen.getAllByRole('row'); | ||
expect(tableRows).toHaveLength(data.length + 1); // Data rows + 1 header row | ||
expect(component.container).toHaveTextContent('License Name 1'); | ||
expect(component.container).toHaveTextContent('License Name 2'); | ||
expect(component.container).toHaveTextContent('20'); | ||
expect(component.container).toHaveTextContent('10'); | ||
expect(component.container).toHaveTextContent('6'); | ||
expect(component.container).toHaveTextContent('1'); | ||
expect(component.container).toHaveTextContent('11'); | ||
expect(component.container).toHaveTextContent('5'); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
const columns = [ | ||
{ | ||
Header: 'License Pool', | ||
accessor: 'licenseName', | ||
}, | ||
{ | ||
Header: 'Purchased', | ||
accessor: 'purchasedSeats', | ||
}, | ||
{ | ||
Header: 'Enrolled', | ||
accessor: 'numberOfStudents', | ||
}, | ||
{ | ||
Header: 'Remaining', | ||
accessor: 'numberOfPendingStudents', | ||
}, | ||
]; | ||
|
||
export { columns }; |
Oops, something went wrong.