import React from 'react';
import { render, screen } from '@testing-library/react';
import { Truncate } from '../Truncate';
jest.mock('../../Tooltip', () => ({
Tooltip: ({ content, position, children, ...props }) => (
Test {content}
{`position: ${position}`}
{children}
)
}));
test('renders with class pf-v5-c-truncate', () => {
render();
const test = screen.getByLabelText('test-id');
expect(test).toHaveClass('pf-v5-c-truncate');
});
test('renders with custom class name passed via prop', () => {
render();
const test = screen.getByLabelText('test-id');
expect(test).toHaveClass('custom-classname');
});
test('renders truncate with empty content', () => {
render();
const test = screen.getByLabelText('test-id');
expect(test).toHaveTextContent('');
});
test('renders truncate with content', () => {
render();
const test = screen.getByLabelText('test-id');
expect(test).toHaveTextContent('Test');
});
test('only renders pf-v5-c-truncate__start with default position', () => {
render();
const start = screen.getByText('Testing truncate content');
expect(start).toHaveClass('pf-v5-c-truncate__start');
expect(start).not.toHaveClass('pf-v5-c-truncate__end');
});
test('renders default truncation', () => {
const { asFragment } = render(
);
expect(asFragment()).toMatchSnapshot();
});
test('renders start truncation with at end', () => {
const { asFragment } = render(
);
expect(asFragment()).toMatchSnapshot();
});
test('renders middle truncation', () => {
render(
);
const start = screen.getByText('Vestibulum interdum risus et enim faucibus, sit amet molestie est ac');
expect(start).toHaveClass('pf-v5-c-truncate__start');
const end = screen.getByText('cumsan.');
expect(end).toHaveClass('pf-v5-c-truncate__end');
});
test('renders different content when trailingNumChars is passed with middle truncate', () => {
render(
);
const start = screen.getByText('Vestibulum interdum risus et enim faucibus, sit amet molestie est accums');
expect(start).toHaveClass('pf-v5-c-truncate__start');
const end = screen.getByText('an.');
expect(end).toHaveClass('pf-v5-c-truncate__end');
});
test('renders tooltip position', () => {
render();
const input = screen.getByText('position: top');
expect(input).toBeVisible();
});
test('renders tooltip content', () => {
render();
const input = screen.getByText('Test Another Tooltip');
expect(input).toBeVisible();
});
test('renders with inherited element props spread to the component', () => {
render();
expect(screen.getByTestId('test-id')).toHaveAccessibleName('labelling-id');
});