feat: Added cross product recommendations experiment initial render + query logic (#158)

This commit is contained in:
Jody Bailey
2023-06-15 15:06:32 +02:00
committed by GitHub
parent f92bd9c8f9
commit 93a4dfb4d9
38 changed files with 1515 additions and 14 deletions

View File

@@ -3,6 +3,7 @@ import PropTypes from 'prop-types';
import { Container, Col, Row } from '@edx/paragon';
import WidgetFooter from 'containers/WidgetContainers/WidgetFooter';
import hooks from './hooks';
export const columnConfig = {
@@ -30,6 +31,11 @@ export const DashboardLayout = ({ children, sidebar }) => {
{sidebar}
</Col>
</Row>
<Row>
<Col>
<WidgetFooter />
</Col>
</Row>
</Container>
);
};

View File

@@ -1,6 +1,7 @@
import { shallow } from 'enzyme';
import { Col, Row } from '@edx/paragon';
import WidgetFooter from 'containers/WidgetContainers/WidgetFooter';
import hooks from './hooks';
import DashboardLayout, { columnConfig } from './DashboardLayout';
@@ -32,6 +33,10 @@ describe('DashboardLayout', () => {
const columns = render().find(Row).find(Col);
expect(columns.at(1).contains(props.sidebar)).toEqual(true);
});
it('displays a footer in the second row', () => {
const columns = render().find(Row).at(1).find(Col);
expect(columns.at(0).containsMatchingElement(<WidgetFooter />)).toBeTruthy();
});
};
const testSnapshot = () => {
test('snapshot', () => {

View File

@@ -41,6 +41,11 @@ exports[`DashboardLayout collapsed snapshot 1`] = `
test-sidebar-content
</Col>
</Row>
<Row>
<Col>
<WidgetFooter />
</Col>
</Row>
</Container>
`;
@@ -90,5 +95,10 @@ exports[`DashboardLayout not collapsed snapshot 1`] = `
test-sidebar-content
</Col>
</Row>
<Row>
<Col>
<WidgetFooter />
</Col>
</Row>
</Container>
`;

View File

@@ -21,6 +21,7 @@ export const Dashboard = () => {
const hasAvailableDashboards = reduxHooks.useHasAvailableDashboards();
const initIsPending = reduxHooks.useRequestIsPending(RequestKeys.initialize);
const showSelectSessionModal = reduxHooks.useShowSelectSessionModal();
return (
<div id="dashboard-container" className="d-flex flex-column p-2 pt-0">
<h1 className="sr-only">{pageTitle}</h1>

View File

@@ -1,13 +1,22 @@
import React from 'react';
import RecommendationsPanel from 'widgets/RecommendationsPanel';
import hooks from 'widgets/ProductRecommendations/hooks';
export const WidgetSidebar = () => (
<div className="widget-sidebar">
<div className="d-flex">
<RecommendationsPanel />
</div>
</div>
);
export const WidgetSidebar = () => {
const showRecommendationsFooter = hooks.useShowRecommendationsFooter();
if (!showRecommendationsFooter) {
return (
<div className="widget-sidebar">
<div className="d-flex">
<RecommendationsPanel />
</div>
</div>
);
}
return null;
};
export default WidgetSidebar;

View File

@@ -1,14 +1,25 @@
import { shallow } from 'enzyme';
import hooks from 'widgets/ProductRecommendations/hooks';
import WidgetSidebar from '.';
jest.mock('widgets/LookingForChallengeWidget', () => 'LookingForChallengeWidget');
jest.mock('widgets/ProductRecommendations/hooks', () => ({
useShowRecommendationsFooter: jest.fn(),
}));
describe('WidgetSidebar', () => {
describe('snapshots', () => {
test('default', () => {
hooks.useShowRecommendationsFooter.mockReturnValueOnce(false);
const wrapper = shallow(<WidgetSidebar />);
expect(wrapper).toMatchSnapshot();
});
});
test('is hidden if footer is shown', () => {
hooks.useShowRecommendationsFooter.mockReturnValueOnce(true);
const wrapper = shallow(<WidgetSidebar />);
expect(wrapper.type()).toBeNull();
});
});

View File

@@ -1,13 +1,22 @@
import React from 'react';
import RecommendationsPanel from 'widgets/RecommendationsPanel';
import hooks from 'widgets/ProductRecommendations/hooks';
export const WidgetSidebar = () => (
<div className="widget-sidebar px-2">
<div className="d-flex">
<RecommendationsPanel />
</div>
</div>
);
export const WidgetSidebar = () => {
const showRecommendationsFooter = hooks.useShowRecommendationsFooter();
if (!showRecommendationsFooter) {
return (
<div className="widget-sidebar px-2">
<div className="d-flex">
<RecommendationsPanel />
</div>
</div>
);
}
return null;
};
export default WidgetSidebar;

View File

@@ -1,14 +1,25 @@
import { shallow } from 'enzyme';
import hooks from 'widgets/ProductRecommendations/hooks';
import WidgetSidebar from '.';
jest.mock('widgets/LookingForChallengeWidget', () => 'LookingForChallengeWidget');
jest.mock('widgets/ProductRecommendations/hooks', () => ({
useShowRecommendationsFooter: jest.fn(),
}));
describe('WidgetSidebar', () => {
describe('snapshots', () => {
test('default', () => {
hooks.useShowRecommendationsFooter.mockReturnValueOnce(false);
const wrapper = shallow(<WidgetSidebar />);
expect(wrapper).toMatchSnapshot();
});
});
test('is hidden if footer is shown', () => {
hooks.useShowRecommendationsFooter.mockReturnValueOnce(true);
const wrapper = shallow(<WidgetSidebar />);
expect(wrapper.type()).toBeNull();
});
});

View File

@@ -0,0 +1,9 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`WidgetFooter snapshots default 1`] = `
<div
className="widget-footer"
>
<ProductRecommendations />
</div>
`;

View File

@@ -0,0 +1,20 @@
import React from 'react';
import ProductRecommendations from 'widgets/ProductRecommendations';
import hooks from 'widgets/ProductRecommendations/hooks';
export const WidgetFooter = () => {
const showRecommendationsFooter = hooks.useShowRecommendationsFooter();
if (showRecommendationsFooter) {
return (
<div className="widget-footer">
<ProductRecommendations />
</div>
);
}
return null;
};
export default WidgetFooter;

View File

@@ -0,0 +1,25 @@
import { shallow } from 'enzyme';
import hooks from 'widgets/ProductRecommendations/hooks';
import WidgetFooter from '.';
jest.mock('widgets/LookingForChallengeWidget', () => 'LookingForChallengeWidget');
jest.mock('widgets/ProductRecommendations/hooks', () => ({
useShowRecommendationsFooter: jest.fn(),
}));
describe('WidgetFooter', () => {
describe('snapshots', () => {
test('default', () => {
hooks.useShowRecommendationsFooter.mockReturnValueOnce(true);
const wrapper = shallow(<WidgetFooter />);
expect(wrapper).toMatchSnapshot();
});
});
test('is hidden when hook returns false', () => {
hooks.useShowRecommendationsFooter.mockReturnValueOnce(false);
const wrapper = shallow(<WidgetFooter />);
expect(wrapper.type()).toBeNull();
});
});

View File

@@ -144,6 +144,8 @@ jest.mock('@edx/paragon', () => jest.requireActual('testUtils').mockNestedCompon
Sheet: 'Sheet',
StatefulButton: 'StatefulButton',
TextFilter: 'TextFilter',
Truncate: 'Truncate',
Skeleton: 'Skeleton',
Spinner: 'Spinner',
PageBanner: 'PageBanner',
Pagination: 'Pagination',

View File

@@ -0,0 +1,104 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`ProductRecommendations matches snapshot 1`] = `
<LoadedView
crossProductCourses={
Array [
Object {
"courseType": "executive-education-2u",
"image": Object {
"src": "https://www.image-2.com/ed79a49b-64c1-48d2-afdc-054bf921e38d-6a76ceb47dea.small.jpg",
},
"owners": Array [
Object {
"key": "HarvardX",
"logoImageUrl": "http://www.image.com/ef72daf3-c9a1-4c00-ba37-b3514392bdcf-8839c516815a.png",
"name": "Harvard University",
},
],
"prospectusPath": "course/introduction-to-computer-sceince",
"title": "Introduction to Computer Science",
},
Object {
"courseType": "bootcamp-2u",
"image": Object {
"src": "https://www.image-2.com/ed79a49b-64c1-48d2-afdc-054bf921e38d-6a76ceb47dea.small.jpg",
},
"owners": Array [
Object {
"key": "HarvardX",
"logoImageUrl": "http://www.image.com/ef72daf3-c9a1-4c00-ba37-b3514392bdcf-8839c516815a.png",
"name": "Harvard University",
},
],
"prospectusPath": "course/introduction-to-computer-sceince",
"title": "Introduction to Computer Science",
},
]
}
openCourses={
Array [
Object {
"courseType": "verified-audit",
"image": Object {
"src": "https://www.image-2.com/ed79a49b-64c1-48d2-afdc-054bf921e38d-6a76ceb47dea.small.jpg",
},
"owners": Array [
Object {
"key": "HarvardX",
"logoImageUrl": "http://www.image.com/ef72daf3-c9a1-4c00-ba37-b3514392bdcf-8839c516815a.png",
"name": "Harvard University",
},
],
"prospectusPath": "course/introduction-to-computer-sceince",
"title": "Introduction to Computer Science",
},
Object {
"courseType": "audit",
"image": Object {
"src": "https://www.image-2.com/ed79a49b-64c1-48d2-afdc-054bf921e38d-6a76ceb47dea.small.jpg",
},
"owners": Array [
Object {
"key": "HarvardX",
"logoImageUrl": "http://www.image.com/ef72daf3-c9a1-4c00-ba37-b3514392bdcf-8839c516815a.png",
"name": "Harvard University",
},
],
"prospectusPath": "course/introduction-to-computer-sceince",
"title": "Introduction to Computer Science",
},
Object {
"courseType": "verified",
"image": Object {
"src": "https://www.image-2.com/ed79a49b-64c1-48d2-afdc-054bf921e38d-6a76ceb47dea.small.jpg",
},
"owners": Array [
Object {
"key": "HarvardX",
"logoImageUrl": "http://www.image.com/ef72daf3-c9a1-4c00-ba37-b3514392bdcf-8839c516815a.png",
"name": "Harvard University",
},
],
"prospectusPath": "course/introduction-to-computer-sceince",
"title": "Introduction to Computer Science",
},
Object {
"courseType": "course",
"image": Object {
"src": "https://www.image-2.com/ed79a49b-64c1-48d2-afdc-054bf921e38d-6a76ceb47dea.small.jpg",
},
"owners": Array [
Object {
"key": "HarvardX",
"logoImageUrl": "http://www.image.com/ef72daf3-c9a1-4c00-ba37-b3514392bdcf-8839c516815a.png",
"name": "Harvard University",
},
],
"prospectusPath": "course/introduction-to-computer-sceince",
"title": "Introduction to Computer Science",
},
]
}
/>
`;

View File

@@ -0,0 +1,10 @@
import { get, stringifyUrl } from 'data/services/lms/utils';
import urls from 'data/services/lms/urls';
export const productRecommendationsUrl = (courseId) => `${urls.api}/learner_recommendations/product_recommendations/${courseId}/`;
const fetchProductRecommendations = (courseId) => get(stringifyUrl(productRecommendationsUrl(courseId)));
export default {
fetchProductRecommendations,
};

View File

@@ -0,0 +1,17 @@
import { get, stringifyUrl } from 'data/services/lms/utils';
import api, { productRecommendationsUrl } from './api';
jest.mock('data/services/lms/utils', () => ({
stringifyUrl: (...args) => ({ stringifyUrl: args }),
get: (...args) => ({ get: args }),
}));
describe('productRecommendationCourses api', () => {
describe('fetchProductRecommendations', () => {
it('calls get with the correct recommendation courses URL', () => {
expect(api.fetchProductRecommendations('CourseRunKey')).toEqual(
get(stringifyUrl(productRecommendationsUrl('CourseRunKey'))),
);
});
});
});

View File

@@ -0,0 +1,46 @@
import React, { useMemo } from 'react';
import PropTypes from 'prop-types';
import { Container } from '@edx/paragon';
import { useIntl } from '@edx/frontend-platform/i18n';
import messages from '../messages';
import { courseShape, courseTypeToProductTypeMap } from '../utils';
import ProductCardContainer from './ProductCardContainer';
const LoadedView = ({ crossProductCourses, openCourses }) => {
const { formatMessage } = useIntl();
const includesCrossProductTypes = crossProductCourses.length === 2;
const finalProductList = useMemo(() => {
if (includesCrossProductTypes) {
const openCourseList = openCourses.slice(0, 2);
return crossProductCourses.concat(openCourseList);
}
return openCourses;
}, [crossProductCourses, openCourses, includesCrossProductTypes]);
const courseTypes = [...new Set(finalProductList.map((item) => courseTypeToProductTypeMap[item.courseType]))];
return (
<Container
size="lg"
className="recommendations-container pt-sm-5 pt-4.5 pb-2 pb-sm-4.5"
>
<h2>
{formatMessage(messages.recommendationsHeading)}
</h2>
<ProductCardContainer finalProductList={finalProductList} courseTypes={courseTypes} />
</Container>
);
};
LoadedView.propTypes = {
crossProductCourses: PropTypes.arrayOf(
PropTypes.shape(courseShape),
).isRequired,
openCourses: PropTypes.arrayOf(
PropTypes.shape(courseShape),
).isRequired,
};
export default LoadedView;

View File

@@ -0,0 +1,34 @@
import React from 'react';
import { shallow } from 'enzyme';
import { mockCrossProductCourses, mockOpenCourses } from '../testData';
import LoadedView from './LoadedView';
describe('ProductRecommendations LoadedView', () => {
it('matches snapshot', () => {
expect(
shallow(
<LoadedView
crossProductCourses={mockCrossProductCourses}
openCourses={mockOpenCourses}
/>,
),
).toMatchSnapshot();
});
describe('with less than 2 cross product courses', () => {
it('passes in one course type and 4 open courses to the ProductCardContainer props', () => {
const wrapper = shallow(
<LoadedView
crossProductCourses={[mockCrossProductCourses[0]]}
openCourses={mockOpenCourses}
/>,
);
const productCardContainerProps = wrapper.find('ProductCardContainer').props();
expect(productCardContainerProps.courseTypes.length).toEqual(1);
expect(productCardContainerProps.courseTypes[0]).toEqual('Course');
expect(productCardContainerProps.finalProductList).toEqual(mockOpenCourses);
});
});
});

View File

@@ -0,0 +1,8 @@
import React from 'react';
import { Skeleton } from '@edx/paragon';
export const LoadingView = () => (
<Skeleton height={100} />
);
export default LoadingView;

View File

@@ -0,0 +1,10 @@
import React from 'react';
import { shallow } from 'enzyme';
import LoadingView from './LoadingView';
describe('ProductRecommendations LoadingView', () => {
it('matches snapshot', () => {
expect(shallow(<LoadingView />)).toMatchSnapshot();
});
});

View File

@@ -0,0 +1,60 @@
import React from 'react';
import PropTypes from 'prop-types';
import {
Badge,
Card,
Truncate,
Hyperlink,
} from '@edx/paragon';
const ProductCard = ({
title,
subtitle,
headerImage,
schoolLogo,
courseType,
url,
}) => (
<Card
className="base-card d-flex text-decoration-none"
as={Hyperlink}
destination={url}
isClickable
>
<Card.ImageCap
src={headerImage}
srcAlt={`header image for ${title}`}
logoSrc={schoolLogo}
logoAlt={`logo for ${subtitle}`}
/>
<Card.Header
className="mt-2"
title={(
<Truncate lines={3} ellipsis="…" className="product-card-title font-weight-bold">
{title}
</Truncate>
)}
subtitle={(
<Truncate lines={1} className="product-card-subtitle font-weight-normal">
{subtitle}
</Truncate>
)}
/>
<Card.Section>
<div className="product-badge position-absolute">
<Badge className="bg-light-500 text-dark-500">{courseType}</Badge>
</div>
</Card.Section>
</Card>
);
ProductCard.propTypes = {
title: PropTypes.string.isRequired,
subtitle: PropTypes.string.isRequired,
headerImage: PropTypes.string.isRequired,
schoolLogo: PropTypes.string.isRequired,
courseType: PropTypes.string.isRequired,
url: PropTypes.string.isRequired,
};
export default ProductCard;

View File

@@ -0,0 +1,29 @@
import React from 'react';
import { shallow } from 'enzyme';
import { mockCrossProductCourses } from '../testData';
import ProductCard from './ProductCard';
import { courseTypeToProductTypeMap } from '../utils';
describe('ProductRecommendations ProductCard', () => {
const course = mockCrossProductCourses[0];
const {
title,
owners: [{ name: subtitle }],
image: { src: headerImage },
owners: [{ logoImageUrl: schoolLogo }],
} = course;
const props = {
title,
subtitle,
headerImage,
schoolLogo,
courseType: courseTypeToProductTypeMap[course.courseType],
url: `https://www.edx.org/${course.prospectusPath}`,
};
it('matches snapshot', () => {
expect(shallow(<ProductCard {...props} />)).toMatchSnapshot();
});
});

View File

@@ -0,0 +1,46 @@
import React from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import { courseShape, courseTypeToProductTypeMap } from '../utils';
import ProductCard from './ProductCard';
import ProductCardHeader from './ProductCardHeader';
const ProductCardContainer = ({ finalProductList, courseTypes }) => (
<div className="product-card-container d-flex">
{finalProductList
&& courseTypes.map((type) => (
<div key={type}>
<ProductCardHeader courseType={type} />
<div
className={classNames('d-flex', {
'course-subcontainer': type === 'Course',
})}
>
{finalProductList
.filter((course) => courseTypeToProductTypeMap[course.courseType] === type)
.map((item) => (
<ProductCard
key={item.title}
url={`https://www.edx.org/${item.prospectusPath}`}
title={item.title}
subtitle={item.owners[0].name}
headerImage={item.image.src}
schoolLogo={item.owners[0].logoImageUrl}
courseType={type}
/>
))}
</div>
</div>
))}
</div>
);
ProductCardContainer.propTypes = {
finalProductList: PropTypes.arrayOf(
PropTypes.shape(courseShape),
).isRequired,
courseTypes: PropTypes.arrayOf(PropTypes.string).isRequired,
};
export default ProductCardContainer;

View File

@@ -0,0 +1,43 @@
import React from 'react';
import { shallow } from 'enzyme';
import { mockCrossProductCourses, mockOpenCourses } from '../testData';
import ProductCardContainer from './ProductCardContainer';
describe('ProductRecommendations ProductCardContainer', () => {
const props = {
finalProductList: [...mockCrossProductCourses, ...mockOpenCourses],
courseTypes: ['Executive Education', 'Boot Camp', 'Course'],
};
it('matches snapshot', () => {
expect(shallow(<ProductCardContainer {...props} />)).toMatchSnapshot();
});
describe('with finalCourseList containing cross product and open courses', () => {
it('renders 3 ProductCardHeaders with the 3 different course types', () => {
const wrapper = shallow(<ProductCardContainer {...props} />);
const productCardHeaders = wrapper.find('ProductCardHeader');
expect(productCardHeaders.length).toEqual(3);
productCardHeaders.forEach((header, index) => {
expect(header.props().courseType).toEqual(props.courseTypes[index]);
});
});
});
describe('with finalCourseList containing only open courses', () => {
it('renders 1 ProductHeader with the one course type', () => {
const openCoursesProps = {
finalProductList: [...mockOpenCourses, ...mockOpenCourses],
courseTypes: ['Course'],
};
const wrapper = shallow(<ProductCardContainer {...openCoursesProps} />);
const productCardHeaders = wrapper.find('ProductCardHeader');
expect(productCardHeaders.length).toEqual(1);
expect(productCardHeaders.at(0).props().courseType).toEqual(openCoursesProps.courseTypes[0]);
});
});
});

View File

@@ -0,0 +1,62 @@
import React from 'react';
import PropTypes from 'prop-types';
import { useIntl } from '@edx/frontend-platform/i18n';
import { Icon, Hyperlink } from '@edx/paragon';
import { ChevronRight } from '@edx/paragon/icons';
import messages from '../messages';
const ProductCardHeader = ({ courseType }) => {
const { formatMessage } = useIntl();
const getProductTypeDetail = (type) => {
switch (type) {
case 'Executive Education':
return {
heading: messages.executiveEducationHeading,
description: messages.executiveEducationDescription,
url: '/executive-education?linked_from=recommender',
};
case 'Boot Camp':
return {
heading: messages.bootcampHeading,
description: messages.bootcampDescription,
url: '/boot-camps?linked_from=recommender',
};
default: {
return {
heading: messages.courseHeading,
description: messages.courseDescription,
url: '/search?tab=course?linked_from=recommender',
};
}
}
};
const productTypeDetail = getProductTypeDetail(courseType);
return (
<div>
<Hyperlink
destination={`https://www.edx.org${productTypeDetail.url}`}
className="base-card-link text-decoration-none"
>
<div className="d-flex align-items-center border-bottom">
<h3 className="h3 mb-2 text-left">
{formatMessage(productTypeDetail.heading)}
</h3>
<Icon src={ChevronRight} className="text-primary-500 ml-2.5" />
</div>
</Hyperlink>
<p className="text-gray-500 x-small mt-2 mb-2">
{formatMessage(productTypeDetail.description)}
</p>
</div>
);
};
ProductCardHeader.propTypes = {
courseType: PropTypes.string.isRequired,
};
export default ProductCardHeader;

View File

@@ -0,0 +1,29 @@
import React from 'react';
import { shallow } from 'enzyme';
import ProductCardHeader from './ProductCardHeader';
describe('ProductRecommendations ProductCardHeader', () => {
const bootCampType = 'Boot Camp';
const executiveEducationType = 'Executive Education';
const courseType = 'Courses';
it('matches snapshot', () => {
expect(shallow(<ProductCardHeader courseType={executiveEducationType} />)).toMatchSnapshot();
});
describe('with bootcamp courseType prop', () => {
it('renders a bootcamp header', () => {
const wrapper = shallow(<ProductCardHeader courseType={bootCampType} />);
expect(wrapper.find('h3').text()).toEqual(bootCampType);
});
});
describe('with course courseType prop', () => {
it('renders a courses header', () => {
const wrapper = shallow(<ProductCardHeader courseType={courseType} />);
expect(wrapper.find('h3').text()).toEqual(courseType);
});
});
});

View File

@@ -0,0 +1,85 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`ProductRecommendations LoadedView matches snapshot 1`] = `
<Container
className="recommendations-container pt-sm-5 pt-4.5 pb-2 pb-sm-4.5"
size="lg"
>
<h2>
You might also like
</h2>
<ProductCardContainer
courseTypes={
Array [
"Executive Education",
"Boot Camp",
"Course",
]
}
finalProductList={
Array [
Object {
"courseType": "executive-education-2u",
"image": Object {
"src": "https://www.image-2.com/ed79a49b-64c1-48d2-afdc-054bf921e38d-6a76ceb47dea.small.jpg",
},
"owners": Array [
Object {
"key": "HarvardX",
"logoImageUrl": "http://www.image.com/ef72daf3-c9a1-4c00-ba37-b3514392bdcf-8839c516815a.png",
"name": "Harvard University",
},
],
"prospectusPath": "course/introduction-to-computer-sceince",
"title": "Introduction to Computer Science",
},
Object {
"courseType": "bootcamp-2u",
"image": Object {
"src": "https://www.image-2.com/ed79a49b-64c1-48d2-afdc-054bf921e38d-6a76ceb47dea.small.jpg",
},
"owners": Array [
Object {
"key": "HarvardX",
"logoImageUrl": "http://www.image.com/ef72daf3-c9a1-4c00-ba37-b3514392bdcf-8839c516815a.png",
"name": "Harvard University",
},
],
"prospectusPath": "course/introduction-to-computer-sceince",
"title": "Introduction to Computer Science",
},
Object {
"courseType": "verified-audit",
"image": Object {
"src": "https://www.image-2.com/ed79a49b-64c1-48d2-afdc-054bf921e38d-6a76ceb47dea.small.jpg",
},
"owners": Array [
Object {
"key": "HarvardX",
"logoImageUrl": "http://www.image.com/ef72daf3-c9a1-4c00-ba37-b3514392bdcf-8839c516815a.png",
"name": "Harvard University",
},
],
"prospectusPath": "course/introduction-to-computer-sceince",
"title": "Introduction to Computer Science",
},
Object {
"courseType": "audit",
"image": Object {
"src": "https://www.image-2.com/ed79a49b-64c1-48d2-afdc-054bf921e38d-6a76ceb47dea.small.jpg",
},
"owners": Array [
Object {
"key": "HarvardX",
"logoImageUrl": "http://www.image.com/ef72daf3-c9a1-4c00-ba37-b3514392bdcf-8839c516815a.png",
"name": "Harvard University",
},
],
"prospectusPath": "course/introduction-to-computer-sceince",
"title": "Introduction to Computer Science",
},
]
}
/>
</Container>
`;

View File

@@ -0,0 +1,7 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`ProductRecommendations LoadingView matches snapshot 1`] = `
<Skeleton
height={100}
/>
`;

View File

@@ -0,0 +1,48 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`ProductRecommendations ProductCard matches snapshot 1`] = `
<Card
as="Hyperlink"
className="base-card d-flex text-decoration-none"
destination="https://www.edx.org/course/introduction-to-computer-sceince"
isClickable={true}
>
<Card.ImageCap
logoAlt="logo for Harvard University"
logoSrc="http://www.image.com/ef72daf3-c9a1-4c00-ba37-b3514392bdcf-8839c516815a.png"
src="https://www.image-2.com/ed79a49b-64c1-48d2-afdc-054bf921e38d-6a76ceb47dea.small.jpg"
srcAlt="header image for Introduction to Computer Science"
/>
<Card.Header
className="mt-2"
subtitle={
<Truncate
className="product-card-subtitle font-weight-normal"
lines={1}
>
Harvard University
</Truncate>
}
title={
<Truncate
className="product-card-title font-weight-bold"
ellipsis="…"
lines={3}
>
Introduction to Computer Science
</Truncate>
}
/>
<Card.Section>
<div
className="product-badge position-absolute"
>
<Badge
className="bg-light-500 text-dark-500"
>
Executive Education
</Badge>
</div>
</Card.Section>
</Card>
`;

View File

@@ -0,0 +1,95 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`ProductRecommendations ProductCardContainer matches snapshot 1`] = `
<div
className="product-card-container d-flex"
>
<div
key="Executive Education"
>
<ProductCardHeader
courseType="Executive Education"
/>
<div
className="d-flex"
>
<ProductCard
courseType="Executive Education"
headerImage="https://www.image-2.com/ed79a49b-64c1-48d2-afdc-054bf921e38d-6a76ceb47dea.small.jpg"
key="Introduction to Computer Science"
schoolLogo="http://www.image.com/ef72daf3-c9a1-4c00-ba37-b3514392bdcf-8839c516815a.png"
subtitle="Harvard University"
title="Introduction to Computer Science"
url="https://www.edx.org/course/introduction-to-computer-sceince"
/>
</div>
</div>
<div
key="Boot Camp"
>
<ProductCardHeader
courseType="Boot Camp"
/>
<div
className="d-flex"
>
<ProductCard
courseType="Boot Camp"
headerImage="https://www.image-2.com/ed79a49b-64c1-48d2-afdc-054bf921e38d-6a76ceb47dea.small.jpg"
key="Introduction to Computer Science"
schoolLogo="http://www.image.com/ef72daf3-c9a1-4c00-ba37-b3514392bdcf-8839c516815a.png"
subtitle="Harvard University"
title="Introduction to Computer Science"
url="https://www.edx.org/course/introduction-to-computer-sceince"
/>
</div>
</div>
<div
key="Course"
>
<ProductCardHeader
courseType="Course"
/>
<div
className="d-flex course-subcontainer"
>
<ProductCard
courseType="Course"
headerImage="https://www.image-2.com/ed79a49b-64c1-48d2-afdc-054bf921e38d-6a76ceb47dea.small.jpg"
key="Introduction to Computer Science"
schoolLogo="http://www.image.com/ef72daf3-c9a1-4c00-ba37-b3514392bdcf-8839c516815a.png"
subtitle="Harvard University"
title="Introduction to Computer Science"
url="https://www.edx.org/course/introduction-to-computer-sceince"
/>
<ProductCard
courseType="Course"
headerImage="https://www.image-2.com/ed79a49b-64c1-48d2-afdc-054bf921e38d-6a76ceb47dea.small.jpg"
key="Introduction to Computer Science"
schoolLogo="http://www.image.com/ef72daf3-c9a1-4c00-ba37-b3514392bdcf-8839c516815a.png"
subtitle="Harvard University"
title="Introduction to Computer Science"
url="https://www.edx.org/course/introduction-to-computer-sceince"
/>
<ProductCard
courseType="Course"
headerImage="https://www.image-2.com/ed79a49b-64c1-48d2-afdc-054bf921e38d-6a76ceb47dea.small.jpg"
key="Introduction to Computer Science"
schoolLogo="http://www.image.com/ef72daf3-c9a1-4c00-ba37-b3514392bdcf-8839c516815a.png"
subtitle="Harvard University"
title="Introduction to Computer Science"
url="https://www.edx.org/course/introduction-to-computer-sceince"
/>
<ProductCard
courseType="Course"
headerImage="https://www.image-2.com/ed79a49b-64c1-48d2-afdc-054bf921e38d-6a76ceb47dea.small.jpg"
key="Introduction to Computer Science"
schoolLogo="http://www.image.com/ef72daf3-c9a1-4c00-ba37-b3514392bdcf-8839c516815a.png"
subtitle="Harvard University"
title="Introduction to Computer Science"
url="https://www.edx.org/course/introduction-to-computer-sceince"
/>
</div>
</div>
</div>
`;

