Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENT-7820 Learner Credit table sorting #373

Merged
merged 2 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
/* eslint-disable react/prop-types */
import { renderWithRouter } from '@edx/frontend-enterprise-utils';
import { screen, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { camelCaseObject } from '@edx/frontend-platform';
import { DashboardContext, initialStateValue } from '../../../testData/Dashboard';
import DashboardDataTable from '../DashboardDataTable';
import { sampleDataTableData } from '../../../testData/constants';
import SubsidyApiService from '../../../../data/services/SubsidyApiService';

// Mock the debounce function
jest.mock('lodash.debounce', () => jest.fn((fn) => fn));

// Mock the subsidy list
const mockGetAllSubsidiesData = sampleDataTableData(10);
jest.mock('../../../../data/services/SubsidyApiService', () => ({
getAllSubsidies: () => Promise.resolve({
data: mockGetAllSubsidiesData,
}),
}));
const apiMock = jest
.spyOn(SubsidyApiService, 'getAllSubsidies')
.mockImplementation(() => Promise.resolve({ data: mockGetAllSubsidiesData }));

// Mock the enterprise customers list
const mockCustomerData = camelCaseObject(mockGetAllSubsidiesData).results.map((subsidy) => ({
Expand Down Expand Up @@ -47,3 +47,24 @@ describe('DashboardDatatable', () => {
await waitFor(() => expect(screen.getByText('Enterprise Customer 1')).toBeTruthy());
});
});

describe('DashboardDatatable SortBy', () => {
it('sorts the data asc and desc', async () => {
renderWithRouter(<DashboardDatatableWrapper />);
const tableHeader = screen.getByText('Start date');

userEvent.click(tableHeader);
await waitFor(() => expect(apiMock).toHaveBeenCalledWith(
expect.objectContaining(
{ sortBy: 'active_datetime' },
),
));

userEvent.click(tableHeader);
await waitFor(() => expect(apiMock).toHaveBeenCalledWith(
expect.objectContaining(
{ sortBy: 'active_datetime' },
),
));
});
});
2 changes: 1 addition & 1 deletion src/Configuration/Provisioning/data/tests/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ describe('sortDatatableData', () => {
expect(sortDataTableData({ sortBy: {} })).toEqual(output);
});
it('returns a sort by expirationDateTime if isActive is passed as the id', () => {
const output = 'expirationDatetime';
const output = 'expiration_datetime';

// desc is true
expect(sortDataTableData({
Expand Down
10 changes: 7 additions & 3 deletions src/Configuration/Provisioning/data/utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useContextSelector } from 'use-context-selector';
import PropTypes from 'prop-types';
import { camelCaseObject, getConfig } from '@edx/frontend-platform';
import snakeCase from 'lodash.snakecase';
import dayjs from './dayjs';
import { ProvisioningContext } from '../ProvisioningContext';
import LmsApiService from '../../../data/services/EnterpriseApiService';
Expand Down Expand Up @@ -650,10 +651,13 @@ export function sortDataTableData({ sortBy }) {
if (!sortByObject) {
return null;
}
if (sortByObject.id === 'isActive') {
return sortByObject.desc ? '-expirationDatetime' : 'expirationDatetime';
let sortString = sortByObject.id;
if (sortString === 'isActive') {
sortString = 'expirationDatetime';
}
return sortByObject.desc ? `-${sortByObject.id}` : sortByObject.id;
sortString = snakeCase(sortString);
sortString = sortByObject.desc ? `-${sortString}` : sortString;
return sortString;
}

/**
Expand Down
3 changes: 1 addition & 2 deletions src/data/services/SubsidyApiService.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { getConfig, snakeCaseObject } from '@edx/frontend-platform';
import { getAuthenticatedHttpClient } from '@edx/frontend-platform/auth';
import snakeCase from 'lodash.snakecase';

class SubsidyApiService {
static apiClient = getAuthenticatedHttpClient;
Expand All @@ -14,7 +13,7 @@ class SubsidyApiService {
const subsidiesURL = `${getConfig().SUBSIDY_BASE_URL}/api/v1/subsidies/`;
const optionalUrlParams = new URLSearchParams(snakeCaseObject({
pageSize,
sortBy: sortBy ? snakeCase(sortBy) : 'uuid',
sortBy: sortBy || 'uuid',
...filteredData,
})).toString();
return SubsidyApiService.apiClient().get(`${subsidiesURL}?page=${pageIndex}&${optionalUrlParams}`);
Expand Down
Loading