Skip to content

Commit

Permalink
chore: Add net new testss
Browse files Browse the repository at this point in the history
  • Loading branch information
brobro10000 committed Dec 5, 2024
1 parent 9269b42 commit c11de81
Showing 1 changed file with 46 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,23 @@ import { render, screen, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { QueryClientProvider } from '@tanstack/react-query';
import '@testing-library/jest-dom/extend-expect';

import { COURSE_STATUSES } from '../../../../../../constants';
import { unenrollFromCourse } from './data';
import UnenrollModal from './UnenrollModal';
import { ToastsContext } from '../../../../../Toasts';
import { queryEnterpriseCourseEnrollments, useEnterpriseCustomer } from '../../../../../app/data';
import {
fetchEnterpriseLearnerDashboard,
isBFFEnabledForEnterpriseCustomer,
learnerDashboardBFFResponse,
queryEnterpriseCourseEnrollments,
queryEnterpriseLearnerDashboardBFF,
useEnterpriseCustomer,
} from '../../../../../app/data';
import { queryClient } from '../../../../../../utils/tests';
import { enterpriseCourseEnrollmentFactory, enterpriseCustomerFactory } from '../../../../../app/data/services/data/__factories__';
import {
enterpriseCourseEnrollmentFactory,
enterpriseCustomerFactory,
} from '../../../../../app/data/services/data/__factories__';

jest.mock('./data', () => ({
unenrollFromCourse: jest.fn(),
Expand All @@ -22,6 +31,13 @@ jest.mock('@edx/frontend-platform/logging', () => ({
jest.mock('../../../../../app/data', () => ({
...jest.requireActual('../../../../../app/data'),
useEnterpriseCustomer: jest.fn(),
isBFFEnabledForEnterpriseCustomer: jest.fn(),
fetchEnterpriseLearnerDashboard: jest.fn(),
}));

jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'),
useParams: () => ({ enterpriseSlug: 'test-enterprise-slug' }),
}));

const mockEnterpriseCustomer = enterpriseCustomerFactory();
Expand All @@ -47,6 +63,10 @@ const UnenrollModalWrapper = ({ ...props }) => {
queryEnterpriseCourseEnrollments(mockEnterpriseCustomer.uuid).queryKey,
[mockEnterpriseCourseEnrollment],
);
mockQueryClient.setQueryData(
queryEnterpriseLearnerDashboardBFF({ enterpriseSlug: 'test-enterprise-slug' }).queryKey,
{ ...learnerDashboardBFFResponse, enterpriseCourseEnrollments: [mockEnterpriseCourseEnrollment] },
);
return (
<QueryClientProvider client={mockQueryClient}>
<ToastsContext.Provider value={{ addToast: mockAddToast }}>
Expand All @@ -60,6 +80,7 @@ describe('<UnenrollModal />', () => {
beforeEach(() => {
jest.clearAllMocks();
useEnterpriseCustomer.mockReturnValue({ data: mockEnterpriseCustomer });
isBFFEnabledForEnterpriseCustomer.mockReturnValue(false);
});

test('should remain closed when `isOpen` is false', () => {
Expand Down Expand Up @@ -92,7 +113,7 @@ describe('<UnenrollModal />', () => {
expect(mockOnClose).toHaveBeenCalledTimes(1);
});

test('should handle unenroll click', async () => {
test('should handle unenroll click, non-BFF', async () => {
unenrollFromCourse.mockResolvedValueOnce();
const props = {
...baseUnenrollModalProps,
Expand All @@ -111,4 +132,25 @@ describe('<UnenrollModal />', () => {
expect(mockAddToast).toHaveBeenCalledWith('You have been unenrolled from the course.');
});
});

test('should handle unenroll click, BFF', async () => {
fetchEnterpriseLearnerDashboard.mockResolvedValueOnce(learnerDashboardBFFResponse);
isBFFEnabledForEnterpriseCustomer.mockReturnValue(true);
const props = {
...baseUnenrollModalProps,
isOpen: true,
};
render(<UnenrollModalWrapper {...props} />);
userEvent.click(screen.getByText('Unenroll'));

await waitFor(() => {
const updatedEnrollments = mockQueryClient.getQueryData(
queryEnterpriseLearnerDashboardBFF({ enterpriseSlug: 'test-enterprise-slug' }).queryKey,
);
expect(updatedEnrollments).toEqual(learnerDashboardBFFResponse);
expect(mockOnSuccess).toHaveBeenCalledTimes(1);
expect(mockAddToast).toHaveBeenCalledTimes(1);
expect(mockAddToast).toHaveBeenCalledWith('You have been unenrolled from the course.');
});
});
});

0 comments on commit c11de81

Please sign in to comment.