View File

@@ -0,0 +1,29 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`ProductRecommendations ProductCardHeader matches snapshot 1`] = `
<div>
<Hyperlink
className="base-card-link text-decoration-none"
destination="https://www.edx.org/executive-education?linked_from=recommender"
>
<div
className="d-flex align-items-center border-bottom"
>
<h3
className="h3 mb-2 text-left"
>
Executive Education
</h3>
<Icon
className="text-primary-500 ml-2.5"
src={[MockFunction icons.ChevronRight]}
/>
</div>
</Hyperlink>
<p
className="text-gray-500 x-small mt-2 mb-2"
>
Short Courses to develop leadership skills
</p>
</div>
`;

View File

@@ -0,0 +1,72 @@
import { useState, useEffect } from 'react';
import { RequestStates, RequestKeys } from 'data/constants/requests';
import { StrictDict } from 'utils';
import { reduxHooks } from 'hooks';
import { SortKeys } from 'data/constants/app';
import api from './api';
import * as module from './hooks';
export const state = StrictDict({
requestState: (val) => useState(val), // eslint-disable-line
data: (val) => useState(val), // eslint-disable-line
});
export const useShowRecommendationsFooter = () => {
const hasCourses = reduxHooks.useHasCourses();
const hasAvailableDashboards = reduxHooks.useHasAvailableDashboards();
const initIsPending = reduxHooks.useRequestIsPending(RequestKeys.initialize);
// Hardcoded to not show until experiment related code is implemented
return !initIsPending && hasCourses && !hasAvailableDashboards && false;
};
export const useMostRecentCourseRunKey = () => {
const mostRecentCourse = reduxHooks.useCurrentCourseList({
sortBy: SortKeys.enrolled,
filters: [],
pageSize: 0,
}).visible[0].courseRun.courseId;
return mostRecentCourse;
};
export const useFetchProductRecommendations = (setRequestState, setData) => {
const courseRunKey = module.useMostRecentCourseRunKey();
useEffect(() => {
let isMounted = true;
api
.fetchProductRecommendations(courseRunKey)
.then((response) => {
if (isMounted) {
setData(response.data);
setRequestState(RequestStates.completed);
}
})
.catch(() => {
if (isMounted) {
setRequestState(RequestStates.failed);
}
});
return () => {
isMounted = false;
};
/* eslint-disable */
}, []);
};
export const useProductRecommendationsData = () => {
const [requestState, setRequestState] = module.state.requestState(RequestStates.pending);
const [data, setData] = module.state.data({});
module.useFetchProductRecommendations(setRequestState, setData);
return {
productRecommendations: data,
isLoading: requestState === RequestStates.pending,
isLoaded: requestState === RequestStates.completed,
hasFailed: requestState === RequestStates.failed
};
};
export default { useProductRecommendationsData, useShowRecommendationsFooter };

