Skip to content

Commit

Permalink
chore: update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
brobro10000 committed Aug 8, 2024
1 parent 7b692e9 commit 45a66f4
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 4 deletions.
86 changes: 85 additions & 1 deletion src/components/app/data/hooks/useCourseMetadata.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import { queryClient } from '../../../../utils/tests';
import { fetchCourseMetadata } from '../services';
import useLateEnrollmentBufferDays from './useLateEnrollmentBufferDays';
import useCourseMetadata from './useCourseMetadata';
import useRedeemablePolicies from './useRedeemablePolicies';

jest.mock('./useEnterpriseCustomer');
jest.mock('./useLateEnrollmentBufferDays');
jest.mock('./useRedeemablePolicies');

jest.mock('../services', () => ({
...jest.requireActual('../services'),
Expand All @@ -27,10 +29,33 @@ const mockCourseMetadata = {
availability: 'Current',
enrollmentStart: dayjs().add(10, 'day').toISOString(),
enrollmentEnd: dayjs().add(15, 'day').toISOString(),
key: 'course-v1:edX+DemoX+2T2020',
isEnrollable: true,
}],
};

const mockBaseRedeemablePolicies = {
redeemablePolicies: [],
expiredPolicies: [],
unexpiredPolicies: [],
learnerContentAssignments: {
assignments: [],
hasAssignments: false,
allocatedAssignments: [],
hasAllocatedAssignments: false,
acceptedAssignments: [],
hasAcceptedAssignments: false,
canceledAssignments: [],
hasCanceledAssignments: false,
expiredAssignments: [],
hasExpiredAssignments: false,
erroredAssignments: [],
hasErroredAssignments: false,
assignmentsForDisplay: [],
hasAssignmentsForDisplay: false,
},
};

describe('useCourseMetadata', () => {
const Wrapper = ({ children }) => (
<QueryClientProvider client={queryClient()}>
Expand All @@ -42,7 +67,8 @@ describe('useCourseMetadata', () => {
fetchCourseMetadata.mockResolvedValue(mockCourseMetadata);
useParams.mockReturnValue({ courseKey: 'edX+DemoX' });
useLateEnrollmentBufferDays.mockReturnValue(undefined);
useSearchParams.mockReturnValue([new URLSearchParams({ course_run_key: 'course-v1:edX+DemoX+T2024' })]);
useSearchParams.mockReturnValue([new URLSearchParams({ course_run_key: 'course-v1:edX+DemoX+2T2024' })]);
useRedeemablePolicies.mockReturnValue({ data: mockBaseRedeemablePolicies });
});
it('should handle resolved value correctly with no select function passed', async () => {
const { result, waitForNextUpdate } = renderHook(() => useCourseMetadata(), { wrapper: Wrapper });
Expand Down Expand Up @@ -107,4 +133,62 @@ describe('useCourseMetadata', () => {
}),
);
});
// TODO: verify test works bc it passes no matter what value I put for the course run key.
it('should return available course run corresponding to allocated course run', async () => {
useParams.mockReturnValue({ courseKey: 'edX+DemoX' });
useLateEnrollmentBufferDays.mockReturnValue(undefined);
useSearchParams.mockReturnValue([new URLSearchParams({})]);

const mockCourseRuns = [
...mockCourseMetadata.courseRuns,
{
...mockCourseMetadata.courseRuns[0],
key: 'course-v1:edX+DemoX+2018',
},
];

const mockAllocatedAssignments = [{
parentContentKey: 'edX+DemoX',
contentKey: 'course-v1:edX+DemoX+2T2020',
isAssignedCourseRun: true,
},
{
parentContentKey: 'edX+DemoX',
contentKey: 'course-v1:edX+DemoX+2018',
isAssignedCourseRun: true,
}, {
parentContentKey: null,
contentKey: 'edX+DemoX',
isAssignedCourseRun: false,
}];
const mockLearnerContentAssignments = {
allocatedAssignments: mockAllocatedAssignments,
hasAllocatedAssignments: mockAllocatedAssignments.length > 0,
};

fetchCourseMetadata.mockResolvedValue({ ...mockCourseMetadata, courseRuns: mockCourseRuns });
useRedeemablePolicies.mockReturnValue({
data: {
...mockBaseRedeemablePolicies,
learnerContentAssignments: {
...mockBaseRedeemablePolicies.learnerContentAssignments, ...mockLearnerContentAssignments,
},
},
});

const { result, waitForNextUpdate } = renderHook(() => useCourseMetadata(), { wrapper: Wrapper });
await waitForNextUpdate();

expect(result.current).toEqual(
expect.objectContaining({
data: {
...mockCourseMetadata,
courseRuns: mockCourseRuns,
availableCourseRuns: mockCourseRuns,
},
isLoading: false,
isFetching: false,
}),
);
});
});
2 changes: 0 additions & 2 deletions src/components/app/data/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -753,12 +753,10 @@ export function filterCourseMetadataByAllocationCourseRun({
courseKey,
}) {
const { learnerContentAssignments } = redeemableLearnerCreditPolicies;

if (learnerContentAssignments.hasAllocatedAssignments) {
const allocatedCourseRunAssignmentKeys = learnerContentAssignments.allocatedAssignments.filter(
(assignment) => assignment?.isAssignedCourseRun && assignment?.parentContentKey === courseKey,
).map(assignment => assignment.contentKey);

const hasAssignedCourseRuns = allocatedCourseRunAssignmentKeys.length > 0;
const hasMultipleAssignedCourseRuns = allocatedCourseRunAssignmentKeys.length > 1;
return {
Expand Down
2 changes: 1 addition & 1 deletion src/components/course/data/courseLoader.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ describe('courseLoader', () => {
expectedQueryCount = 15;
}
} else {
expectedQueryCount = 13;
expectedQueryCount = 14;
}
expect(mockQueryClient.ensureQueryData).toHaveBeenCalledTimes(expectedQueryCount);

Expand Down

0 comments on commit 45a66f4

Please sign in to comment.