|
| 1 | +import React from 'react'; |
| 2 | +import { describe, expect, it, vi } from 'vitest'; |
| 3 | +import { render, screen } from '@testing-library/react'; |
| 4 | +import userEvent from '@testing-library/user-event'; |
| 5 | +import Workspace2ClosePromptModal from './workspace2-close-prompt.modal'; |
| 6 | + |
| 7 | +vi.mock('react-i18next', () => ({ |
| 8 | + Trans: ({ |
| 9 | + defaults, |
| 10 | + values, |
| 11 | + components, |
| 12 | + }: { |
| 13 | + defaults: string; |
| 14 | + values?: Record<string, string>; |
| 15 | + components?: Record<string, React.ReactElement>; |
| 16 | + }) => { |
| 17 | + let text = defaults; |
| 18 | + for (const [key, value] of Object.entries(values ?? {})) { |
| 19 | + text = text.replaceAll(`{{${key}}}`, value); |
| 20 | + } |
| 21 | + const parts = text.split(/<strong>(.*?)<\/strong>/); |
| 22 | + return ( |
| 23 | + <>{parts.map((part, i) => (i % 2 === 1 ? React.cloneElement(components.strong, { key: i }, part) : part))}</> |
| 24 | + ); |
| 25 | + }, |
| 26 | +})); |
| 27 | + |
| 28 | +describe('Workspace2ClosePromptModal', () => { |
| 29 | + it('renders a sentence with the bolded workspace title when one workspace is affected', () => { |
| 30 | + render( |
| 31 | + <Workspace2ClosePromptModal |
| 32 | + onConfirm={vi.fn()} |
| 33 | + onCancel={vi.fn()} |
| 34 | + affectedWorkspaceTitles={['Add drug order']} |
| 35 | + />, |
| 36 | + ); |
| 37 | + |
| 38 | + expect(screen.getByText('Discard unsaved changes?')).toBeInTheDocument(); |
| 39 | + expect(screen.getByText('Add drug order')).toBeInTheDocument(); |
| 40 | + expect(screen.getByText('Add drug order').tagName).toBe('STRONG'); |
| 41 | + expect(screen.getByText(/has unsaved changes\. Closing it will discard them\./)).toBeInTheDocument(); |
| 42 | + expect(screen.queryByRole('list')).not.toBeInTheDocument(); |
| 43 | + }); |
| 44 | + |
| 45 | + it('renders a count and a list of bolded workspace titles when multiple workspaces are affected', () => { |
| 46 | + render( |
| 47 | + <Workspace2ClosePromptModal |
| 48 | + onConfirm={vi.fn()} |
| 49 | + onCancel={vi.fn()} |
| 50 | + affectedWorkspaceTitles={['Add drug order', 'Visit note', 'Record vitals']} |
| 51 | + />, |
| 52 | + ); |
| 53 | + |
| 54 | + expect(screen.getByText('Discard unsaved changes?')).toBeInTheDocument(); |
| 55 | + expect( |
| 56 | + screen.getByText('3 workspaces have unsaved changes. Closing them will discard the changes:'), |
| 57 | + ).toBeInTheDocument(); |
| 58 | + const listItems = screen.getAllByRole('listitem'); |
| 59 | + expect(listItems).toHaveLength(3); |
| 60 | + expect(listItems.map((item) => item.textContent)).toEqual(['Add drug order', 'Visit note', 'Record vitals']); |
| 61 | + for (const title of ['Add drug order', 'Visit note', 'Record vitals']) { |
| 62 | + expect(screen.getByText(title).tagName).toBe('STRONG'); |
| 63 | + } |
| 64 | + }); |
| 65 | + |
| 66 | + it('calls onCancel when clicking "Keep editing"', async () => { |
| 67 | + const user = userEvent.setup(); |
| 68 | + const onCancel = vi.fn(); |
| 69 | + render( |
| 70 | + <Workspace2ClosePromptModal |
| 71 | + onConfirm={vi.fn()} |
| 72 | + onCancel={onCancel} |
| 73 | + affectedWorkspaceTitles={['Add drug order']} |
| 74 | + />, |
| 75 | + ); |
| 76 | + |
| 77 | + await user.click(screen.getByRole('button', { name: 'Keep editing' })); |
| 78 | + expect(onCancel).toHaveBeenCalledTimes(1); |
| 79 | + }); |
| 80 | + |
| 81 | + it('calls onCancel when clicking the header close button', async () => { |
| 82 | + const user = userEvent.setup(); |
| 83 | + const onCancel = vi.fn(); |
| 84 | + render( |
| 85 | + <Workspace2ClosePromptModal |
| 86 | + onConfirm={vi.fn()} |
| 87 | + onCancel={onCancel} |
| 88 | + affectedWorkspaceTitles={['Add drug order']} |
| 89 | + />, |
| 90 | + ); |
| 91 | + |
| 92 | + await user.click(screen.getByRole('button', { name: /close/i })); |
| 93 | + expect(onCancel).toHaveBeenCalledTimes(1); |
| 94 | + }); |
| 95 | + |
| 96 | + it('calls onConfirm when clicking "Discard changes"', async () => { |
| 97 | + const user = userEvent.setup(); |
| 98 | + const onConfirm = vi.fn(); |
| 99 | + render( |
| 100 | + <Workspace2ClosePromptModal |
| 101 | + onConfirm={onConfirm} |
| 102 | + onCancel={vi.fn()} |
| 103 | + affectedWorkspaceTitles={['Add drug order']} |
| 104 | + />, |
| 105 | + ); |
| 106 | + |
| 107 | + await user.click(screen.getByRole('button', { name: /discard changes/i })); |
| 108 | + expect(onConfirm).toHaveBeenCalledTimes(1); |
| 109 | + }); |
| 110 | +}); |
0 commit comments