View File

@@ -0,0 +1,243 @@
import React from 'react';
import { waitFor } from '@testing-library/react';
import { MockUseState } from 'testUtils';
import { RequestStates } from 'data/constants/requests';
import { reduxHooks } from 'hooks';
import { wait } from './utils';
import api from './api';
import * as hooks from './hooks';
jest.mock('./api', () => ({
fetchProductRecommendations: jest.fn(),
}));
jest.mock('hooks', () => ({
reduxHooks: {
useCurrentCourseList: jest.fn(),
useHasCourses: jest.fn(),
useHasAvailableDashboards: jest.fn(),
useRequestIsPending: jest.fn(),
},
}));
const state = new MockUseState(hooks);
const mostRecentCourseRunKey = 'course ID 1';
const courses = [
{
courseRun: {
courseId: mostRecentCourseRunKey,
},
},
{
courseRun: {
courseId: 'course ID 2',
},
},
];
const courseListData = {
visible: courses,
numPages: 0,
};
let output;
describe('ProductRecommendations hooks', () => {
beforeEach(() => {
jest.resetAllMocks();
});
describe('state fields', () => {
state.testGetter(state.keys.requestState);
state.testGetter(state.keys.data);
});
describe('useMostRecentCourseRunKey', () => {
it('returns the courseId of the first course in the sorted visible array', () => {
reduxHooks.useCurrentCourseList.mockReturnValueOnce(courseListData);
expect(hooks.useMostRecentCourseRunKey()).toBe(mostRecentCourseRunKey);
});
});
describe('useShowRecommendationsFooter', () => {
// TODO: Update when hardcoded value is removed
it('returns whether the footer widget should show', () => {
reduxHooks.useHasCourses.mockReturnValueOnce(true);
reduxHooks.useHasAvailableDashboards.mockReturnValueOnce(false);
reduxHooks.useRequestIsPending.mockReturnValueOnce(false);
expect(hooks.useShowRecommendationsFooter()).toBeFalsy();
});
});
describe('useFetchProductRecommendations', () => {
describe('behavior', () => {
describe('useEffect call', () => {
let calls;
let cb;
const response = { data: 'response data' };
const setRequestState = jest.fn();
const setData = jest.fn();
beforeEach(() => {
reduxHooks.useCurrentCourseList.mockReturnValue(courseListData);
hooks.useFetchProductRecommendations(setRequestState, setData);
({ calls } = React.useEffect.mock);
([[cb]] = calls);
});
it('calls useEffect once', () => {
expect(calls.length).toEqual(1);
});
it('calls fetchProductRecommendations with the most recently enrolled courseId', () => {
api.fetchProductRecommendations.mockReturnValueOnce(Promise.resolve(response));
cb();
expect(api.fetchProductRecommendations).toHaveBeenCalledWith(mostRecentCourseRunKey);
});
describe('successful fetch on mounted component', () => {
it('sets the request state to completed and loads response', async () => {
let resolveFn;
api.fetchProductRecommendations.mockReturnValueOnce(new Promise(resolve => {
resolveFn = resolve;
}));
cb();
expect(api.fetchProductRecommendations).toHaveBeenCalledWith(mostRecentCourseRunKey);
expect(setRequestState).not.toHaveBeenCalled();
expect(setData).not.toHaveBeenCalled();
resolveFn(response);
await waitFor(() => {
expect(setRequestState).toHaveBeenCalledWith(RequestStates.completed);
expect(setData).toHaveBeenCalledWith(response.data);
});
});
});
describe('successful fetch on unmounted component', () => {
it('does not set the state', async () => {
let resolveFn;
api.fetchProductRecommendations.mockReturnValueOnce(new Promise(resolve => {
resolveFn = resolve;
}));
const unMount = cb();
expect(api.fetchProductRecommendations).toHaveBeenCalledWith(mostRecentCourseRunKey);
expect(setRequestState).not.toHaveBeenCalled();
expect(setData).not.toHaveBeenCalled();
unMount();
resolveFn(response);
await wait(10);
expect(setRequestState).not.toHaveBeenCalled();
expect(setData).not.toHaveBeenCalled();
});
});
describe('unsuccessful fetch on mounted component', () => {
it('sets the request state to failed and does not set the data state', async () => {
let rejectFn;
api.fetchProductRecommendations.mockReturnValueOnce(new Promise((resolve, reject) => {
rejectFn = reject;
}));
cb();
expect(api.fetchProductRecommendations).toHaveBeenCalledWith(mostRecentCourseRunKey);
expect(setRequestState).not.toHaveBeenCalled();
expect(setData).not.toHaveBeenCalled();
rejectFn();
await waitFor(() => {
expect(setRequestState).toHaveBeenCalledWith(RequestStates.failed);
expect(setData).not.toHaveBeenCalled();
});
});
});
describe('unsuccessful fetch on unmounted component', () => {
it('does not set the state', async () => {
let rejectFn;
api.fetchProductRecommendations.mockReturnValueOnce(new Promise((resolve, reject) => {
rejectFn = reject;
}));
const unMount = cb();
expect(api.fetchProductRecommendations).toHaveBeenCalledWith(mostRecentCourseRunKey);
expect(setRequestState).not.toHaveBeenCalled();
expect(setData).not.toHaveBeenCalled();
unMount();
rejectFn();
await wait(10);
expect(setRequestState).not.toHaveBeenCalled();
expect(setData).not.toHaveBeenCalled();
});
});
});
});
});
describe('useProductRecommendationsData', () => {
let fetchSpy;
beforeEach(() => {
state.mock();
fetchSpy = jest.spyOn(hooks, 'useFetchProductRecommendations').mockImplementationOnce(() => {});
output = hooks.useProductRecommendationsData();
});
it('calls useFetchProductRecommendations with setRequestState and setData', () => {
expect(fetchSpy).toHaveBeenCalledWith(state.setState.requestState, state.setState.data);
});
it('initializes requestState as RequestStates.pending', () => {
state.expectInitializedWith(state.keys.requestState, RequestStates.pending);
});
describe('return values', () => {
describe('when the request is completed, with returned response object', () => {
const mockResponse = { crossProductCourses: {}, amplitudeCourses: {} };
beforeEach(() => {
state.mockVal(state.keys.requestState, RequestStates.completed);
state.mockVal(state.keys.data, mockResponse);
output = hooks.useProductRecommendationsData();
});
it('is not loading', () => {
expect(output.isLoading).toEqual(false);
});
it('is loaded', () => {
expect(output.isLoaded).toEqual(true);
});
it('has not failed', () => {
expect(output.hasFailed).toEqual(false);
});
it('returns country code', () => {
expect(output.productRecommendations).toEqual(mockResponse);
});
});
describe('when the request is pending', () => {
beforeEach(() => {
state.mockVal(state.keys.requestState, RequestStates.pending);
state.mockVal(state.keys.data, {});
output = hooks.useProductRecommendationsData();
});
it('is loading', () => {
expect(output.isLoading).toEqual(true);
});
it('is not loaded', () => {
expect(output.isLoaded).toEqual(false);
});
it('has not failed', () => {
expect(output.hasFailed).toEqual(false);
});
it('returns empty object', () => {
expect(output.productRecommendations).toEqual({});
});
});
describe('when the request has failed', () => {
beforeEach(() => {
state.mockVal(state.keys.requestState, RequestStates.failed);
state.mockVal(state.keys.data, {});
output = hooks.useProductRecommendationsData();
});
it('is not loading', () => {
expect(output.isLoading).toEqual(false);
});
it('is not loaded', () => {
expect(output.isLoaded).toEqual(false);
});
it('has failed', () => {
expect(output.hasFailed).toEqual(true);
});
it('returns empty object', () => {
expect(output.productRecommendations).toEqual({});
});
});
});
});
});

