|
1 | 1 | import { mount } from 'enzyme'; |
2 | 2 | import { IntlProvider } from '@edx/frontend-platform/i18n'; |
| 3 | +import { act } from 'react-dom/test-utils'; |
3 | 4 | import CoursesTable from './CoursesTable'; |
| 5 | +import * as api from './data/api'; |
4 | 6 |
|
5 | 7 | export const intlProviderWrapper = (component) => ( |
6 | 8 | <IntlProvider locale="en" messages={{}}> |
@@ -359,4 +361,138 @@ describe('CoursesTable', () => { |
359 | 361 | expect(wrapper.text()).toContain('No results found'); |
360 | 362 | }); |
361 | 363 | }); |
| 364 | + |
| 365 | + describe('CoursesTable save workflow', () => { |
| 366 | + const mockCourses = [ |
| 367 | + { |
| 368 | + course_name: 'Test Course A', |
| 369 | + number: 'CS101', |
| 370 | + run: 'run1', |
| 371 | + status: 'active', |
| 372 | + role: 'staff', |
| 373 | + org: 'edx', |
| 374 | + course_url: 'https://example.com/course-a', |
| 375 | + }, |
| 376 | + { |
| 377 | + course_name: 'Test Course B', |
| 378 | + number: 'CS102', |
| 379 | + run: 'run2', |
| 380 | + status: 'archived', |
| 381 | + role: 'instructor', |
| 382 | + org: 'mitx', |
| 383 | + course_url: 'https://example.com/course-b', |
| 384 | + }, |
| 385 | + { |
| 386 | + course_name: 'Test Course c', |
| 387 | + number: 'CS103', |
| 388 | + run: 'run3', |
| 389 | + status: 'active', |
| 390 | + org: 'harvard', |
| 391 | + role: null, |
| 392 | + course_url: 'https://example.com/course-c', |
| 393 | + }, |
| 394 | + { |
| 395 | + course_name: 'Test Course d', |
| 396 | + number: 'CS104', |
| 397 | + run: 'run4', |
| 398 | + status: 'active', |
| 399 | + org: 'harvard', |
| 400 | + role: null, |
| 401 | + course_url: 'https://example.com/course-d', |
| 402 | + }, |
| 403 | + { |
| 404 | + course_name: 'Test Course e', |
| 405 | + number: 'CS105', |
| 406 | + run: 'run5', |
| 407 | + status: 'active', |
| 408 | + org: 'harvard', |
| 409 | + role: null, |
| 410 | + course_url: 'https://example.com/course-e', |
| 411 | + }, |
| 412 | + { |
| 413 | + course_name: 'Test Course f', |
| 414 | + number: 'CS106', |
| 415 | + run: 'run6', |
| 416 | + status: 'active', |
| 417 | + org: 'harvard', |
| 418 | + role: null, |
| 419 | + course_url: 'https://example.com/course-f', |
| 420 | + }, |
| 421 | + { |
| 422 | + course_name: 'Test Course g', |
| 423 | + number: 'CS107', |
| 424 | + run: 'run7', |
| 425 | + status: 'active', |
| 426 | + org: 'harvard', |
| 427 | + role: null, |
| 428 | + course_url: 'https://example.com/course-g', |
| 429 | + }, |
| 430 | + ]; |
| 431 | + |
| 432 | + const setCourseUpdateErrorsMock = jest.fn(); |
| 433 | + const defaultProps = { |
| 434 | + userCourses: mockCourses, |
| 435 | + username: 'test', |
| 436 | + |
| 437 | + setCourseUpdateErrors: setCourseUpdateErrorsMock, |
| 438 | + }; |
| 439 | + |
| 440 | + beforeEach(() => { |
| 441 | + jest.spyOn(api, 'updateUserRolesInCourses').mockResolvedValue([]); // mock API success |
| 442 | + }); |
| 443 | + |
| 444 | + afterEach(() => { |
| 445 | + jest.clearAllMocks(); |
| 446 | + }); |
| 447 | + |
| 448 | + it('opens ChangeConfirmationModal and confirms save', async () => { |
| 449 | + wrapper = mount(intlProviderWrapper(<CoursesTable {...defaultProps} />)); |
| 450 | + |
| 451 | + // make changes in course table |
| 452 | + wrapper.find('input[type="checkbox"]').at(1).simulate('change'); |
| 453 | + wrapper.find('input[type="checkbox"]').at(3).simulate('change'); |
| 454 | + wrapper.find('input[type="checkbox"]').at(4).simulate('change'); |
| 455 | + wrapper.find('input[type="checkbox"]').at(5).simulate('change'); |
| 456 | + wrapper.find('input[type="checkbox"]').at(6).simulate('change'); |
| 457 | + wrapper.find('input[type="checkbox"]').at(7).simulate('change'); |
| 458 | + wrapper.find('[data-testid="role-dropdown-run2"]').at(0).simulate('click'); |
| 459 | + wrapper.find('[data-testid="role-dropdown-item-staff-run2"]').at(0).simulate('click'); |
| 460 | + |
| 461 | + // open, then close, then re-open modal and also try show more course changes |
| 462 | + wrapper.find('[data-testid="save-course-changes"]').at(0).simulate('click'); |
| 463 | + wrapper.find('[data-testid="cancel-save-course-changes"]').at(0).simulate('click'); |
| 464 | + wrapper.find('[data-testid="save-course-changes"]').at(0).simulate('click'); |
| 465 | + wrapper.find('[data-testid="show-more-changes"]').at(0).simulate('click'); |
| 466 | + |
| 467 | + // confirm save in modal |
| 468 | + wrapper.find('[data-testid="confirm-save-course-changes"]').at(0).simulate('click'); |
| 469 | + |
| 470 | + // wait for async useEffect |
| 471 | + jest.useFakeTimers(); |
| 472 | + await act(async () => { |
| 473 | + await Promise.resolve(); |
| 474 | + jest.runAllTimers(); // for the setTimeout in useEffect |
| 475 | + }); |
| 476 | + |
| 477 | + wrapper.update(); |
| 478 | + |
| 479 | + expect(api.updateUserRolesInCourses).toHaveBeenCalledWith({ |
| 480 | + userEmail: defaultProps.email, |
| 481 | + changedCourses: expect.any(Object), |
| 482 | + }); |
| 483 | + }); |
| 484 | + it('adds beforeunload listener and prevents unload when there are unsaved changes', () => { |
| 485 | + wrapper = mount(intlProviderWrapper(<CoursesTable {...defaultProps} />)); |
| 486 | + // make changes in course table and it will set hasUnsavedChangesRef.current to true |
| 487 | + wrapper.find('input[type="checkbox"]').at(1).simulate('change'); |
| 488 | + const event = new Event('beforeunload'); |
| 489 | + Object.defineProperty(event, 'returnValue', { |
| 490 | + writable: true, |
| 491 | + value: undefined, |
| 492 | + }); |
| 493 | + window.dispatchEvent(event); |
| 494 | + |
| 495 | + expect(event.returnValue).toBe(''); |
| 496 | + }); |
| 497 | + }); |
362 | 498 | }); |
0 commit comments