Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(eslint): Added React Testing Library linter plugin #9805

Merged
merged 1 commit into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Javascript builds
node_modules
dist
__tests__
thirdparty
tsc_out
.out
Expand Down
9 changes: 9 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,15 @@
"rules": {
"patternfly-react/no-anonymous-functions": "off"
}
},
{
"files": ["**/*.test.[jt]s?(x)"],
"extends": ["plugin:testing-library/react"],
"rules": {
"testing-library/no-node-access": "off",
"react/jsx-key": "off",
"no-console": "off"
}
}
]
}
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"eslint-plugin-markdown": "^3.0.0",
"eslint-plugin-prettier": "^5.0.0",
"eslint-plugin-react": "^7.32.2",
"eslint-plugin-testing-library": "^6.1.0",
"fs-extra": "^11.1.1",
"glob": "^9.3.0",
"husky": "^4.3.0",
Expand Down Expand Up @@ -85,6 +86,7 @@
"lint:md": "yarn eslint packages --ext md --no-eslintrc --config .eslintrc-md.json --cache",
"lint:ts": "yarn lint packages/*/src",
"lint:versions": "node scripts/verifyPatternflyVersions.js",
"lint:tests": "yarn lint packages/*/src/components/*/__tests__/*.test.*",
"prepare": "ts-patch install -s",
"serve:docs": "yarn workspace @patternfly/react-docs serve",
"serve:integration": "lerna run serve:demo-app",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@ import { render } from '@testing-library/react';
import { AboutModalBox } from '../AboutModalBox';

test('AboutModalBox Test', () => {
const { asFragment } = render(
<AboutModalBox aria-labelledby="id">
This is a AboutModalBox
</AboutModalBox>
);
const { asFragment } = render(<AboutModalBox aria-labelledby="id">This is a AboutModalBox</AboutModalBox>);
expect(asFragment()).toMatchSnapshot();
});
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ test('Renders children', () => {
});

test('Matches the snapshot', () => {
const { asFragment } = render(
<AccordionItem>Test</AccordionItem>
);
const { asFragment } = render(<AccordionItem>Test</AccordionItem>);
expect(asFragment()).toMatchSnapshot();
});
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ test('Alert Group renders without children', () => {
});