View File

@@ -0,0 +1,29 @@
import React from 'react';
import './index.scss';
import { useWindowSize, breakpoints } from '@edx/paragon';
import { useProductRecommendationsData } from './hooks';
import LoadingView from './components/LoadingView';
import LoadedView from './components/LoadedView';
const ProductRecommendations = () => {
const { productRecommendations, isLoading, isLoaded } = useProductRecommendationsData();
const { width } = useWindowSize();
const isMobile = width < breakpoints.small.minWidth;
if (isLoading && !isMobile) {
return <LoadingView />;
}
if (isLoaded && !isMobile) {
return (
<LoadedView
openCourses={productRecommendations.amplitudeCourses}
crossProductCourses={productRecommendations.crossProductCourses}
/>
);
}
return null;
};
export default ProductRecommendations;

View File

@@ -0,0 +1,73 @@
@import "@edx/paragon/scss/core/core";
$horizontal-card-gap: 20px;
$vertical-card-gap: 24px;
.base-card {
height: 332px;
width: 270px !important;
.pgn__card-image-cap {
height: 104px;
object: {
fit: cover;
position: top center;
}
}
.pgn__card-logo-cap {
bottom: -1.5rem;
object: {
fit: scale-down;
position: center center;
}
}
.product-card-title {
font: {
size: 1.125rem;
}
line-height: 24px ;
}
.product-card-subtitle {
font: {
size: 0.875rem;
}
line-height: 24px;
}
.product-badge {
bottom: 2.75rem;
}
}
.product-card-container {
gap: $vertical-card-gap $horizontal-card-gap;
margin: 0 (-$horizontal-card-gap);
padding: 1rem $horizontal-card-gap;
.course-subcontainer {
gap: $vertical-card-gap $horizontal-card-gap;
}
@include media-breakpoint-down(lg) {
overflow-x: scroll;
}
}
// Workaround for giving the sub-container a greyish background that stretches to the full width of the browser window
// while being placed within the boundaries of the parent container's dimensions
.recommendations-container::before {
content: "";
position: absolute;
top: 0;
left: 50%;
transform: translateX(-50%);
width: 100vw;
height: 100%;
background-color: $light-200;
z-index: -1;
}

