Skip to content

Commit

Permalink
fix: Update failing tests 2 (#1047)
Browse files Browse the repository at this point in the history
  • Loading branch information
brobro10000 authored Apr 12, 2024
1 parent c70e909 commit 33f06e0
Show file tree
Hide file tree
Showing 31 changed files with 591 additions and 547 deletions.
1 change: 1 addition & 0 deletions src/components/app/data/hooks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,4 @@ export { default as useLearnerPathwayProgressData } from './useLearnerPathwayPro
export { default as useHasAvailableSubsidiesOrRequests } from './useHasAvailableSubsidiesOrRequests';
export { default as useAcademies } from './useAcademies';
export { default as useAcademyDetails } from './useAcademyDetails';
export { default as usePassLearnerCsodParams } from './usePassLearnerCsodParams';
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,10 @@ jest.mock('./useContentHighlightsConfiguration', () => jest.fn().mockReturnValue
}));
jest.mock('./useIsAssignmentsOnlyLearner', () => jest.fn().mockReturnValue(false));

const appContextValue = {
authenticatedUser: authenticatedUserFactory(),
};
const mockAuthenticatedUser = authenticatedUserFactory();

const wrapper = ({ children }) => (
<AppContext.Provider value={appContextValue}>
<AppContext.Provider value={{ authenticatedUser: mockAuthenticatedUser }}>
{children}
</AppContext.Provider>
);
Expand Down
2 changes: 1 addition & 1 deletion src/components/course/course-header/CourseHeader.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ import {
useEnterpriseCustomer,
useEnterpriseCustomerContainsContent,
useIsAssignmentsOnlyLearner,
usePassLearnerCsodParams,
} from '../../app/data';
import usePassLearnerCsodParams from '../../app/data/hooks/usePassLearnerCsodParams';

