diff --git a/src/components/dashboard/main-content/course-enrollments/course-cards/unenroll/UnenrollModal.test.jsx b/src/components/dashboard/main-content/course-enrollments/course-cards/unenroll/UnenrollModal.test.jsx index 5f20835d6..ff293e947 100644 --- a/src/components/dashboard/main-content/course-enrollments/course-cards/unenroll/UnenrollModal.test.jsx +++ b/src/components/dashboard/main-content/course-enrollments/course-cards/unenroll/UnenrollModal.test.jsx @@ -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(), @@ -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(); @@ -47,6 +63,10 @@ const UnenrollModalWrapper = ({ ...props }) => { queryEnterpriseCourseEnrollments(mockEnterpriseCustomer.uuid).queryKey, [mockEnterpriseCourseEnrollment], ); + mockQueryClient.setQueryData( + queryEnterpriseLearnerDashboardBFF({ enterpriseSlug: 'test-enterprise-slug' }).queryKey, + { ...learnerDashboardBFFResponse, enterpriseCourseEnrollments: [mockEnterpriseCourseEnrollment] }, + ); return ( @@ -60,6 +80,7 @@ describe('', () => { beforeEach(() => { jest.clearAllMocks(); useEnterpriseCustomer.mockReturnValue({ data: mockEnterpriseCustomer }); + isBFFEnabledForEnterpriseCustomer.mockReturnValue(false); }); test('should remain closed when `isOpen` is false', () => { @@ -92,7 +113,7 @@ describe('', () => { expect(mockOnClose).toHaveBeenCalledTimes(1); }); - test('should handle unenroll click', async () => { + test('should handle unenroll click, non-BFF', async () => { unenrollFromCourse.mockResolvedValueOnce(); const props = { ...baseUnenrollModalProps, @@ -111,4 +132,25 @@ describe('', () => { 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(); + 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.'); + }); + }); });