Skip to content
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .env.development
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
NODE_ENV='development'
PORT=8080
PORT=1998
ACCESS_TOKEN_COOKIE_NAME='edx-jwt-cookie-header-payload'
BASE_URL='http://localhost:8080'
BASE_URL='http://localhost:1998'
CREDENTIALS_BASE_URL='http://localhost:18150'
CSRF_TOKEN_API_PATH='/csrf/api/v1/token'
ECOMMERCE_BASE_URL='http://localhost:18130'
Expand Down
2 changes: 1 addition & 1 deletion .env.test
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
ACCESS_TOKEN_COOKIE_NAME='edx-jwt-cookie-header-payload'
BASE_URL='http://localhost:1995'
BASE_URL='http://localhost:1998'
CREDENTIALS_BASE_URL='http://localhost:18150'
CSRF_TOKEN_API_PATH='/csrf/api/v1/token'
ECOMMERCE_BASE_URL='http://localhost:18130'
Expand Down
4 changes: 2 additions & 2 deletions jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ module.exports = createConfig('jest', {
// setupFilesAfterEnv is used after the jest environment has been loaded. In general this is what you want.
// If you want to add config BEFORE jest loads, use setupFiles instead.
setupFilesAfterEnv: [
'<rootDir>/src/setupTest.js',
'<rootDir>/src/setupTest.tsx',
],
coveragePathIgnorePatterns: [
'src/setupTest.js',
'src/setupTest.tsx',
'src/i18n',
],
});
Expand Down
63 changes: 49 additions & 14 deletions src/App.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { render, screen } from './setupTest';
import { mockCourseDiscoveryResponse } from './сatalog/__mocks__';
import messages from './сatalog/messages';
import { useCourseDiscovery } from './сatalog/data/hooks';
import { render, within, waitFor } from './setupTest';
import { ROUTES } from './routes';
import App from './App';

Expand All @@ -11,12 +14,14 @@ jest.mock('@edx/frontend-platform', () => ({
})),
}));

