Skip to content

Commit

Permalink
fix: ensure run-based assignments are taken into account on course pa…
Browse files Browse the repository at this point in the history
…ge redirect (#1196)
  • Loading branch information
adamstankiewicz authored Sep 18, 2024
1 parent 5bca07b commit f35543e
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 11 deletions.
71 changes: 63 additions & 8 deletions src/components/course/data/courseLoader.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ jest.mock('../../app/data', () => ({
}));

const mockCourseKey = 'edX+DemoX';
const mockCourseRunKey = 'course-v1:edX+DemoX+Demo_Course';
const mockSubscriptionCatalog = 'test-subscription-catalog-uuid';
const mockEnterpriseCustomer = enterpriseCustomerFactory();
extractEnterpriseCustomer.mockResolvedValue(mockEnterpriseCustomer);
Expand Down Expand Up @@ -74,39 +75,89 @@ describe('courseLoader', () => {
assignments: {
allocatedAssignments: [],
},
hasAssignmentForCourse: false,
},
{
hasCourseMetadata: true,
isAssignmentOnlyLearner: false,
assignments: {
allocatedAssignments: [{ contentKey: mockCourseKey }],
hasAllocatedAssignments: true,
allocatedAssignments: [{
contentKey: mockCourseKey,
parentContentKey: null,
isAssignedCourseRun: false,
}],
},
hasAssignmentForCourse: true,
},
{
hasCourseMetadata: true,
isAssignmentOnlyLearner: false,
assignments: {
hasAllocatedAssignments: true,
allocatedAssignments: [{
contentKey: mockCourseRunKey,
parentContentKey: mockCourseKey,
isAssignedCourseRun: true,
}],
},
hasAssignmentForCourse: true,
},
{
hasCourseMetadata: true,
isAssignmentOnlyLearner: true,
assignments: {
allocatedAssignments: [{ contentKey: mockCourseKey }],
},
hasAssignmentForCourse: true,
},
{
hasCourseMetadata: true,
isAssignmentOnlyLearner: true,
assignments: {
hasAllocatedAssignments: true,
allocatedAssignments: [{
contentKey: mockCourseRunKey,
parentContentKey: mockCourseKey,
isAssignedCourseRun: true,
}],
},
hasAssignmentForCourse: true,
},
{
hasCourseMetadata: true,
isAssignmentOnlyLearner: true,
assignments: {
allocatedAssignments: [{ contentKey: 'edX+DemoY' }],
},
hasAssignmentForCourse: false,
},
{
hasCourseMetadata: true,
isAssignmentOnlyLearner: true,
assignments: {
hasAllocatedAssignments: true,
allocatedAssignments: [{
contentKey: 'course-v1:edX+DemoY+Demo_Course',
parentContentKey: 'edX+DemoY',
isAssignedCourseRun: true,
}],
},
hasAssignmentForCourse: false,
},
{
hasCourseMetadata: false,
isAssignmentOnlyLearner: false,
assignments: {
allocatedAssignments: [],
},
hasAssignmentForCourse: false,
},
])('ensures the requisite course-related metadata data is resolved (%s)', async ({
hasCourseMetadata,
isAssignmentOnlyLearner,
assignments,
hasAssignmentForCourse,
}) => {
const mockCourseMetadata = {
key: mockCourseKey,
Expand All @@ -118,10 +169,19 @@ describe('courseLoader', () => {
}],
};

const mockAllocatedAssignments = assignments?.allocatedAssignments || [];

// When `ensureQueryData` is called with the course metadata
// query, ensure its mock return value is the course metadata
// for the dependent course redemption eligibility query.
const courseMetadataQuery = queryCourseMetadata(mockCourseKey);
let courseMetadataQuery = queryCourseMetadata(mockCourseKey);
const hasAssignedCourseRunsForCourse = mockAllocatedAssignments.some(
(assignment) => assignment.isAssignedCourseRun && assignment.parentContentKey === mockCourseKey,
);
if (hasAssignedCourseRunsForCourse) {
courseMetadataQuery = queryCourseMetadata(mockCourseKey, mockCourseRunKey);
}

when(mockQueryClient.ensureQueryData).calledWith(
expect.objectContaining({
queryKey: courseMetadataQuery.queryKey,
Expand All @@ -135,7 +195,6 @@ describe('courseLoader', () => {

// When `ensureQueryData` is called with the redeemable policies query,
// ensure its mock return value is valid.
const mockAllocatedAssignments = assignments?.allocatedAssignments || [];
when(mockQueryClient.ensureQueryData).calledWith(
expect.objectContaining({
queryKey: queryRedeemablePolicies({
Expand Down Expand Up @@ -268,10 +327,6 @@ describe('courseLoader', () => {
},
],
});

const hasAssignmentForCourse = !!assignments?.allocatedAssignments.some(
assignment => assignment.contentKey === mockCourseKey,
);
if (isAssignmentOnlyLearner && !hasAssignmentForCourse) {
expect(await screen.findByTestId('dashboard')).toBeInTheDocument();
} else {
Expand Down Expand Up @@ -353,7 +408,7 @@ describe('courseLoader', () => {
// Course metadata query
expect(mockQueryClient.ensureQueryData).toHaveBeenCalledWith(
expect.objectContaining({
queryKey: queryCourseMetadata(mockCourseMetadata.key).queryKey,
queryKey: courseMetadataQuery.queryKey,
queryFn: expect.any(Function),
}),
);
Expand Down
7 changes: 4 additions & 3 deletions src/components/course/data/courseLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,10 @@ const makeCourseLoader: Types.MakeRouteLoaderFunctionWithQueryClient = function
redeemableLearnerCreditPolicies,
hasCurrentEnterpriseOffers,
});
const isCourseAssigned = redeemableLearnerCreditPolicies.learnerContentAssignments.allocatedAssignments.some(
(assignment) => assignment.contentKey === courseKey,
);
const { isCourseAssigned } = determineAllocatedAssignmentsForCourse({
courseKey,
redeemableLearnerCreditPolicies,
});
// If learner is an assignment-only learner and is not assigned to the currently
// viewed course, redirect to the Dashboard page route.
if (isAssignmentOnlyLearner && !isCourseAssigned) {
Expand Down

0 comments on commit f35543e

Please sign in to comment.