|
3 | 3 | * @jest-environment jsdom |
4 | 4 | */ |
5 | 5 |
|
| 6 | +import Swal from 'sweetalert2' |
6 | 7 | import { defineCaseContactsTable } from '../src/dashboard' |
| 8 | +import { fireSwalFollowupAlert } from '../src/case_contact' |
| 9 | +jest.mock('sweetalert2', () => ({ |
| 10 | + __esModule: true, |
| 11 | + default: { fire: jest.fn() } |
| 12 | +})) |
| 13 | + |
| 14 | +jest.mock('../src/case_contact', () => ({ |
| 15 | + fireSwalFollowupAlert: jest.fn() |
| 16 | +})) |
7 | 17 |
|
8 | 18 | // Mock DataTable |
9 | 19 | const mockDataTable = jest.fn() |
@@ -382,10 +392,259 @@ describe('defineCaseContactsTable', () => { |
382 | 392 | expect(columns[10].searchable).toBe(false) |
383 | 393 | }) |
384 | 394 |
|
385 | | - it('renders ellipsis icon', () => { |
386 | | - const rendered = columns[10].render(null, 'display', {}) |
| 395 | + it('renders a button toggle with aria-label containing the contact date', () => { |
| 396 | + const row = { id: '1', occurred_at: 'July 01, 2024', can_edit: 'true', can_destroy: 'true', edit_path: '/case_contacts/1/edit', followup_id: '' } |
| 397 | + const rendered = columns[10].render(null, 'display', row) |
| 398 | + |
| 399 | + expect(rendered).toContain('class="fas fa-ellipsis-v"') |
| 400 | + expect(rendered).toContain('aria-label="Actions for case contact on July 01, 2024"') |
| 401 | + expect(rendered).toContain('type="button"') |
| 402 | + }) |
| 403 | + |
| 404 | + it('renders the ellipsis icon as aria-hidden', () => { |
| 405 | + const row = { id: '1', occurred_at: 'July 01, 2024', can_edit: 'true', can_destroy: 'true', edit_path: '/case_contacts/1/edit', followup_id: '' } |
| 406 | + const rendered = columns[10].render(null, 'display', row) |
| 407 | + |
| 408 | + expect(rendered).toContain('aria-hidden="true"') |
| 409 | + }) |
| 410 | + |
| 411 | + it('renders Edit item when can_edit is "true"', () => { |
| 412 | + const row = { id: '1', occurred_at: 'July 01, 2024', can_edit: 'true', can_destroy: 'false', edit_path: '/case_contacts/1/edit', followup_id: '' } |
| 413 | + const rendered = columns[10].render(null, 'display', row) |
| 414 | + |
| 415 | + expect(rendered).toContain('href="/case_contacts/1/edit"') |
| 416 | + expect(rendered).toContain('Edit') |
| 417 | + }) |
| 418 | + |
| 419 | + it('renders Edit as disabled when can_edit is "false"', () => { |
| 420 | + const row = { id: '1', occurred_at: 'July 01, 2024', can_edit: 'false', can_destroy: 'false', edit_path: '/case_contacts/1/edit', followup_id: '' } |
| 421 | + const rendered = columns[10].render(null, 'display', row) |
| 422 | + |
| 423 | + expect(rendered).toContain('Edit') |
| 424 | + expect(rendered).toContain('disabled') |
| 425 | + expect(rendered).toContain('aria-disabled="true"') |
| 426 | + expect(rendered).not.toContain('href="/case_contacts/1/edit"') |
| 427 | + }) |
| 428 | + |
| 429 | + it('renders Delete item when can_destroy is "true"', () => { |
| 430 | + const row = { id: '1', occurred_at: 'July 01, 2024', can_edit: 'false', can_destroy: 'true', edit_path: '/case_contacts/1/edit', followup_id: '' } |
| 431 | + const rendered = columns[10].render(null, 'display', row) |
| 432 | + |
| 433 | + expect(rendered).toContain('cc-delete-action') |
| 434 | + expect(rendered).toContain('data-id="1"') |
| 435 | + expect(rendered).toContain('Delete') |
| 436 | + }) |
| 437 | + |
| 438 | + it('renders Delete as disabled when can_destroy is "false"', () => { |
| 439 | + const row = { id: '1', occurred_at: 'July 01, 2024', can_edit: 'false', can_destroy: 'false', edit_path: '/case_contacts/1/edit', followup_id: '' } |
| 440 | + const rendered = columns[10].render(null, 'display', row) |
| 441 | + |
| 442 | + expect(rendered).toContain('Delete') |
| 443 | + expect(rendered).toContain('disabled') |
| 444 | + expect(rendered).toContain('aria-disabled="true"') |
| 445 | + expect(rendered).not.toContain('cc-delete-action') |
| 446 | + }) |
| 447 | + |
| 448 | + it('renders Set Reminder when followup_id is empty', () => { |
| 449 | + const row = { id: '1', occurred_at: 'July 01, 2024', can_edit: 'false', can_destroy: 'false', edit_path: '/case_contacts/1/edit', followup_id: '' } |
| 450 | + const rendered = columns[10].render(null, 'display', row) |
| 451 | + |
| 452 | + expect(rendered).toContain('cc-set-reminder-action') |
| 453 | + expect(rendered).toContain('Set Reminder') |
| 454 | + expect(rendered).not.toContain('Resolve Reminder') |
| 455 | + }) |
| 456 | + |
| 457 | + it('renders Resolve Reminder when followup_id is present', () => { |
| 458 | + const row = { id: '1', occurred_at: 'July 01, 2024', can_edit: 'false', can_destroy: 'false', edit_path: '/case_contacts/1/edit', followup_id: '42' } |
| 459 | + const rendered = columns[10].render(null, 'display', row) |
| 460 | + |
| 461 | + expect(rendered).toContain('cc-resolve-reminder-action') |
| 462 | + expect(rendered).toContain('data-followup-id="42"') |
| 463 | + expect(rendered).toContain('Resolve Reminder') |
| 464 | + expect(rendered).not.toContain('Set Reminder') |
| 465 | + }) |
| 466 | + |
| 467 | + it('always renders the reminder menu item', () => { |
| 468 | + const row = { id: '1', occurred_at: 'July 01, 2024', can_edit: 'false', can_destroy: 'false', edit_path: '/case_contacts/1/edit', followup_id: '' } |
| 469 | + const rendered = columns[10].render(null, 'display', row) |
| 470 | + |
| 471 | + expect(rendered).toMatch(/Set Reminder|Resolve Reminder/) |
| 472 | + }) |
| 473 | + }) |
| 474 | + }) |
| 475 | + |
| 476 | + describe('click handlers', () => { |
| 477 | + let mockAjaxReload |
| 478 | + let mockTableInstance |
| 479 | + |
| 480 | + const clickActionButton = (action, attrs = {}) => { |
| 481 | + const dataAttrs = Object.entries(attrs).map(([k, v]) => `data-${k}="${v}"`).join(' ') |
| 482 | + $('table#case_contacts tbody').append( |
| 483 | + `<tr><td><button class="cc-${action}-action" ${dataAttrs}>${action}</button></td></tr>` |
| 484 | + ) |
| 485 | + $(`.cc-${action}-action`).trigger('click') |
| 486 | + } |
| 487 | + |
| 488 | + beforeEach(() => { |
| 489 | + mockAjaxReload = jest.fn() |
| 490 | + mockTableInstance = { ajax: { reload: mockAjaxReload } } |
| 491 | + mockDataTable.mockReturnValue(mockTableInstance) |
| 492 | + |
| 493 | + // Add CSRF meta tag |
| 494 | + document.head.innerHTML = '<meta name="csrf-token" content="test-csrf-token">' |
| 495 | + |
| 496 | + defineCaseContactsTable() |
| 497 | + }) |
| 498 | + |
| 499 | + afterEach(() => { |
| 500 | + Swal.fire.mockReset() |
| 501 | + fireSwalFollowupAlert.mockReset() |
| 502 | + }) |
| 503 | + |
| 504 | + describe('Delete action', () => { |
| 505 | + it('shows a SweetAlert confirmation dialog when cc-delete-action is clicked', () => { |
| 506 | + Swal.fire.mockResolvedValue({ isConfirmed: false }) |
| 507 | + |
| 508 | + clickActionButton('delete', { id: '42' }) |
| 509 | + |
| 510 | + expect(Swal.fire).toHaveBeenCalled() |
| 511 | + }) |
| 512 | + |
| 513 | + it('sends DELETE request when confirmed', async () => { |
| 514 | + Swal.fire.mockResolvedValue({ isConfirmed: true }) |
| 515 | + const ajaxSpy = jest.spyOn($, 'ajax').mockImplementation(({ success }) => success && success()) |
| 516 | + |
| 517 | + clickActionButton('delete', { id: '42' }) |
| 518 | + |
| 519 | + await Promise.resolve() |
| 520 | + |
| 521 | + expect(ajaxSpy).toHaveBeenCalledWith(expect.objectContaining({ |
| 522 | + url: '/case_contacts/42', |
| 523 | + type: 'DELETE', |
| 524 | + headers: { 'X-CSRF-Token': 'test-csrf-token', Accept: 'application/json' } |
| 525 | + })) |
| 526 | + expect(ajaxSpy.mock.calls[0][0]).not.toHaveProperty('dataType') |
| 527 | + |
| 528 | + ajaxSpy.mockRestore() |
| 529 | + }) |
| 530 | + |
| 531 | + it('does not send DELETE request when cancelled', async () => { |
| 532 | + Swal.fire.mockResolvedValue({ isConfirmed: false }) |
| 533 | + const ajaxSpy = jest.spyOn($, 'ajax').mockImplementation() |
| 534 | + |
| 535 | + clickActionButton('delete', { id: '42' }) |
| 536 | + |
| 537 | + await Promise.resolve() |
| 538 | + |
| 539 | + expect(ajaxSpy).not.toHaveBeenCalled() |
| 540 | + |
| 541 | + ajaxSpy.mockRestore() |
| 542 | + }) |
| 543 | + |
| 544 | + it('reloads the DataTable without resetting pagination after successful delete', async () => { |
| 545 | + Swal.fire.mockResolvedValue({ isConfirmed: true }) |
| 546 | + jest.spyOn($, 'ajax').mockImplementation(({ success }) => success && success()) |
| 547 | + |
| 548 | + clickActionButton('delete', { id: '42' }) |
| 549 | + |
| 550 | + await Promise.resolve() |
| 551 | + |
| 552 | + expect(mockAjaxReload).toHaveBeenCalledWith(null, false) |
| 553 | + }) |
| 554 | + }) |
| 555 | + |
| 556 | + describe('Set Reminder action', () => { |
| 557 | + it('calls fireSwalFollowupAlert when cc-set-reminder-action is clicked', () => { |
| 558 | + fireSwalFollowupAlert.mockResolvedValue({ isConfirmed: false }) |
| 559 | + |
| 560 | + clickActionButton('set-reminder', { id: '5' }) |
| 561 | + |
| 562 | + expect(fireSwalFollowupAlert).toHaveBeenCalled() |
| 563 | + }) |
| 564 | + |
| 565 | + it('posts to the followups endpoint with CSRF header when confirmed without a note', async () => { |
| 566 | + fireSwalFollowupAlert.mockResolvedValue({ value: '', isConfirmed: true }) |
| 567 | + const ajaxSpy = jest.spyOn($, 'ajax').mockImplementation(({ success }) => success && success()) |
| 568 | + |
| 569 | + clickActionButton('set-reminder', { id: '5' }) |
| 570 | + |
| 571 | + await Promise.resolve() |
| 572 | + |
| 573 | + expect(ajaxSpy).toHaveBeenCalledWith(expect.objectContaining({ |
| 574 | + url: '/case_contacts/5/followups', |
| 575 | + type: 'POST', |
| 576 | + data: {}, |
| 577 | + headers: { 'X-CSRF-Token': 'test-csrf-token', Accept: 'application/json' } |
| 578 | + })) |
| 579 | + |
| 580 | + ajaxSpy.mockRestore() |
| 581 | + }) |
| 582 | + |
| 583 | + it('posts with note when confirmed with a note', async () => { |
| 584 | + fireSwalFollowupAlert.mockResolvedValue({ value: 'My note', isConfirmed: true }) |
| 585 | + const ajaxSpy = jest.spyOn($, 'ajax').mockImplementation(({ success }) => success && success()) |
| 586 | + |
| 587 | + clickActionButton('set-reminder', { id: '5' }) |
| 588 | + |
| 589 | + await Promise.resolve() |
| 590 | + |
| 591 | + expect(ajaxSpy).toHaveBeenCalledWith(expect.objectContaining({ |
| 592 | + url: '/case_contacts/5/followups', |
| 593 | + type: 'POST', |
| 594 | + data: { note: 'My note' }, |
| 595 | + headers: { 'X-CSRF-Token': 'test-csrf-token', Accept: 'application/json' } |
| 596 | + })) |
| 597 | + |
| 598 | + ajaxSpy.mockRestore() |
| 599 | + }) |
| 600 | + |
| 601 | + it('does not post when cancelled', async () => { |
| 602 | + fireSwalFollowupAlert.mockResolvedValue({ isConfirmed: false }) |
| 603 | + const ajaxSpy = jest.spyOn($, 'ajax').mockImplementation() |
| 604 | + |
| 605 | + clickActionButton('set-reminder', { id: '5' }) |
| 606 | + |
| 607 | + await Promise.resolve() |
| 608 | + |
| 609 | + expect(ajaxSpy).not.toHaveBeenCalled() |
| 610 | + |
| 611 | + ajaxSpy.mockRestore() |
| 612 | + }) |
| 613 | + |
| 614 | + it('reloads the DataTable without resetting pagination after creating a reminder', async () => { |
| 615 | + fireSwalFollowupAlert.mockResolvedValue({ value: '', isConfirmed: true }) |
| 616 | + jest.spyOn($, 'ajax').mockImplementation(({ success }) => success && success()) |
| 617 | + |
| 618 | + clickActionButton('set-reminder', { id: '5' }) |
| 619 | + |
| 620 | + await Promise.resolve() |
| 621 | + |
| 622 | + expect(mockAjaxReload).toHaveBeenCalledWith(null, false) |
| 623 | + }) |
| 624 | + }) |
| 625 | + |
| 626 | + describe('Resolve Reminder action', () => { |
| 627 | + it('sends PATCH request when cc-resolve-reminder-action is clicked', () => { |
| 628 | + const ajaxSpy = jest.spyOn($, 'ajax').mockImplementation(({ success }) => success && success()) |
| 629 | + |
| 630 | + clickActionButton('resolve-reminder', { id: '5', 'followup-id': '42' }) |
| 631 | + |
| 632 | + expect(ajaxSpy).toHaveBeenCalledWith(expect.objectContaining({ |
| 633 | + url: '/followups/42/resolve', |
| 634 | + type: 'PATCH', |
| 635 | + headers: { 'X-CSRF-Token': 'test-csrf-token', Accept: 'application/json' } |
| 636 | + })) |
| 637 | + expect(ajaxSpy.mock.calls[0][0]).not.toHaveProperty('dataType') |
| 638 | + |
| 639 | + ajaxSpy.mockRestore() |
| 640 | + }) |
| 641 | + |
| 642 | + it('reloads the DataTable without resetting pagination after resolving a reminder', () => { |
| 643 | + jest.spyOn($, 'ajax').mockImplementation(({ success }) => success && success()) |
| 644 | + |
| 645 | + clickActionButton('resolve-reminder', { id: '5', 'followup-id': '42' }) |
387 | 646 |
|
388 | | - expect(rendered).toBe('<i class="fas fa-ellipsis-v"></i>') |
| 647 | + expect(mockAjaxReload).toHaveBeenCalledWith(null, false) |
389 | 648 | }) |
390 | 649 | }) |
391 | 650 | }) |
|
0 commit comments