jest.mock('@edx/frontend-platform/react', () => ({
AppProvider: ({ children }: { children: React.ReactNode }) => <div data-testid="app-provider">{children}</div>,
jest.mock('./сatalog/data/hooks', () => ({
useCourseDiscovery: jest.fn(),
}));

jest.mock('@openedx/paragon', () => ({
Container: ({ children }: { children: React.ReactNode }) => <div>{children}</div>,
const mockCourseDiscovery = useCourseDiscovery as jest.Mock;

jest.mock('@edx/frontend-platform/react', () => ({
AppProvider: ({ children }: { children: React.ReactNode }) => <div data-testid="app-provider">{children}</div>,
}));

jest.mock('@edx/frontend-component-header', () => function getHeader() {
Expand All @@ -32,31 +37,61 @@ describe('App', () => {
document.body.innerHTML = '';
});

mockCourseDiscovery.mockReturnValue({
data: mockCourseDiscoveryResponse,
isLoading: false,
isError: false,
});

it('renders HomePage on "/" route', () => {
window.testHistory = [ROUTES.HOME];

render(<App />);
expect(screen.getByTestId('home-page')).toBeInTheDocument();
const { getByTestId } = render(<App />);
expect(getByTestId('home-page')).toBeInTheDocument();
});

it('renders CatalogPage on "/courses" route', () => {
it('renders CatalogPage with course cards at /courses route', async () => {
window.testHistory = [ROUTES.COURSES];

render(<App />);
expect(screen.getByTestId('catalog-page')).toBeInTheDocument();
const { getByText, getAllByRole, queryByTestId } = render(<App />);

await waitFor(() => {
expect(queryByTestId('spinner')).not.toBeInTheDocument();
});

expect(
getByText(
messages.totalCoursesHeading.defaultMessage.replace(
'{totalCourses}',
mockCourseDiscoveryResponse.results.length,
),
),
).toBeInTheDocument();

const courseCards = getAllByRole('link');
expect(courseCards.length).toBe(mockCourseDiscoveryResponse.results.length);

courseCards.forEach((card, index) => {
const course = mockCourseDiscoveryResponse.results[index];
const cardContent = within(card);

expect(card).toHaveAttribute('href', `/courses/${course.id}/about`);
expect(cardContent.getByText(course.data.content.displayName)).toBeInTheDocument();
expect(cardContent.getByText(course.data.org)).toBeInTheDocument();
});
});

it('renders CourseAboutPage on "/courses/some-course-id/about"', () => {
window.testHistory = [ROUTES.COURSE_ABOUT];

render(<App />);
expect(screen.getByTestId('course-about-page')).toBeInTheDocument();
const { getByTestId } = render(<App />);
expect(getByTestId('course-about-page')).toBeInTheDocument();
});

it('renders NotFoundPage on unknown route', () => {
window.testHistory = ['/some-unknown-path'];

render(<App />);
expect(screen.getByTestId('not-found-page')).toBeInTheDocument();
const { getByTestId } = render(<App />);
expect(getByTestId('not-found-page')).toBeInTheDocument();
});
});
20 changes: 20 additions & 0 deletions src/__mocks__/course.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export const mockCourseResponse = {
id: 'course-v1:edX+DemoX+Demo_Course',
data: {
id: 'course-v1:edX+DemoX+Demo_Course',
course: 'Demo Course',
start: '2024-04-01T00:00:00Z',
imageUrl: '/asset-v1:edX+DemoX+Demo_Course+type@asset+block@course_image.jpg',
org: 'edX',
orgImg: '/asset-v1:edX+DemoX+Demo_Course+type@asset+block@org_image.jpg',
content: {
displayName: 'Demonstration Course',
overview: 'Course overview',
number: 'DemoX',
},
number: 'DemoX',
modes: ['audit', 'verified'],
language: 'en',
catalogVisibility: 'both',
},
};
1 change: 1 addition & 0 deletions src/__mocks__/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { mockCourseResponse } from './course';
Binary file added src/assets/no-course-image.jpg

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be possible to use SVGs for this instead?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replaced, good idea

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/assets/no-org-image.jpg
Comment thread
brian-smith-tcril marked this conversation as resolved.
Resolved
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
26 changes: 26 additions & 0 deletions src/custom.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
type StaticImageData = string;

declare module '*.png' {
const content: StaticImageData;
export default content;
}

declare module '*.jpg' {
const content: StaticImageData;
export default content;
}

declare module '*.jpeg' {
const content: StaticImageData;
export default content;
}

declare module '*.svg' {
const content: StaticImageData;
export default content;
}

declare module '*.gif' {
const content: StaticImageData;
export default content;
}
39 changes: 39 additions & 0 deletions src/generic/alert-notification/AlertNotification.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { render } from '../../setupTest';
import { AlertNotificationProps } from './types';
import { AlertNotification } from '.';

const renderComponent = (props: AlertNotificationProps) => render(<AlertNotification {...props} />);

describe('AlertNotification', () => {
it('renders with default props', () => {
const { getByText, getByRole } = renderComponent({
title: 'Test Title',
message: 'Test Message',
variant: 'info',
});

expect(getByText('Test Title')).toBeInTheDocument();
expect(getByText('Test Message')).toBeInTheDocument();
expect(getByRole('alert')).toHaveClass('alert-info');
});

it('renders with custom variant', () => {
const { getByRole } = renderComponent({
title: 'Warning Title',
message: 'Warning Message',
variant: 'warning',
});

expect(getByRole('alert')).toHaveClass('alert-warning');
});

it('displays the info icon', () => {
const { getByRole } = renderComponent({
title: 'Alert with Icon',
message: 'Has info icon',
variant: 'info',
});

expect(getByRole('alert').querySelector('svg')).toBeInTheDocument();
});
});
13 changes: 13 additions & 0 deletions src/generic/alert-notification/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Alert as BaseAlert } from '@openedx/paragon';
import { Info as InfoIcon } from '@openedx/paragon/icons';

import { AlertNotificationProps } from './types';

export const AlertNotification = ({
variant = 'info', title, message,
}: AlertNotificationProps) => (
<BaseAlert variant={variant} icon={InfoIcon}>
<BaseAlert.Heading>{title}</BaseAlert.Heading>
<p>{message}</p>
</BaseAlert>
);
Comment thread
peterkulko marked this conversation as resolved.
Resolved
5 changes: 5 additions & 0 deletions src/generic/alert-notification/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface AlertNotificationProps {
variant?: string;
title: string;
message: string;
}
55 changes: 55 additions & 0 deletions src/generic/course-card/CourseCard.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { getConfig } from '@edx/frontend-platform';

import { mockCourseResponse } from '../../__mocks__';
import { render } from '../../setupTest';
import { CourseCard } from '.';

import messages from './messages';

describe('CourseCard', () => {
const renderComponent = (course = mockCourseResponse) => render(
<CourseCard course={course} />,
);

it('renders course information correctly', () => {
const { getByText } = renderComponent();

expect(getByText(mockCourseResponse.data.content.displayName)).toBeInTheDocument();
expect(getByText(mockCourseResponse.data.org)).toBeInTheDocument();
expect(getByText('Starts: Apr 1, 2024')).toBeInTheDocument();
});

@diana-villalvazo-wgu diana-villalvazo-wgu Jun 16, 2025 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional: RTL generally recommend using the screen helper, it's shorter and helps if you also appreciate the shorter syntax and less need to pick which selectors to define, so instead of

const { getByText } = renderComponent();
expect( getByText(...) ).toBlah()

they encourage to do:

import { ..., screen } from '../setupTest';

renderComponent();
expect( screen.getByText(...) ).toBlah()

Again this is optional, not a requirement.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If RTL generally recommend, then I also support this idea 💯
Added screen for tests in this PR (other tests will be updated iteratively, when adding subsequent PRs)


it('renders course image with correct src and fallback', () => {
const { getByAltText } = renderComponent();

const image = getByAltText(mockCourseResponse.data.content.displayName);
expect(image).toHaveAttribute('src', `${getConfig().LMS_BASE_URL}${mockCourseResponse.data.imageUrl}`);
});

it('renders organization logo with correct src and fallback', () => {
const { getByAltText } = renderComponent();

const logo = getByAltText(mockCourseResponse.data.org);
expect(logo).toHaveAttribute('src', `${getConfig().LMS_BASE_URL}${mockCourseResponse.data.orgImg}`);
});

it('formats the link destination correctly', () => {
const { getByRole } = renderComponent();

const link = getByRole('link');
expect(link).toHaveAttribute('href', `/courses/${mockCourseResponse.id}/about`);
});

it('handles missing start date gracefully', () => {
const courseWithoutStart = {
...mockCourseResponse,
data: {
...mockCourseResponse.data,
start: '',
},
};
const { queryByText } = renderComponent(courseWithoutStart);

expect(queryByText(messages.startDate.defaultMessage.replace('{startDate}', courseWithoutStart.data.start))).not.toBeInTheDocument();
});
});
1 change: 1 addition & 0 deletions src/generic/course-card/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const DATE_FORMAT_OPTIONS = { month: 'short', day: 'numeric', year: 'numeric' } as const;
32 changes: 32 additions & 0 deletions src/generic/course-card/index.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
.course-card.pgn__card {
&.course-card-desktop {
width: var(--catalog-generic-course-card-desktop-width, 24.75rem);
Comment thread
brian-smith-tcril marked this conversation as resolved.
Resolved
}

.pgn__card-wrapper-image-cap.vertical {
max-height: var(--catalog-generic-course-card-desktop-image-height, 8.75rem);
Comment thread
brian-smith-tcril marked this conversation as resolved.
Resolved
}

.pgn__card-header {
padding: var(--pgn-spacing-spacer-0) var(--pgn-spacing-spacer-4);
margin-bottom: var(--pgn-spacing-spacer-4);

.pgn__card-header-content {
margin-top: var(--pgn-spacing-spacer-4);
}

.pgn__card-header-title-md {
margin-bottom: var(--pgn-spacing-spacer-1);
line-height: 1.75rem;
}

.pgn__card-header-subtitle-md {
margin-top: var(--pgn-spacing-spacer-0);
}
}

.pgn__card-footer {
padding-left: var(--pgn-spacing-spacer-4);
padding-right: var(--pgn-spacing-spacer-4);
}
}
49 changes: 49 additions & 0 deletions src/generic/course-card/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { Link } from 'react-router-dom';
import { Card, useMediaQuery, breakpoints } from '@openedx/paragon';
import { useIntl } from '@edx/frontend-platform/i18n';

import { CourseCardProps } from './types';
import messages from './messages';
import { getFullImageUrl } from './utils';
import { DATE_FORMAT_OPTIONS } from './constants';

import noCourseImg from '../../assets/no-course-image.jpg';
import noOrgImg from '../../assets/no-org-image.jpg';

export const CourseCard = ({ course }: CourseCardProps) => {
const intl = useIntl();
const isExtraSmall = useMediaQuery({ maxWidth: breakpoints.small.maxWidth });

const formattedDate = course?.data?.start
? intl.formatDate(new Date(course.data.start), DATE_FORMAT_OPTIONS)
: '';

return (
<Card
as={Link}
to={`/courses/${course.id}/about`}
className={`course-card ${isExtraSmall ? 'w-100' : 'course-card-desktop'}`}
isClickable
>
<Card.ImageCap
src={getFullImageUrl(course.data.imageUrl)}
fallbackSrc={noCourseImg}
srcAlt={course.data.content.displayName}
logoSrc={course.data.orgImg ? getFullImageUrl(course.data.orgImg) : undefined}
fallbackLogoSrc={!course.data.orgImg && noOrgImg}
logoAlt={course.data.org}
/>
<Card.Header
title={course.data.content.displayName}
subtitle={course.data.org}
/>
{formattedDate && (
<Card.Footer className="justify-content-start">
{intl.formatMessage(messages.startDate, {
startDate: formattedDate,
})}
</Card.Footer>
)}
</Card>
);
};
11 changes: 11 additions & 0 deletions src/generic/course-card/messages.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { defineMessages } from '@edx/frontend-platform/i18n';

const messages = defineMessages({
startDate: {
id: 'generic.course-card.start-date',
defaultMessage: 'Starts: {startDate}',
description: 'Start date',
},
});

export default messages;
Loading