Skip to content

Commit

Permalink
fix: improved test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
mahamakifdar19 committed Sep 21, 2023
1 parent 4f107ed commit 5aecd8f
Show file tree
Hide file tree
Showing 4 changed files with 193 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -111,5 +111,28 @@ describe('<BudgetDetailPage />', () => {
expect(screen.getByText('Overview'));
expect(screen.getByText('No results found'));
});

it('displays loading message while loading data', async () => {
useOfferSummary.mockReturnValue({
isLoading: true,
offerSummary: null,
});
useOfferRedemptions.mockReturnValue({
isLoading: true,
offerRedemptions: {
itemCount: 0,
pageCount: 0,
results: [],
},
fetchOfferRedemptions: jest.fn(),
});

render(<BudgetDetailPageWrapper
enterpriseUUID={enterpriseUUID}
enterpriseSlug={enterpriseId}
/>);

expect(screen.getByText('loading'));
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,11 @@ describe('useOfferSummary', () => {
describe('useOfferRedemptions', () => {
it('should fetch enrollment/redemptions metadata for enterprise offer', async () => {
EnterpriseDataApiService.fetchCourseEnrollments.mockResolvedValueOnce({ data: mockOfferEnrollmentsResponse });
const budgetId = 'test-budget-id';
const { result, waitForNextUpdate } = renderHook(() => useOfferRedemptions(
TEST_ENTERPRISE_UUID,
mockEnterpriseOffer.id,
budgetId,
));

expect(result.current).toMatchObject({
Expand Down Expand Up @@ -122,6 +124,7 @@ describe('useOfferRedemptions', () => {
ordering: '-enrollment_date', // default sort order
searchAll: mockOfferEnrollments[0].user_email,
ignoreNullCourseListPrice: true,
budgetId,
};
expect(EnterpriseDataApiService.fetchCourseEnrollments).toHaveBeenCalledWith(
TEST_ENTERPRISE_UUID,
Expand All @@ -136,5 +139,7 @@ describe('useOfferRedemptions', () => {
isLoading: false,
fetchOfferRedemptions: expect.any(Function),
});

expect(expectedApiOptions.budgetId).toBe(budgetId);
});
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { transformOfferSummary } from '../utils';
import { transformOfferSummary, getBudgetStatus } from '../utils';
import { EXEC_ED_OFFER_TYPE } from '../constants';

describe('transformOfferSummary', () => {
Expand Down Expand Up @@ -51,4 +51,66 @@ describe('transformOfferSummary', () => {
budgetsSummary: [],
});
});

it('should handle when budgetsSummary is provided', () => {
const offerSummary = {
maxDiscount: 1000,
amountOfOfferSpent: 500,
remainingBalance: 500,
percentOfOfferSpent: 0.5,
offerType: 'Site',
offerId: '123',
budgets: [
{
id: 123,
start: '2022-01-01',
end: '2022-01-01',
available: 200,
spent: 100,
enterpriseSlug: 'test-enterprise',
}],
};

expect(transformOfferSummary(offerSummary)).toEqual({
totalFunds: 1000,
redeemedFunds: 500,
remainingFunds: 500,
percentUtilized: 0.5,
offerType: 'Site',
redeemedFundsExecEd: NaN,
redeemedFundsOcm: NaN,
offerId: '123',
budgetsSummary: [{
id: 123,
start: '2022-01-01',
end: '2022-01-01',
available: 200,
spent: 100,
enterpriseSlug: 'test-enterprise',
}],
});
});
});

describe('getBudgetStatus', () => {
it('should return "upcoming" when the current date is before the start date', () => {
const startDateStr = '2023-09-30';
const endDateStr = '2023-10-30';
const result = getBudgetStatus(startDateStr, endDateStr);
expect(result).toEqual('Upcoming');
});

it('should return "active" when the current date is between the start and end dates', () => {
const startDateStr = '2023-09-01';
const endDateStr = '2023-09-30';
const result = getBudgetStatus(startDateStr, endDateStr);
expect(result).toEqual('Active');
});

it('should return "expired" when the current date is after the end date', () => {
const startDateStr = '2023-08-01';
const endDateStr = '2023-08-31';
const result = getBudgetStatus(startDateStr, endDateStr);
expect(result).toEqual('Expired');
});
});
102 changes: 102 additions & 0 deletions src/components/learner-credit-management/tests/BudgetCard.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -114,5 +114,107 @@ describe('<BudgetCard />', () => {
const firstElementWithTestId = elementsWithTestId[0];
expect(firstElementWithTestId).toHaveTextContent(formattedString);
});

it('renders SubBudgetCard when offerType is ecommerceApi', () => {
const mockOffer = {
id: mockEnterpriseOfferId,
name: mockOfferDisplayName,
start: '2022-01-01',
end: '2023-01-01',
offerType: 'ecommerceApi',
};
const mockOfferRedemption = {
created: '2022-02-01',
enterpriseEnrollmentId: mockEnterpriseOfferEnrollmentId,
};
useOfferSummary.mockReturnValue({
isLoading: false,
offerSummary: {
totalFunds: 5000,
redeemedFunds: 200,
remainingFunds: 4800,
percentUtilized: 0.04,
offerType: 'learner_credit',
budgetsSummary: [
{
id: 123,
start: '2022-01-01',
end: '2022-01-01',
available: 200,
spent: 100,
enterpriseSlug: enterpriseId,
},
],
},
});
useOfferRedemptions.mockReturnValue({
isLoading: false,
offerRedemptions: {
results: [mockOfferRedemption],
itemCount: 1,
pageCount: 1,
},
fetchOfferRedemptions: jest.fn(),
});

render(<BudgetCardWrapper
offer={mockOffer}
enterpriseUUID={enterpriseUUID}
enterpriseSlug={enterpriseId}
/>);

expect(screen.getByTestId('view-budget')).toBeInTheDocument();
});

it('renders SubBudgetCard when offerType is not ecommerceApi', () => {
const mockOffer = {
id: mockEnterpriseOfferId,
name: mockOfferDisplayName,
start: '2022-01-01',
end: '2023-01-01',
offerType: 'otherOfferType',
};
const mockOfferRedemption = {
created: '2022-02-01',
enterpriseEnrollmentId: mockEnterpriseOfferEnrollmentId,
};
useOfferSummary.mockReturnValue({
isLoading: false,
offerSummary: {
totalFunds: 5000,
redeemedFunds: 200,
remainingFunds: 4800,
percentUtilized: 0.04,
offerType: 'learner_credit',
budgetsSummary: [
{
id: 123,
start: '2022-01-01',
end: '2022-01-01',
available: 200,
spent: 100,
enterpriseSlug: enterpriseId,
},
],
},
});
useOfferRedemptions.mockReturnValue({
isLoading: false,
offerRedemptions: {
results: [mockOfferRedemption],
itemCount: 1,
pageCount: 1,
},
fetchOfferRedemptions: jest.fn(),
});

render(<BudgetCardWrapper
offer={mockOffer}
enterpriseUUID={enterpriseUUID}
enterpriseSlug={enterpriseId}
/>);

expect(screen.getByTestId('view-budget')).toBeInTheDocument();
});
});
});

0 comments on commit 5aecd8f

Please sign in to comment.