|
1 | 1 | import React from 'react'; |
2 | | -import { render, screen } from '@testing-library/react'; |
| 2 | +import { render, screen, fireEvent } from '@testing-library/react'; |
3 | 3 | import { describe, it, expect } from 'vitest'; |
4 | 4 | import HelpText from '../HelpText'; |
5 | 5 |
|
@@ -78,4 +78,170 @@ describe('HelpText', () => { |
78 | 78 | const helpDiv = container.querySelector('div'); |
79 | 79 | expect(helpDiv).toHaveClass('[&_p]:inline', '[&_p]:mb-0'); |
80 | 80 | }); |
| 81 | + |
| 82 | + // Truncation functionality tests |
| 83 | + describe('Text truncation', () => { |
| 84 | + it('does not truncate short text', () => { |
| 85 | + const shortText = 'This is a short help text'; |
| 86 | + render(<HelpText helpText={shortText} />); |
| 87 | + |
| 88 | + expect(screen.getByText(shortText)).toBeInTheDocument(); |
| 89 | + expect(screen.queryByText('Show more')).not.toBeInTheDocument(); |
| 90 | + }); |
| 91 | + |
| 92 | + it('truncates long text and shows "Show more" button', () => { |
| 93 | + const longText = 'This is a very long help text that exceeds the maximum length of 80 characters and should be truncated with an ellipsis and show more button because it is longer than the threshold'; |
| 94 | + render(<HelpText helpText={longText} />); |
| 95 | + |
| 96 | + // Should show truncated text with ellipsis |
| 97 | + expect(screen.getByText(/\.\.\./)).toBeInTheDocument(); |
| 98 | + // Should show "Show more" button |
| 99 | + expect(screen.getByText('Show more')).toBeInTheDocument(); |
| 100 | + // Should not show the full text initially |
| 101 | + expect(screen.queryByText(longText)).not.toBeInTheDocument(); |
| 102 | + }); |
| 103 | + |
| 104 | + it('expands text when "Show more" is clicked', () => { |
| 105 | + const longText = 'This is a very long help text that exceeds the maximum length of 80 characters and should be truncated with an ellipsis and show more button because it is longer than the threshold'; |
| 106 | + render(<HelpText helpText={longText} />); |
| 107 | + |
| 108 | + const showMoreButton = screen.getByText('Show more'); |
| 109 | + fireEvent.click(showMoreButton); |
| 110 | + |
| 111 | + // Should show full text after clicking |
| 112 | + expect(screen.getByText(longText)).toBeInTheDocument(); |
| 113 | + // Button should change to "Show less" |
| 114 | + expect(screen.getByText('Show less')).toBeInTheDocument(); |
| 115 | + expect(screen.queryByText('Show more')).not.toBeInTheDocument(); |
| 116 | + }); |
| 117 | + |
| 118 | + it('collapses text when "Show less" is clicked', () => { |
| 119 | + const longText = 'This is a very long help text that exceeds the maximum length of 80 characters and should be truncated with an ellipsis and show more button because it is longer than the threshold'; |
| 120 | + render(<HelpText helpText={longText} />); |
| 121 | + |
| 122 | + const showMoreButton = screen.getByText('Show more'); |
| 123 | + fireEvent.click(showMoreButton); |
| 124 | + |
| 125 | + const showLessButton = screen.getByText('Show less'); |
| 126 | + fireEvent.click(showLessButton); |
| 127 | + |
| 128 | + // Should show truncated text again |
| 129 | + expect(screen.getByText(/\.\.\./)).toBeInTheDocument(); |
| 130 | + expect(screen.getByText('Show more')).toBeInTheDocument(); |
| 131 | + expect(screen.queryByText('Show less')).not.toBeInTheDocument(); |
| 132 | + }); |
| 133 | + |
| 134 | + it('truncates combined text with default value when total length exceeds limit', () => { |
| 135 | + const longHelpText = 'This is a moderately long help text that when combined with default value it exceeds the limit'; |
| 136 | + const defaultValue = 'very-long-default-value-that-makes-total-exceed-limit'; |
| 137 | + |
| 138 | + render(<HelpText helpText={longHelpText} defaultValue={defaultValue} />); |
| 139 | + |
| 140 | + // Should show "Show more" button when combined text is too long |
| 141 | + expect(screen.getByText('Show more')).toBeInTheDocument(); |
| 142 | + expect(screen.getByText(/\.\.\./)).toBeInTheDocument(); |
| 143 | + }); |
| 144 | + |
| 145 | + it('does not truncate when only default value is present and under limit', () => { |
| 146 | + const shortDefaultValue = 'short-default'; |
| 147 | + render(<HelpText defaultValue={shortDefaultValue} />); |
| 148 | + |
| 149 | + expect(screen.getByText(shortDefaultValue)).toBeInTheDocument(); |
| 150 | + expect(screen.queryByText('Show more')).not.toBeInTheDocument(); |
| 151 | + }); |
| 152 | + |
| 153 | + it('truncates when only default value is present and exceeds limit', () => { |
| 154 | + const longDefaultValue = 'this-is-a-very-long-default-value-that-exceeds-the-maximum-character-limit-of-80-characters-and-the-threshold-so-it-should-be-truncated'; |
| 155 | + render(<HelpText defaultValue={longDefaultValue} />); |
| 156 | + |
| 157 | + expect(screen.getByText('Show more')).toBeInTheDocument(); |
| 158 | + expect(screen.getByText(/\.\.\./)).toBeInTheDocument(); |
| 159 | + }); |
| 160 | + |
| 161 | + it('preserves markdown formatting in truncated text', () => { |
| 162 | + const longTextWithMarkdown = 'This is a very long help text with `code formatting` and **bold text** that exceeds the maximum length of 80 characters and the threshold so it should be truncated properly'; |
| 163 | + render(<HelpText helpText={longTextWithMarkdown} />); |
| 164 | + |
| 165 | + expect(screen.getByText('Show more')).toBeInTheDocument(); |
| 166 | + |
| 167 | + // Click to expand and check markdown is preserved |
| 168 | + fireEvent.click(screen.getByText('Show more')); |
| 169 | + expect(screen.getByText('code formatting')).toHaveClass('font-mono'); |
| 170 | + expect(screen.getByText('bold text')).toBeInTheDocument(); |
| 171 | + }); |
| 172 | + |
| 173 | + it('preserves markdown formatting in truncated text with code blocks', () => { |
| 174 | + // Create text with TLS certificate that will be truncated in the middle of the certificate |
| 175 | + const longTextWithCodeBlock = 'To configure TLS, provide your certificate:\n\n```\n-----BEGIN CERTIFICATE-----\nMIIDXTCCAkWgAwIBAgIJAKoK/heBjcOuMA0GCSqGSIb3DQEBBQUAMEUxCzAJBgNV\nBAYTAkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEwHwYDVQQKDBhJbnRlcm5ldCBX\naWRnaXRzIFB0eSBMdGQwHhcNMTcwODI3MjM1NzU5WhcNMTgwODI3MjM1NzU5WjBF\nMQswCQYDVQQGEwJBVTETMBEGA1UECAwKU29tZS1TdGF0ZTEhMB8GA1UECgwYSW50\nZXJuZXQgV2lkZ2l0cyBQdHkgTHRkMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIB\nCgKCAQEAuuB5/8GvxwqhkzCQF22XA0cGRUbKCzlJHoFGELqoZKxSV8j9C7sW7A==\n-----END CERTIFICATE-----\n```\n\nEnsure the certificate is valid.'; |
| 176 | + const { container } = render(<HelpText helpText={longTextWithCodeBlock} />); |
| 177 | + |
| 178 | + expect(screen.getByText('Show more')).toBeInTheDocument(); |
| 179 | + |
| 180 | + // Click to expand and check code block markdown is preserved |
| 181 | + fireEvent.click(screen.getByText('Show more')); |
| 182 | + |
| 183 | + // Check that certificate content is present (may be in different elements) |
| 184 | + expect(screen.getByText(/BEGIN CERTIFICATE/)).toBeInTheDocument(); |
| 185 | + expect(screen.getByText(/END CERTIFICATE/)).toBeInTheDocument(); |
| 186 | + |
| 187 | + // Check that pre/code elements exist for code block |
| 188 | + const preElements = container.querySelectorAll('pre'); |
| 189 | + const codeElements = container.querySelectorAll('code'); |
| 190 | + expect(preElements.length).toBeGreaterThan(0); |
| 191 | + expect(codeElements.length).toBeGreaterThan(0); |
| 192 | + }); |
| 193 | + |
| 194 | + it('applies correct CSS classes to show more/less button', () => { |
| 195 | + const longText = 'This is a very long help text that exceeds the maximum length of 80 characters and should be truncated with an ellipsis and show more button because it is longer than the threshold'; |
| 196 | + render(<HelpText helpText={longText} />); |
| 197 | + |
| 198 | + const showMoreButton = screen.getByText('Show more'); |
| 199 | + expect(showMoreButton).toHaveClass('ml-1', 'text-blue-600', 'hover:text-blue-800', 'text-xs', 'cursor-pointer'); |
| 200 | + expect(showMoreButton).toHaveAttribute('type', 'button'); |
| 201 | + }); |
| 202 | + |
| 203 | + it('handles text exactly at maxTextLength', () => { |
| 204 | + // Create text that's exactly 80 characters (the maxTextLength) |
| 205 | + const exactMaxText = 'a'.repeat(80); |
| 206 | + render(<HelpText helpText={exactMaxText} />); |
| 207 | + |
| 208 | + // Should not show "Show more" button at exactly maxTextLength |
| 209 | + expect(screen.queryByText('Show more')).not.toBeInTheDocument(); |
| 210 | + }); |
| 211 | + |
| 212 | + it('handles text within the truncation threshold', () => { |
| 213 | + // Create text that's 100 characters (over maxTextLength of 80 but within threshold of 40) |
| 214 | + const withinThresholdText = 'a'.repeat(100); |
| 215 | + render(<HelpText helpText={withinThresholdText} />); |
| 216 | + |
| 217 | + // Should not show "Show more" button when within threshold (80 + 40 = 120) |
| 218 | + expect(screen.queryByText('Show more')).not.toBeInTheDocument(); |
| 219 | + }); |
| 220 | + |
| 221 | + it('handles text at the edge of truncation threshold', () => { |
| 222 | + // Create text that's exactly 120 characters (maxTextLength + threshold = 80 + 40) |
| 223 | + const atThresholdEdgeText = 'a'.repeat(120); |
| 224 | + render(<HelpText helpText={atThresholdEdgeText} />); |
| 225 | + |
| 226 | + // Should not show "Show more" button when exactly at threshold edge |
| 227 | + expect(screen.queryByText('Show more')).not.toBeInTheDocument(); |
| 228 | + }); |
| 229 | + |
| 230 | + it('handles text just over the truncation threshold', () => { |
| 231 | + // Create text that's 121 characters (over maxTextLength + threshold = 80 + 40) |
| 232 | + const justOverThresholdText = 'a'.repeat(121); |
| 233 | + render(<HelpText helpText={justOverThresholdText} />); |
| 234 | + |
| 235 | + // Should show "Show more" button when over threshold limit |
| 236 | + expect(screen.getByText('Show more')).toBeInTheDocument(); |
| 237 | + }); |
| 238 | + |
| 239 | + it('maintains proper data-testid when truncated', () => { |
| 240 | + const longText = 'This is a very long help text that exceeds the maximum length of 80 characters and the threshold so it should be truncated'; |
| 241 | + const { container } = render(<HelpText helpText={longText} dataTestId="custom-test-id" />); |
| 242 | + |
| 243 | + const helpDiv = container.querySelector('[data-testid="help-text-custom-test-id"]'); |
| 244 | + expect(helpDiv).toBeInTheDocument(); |
| 245 | + }); |
| 246 | + }); |
81 | 247 | }); |
0 commit comments