const CourseHeader = () => {
const location = useLocation();
Expand Down
1 change: 1 addition & 0 deletions src/components/course/course-header/CourseRunCards.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const CourseRunCards = () => {
const { data: { catalogList } } = useEnterpriseCustomerContainsContent([courseKey]);
const { data: { enterpriseCourseEnrollments } } = useEnterpriseCourseEnrollments();
const { data: userEntitlements } = useUserEntitlements();

return (
<CardGrid
columnSizes={{ xs: 12, md: 6, lg: 5 }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ const CourseRunCard = ({
defaultMessage: 'Enroll',
description: 'Default button label for enrolling in a course run.',
});

const location = useLocation();
const { data: enterpriseCustomer } = useEnterpriseCustomer();
const { data: courseMetadata } = useCourseMetadata();
Expand Down
10 changes: 7 additions & 3 deletions src/components/course/course-header/tests/CourseHeader.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import userEvent from '@testing-library/user-event';
import '@testing-library/jest-dom/extend-expect';

import { IntlProvider } from '@edx/frontend-platform/i18n';
import { AppContext } from '@edx/frontend-platform/react';
import CourseHeader from '../CourseHeader';

import { COURSE_PACING_MAP } from '../../data/constants';
Expand All @@ -18,7 +19,7 @@ import {
useIsAssignmentsOnlyLearner,
} from '../../../app/data';
import { renderWithRouterProvider } from '../../../../utils/tests';
import { enterpriseCustomerFactory } from '../../../app/data/services/data/__factories__';
import { authenticatedUserFactory, enterpriseCustomerFactory } from '../../../app/data/services/data/__factories__';
import { useIsCourseAssigned } from '../../data';

jest.mock('../CourseRunCards', () => function CourseRunCards() {
Expand All @@ -40,6 +41,7 @@ jest.mock('../../../app/data', () => ({
useEnterpriseCustomerContainsContent: jest.fn(),
useIsAssignmentsOnlyLearner: jest.fn(),
useCourseReviews: jest.fn(),
usePassLearnerCsodParams: jest.fn(),
}));

jest.mock('../../data', () => ({
Expand Down Expand Up @@ -115,10 +117,12 @@ const mockEnterpriseCourseEnrollment = {
linkToCourse: 'http://course.url',
mode: 'verified',
};

const mockAuthenticatedUser = authenticatedUserFactory();
const CourseHeaderWrapper = () => (
<IntlProvider locale="en">
<CourseHeader />
<AppContext.Provider value={{ authenticatedUser: mockAuthenticatedUser }}>
<CourseHeader />
</AppContext.Provider>
</IntlProvider>
);

Expand Down
136 changes: 103 additions & 33 deletions src/components/course/course-header/tests/CourseRunCard.test.jsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,47 @@
import React from 'react';
import { screen, render } from '@testing-library/react';
import { screen } from '@testing-library/react';
import '@testing-library/jest-dom/extend-expect';

import { AppContext } from '@edx/frontend-platform/react';
import CourseRunCard from '../CourseRunCard';
import { CourseContext } from '../../CourseContextProvider';
import { useCourseRunCardData } from '../data';
import { findUserEnrollmentForCourseRun } from '../../data/utils';
import { authenticatedUserFactory, enterpriseCustomerFactory } from '../../../app/data/services/data/__factories__';
import {
useCouponCodes,
useCourseMetadata,
useCourseRedemptionEligibility,
useEnterpriseCourseEnrollments,
useEnterpriseCustomer,
useEnterpriseCustomerContainsContent,
useEnterpriseOffers,
useSubscriptions,
} from '../../../app/data';
import {
LEARNER_CREDIT_SUBSIDY_TYPE,
useCanUserRequestSubsidyForCourse,
useUserSubsidyApplicableToCourse,
} from '../../data';
import { renderWithRouterProvider } from '../../../../utils/tests';

jest.mock('../../../app/data', () => ({
...jest.requireActual('../../../app/data'),
useEnterpriseCustomer: jest.fn(),
useCourseMetadata: jest.fn(),
useSubscriptions: jest.fn(),
useEnterpriseOffers: jest.fn(),
useEnterpriseCustomerContainsContent: jest.fn(),
useCouponCodes: jest.fn(),
useCourseRedemptionEligibility: jest.fn(),
useEnterpriseCourseEnrollments: jest.fn(),
}));

jest.mock('../../data', () => ({
...jest.requireActual('../../data'),
useCanUserRequestSubsidyForCourse: jest.fn(),
useUserSubsidyApplicableToCourse: jest.fn(),
useCourseListPrice: jest.fn(),
}));

jest.mock('../../data/utils', () => ({
...jest.requireActual('../../data/utils'),
Expand All @@ -20,51 +56,85 @@ jest.mock('../data', () => ({
action: 'Action',
}),
}));

const mockCourseRunKey = 'course-v1:edX+DemoX+Demo_Course';
const mockCourseRunUrl = 'http://course.url';
const mockUserEnrollment = {
id: 1,
isEnrollmentActive: true,
isRevoked: false,
courseRunId: mockCourseRunKey,
courseRunUrl: mockCourseRunUrl,
linkToCourse: mockCourseRunUrl,
};
const mockCourseRun = {
key: 'course-v1:edX+DemoX+Demo_Course',
key: mockCourseRunKey,
availability: 'Current',
start: '2020-01-01T00:00:00Z',
pacingType: 'self_paced',
enrollmentCount: 0,
linkToCourse: mockCourseRunUrl,
};

const mockUserEnrollment = {
id: 1,
isEnrollmentActive: true,
isRevoked: false,
courseRunId: mockCourseRun.key,
courseRunUrl: 'http://course.url',
};
const mockUserSubsidy = { subsidyType: 'learnerCredit' };
const mockUserEnrollments = [mockUserEnrollment];
const mockUserCanRequestSubsidy = false;
const mockAuthenticatedUser = authenticatedUserFactory();
const CourseRunCardWrapper = (props) => (
<AppContext.Provider value={{ authenticatedUser: mockAuthenticatedUser }}>
<CourseRunCard
courseRun={mockCourseRun}
{...props}
/>
</AppContext.Provider>
);

const CourseRunCardWrapper = (props) => {
const courseContextValue = {
state: {
course: {
entitlements: [],
},
userEnrollments: mockUserEnrollments,
},
userSubsidyApplicableToCourse: mockUserSubsidy,
userCanRequestSubsidyForCourse: mockUserCanRequestSubsidy,
};
return (
<CourseContext.Provider value={courseContextValue}>
<CourseRunCard
courseRun={mockCourseRun}
{...props}
/>
</CourseContext.Provider>
);
};
const mockEnterpriseCustomer = enterpriseCustomerFactory();

describe('<CourseRunCard />', () => {
beforeEach(() => {
jest.clearAllMocks();
useEnterpriseCustomer.mockReturnValue({
data: mockEnterpriseCustomer,
});
useCourseMetadata.mockReturnValue({ data: { entitlements: [] } });
useCourseRedemptionEligibility.mockReturnValue({ data: { listPrice: 199 } });
useSubscriptions.mockReturnValue({
data: {
customerAgreement: undefined,
subscriptionLicense: undefined,
subscriptionPlan: undefined,
shouldShowActivationSuccessMessage: false,
},
});
useEnterpriseCustomerContainsContent.mockReturnValue({
data: {
containsContentItems: false,
catalogList: [],
},
});
useEnterpriseOffers.mockReturnValue({
data: {
enterpriseOffers: [],
currentEnterpriseOffers: [],
canEnrollWithEnterpriseOffers: false,
},
});
useCouponCodes.mockReturnValue({
data: {
couponCodeAssignments: [],
},
});
useEnterpriseCourseEnrollments.mockReturnValue({ data: { enterpriseCourseEnrollments: mockUserEnrollment } });
useUserSubsidyApplicableToCourse.mockReturnValue({
userSubsidyApplicableToCourse: {
subsidyType: LEARNER_CREDIT_SUBSIDY_TYPE,
},
missingUserSubsidyReason: undefined,
});
useCanUserRequestSubsidyForCourse.mockReturnValue(false);
});
test('renders', () => {
findUserEnrollmentForCourseRun.mockReturnValue(mockUserEnrollment);
render(<CourseRunCardWrapper />);
renderWithRouterProvider(<CourseRunCardWrapper />);
expect(screen.getByText('Heading')).toBeInTheDocument();
expect(screen.getByText('Subheading')).toBeInTheDocument();
expect(screen.getByText('Action')).toBeInTheDocument();
Expand Down
Loading

0 comments on commit 33f06e0

Please sign in to comment.