Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…n-portal into mashal-m/react-upgrade-to-v17
  • Loading branch information
mashal-m committed Aug 24, 2023
2 parents 570f1b0 + 4266584 commit d8a0b20
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 23 deletions.
24 changes: 12 additions & 12 deletions src/components/EnterpriseSubsidiesContext/data/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,26 +41,26 @@ export const useEnterpriseOffers = ({ enablePortalLearnerCreditManagementScreen,
let activeSubsidyFound = false;
if (results.length !== 0) {
let subsidy = results[0];
const offerData = [];
let activeSubsidyData = {};
for (let i = 0; i < results.length; i++) {
subsidy = results[i];
activeSubsidyFound = source === 'ecommerceApi'
? subsidy.isCurrent
: subsidy.isActive;
if (activeSubsidyFound === true) {
break;
activeSubsidyData = {
id: subsidy.uuid || subsidy.id,
name: subsidy.title || subsidy.displayName,
start: subsidy.activeDatetime || subsidy.startDatetime,
end: subsidy.expirationDatetime || subsidy.endDatetime,
isCurrent: activeSubsidyFound,
};
offerData.push(activeSubsidyData);
setCanManageLearnerCredit(true);
}
}
if (activeSubsidyFound === true) {
const offerData = {
id: subsidy.uuid || subsidy.id,
name: subsidy.title || subsidy.displayName,
start: subsidy.activeDatetime || subsidy.startDatetime,
end: subsidy.expirationDatetime || subsidy.endDatetime,
isCurrent: activeSubsidyFound,
};
setOffers([offerData]);
setCanManageLearnerCredit(true);
}
setOffers(offerData);
}
} catch (error) {
logError(error);
Expand Down
24 changes: 14 additions & 10 deletions src/components/learner-credit-management/BudgetCard.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { useOfferRedemptions, useOfferSummary } from './data/hooks';
import LearnerCreditAggregateCards from './LearnerCreditAggregateCards';
import LearnerCreditAllocationTable from './LearnerCreditAllocationTable';
import { ROUTE_NAMES } from '../EnterpriseApp/data/constants';
import { EXEC_ED_OFFER_TYPE } from './data/constants';

const BudgetCard = ({
offer,
Expand Down Expand Up @@ -136,16 +137,19 @@ const BudgetCard = ({
</Stack>
</Card.Body>
</Card>
<Card
orientation="horizontal"
>
<Card.Body>
<Stack gap={4}>
{renderCardHeader('Executive Education')}
{renderCardSection(offerSummary?.remainingFunds, offerSummary?.redeemedFundsExecEd)}
</Stack>
</Card.Body>
</Card>
{offerSummary?.offerType === EXEC_ED_OFFER_TYPE
&& (
<Card
orientation="horizontal"
>
<Card.Body>
<Stack gap={4}>
{renderCardHeader('Executive Education')}
{renderCardSection(offerSummary?.remainingFunds, offerSummary?.redeemedFundsExecEd)}
</Stack>
</Card.Body>
</Card>
)}
</>
)
: (
Expand Down
2 changes: 2 additions & 0 deletions src/components/learner-credit-management/data/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ export const LOW_REMAINING_BALANCE_PERCENT_THRESHOLD = 0.75;
export const NO_BALANCE_REMAINING_DOLLAR_THRESHOLD = 100;

export const DATE_FORMAT = 'MMMM DD, YYYY';

export const EXEC_ED_OFFER_TYPE = 'learner_credit';
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { transformOfferSummary } from '../utils';
import { EXEC_ED_OFFER_TYPE } from '../constants';

describe('transformOfferSummary', () => {
it('should return null if there is no offerSummary', () => {
Expand All @@ -11,6 +12,7 @@ describe('transformOfferSummary', () => {
amountOfOfferSpent: 1.34,
remainingBalance: -0.34,
percentOfOfferSpent: 1.34,
offerType: EXEC_ED_OFFER_TYPE,
};

expect(transformOfferSummary(offerSummary)).toEqual({
Expand All @@ -20,6 +22,7 @@ describe('transformOfferSummary', () => {
redeemedFundsOcm: NaN,
remainingFunds: 0.0,
percentUtilized: 1.0,
offerType: EXEC_ED_OFFER_TYPE,
});
});

Expand All @@ -29,13 +32,15 @@ describe('transformOfferSummary', () => {
amountOfOfferSpent: 100,
remainingBalance: null,
percentOfOfferSpent: null,
offerType: 'Site',
};

expect(transformOfferSummary(offerSummary)).toEqual({
totalFunds: null,
redeemedFunds: 100,
remainingFunds: null,
percentUtilized: null,
offerType: 'Site',
});
});
});
2 changes: 2 additions & 0 deletions src/components/learner-credit-management/data/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export const transformOfferSummary = (offerSummary) => {
if (percentUtilized) {
percentUtilized = Math.min(percentUtilized, 1.0);
}
const { offerType } = offerSummary;

return {
totalFunds,
Expand All @@ -45,6 +46,7 @@ export const transformOfferSummary = (offerSummary) => {
redeemedFundsExecEd,
remainingFunds,
percentUtilized,
offerType,
};
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import '@testing-library/jest-dom/extend-expect';
import { IntlProvider } from '@edx/frontend-platform/i18n';
import BudgetCard from '../BudgetCard';
import { useOfferSummary, useOfferRedemptions } from '../data/hooks';
import { EXEC_ED_OFFER_TYPE } from '../data/constants';

jest.mock('../data/hooks');
useOfferSummary.mockReturnValue({
Expand Down Expand Up @@ -51,6 +52,7 @@ const mockOfferSummary = {
redeemedFunds: 200,
remainingFunds: 4800,
percentUtilized: 0.04,
offerType: EXEC_ED_OFFER_TYPE,
};

const BudgetCardWrapper = ({ ...rest }) => (
Expand All @@ -67,7 +69,7 @@ describe('<BudgetCard />', () => {
jest.clearAllMocks();
});

it('displays correctly', () => {
it('displays correctly for all offers', () => {
const mockOffer = {
id: mockEnterpriseOfferId,
name: mockOfferDisplayName,
Expand Down Expand Up @@ -105,6 +107,50 @@ describe('<BudgetCard />', () => {
expect(firstElementWithTestId).toHaveTextContent(formattedString);
});

it('displays correctly for Offer type Site', () => {
const mockOffer = {
id: mockEnterpriseOfferId,
name: mockOfferDisplayName,
start: '2022-01-01',
end: '2023-01-01',
};
const mockOfferRedemption = {
created: '2022-02-01',
enterpriseEnrollmentId: mockEnterpriseOfferEnrollmentId,
};
useOfferSummary.mockReturnValue({
isLoading: false,
offerSummary: {
totalFunds: 5000,
redeemedFunds: 200,
remainingFunds: 4800,
percentUtilized: 0.04,
offerType: 'Site',
},
});
useOfferRedemptions.mockReturnValue({
isLoading: false,
offerRedemptions: {
results: [mockOfferRedemption],
itemCount: 1,
pageCount: 1,
},
fetchOfferRedemptions: jest.fn(),
});
render(<BudgetCardWrapper
offer={mockOffer}
enterpriseUUID={enterpriseUUID}
enterpriseSlug={enterpriseId}
/>);
expect(screen.getByText('Open Courses Marketplace'));
expect(screen.queryByText('Executive Education')).not.toBeInTheDocument();
expect(screen.getByText(`$${mockOfferSummary.redeemedFunds.toLocaleString()}`));
const formattedString = `${dayjs(mockOffer.start).format('MMMM D, YYYY')} - ${dayjs(mockOffer.end).format('MMMM D, YYYY')}`;
const elementsWithTestId = screen.getAllByTestId('offer-date');
const firstElementWithTestId = elementsWithTestId[0];
expect(firstElementWithTestId).toHaveTextContent(formattedString);
});

it('displays table on clicking view budget', async () => {
const mockOffer = {
id: mockEnterpriseOfferId,
Expand Down

0 comments on commit d8a0b20

Please sign in to comment.