View File

@@ -0,0 +1,91 @@
import React from 'react';
import { shallow } from 'enzyme';
import { useWindowSize } from '@edx/paragon';
import hooks from './hooks';
import ProductRecommendations from './index';
import LoadingView from './components/LoadingView';
import LoadedView from './components/LoadedView';
import { mockResponse } from './testData';
jest.mock('./hooks', () => ({
useProductRecommendationsData: jest.fn(),
}));
jest.mock('./components/LoadingView', () => 'LoadingView');
jest.mock('./components/LoadedView', () => 'LoadedView');
describe('ProductRecommendations', () => {
const defaultValues = {
productRecommendations: {},
isLoading: false,
isLoaded: false,
hasFailed: false,
};
const successfullLoadValues = {
...defaultValues,
isLoaded: true,
productRecommendations: mockResponse,
};
const desktopWindowSize = {
width: 1400,
height: 943,
};
it('matches snapshot', () => {
useWindowSize.mockReturnValueOnce(desktopWindowSize);
hooks.useProductRecommendationsData.mockReturnValueOnce({
...successfullLoadValues,
});
expect(shallow(<ProductRecommendations />)).toMatchSnapshot();
});
it('renders the LoadedView with course data if the request completed', () => {
useWindowSize.mockReturnValueOnce(desktopWindowSize);
hooks.useProductRecommendationsData.mockReturnValueOnce({
...successfullLoadValues,
});
expect(shallow(<ProductRecommendations />)).toMatchObject(
shallow(
<LoadedView
openCourses={mockResponse.amplitudeCourses}
crossProductCourses={mockResponse.crossProductCourses}
/>,
),
);
});
it('renders the LoadingView if the request is pending', () => {
useWindowSize.mockReturnValueOnce(desktopWindowSize);
hooks.useProductRecommendationsData.mockReturnValueOnce({
...defaultValues,
isLoading: true,
});
expect(shallow(<ProductRecommendations />)).toMatchObject(
shallow(<LoadingView />),
);
});
it('renders nothing if the request has failed', () => {
useWindowSize.mockReturnValueOnce(desktopWindowSize);
hooks.useProductRecommendationsData.mockReturnValueOnce({
...defaultValues,
hasFailed: true,
});
const wrapper = shallow(<ProductRecommendations />);
expect(wrapper.type()).toBeNull();
});
it('renders nothing if the width of the screen size is less than 576px (mobile view)', () => {
useWindowSize.mockReturnValueOnce({ width: 575, height: 976 });
hooks.useProductRecommendationsData.mockReturnValueOnce({
...successfullLoadValues,
});
const wrapper = shallow(<ProductRecommendations />);
expect(wrapper.type()).toBeNull();
});
});