test('Alert Group works with n children', () => {
const { asFragment } = render(
render(
<AlertGroup data-testid="container">
<Alert variant="success" title="alert title" />
<Alert variant="warning" title="another alert title" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,11 @@ test('Clicking backToTop scrolls back to top of the element passed via scrollabl
const user = userEvent.setup();
const wrapper = document.getElementById('backToTopWrapper');
fireEvent.scroll(wrapper as HTMLElement, { target: { scrollY: 401 } });
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
wrapper!.scrollTo = jest.fn();
await user.click(screen.getByRole(`button`).parentElement as Element);

expect(wrapper?.scrollTo).toBeCalledTimes(1);
expect(wrapper?.scrollTo).toHaveBeenCalledTimes(1);
});

test('Passes correct text content to button child component', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/react-core/src/components/Button/Button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as React from 'react';
import styles from '@patternfly/react-styles/css/components/Button/button';
import { css } from '@patternfly/react-styles';
import { Spinner, spinnerSize } from '../Spinner';
import { useOUIAProps, OUIAProps } from '../../helpers';
import { useOUIAProps, OUIAProps } from '../../helpers/OUIA/ouia';
import { Badge } from '../Badge';

export enum ButtonVariant {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import React from 'react';

import { render, screen } from '@testing-library/react';

import CartArrowDownIcon from '@patternfly/react-icons/dist/esm/icons/cart-arrow-down-icon';
import { Button, ButtonVariant } from '../Button';

Object.values(ButtonVariant).forEach((variant) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ test('Next year dates have correct year in aria label', () => {
});

test('InlineProps render correct wrapper component and attributes', () => {
render(<CalendarMonth inlineProps={{component: 'article', title: <div id="hi">Title</div>, ariaLabelledby: "hi"}} />);
render(
<CalendarMonth inlineProps={{ component: 'article', title: <div id="hi">Title</div>, ariaLabelledby: 'hi' }} />
);

const article = screen.getByRole('article');
expect(article).toHaveAttribute('aria-labelledby', 'hi');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('CardBody', () => {
test('allows passing in a React Component as the component', () => {
const Component = () => <div>im a div</div>;

render(<CardBody component={(Component as unknown) as keyof JSX.IntrinsicElements} />);
render(<CardBody component={Component as unknown as keyof JSX.IntrinsicElements} />);
expect(screen.getByText('im a div')).toBeInTheDocument();
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe('CardFooter', () => {

test('allows passing in a React Component as the component', () => {
const Component = () => <div>im a div</div>;
render(<CardFooter component={(Component as unknown) as keyof JSX.IntrinsicElements} />);
render(<CardFooter component={Component as unknown as keyof JSX.IntrinsicElements} />);
expect(screen.getByText('im a div')).toBeInTheDocument();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ test('chip group with closeBtnAriaLabel', () => {
</ChipGroup>
);
expect(screen.getByLabelText('close button aria label')).toBeInTheDocument();
expect(screen.getByLabelText('close button aria label')).toHaveAccessibleName("close button aria label category");
expect(screen.getByLabelText('close button aria label')).toHaveAccessibleName('close button aria label category');
});

test('chip group onClick', async () => {
Expand Down Expand Up @@ -169,8 +169,6 @@ test('chip group expanded', async () => {
});

test('overflow chip does not render by default when < 4 children are passed', async () => {
const user = userEvent.setup();

render(
<ChipGroup>
<Chip>1</Chip>
Expand All @@ -183,8 +181,6 @@ test('overflow chip does not render by default when < 4 children are passed', as
});

test('overflow chip collapsed by default', async () => {
const user = userEvent.setup();

render(
<ChipGroup>
<Chip>1</Chip>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React from 'react';
import { screen, render } from '@testing-library/react';
import { ClipboardCopyToggle } from '../ClipboardCopyToggle';
import styles from '@patternfly/react-styles/css/components/ClipboardCopy/clipboard-copy';
import userEvent from '@testing-library/user-event';

const onClickMock = jest.fn();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,9 @@ test('disabled date picker', () => {
test('Date picker with multiple validators does not show invalid icon on valid input', async () => {
const user = userEvent.setup();

const rangeValidator = (date: Date) => {
return '';
};
const rangeValidator = (_date: Date) => '';

const rangeValidatorB = (date: Date) => {
return '';
};
const rangeValidatorB = (_date: Date) => '';

render(<DatePicker value="2020-03-17" validators={[rangeValidator, rangeValidatorB]} />);

Expand Down Expand Up @@ -119,7 +115,7 @@ test('Does not render emptyDateText when requiredDateOptions.isRequired is false
await user.click(screen.getByRole('textbox'));
await user.click(document.body);

expect(screen.queryByText('Date cannot be blank')).not.toBeInTheDocument;
expect(screen.queryByText('Date cannot be blank')).not.toBeInTheDocument();
});

test('Renders text input as invalid on blur when requiredDateOptions.isRequired is true', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,8 @@ test(`Test all insets`, () => {
'inset2xl',
'inset3xl'
] as ['insetNone', 'insetXs', 'insetSm', 'insetMd', 'insetLg', 'insetXl', 'inset2xl', 'inset3xl']);

insetValues.forEach((insetValue) => {

const modifiers = ['none', 'xs', 'sm', 'md', 'lg', 'xl', '2xl', '3xl'];

const classValue = modifiers.forEach((modifier, index) => {
const smClass = `pf-m-inset-${modifier}-on-sm`;
const mdClass = `pf-m-inset-${modifier}-on-md`;
const lgClass = `pf-m-inset-${modifier}-on-lg`;
const xlClass = `pf-m-inset-${modifier}-on-xl`;
const xl2Class = `pf-m-inset-${modifier}-on-2xl`;
});

insetValues.forEach((insetValue) => {
render(
<Divider
inset={{
Expand All @@ -92,7 +81,7 @@ test(`Test all insets`, () => {
});
const modifiers = ['none', 'xs', 'sm', 'md', 'lg', 'xl', '2xl', '3xl'];

const classValue = modifiers.forEach((modifier, index) => {
modifiers.forEach((modifier, index) => {
const smClass = `pf-m-inset-${modifier}-on-sm`;
const mdClass = `pf-m-inset-${modifier}-on-md`;
const lgClass = `pf-m-inset-${modifier}-on-lg`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
DrawerPanelContent
} from '../';
import React from 'react';
import { render, screen } from '@testing-library/react';
import { render } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { KeyTypes } from '../../../helpers';

Expand Down Expand Up @@ -134,13 +134,9 @@ test('Resizeable DrawerPanelContent can be wrapped in a context without causing

const panelContent = (
<TestContext.Provider value={{}}>
<DrawerPanelContent
isResizable
>
<DrawerPanelContent isResizable>
<DrawerHead>
<span>
drawer-panel
</span>
<span>drawer-panel</span>
<DrawerActions>
<DrawerCloseButton />
</DrawerActions>
Expand All @@ -163,4 +159,4 @@ test('Resizeable DrawerPanelContent can be wrapped in a context without causing
await user.keyboard(`{${KeyTypes.ArrowLeft}}`);

expect(consoleError).not.toHaveBeenCalled();
})
});
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';
import { Dropdown } from '../../Dropdown';
import { render, screen, waitFor } from '@testing-library/react';
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';

jest.mock('../../Menu');
Expand Down Expand Up @@ -176,7 +176,7 @@ test('onOpenChange is called when passed and user presses tab key', async () =>
</Dropdown>
);

//focus dropdown
// focus dropdown
const dropdown = screen.getByRole('button', { name: 'Dropdown' });
await user.click(dropdown);
await user.keyboard('{Tab}');
Expand All @@ -194,7 +194,7 @@ test('onOpenChange is called when passed and user presses esc key', async () =>
</Dropdown>
);

//focus dropdown
// focus dropdown
const dropdown = screen.getByRole('button', { name: 'Dropdown' });
await user.click(dropdown);
await user.keyboard('{Escape}');
Expand Down
Loading
Loading