Skip to content

Commit

Permalink
ENT-7820 Learner Credit table sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
jono-booth committed Jan 8, 2024
1 parent f00e83f commit ed84bd3
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 10 deletions.
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' },
),
));
});
});
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

0 comments on commit ed84bd3

Please sign in to comment.