View File

@@ -0,0 +1,41 @@
import { defineMessages } from '@edx/frontend-platform/i18n';
const messages = defineMessages({
recommendationsHeading: {
id: 'ProductRecommendations.recommendationsHeading',
defaultMessage: 'You might also like',
description: 'Title for a list of recommended courses',
},
executiveEducationHeading: {
id: 'ProductRecommendations.executiveEducationHeading',
defaultMessage: 'Executive Education',
description: 'Heading for an executive education course recommendation',
},
executiveEducationDescription: {
id: 'ProductRecommendations.executiveEducationDescription',
defaultMessage: 'Short Courses to develop leadership skills',
description: 'Short description of an executive education course',
},
bootcampHeading: {
id: 'ProductRecommendations.bootcampHeading',
defaultMessage: 'Boot Camp',
description: 'Heading for a bootcamp course recommendation',
},
bootcampDescription: {
id: 'ProductRecommendations.bootcampDescription',
defaultMessage: 'Intensive, hands-on, project based training',
description: 'Short description of a bootcamp course',
},
courseHeading: {
id: 'ProductRecommendations.courseHeading',
defaultMessage: 'Courses',
description: 'Heading for an open course recommendation',
},
courseDescription: {
id: 'ProductRecommendations.courseDescription',
defaultMessage: 'Find new interests and advance your career',
description: 'Heading for an open course recommendation',
},
});
export default messages;

