import { render, screen } from '@testing-library/react'; import userEvent from '@testing-library/user-event'; import { Truncate } from '../Truncate'; const showModalMock = jest.fn(); jest.mock('../../../../components/modal/ModalProvider', () => ({ useModalLauncher: () => showModalMock, })); describe('Truncate', () => { const shortContent = 'Hello'; const exactLengthContent = 'A'.repeat(80); // Exactly 80 characters const longContent = 'This is a very long text that exceeds the default maximum length of 80 characters and should be truncated'; beforeEach(() => { showModalMock.mockClear(); }); it('should render full text when content is shorter than 80 characters', () => { render(); expect(screen.getByText(shortContent)).toBeInTheDocument(); expect(screen.queryByRole('button')).not.toBeInTheDocument(); }); it('should render full text when content is exactly 80 characters', () => { render(); expect(screen.getByText(exactLengthContent)).toBeInTheDocument(); expect(screen.queryByRole('button')).not.toBeInTheDocument(); }); it('should truncate text when content is longer than 80 characters', () => { render(); expect(screen.getByText(`${longContent.slice(0, 80)}...`)).toBeInTheDocument(); expect(screen.getByRole('button', { name: 'more' })).toBeInTheDocument(); }); it('should open modal with full text when "more" is clicked', async () => { const user = userEvent.setup(); render(); await user.click(screen.getByRole('button', { name: 'more' })); expect(showModalMock).toHaveBeenCalledTimes(1); }); it('should use custom maxLength when provided', () => { render(); expect(screen.getByText('Hello...')).toBeInTheDocument(); expect(screen.getByRole('button', { name: 'more' })).toBeInTheDocument(); }); it('should call showModal when "more" is clicked', async () => { const user = userEvent.setup(); render(); await user.click(screen.getByRole('button', { name: 'more' })); expect(showModalMock).toHaveBeenCalledTimes(1); }); it('should call showModal each time "more" is clicked', async () => { const user = userEvent.setup(); render(); await user.click(screen.getByRole('button', { name: 'more' })); expect(showModalMock).toHaveBeenCalledTimes(1); await user.click(screen.getByRole('button', { name: 'more' })); expect(showModalMock).toHaveBeenCalledTimes(2); }); it('should handle empty string content', () => { render(); expect(screen.queryByRole('button')).not.toBeInTheDocument(); }); }); describe('Truncate (expandInline mode)', () => { const shortContent = 'Hello'; const longContent = 'This is a very long text that exceeds the default maximum length of 80 characters and should be truncated'; it('renders full text with no button when content is short', () => { render(); expect(screen.getByText(shortContent)).toBeInTheDocument(); expect(screen.queryByRole('button')).not.toBeInTheDocument(); }); it('renders full text with no button when content is exactly maxLength characters', () => { const exactContent = 'A'.repeat(80); render(); expect(screen.getByText(exactContent)).toBeInTheDocument(); expect(screen.queryByRole('button')).not.toBeInTheDocument(); }); it('renders truncated text and "more" button when content exceeds maxLength', () => { render(); expect(screen.getByText(`${longContent.slice(0, 80)}...`)).toBeInTheDocument(); expect(screen.getByRole('button', { name: 'more' })).toBeInTheDocument(); }); it('reveals full text and switches button to "less" when "more" is clicked', async () => { const user = userEvent.setup(); render(); await user.click(screen.getByRole('button', { name: 'more' })); expect(screen.getByText(longContent)).toBeInTheDocument(); expect(screen.getByRole('button', { name: 'less' })).toBeInTheDocument(); expect(screen.queryByRole('button', { name: 'more' })).not.toBeInTheDocument(); }); it('re-collapses to truncated text when "less" is clicked', async () => { const user = userEvent.setup(); render(); await user.click(screen.getByRole('button', { name: 'more' })); await user.click(screen.getByRole('button', { name: 'less' })); expect(screen.getByText(`${longContent.slice(0, 80)}...`)).toBeInTheDocument(); expect(screen.getByRole('button', { name: 'more' })).toBeInTheDocument(); }); it('respects a custom maxLength', () => { render(); expect(screen.getByText('Hello...')).toBeInTheDocument(); expect(screen.getByRole('button', { name: 'more' })).toBeInTheDocument(); }); it('renders no button for empty string', () => { render(); expect(screen.queryByRole('button')).not.toBeInTheDocument(); }); it('does not open a modal in expandInline mode', async () => { const user = userEvent.setup(); render(); await user.click(screen.getByRole('button', { name: 'more' })); expect(screen.queryByRole('dialog')).not.toBeInTheDocument(); }); });