Skip to content

Commit ffe6ebb

Browse files
committed
Wrapped visible content in classless spans
1 parent ad82743 commit ffe6ebb

File tree

3 files changed

+14
-24
lines changed

3 files changed

+14
-24
lines changed

packages/react-core/src/components/Truncate/Truncate.tsx

+4-4
Original file line numberDiff line numberDiff line change
@@ -186,14 +186,14 @@ export const Truncate: React.FunctionComponent<TruncateProps> = ({
186186
<>
187187
{renderVisuallyHiddenContent(content.slice(0, maxCharsDisplayed * -1))}
188188
{omissionElement}
189-
{content.slice(maxCharsDisplayed * -1)}
189+
<span>{content.slice(maxCharsDisplayed * -1)}</span>
190190
</>
191191
);
192192
}
193193
if (position === TruncatePosition.end) {
194194
return (
195195
<>
196-
{content.slice(0, maxCharsDisplayed)}
196+
<span>{content.slice(0, maxCharsDisplayed)}</span>
197197
{omissionElement}
198198
{renderVisuallyHiddenContent(content.slice(maxCharsDisplayed))}
199199
</>
@@ -204,10 +204,10 @@ export const Truncate: React.FunctionComponent<TruncateProps> = ({
204204
const trueMiddleEnd = Math.ceil(maxCharsDisplayed / 2) * -1;
205205
return (
206206
<>
207-
{content.slice(0, trueMiddleStart)}
207+
<span>{content.slice(0, trueMiddleStart)}</span>
208208
{omissionElement}
209209
{renderVisuallyHiddenContent(content.slice(trueMiddleStart, trueMiddleEnd))}
210-
{content.slice(trueMiddleEnd)}
210+
<span>{content.slice(trueMiddleEnd)}</span>
211211
</>
212212
);
213213
};

packages/react-core/src/components/Truncate/__tests__/Truncate.test.tsx

+7-19
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ test(`renders with class ${styles.truncate}`, () => {
2424

2525
const test = screen.getByLabelText('test-id');
2626

27-
expect(test).toHaveClass(styles.truncate);
27+
expect(test).toHaveClass(styles.truncate, { exact: true });
2828
});
2929

3030
test('renders with custom class name passed via prop', () => {
@@ -150,22 +150,16 @@ test('renders with inherited element props spread to the component', () => {
150150
});
151151

152152
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-
159153
test(`Does not render with class class-tbd when maxCharsDisplayed is 0`, () => {
160154
render(<Truncate maxCharsDisplayed={0} data-testid="truncate-component" content="Test content" />);
161155

162-
expect(screen.getByText('Test content')).not.toHaveClass('class-tbd');
156+
expect(screen.getByTestId('truncate-component')).not.toHaveClass('class-tbd');
163157
});
164158

165159
test(`Renders with class class-tbd when maxCharsDisplayed is greater than 0`, () => {
166160
render(<Truncate maxCharsDisplayed={1} data-testid="truncate-component" content="Test content" />);
167161

168-
expect(screen.getByText('T')).toHaveClass('class-tbd');
162+
expect(screen.getByTestId('truncate-component')).toHaveClass('class-tbd');
169163
});
170164

171165
test('Renders with hidden truncated content at end by default when maxCharsDisplayed is passed', () => {
@@ -176,17 +170,11 @@ describe('Truncation with maxCharsDisplayed', () => {
176170
});
177171

178172
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');
173+
render(<Truncate position="middle" content="Middle position contents being truncated" maxCharsDisplayed={10} />);
174+
175+
expect(screen.getByText('Middl')).not.toHaveClass('pf-v6-screen-reader');
189176
expect(screen.getByText('e position contents being trun')).toHaveClass('pf-v6-screen-reader');
177+
expect(screen.getByText('cated')).not.toHaveClass('pf-v6-screen-reader');
190178
});
191179

192180
test('Renders with hidden truncated content at start when maxCharsDisplayed is passed and position="start"', () => {

packages/react-core/src/components/Truncate/__tests__/__snapshots__/Truncate.test.tsx.snap

+3-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ exports[`Truncation with maxCharsDisplayed Matches snapshot with default positio
1616
<span
1717
class="pf-v6-c-truncate class-tbd"
1818
>
19-
Tes
19+
<span>
20+
Tes
21+
</span>
2022
<span
2123
aria-hidden="true"
2224
>

0 commit comments

Comments
 (0)