View File

@@ -0,0 +1,31 @@
export const getCoursesWithType = (courseTypes) => {
const courses = [];
courseTypes.forEach((type) => {
courses.push({
title: 'Introduction to Computer Science',
image: {
src: 'https://www.image-2.com/ed79a49b-64c1-48d2-afdc-054bf921e38d-6a76ceb47dea.small.jpg',
},
prospectusPath: 'course/introduction-to-computer-sceince',
owners: [
{
key: 'HarvardX',
name: 'Harvard University',
logoImageUrl: 'http://www.image.com/ef72daf3-c9a1-4c00-ba37-b3514392bdcf-8839c516815a.png',
},
],
courseType: type,
});
});
return courses;
};
export const mockCrossProductCourses = getCoursesWithType(['executive-education-2u', 'bootcamp-2u']);
export const mockOpenCourses = getCoursesWithType(['verified-audit', 'audit', 'verified', 'course']);
export const mockResponse = {
crossProductCourses: mockCrossProductCourses,
amplitudeCourses: mockOpenCourses,
};

View File

@@ -0,0 +1,41 @@
import PropTypes from 'prop-types';
export const courseShape = {
uuid: PropTypes.string,
title: PropTypes.string,
image: PropTypes.shape({
src: PropTypes.string,
}),
prospectusPath: PropTypes.string,
owners: PropTypes.arrayOf(
PropTypes.shape({
key: PropTypes.string,
name: PropTypes.string,
logoImageUrl: PropTypes.string,
}),
),
activeCourseRun: PropTypes.shape({
key: PropTypes.string,
marketingUrl: PropTypes.string,
}),
courseType: PropTypes.string,
};
export const courseTypeToProductTypeMap = {
course: 'Course',
'verified-audit': 'Course',
verified: 'Course',
audit: 'Course',
'credit-verified-audit': 'Course',
'spoc-verified-audit': 'Course',
professional: 'Professional Certificate',
'bootcamp-2u': 'Boot Camp',
'executive-education-2u': 'Executive Education',
'executive-education': 'Executive Education',
masters: "Master's",
'masters-verified-audit': "Master's",
};
export const wait = (time) => new Promise((resolve) => {
setTimeout(resolve, time);
});