Skip to content

Commit 4898eab

Browse files
author
Eric Olkowski
committed
Wrote tests for max chars logic
1 parent a7077ec commit 4898eab

2 files changed

Lines changed: 105 additions & 1 deletion

File tree

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

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { render, screen } from '@testing-library/react';
1+
import { render, screen, within } from '@testing-library/react';
22
import { Truncate } from '../Truncate';
33
import styles from '@patternfly/react-styles/css/components/Truncate/truncate';
44
import '@testing-library/jest-dom';
@@ -148,3 +148,75 @@ test('renders with inherited element props spread to the component', () => {
148148

149149
expect(screen.getByTestId('test-id')).toHaveAccessibleName('labelling-id');
150150
});
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+
});

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,37 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`Truncation with maxCharsDisplayed Matches snapshot with default position 1`] = `
4+
<DocumentFragment>
5+
<div
6+
data-testid="Tooltip-mock"
7+
>
8+
<div
9+
data-testid="Tooltip-mock-content-container"
10+
>
11+
Test Test truncation content
12+
</div>
13+
<p>
14+
position: top
15+
</p>
16+
<span
17+
class="pf-v6-c-truncate class-tbd"
18+
>
19+
Tes
20+
<span
21+
aria-hidden="true"
22+
>
23+
24+
</span>
25+
<span
26+
class="pf-v6-screen-reader"
27+
>
28+
t truncation content
29+
</span>
30+
</span>
31+
</div>
32+
</DocumentFragment>
33+
`;
34+
335
exports[`renders default truncation 1`] = `
436
<DocumentFragment>
537
<div

0 commit comments

Comments
 (0)