Skip to content

Commit

Permalink
chore(HelperText): update tests (#9761)
Browse files Browse the repository at this point in the history
* chore(HelperText): update tests

* break out helpertextitem tests, update tests

* updates

* fix lint
  • Loading branch information
kmcfaul authored Nov 15, 2023
1 parent a7075e0 commit a816ddd
Show file tree
Hide file tree
Showing 4 changed files with 177 additions and 280 deletions.
Original file line number Diff line number Diff line change
@@ -1,66 +1,76 @@
import * as React from 'react';
import { render } from '@testing-library/react';
import { render, screen } from '@testing-library/react';
import { HelperText } from '../HelperText';
import { HelperTextItem } from '../HelperTextItem';
import CheckIcon from '@patternfly/react-icons/dist/esm/icons/check-icon';

describe('HelperText', () => {
test('simple helper text renders successfully', () => {
const { asFragment } = render(
<HelperText>
<HelperTextItem>help test text</HelperTextItem>
</HelperText>
);
expect(asFragment()).toMatchSnapshot();
});
import styles from '@patternfly/react-styles/css/components/HelperText/helper-text';

Object.values(['default', 'indeterminate', 'warning', 'success', 'invalid']).forEach((variant) => {
test(`${variant} helper text variant applies successfully`, () => {
const { asFragment } = render(
<HelperTextItem variant={variant as 'default' | 'indeterminate' | 'warning' | 'success' | 'error'}>
{variant} help test text
</HelperTextItem>
);
expect(asFragment()).toMatchSnapshot();
});
});
test('Renders to match snapshot', () => {
const { asFragment } = render(<HelperText />);
expect(asFragment()).toMatchSnapshot();
});

test('Renders without children', () => {
render(
<div data-testid="container">
<HelperText />
</div>
);

expect(screen.getByTestId('container').firstChild).toBeVisible();
});

test('Renders default classes', () => {
render(<HelperText>text</HelperText>);
expect(screen.getByText('text')).toHaveClass(styles.helperText);
});

test('variant comonent helper text renders properly', () => {
const { asFragment } = render(
<HelperText component="ul">
<HelperTextItem component="li">help test text 1</HelperTextItem>
<HelperTextItem component="li">help test text 2</HelperTextItem>
</HelperText>
);
expect(asFragment()).toMatchSnapshot();
});
test('Renders id when id is passed', () => {
render(<HelperText id="helper-id">text </HelperText>);
expect(screen.getByText('text')).toHaveAttribute('id', 'helper-id');
});

test('icon helper text renders properly', () => {
const { asFragment } = render(
<HelperText>
<HelperTextItem icon={<CheckIcon />}>help test text</HelperTextItem>
</HelperText>
);
expect(asFragment()).toMatchSnapshot();
});
test('Renders aria-live when isLiveRegion is passed', () => {
render(<HelperText isLiveRegion>text </HelperText>);
expect(screen.getByText('text')).toHaveAttribute('aria-live', 'polite');
});

test('Does not render aria-live by default', () => {
render(<HelperText>text </HelperText>);
expect(screen.getByText('text')).not.toHaveAttribute('aria-live');
});

test('dynamic helper text renders successfully', () => {
const { asFragment } = render(
<HelperText>
<HelperTextItem isDynamic>help test text</HelperTextItem>
</HelperText>
);
expect(asFragment()).toMatchSnapshot();
});
test('Spreads additional props when passed', () => {
render(<HelperText dir="rtl">text </HelperText>);
expect(screen.getByText('text')).toHaveAttribute('dir', 'rtl');
});

test('Renders custom className', () => {
render(<HelperText className="custom">text </HelperText>);
expect(screen.getByText('text')).toHaveClass('custom');
});

test('Renders with element passed to component prop', () => {
render(<HelperText component="ul">text</HelperText>);
expect(screen.getByText('text').tagName).toBe('UL');
});

test('Renders with div by default when no component prop is passed', () => {
render(<HelperText>text</HelperText>);
expect(screen.getByText('text').tagName).toBe('DIV');
});

test('Renders aria-label and role when component = ul', () => {
render(
<HelperText component="ul" aria-label="helper">
text
</HelperText>
);
expect(screen.getByText('text')).toHaveAttribute('aria-label', 'helper');
expect(screen.getByText('text')).toHaveAttribute('role', 'list');
});

test('helper text block renders successfully', () => {
const { asFragment } = render(
<HelperText>
<HelperTextItem>help test text 1</HelperTextItem>
<HelperTextItem>help test text 2</HelperTextItem>
<HelperTextItem>help test text 3</HelperTextItem>
</HelperText>
);
expect(asFragment()).toMatchSnapshot();
});
test('Does not render aria-label and role when component != ul', () => {
render(<HelperText aria-label="helper">text</HelperText>);
expect(screen.getByText('text')).not.toHaveAttribute('aria-label', 'helper');
expect(screen.getByText('text')).not.toHaveAttribute('role', 'list');
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import * as React from 'react';
import { render, screen } from '@testing-library/react';
import { HelperTextItem } from '../HelperTextItem';

import styles from '@patternfly/react-styles/css/components/HelperText/helper-text';

test('Renders to match snapshot', () => {
const { asFragment } = render(<HelperTextItem>help test text</HelperTextItem>);
expect(asFragment()).toMatchSnapshot();
});

test('Renders without children', () => {
render(
<div data-testid="container">
<HelperTextItem />
</div>
);

expect(screen.getByTestId('container').firstChild).toBeVisible();
});

test('Renders default classes', () => {
render(<HelperTextItem>help test text 1</HelperTextItem>);
expect(screen.getByText('help test text 1')).toHaveClass(styles.helperTextItemText);
expect(screen.getByText('help test text 1').parentElement).toHaveClass(styles.helperTextItem);
});

test('Renders custom className', () => {
render(<HelperTextItem className="custom">help test text 1</HelperTextItem>);
expect(screen.getByText('help test text 1').parentElement).toHaveClass('custom');
});

Object.values(['indeterminate', 'warning', 'success', 'error']).forEach((variant) => {
test(`Renders with class ${styles.modifiers[variant]} when variant = ${variant}`, () => {
render(
<HelperTextItem variant={variant as 'default' | 'indeterminate' | 'warning' | 'success' | 'error'}>
text
</HelperTextItem>
);
expect(screen.getByText('text').parentElement).toHaveClass(styles.modifiers[variant]);
});
});

test('Renders id when id is passed', () => {
render(<HelperTextItem id="text-item">help test text 1</HelperTextItem>);
expect(screen.getByText('help test text 1').parentElement).toHaveAttribute('id', 'text-item');
});

test('Renders as div by default when component is not passed', () => {
render(<HelperTextItem>help test text 1</HelperTextItem>);
expect(screen.getByText('help test text 1').parentElement?.tagName).toBe('DIV');
});

test('Renders with element passed to component prop', () => {
render(<HelperTextItem component="li">help test text 1</HelperTextItem>);
expect(screen.getByText('help test text 1').parentElement?.tagName).toBe('LI');
});

test('Renders custom icon', () => {
render(<HelperTextItem icon={<div>test</div>}>help test text</HelperTextItem>);
expect(screen.getByText('test').parentElement).toHaveClass(styles.helperTextItemIcon);
});

test('Renders default icon when hasIcon = true and icon is not passed', () => {
render(<HelperTextItem hasIcon>help test text</HelperTextItem>);
expect(screen.getByText('help test text').parentElement?.querySelector('span')).toHaveClass(
styles.helperTextItemIcon
);
});

test('Renders custom icon when icon is passed and hasIcon = true', () => {
render(
<HelperTextItem hasIcon icon={<div>test</div>}>
help test text
</HelperTextItem>
);
expect(screen.getByText('test').parentElement).toHaveClass(styles.helperTextItemIcon);
});

test('Renders dynamic helper text', () => {
render(<HelperTextItem isDynamic>help test text</HelperTextItem>);
expect(screen.getByText('help test text').parentElement).toHaveClass(styles.modifiers.dynamic);
expect(screen.getByText('help test text').querySelector('span')).toHaveClass('pf-v5-screen-reader');
});

test('Renders custom screenreader text when isDynamic = true and screenReaderText is passed', () => {
render(
<HelperTextItem isDynamic screenReaderText="sr test">
help test text
</HelperTextItem>
);
expect(screen.getByText('help test text').querySelector('span')).toHaveTextContent('sr test');
});
Loading

0 comments on commit a816ddd

Please sign in to comment.