Skip to content

Commit

Permalink
fix: ensure matching reason types and user messages on course page (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
adamstankiewicz authored Jan 10, 2025
1 parent d4f8be9 commit f586265
Show file tree
Hide file tree
Showing 3 changed files with 213 additions and 73 deletions.
1 change: 1 addition & 0 deletions src/components/course/data/hooks.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,7 @@ export const useUserSubsidyApplicableToCourse = () => {
userSubsidyApplicableToCourse?.availableCourseRuns || onlyUnrestrictedCourseRuns
);
}

return useMemo(() => ({
userSubsidyApplicableToCourse,
missingUserSubsidyReason,
Expand Down
173 changes: 139 additions & 34 deletions src/components/course/data/tests/utils.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
processCourseSubjects,
transformedCourseMetadata,
} from '../utils';
import { LICENSE_STATUS } from '../../../enterprise-user-subsidy/data/constants';

jest.mock('@edx/frontend-platform', () => ({
ensureConfig: jest.fn(),
Expand All @@ -39,6 +40,8 @@ jest.mock('@edx/frontend-platform', () => ({
}),
}));

const mockCatalogUuid = 'test-catalog-uuid';

describe('findCouponCodeForCourse', () => {
const couponCodes = [{
code: 'bearsRus',
Expand Down Expand Up @@ -441,69 +444,125 @@ describe('getMissingSubsidyReasonActions', () => {
});

describe('getSubscriptionDisabledEnrollmentReasonType', () => {
const mockCatalogUuid = 'test-catalog-uuid';

it.each([
// No subscriptions for customer. Expected: undefined
{
subscriptionLicense: {
subscriptionPlan: {
isCurrent: false,
},
customerAgreement: null,
catalogsWithCourse: [mockCatalogUuid],
subscriptionLicense: undefined,
hasEnterpriseAdminUsers: true,
expectedReasonType: undefined,
},
// No applicable subscription plan for the course, mismatching catalogs. Expected: undefined
{
customerAgreement: {
availableSubscriptionCatalogs: ['another-catalog-uuid'],
},
catalogsWithCourse: [mockCatalogUuid],
subscriptionLicense: undefined,
hasEnterpriseAdminUsers: true,
expectedReasonType: DISABLED_ENROLL_REASON_TYPES.SUBSCRIPTION_EXPIRED,
expectedReasonType: undefined,
},
// License not assigned. Expected: SUBSCRIPTION_LICENSE_NOT_ASSIGNED
{
customerAgreement: {
availableSubscriptionCatalogs: [mockCatalogUuid],
},
catalogsWithCourse: [mockCatalogUuid],
subscriptionLicense: undefined,
hasEnterpriseAdminUsers: true,
expectedReasonType: DISABLED_ENROLL_REASON_TYPES.SUBSCRIPTION_LICENSE_NOT_ASSIGNED,
},
// License not assigned, no admins. Expected: SUBSCRIPTION_LICENSE_NOT_ASSIGNED_NO_ADMINS
{
customerAgreement: {
availableSubscriptionCatalogs: [mockCatalogUuid],
},
catalogsWithCourse: [mockCatalogUuid],
subscriptionLicense: undefined,
hasEnterpriseAdminUsers: false,
expectedReasonType: DISABLED_ENROLL_REASON_TYPES.SUBSCRIPTION_LICENSE_NOT_ASSIGNED_NO_ADMINS,
},
])('handles no subscriptions and/or license: %s', ({
customerAgreement,
catalogsWithCourse,
subscriptionLicense,
hasEnterpriseAdminUsers,
expectedReasonType,
}) => {
const reasonType = getSubscriptionDisabledEnrollmentReasonType({
customerAgreement,
catalogsWithCourse,
subscriptionLicense,
hasEnterpriseAdminUsers,
});
expect(reasonType).toEqual(expectedReasonType);
});

it.each([
// Expired license, with admins. Expected: SUBSCRIPTION_EXPIRED
{
customerAgreement: {
availableSubscriptionCatalogs: [mockCatalogUuid],
},
catalogsWithCourse: [mockCatalogUuid],
subscriptionLicense: {
status: LICENSE_STATUS.ACTIVATED,
subscriptionPlan: {
isCurrent: false,
enterpriseCatalogUuid: mockCatalogUuid,
},
},
hasEnterpriseAdminUsers: false,
expectedReasonType: DISABLED_ENROLL_REASON_TYPES.SUBSCRIPTION_EXPIRED_NO_ADMINS,
catalogsWithCourse: [mockCatalogUuid],
hasEnterpriseAdminUsers: true,
expectedReasonType: DISABLED_ENROLL_REASON_TYPES.SUBSCRIPTION_EXPIRED,
},
// Expired license, without admins. Expected: SUBSCRIPTION_EXPIRED_NO_ADMINS
{
customerAgreement: {
availableSubscriptionCatalogs: [mockCatalogUuid],
},
catalogsWithCourse: [mockCatalogUuid],
subscriptionLicense: {
status: LICENSE_STATUS.ACTIVATED,
subscriptionPlan: {
isCurrent: false,
enterpriseCatalogUuid: mockCatalogUuid,
},
},
hasEnterpriseAdminUsers: true,
expectedReasonType: DISABLED_ENROLL_REASON_TYPES.SUBSCRIPTION_EXPIRED,
catalogsWithCourse: ['fake-catalog-uuid'],
hasEnterpriseAdminUsers: false,
expectedReasonType: DISABLED_ENROLL_REASON_TYPES.SUBSCRIPTION_EXPIRED_NO_ADMINS,
},
// Current license, with admins. Expected: undefined
{
subscriptionLicense: {
status: LICENSE_STATUS.ACTIVATED,
subscriptionPlan: {
isCurrent: true,
enterpriseCatalogUuid: mockCatalogUuid,
},
},
hasEnterpriseAdminUsers: true,
expectedReasonType: undefined,
catalogsWithCourse: [mockCatalogUuid],
},
// Current license, no admins. Expected: undefined
{
subscriptionLicense: {
status: LICENSE_STATUS.ACTIVATED,
subscriptionPlan: {
isCurrent: true,
enterpriseCatalogUuid: mockCatalogUuid,
},
},
hasEnterpriseAdminUsers: true,
hasEnterpriseAdminUsers: false,
expectedReasonType: undefined,
catalogsWithCourse: [mockCatalogUuid],
},
])('handles expired subscription: %s', ({
customerAgreement,
catalogsWithCourse,
subscriptionLicense,
hasEnterpriseAdminUsers,
expectedReasonType,
catalogsWithCourse,
}) => {
const customerAgreement = {
availableSubscriptionCatalogs: [],
};

const reasonType = getSubscriptionDisabledEnrollmentReasonType({
customerAgreement,
catalogsWithCourse,
Expand Down Expand Up @@ -551,7 +610,7 @@ describe('getSubscriptionDisabledEnrollmentReasonType', () => {
},
},
],
availableSubscriptionCatalogs: [],
availableSubscriptionCatalogs: [mockCatalogUuid],
};

const reasonType = getSubscriptionDisabledEnrollmentReasonType({
Expand Down Expand Up @@ -760,8 +819,6 @@ describe('isCurrentCoupon', () => {
});

describe('getCouponCodesDisabledEnrollmentReasonType', () => {
const testCatalogUuid = 'test-catalog-uuid';

afterEach(() => {
MockDate.reset();
});
Expand All @@ -776,9 +833,9 @@ describe('getCouponCodesDisabledEnrollmentReasonType', () => {
},
{
todaysDate: '2023-08-02T12:00:00Z',
catalogsWithCourse: [testCatalogUuid],
catalogsWithCourse: [mockCatalogUuid],
couponsOverview: [{
enterpriseCatalogUuid: testCatalogUuid,
enterpriseCatalogUuid: mockCatalogUuid,
startDate: '2023-08-01T12:00:00Z',
endDate: '2024-08-01T12:00:00Z',
numUnassigned: 100,
Expand All @@ -788,9 +845,9 @@ describe('getCouponCodesDisabledEnrollmentReasonType', () => {
},
{
todaysDate: '2023-08-02T12:00:00Z',
catalogsWithCourse: [testCatalogUuid],
catalogsWithCourse: [mockCatalogUuid],
couponsOverview: [{
enterpriseCatalogUuid: testCatalogUuid,
enterpriseCatalogUuid: mockCatalogUuid,
startDate: '2023-08-01T12:00:00Z',
endDate: '2024-08-01T12:00:00Z',
numUnassigned: 0,
Expand All @@ -800,9 +857,9 @@ describe('getCouponCodesDisabledEnrollmentReasonType', () => {
},
{
todaysDate: '2023-10-31T12:00:00Z',
catalogsWithCourse: [testCatalogUuid],
catalogsWithCourse: [mockCatalogUuid],
couponsOverview: [{
enterpriseCatalogUuid: testCatalogUuid,
enterpriseCatalogUuid: mockCatalogUuid,
startDate: '2023-08-01T12:00:00Z',
endDate: '2023-08-31T12:00:00Z',
numUnassigned: 100,
Expand Down Expand Up @@ -832,7 +889,10 @@ describe('getCouponCodesDisabledEnrollmentReasonType', () => {

describe('getMissingApplicableSubsidyReason', () => {
const baseMockData = {
enterpriseAdminUsers: [{}],
enterpriseAdminUsers: [{
email: '[email protected]',
lmsUserId: 3,
}],
catalogsWithCourse: [],
couponsOverview: {},
customerAgreement: {
Expand All @@ -843,6 +903,7 @@ describe('getMissingApplicableSubsidyReason', () => {
missingSubsidyAccessPolicyReason: null,
enterpriseOffers: [],
};

it('returns NO_SUBSIDY_NO_ADMINS if there are no admins', () => {
const result = getMissingApplicableSubsidyReason({
...baseMockData,
Expand All @@ -851,6 +912,11 @@ describe('getMissingApplicableSubsidyReason', () => {
expect(result.reason).toEqual(DISABLED_ENROLL_REASON_TYPES.NO_SUBSIDY_NO_ADMINS);
});

it('returns NO_SUBSIDY if there are admins but no subsidy', () => {
const result = getMissingApplicableSubsidyReason(baseMockData);
expect(result.reason).toEqual(DISABLED_ENROLL_REASON_TYPES.NO_SUBSIDY);
});

it('returns CONTENT_NOT_IN_CATALOG if containsContentItems is false', () => {
const result = getMissingApplicableSubsidyReason({ ...baseMockData, containsContentItems: false });
expect(result.reason).toEqual(DISABLED_ENROLL_REASON_TYPES.CONTENT_NOT_IN_CATALOG);
Expand All @@ -865,12 +931,12 @@ describe('getMissingApplicableSubsidyReason', () => {
};
const mockData = {
...baseMockData,
catalogsWithCourse: ['test-catalog-uuid'],
catalogsWithCourse: [mockCatalogUuid],
couponsOverview:
{
data: {
results: [{
enterpriseCatalogUuid: 'test-catalog-uuid',
enterpriseCatalogUuid: mockCatalogUuid,
numUnassigned: 100,
...couponProperties,
}],
Expand All @@ -884,17 +950,43 @@ describe('getMissingApplicableSubsidyReason', () => {
it('returns SUBSCRIPTION_EXPIRED if there is an expired subscription', () => {
const mockData = {
...baseMockData,
customerAgreement: {
availableSubscriptionCatalogs: [mockCatalogUuid],
},
subscriptionLicense: {
// Subscription license is activated, but expired.
status: LICENSE_STATUS.ACTIVATED,
subscriptionPlan: {
isCurrent: false,
enterpriseCatalogUuid: mockCatalogUuid,
},
},
catalogsWithCourse: ['test-catalog-uuid'],
catalogsWithCourse: [mockCatalogUuid],
};
const result = getMissingApplicableSubsidyReason(mockData);
expect(result.reason).toEqual(DISABLED_ENROLL_REASON_TYPES.SUBSCRIPTION_EXPIRED);
});

it('returns SUBSCRIPTION_DEACTIVATED if the subscription is deactivated', () => {
const mockData = {
...baseMockData,
customerAgreement: {
availableSubscriptionCatalogs: [mockCatalogUuid],
},
subscriptionLicense: {
// Subscription license is current but deactivated (revoked).
status: LICENSE_STATUS.REVOKED,
subscriptionPlan: {
isCurrent: true,
enterpriseCatalogUuid: mockCatalogUuid,
},
},
catalogsWithCourse: [mockCatalogUuid],
};
const result = getMissingApplicableSubsidyReason(mockData);
expect(result.reason).toEqual(DISABLED_ENROLL_REASON_TYPES.SUBSCRIPTION_DEACTIVATED);
});

it('returns ENTERPRISE_OFFER_EXPIRED if there is an expired enterprise offer', () => {
const enterpriseOfferProperties = {
id: 1,
Expand All @@ -910,6 +1002,19 @@ describe('getMissingApplicableSubsidyReason', () => {
const result = getMissingApplicableSubsidyReason(mockData);
expect(result.reason).toEqual(DISABLED_ENROLL_REASON_TYPES.ENTERPRISE_OFFER_EXPIRED);
});

it('returns MISSING_SUBSIDY_ACCESS_POLICY if missingSubsidyAccessPolicyReason is present', () => {
const mockData = {
...baseMockData,
missingSubsidyAccessPolicyReason: {
reason: DISABLED_ENROLL_REASON_TYPES.MISSING_SUBSIDY_ACCESS_POLICY,
userMessage: 'Custom user message.',
},
};
const result = getMissingApplicableSubsidyReason(mockData);
expect(result.reason).toEqual(DISABLED_ENROLL_REASON_TYPES.MISSING_SUBSIDY_ACCESS_POLICY);
expect(result.userMessage).toEqual('Custom user message.');
});
});

describe('transformedCourseMetadata', () => {
Expand Down
Loading

0 comments on commit f586265

Please sign in to comment.