|
1 | | -import { render, screen } from '@testing-library/react'; |
| 1 | +import { render, screen, within } from '@testing-library/react'; |
2 | 2 | import { Truncate } from '../Truncate'; |
3 | 3 | import styles from '@patternfly/react-styles/css/components/Truncate/truncate'; |
4 | 4 | import '@testing-library/jest-dom'; |
@@ -148,3 +148,75 @@ test('renders with inherited element props spread to the component', () => { |
148 | 148 |
|
149 | 149 | expect(screen.getByTestId('test-id')).toHaveAccessibleName('labelling-id'); |
150 | 150 | }); |
| 151 | + |
| 152 | +describe('Truncation with maxCharsDisplayed', () => { |
| 153 | + test(`Does not render with class class-tbd when maxCharsDisplayed is not passed`, () => { |
| 154 | + render(<Truncate data-testid="truncate-component" content="Test content" />); |
| 155 | + |
| 156 | + expect(screen.getByText('Test content')).not.toHaveClass('class-tbd'); |
| 157 | + }); |
| 158 | + |
| 159 | + test(`Does not render with class class-tbd when maxCharsDisplayed is 0`, () => { |
| 160 | + render(<Truncate maxCharsDisplayed={0} data-testid="truncate-component" content="Test content" />); |
| 161 | + |
| 162 | + expect(screen.getByText('Test content')).not.toHaveClass('class-tbd'); |
| 163 | + }); |
| 164 | + |
| 165 | + test(`Renders with class class-tbd when maxCharsDisplayed is greater than 0`, () => { |
| 166 | + render(<Truncate maxCharsDisplayed={1} data-testid="truncate-component" content="Test content" />); |
| 167 | + |
| 168 | + expect(screen.getByText('T')).toHaveClass('class-tbd'); |
| 169 | + }); |
| 170 | + |
| 171 | + test('Renders with hidden truncated content at end by default when maxCharsDisplayed is passed', () => { |
| 172 | + render(<Truncate content="Default end position content truncated" maxCharsDisplayed={6} />); |
| 173 | + |
| 174 | + expect(screen.getByText('Defaul')).not.toHaveClass('pf-v6-screen-reader'); |
| 175 | + expect(screen.getByText('t end position content truncated')).toHaveClass('pf-v6-screen-reader'); |
| 176 | + }); |
| 177 | + |
| 178 | + test('Renders with hidden truncated content at middle position when maxCharsDisplayed is passed and position="middle"', () => { |
| 179 | + render( |
| 180 | + <Truncate |
| 181 | + data-testid="truncate-component" |
| 182 | + position="middle" |
| 183 | + content="Middle position contents being truncated" |
| 184 | + maxCharsDisplayed={10} |
| 185 | + /> |
| 186 | + ); |
| 187 | + |
| 188 | + expect(screen.getByTestId('truncate-component')).not.toHaveClass('pf-v6-screen-reader'); |
| 189 | + expect(screen.getByText('e position contents being trun')).toHaveClass('pf-v6-screen-reader'); |
| 190 | + }); |
| 191 | + |
| 192 | + test('Renders with hidden truncated content at start when maxCharsDisplayed is passed and position="start"', () => { |
| 193 | + render(<Truncate content="Start position content truncated" maxCharsDisplayed={4} />); |
| 194 | + |
| 195 | + expect(screen.getByText('Star')).not.toHaveClass('pf-v6-screen-reader'); |
| 196 | + expect(screen.getByText('t position content truncated')).toHaveClass('pf-v6-screen-reader'); |
| 197 | + }); |
| 198 | + |
| 199 | + test('Renders full content when maxCharsDisplayed exceeds the length of the content', () => { |
| 200 | + render(<Truncate content="This full content is rendered" maxCharsDisplayed={90} />); |
| 201 | + |
| 202 | + expect(screen.getByText('This full content is rendered')).not.toHaveClass('pf-v6-screen-reader'); |
| 203 | + }); |
| 204 | + |
| 205 | + test('Renders ellipsis as omission content by default', () => { |
| 206 | + render(<Truncate content="Test truncation content" maxCharsDisplayed={5} />); |
| 207 | + |
| 208 | + expect(screen.getByText('\u2026')).toHaveAttribute('aria-hidden', 'true'); |
| 209 | + }); |
| 210 | + |
| 211 | + test('Renders custom omission content when omissionContent is passed', () => { |
| 212 | + render(<Truncate omissionContent="---" content="Test truncation content" maxCharsDisplayed={5} />); |
| 213 | + |
| 214 | + expect(screen.getByText('---')).toHaveAttribute('aria-hidden', 'true'); |
| 215 | + }); |
| 216 | + |
| 217 | + test('Matches snapshot with default position', () => { |
| 218 | + const { asFragment } = render(<Truncate content="Test truncation content" maxCharsDisplayed={3} />); |
| 219 | + |
| 220 | + expect(asFragment()).toMatchSnapshot(); |
| 221 | + }); |
| 222 | +}); |
0 commit comments