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(Panel): update component unit tests to meet new RTL standards #8346

Merged
merged 2 commits into from
Dec 6, 2022
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
154 changes: 96 additions & 58 deletions packages/react-core/src/components/Panel/__tests__/Panel.test.tsx
Original file line number Diff line number Diff line change
@@ -1,62 +1,100 @@
import React from 'react';
import { render } from '@testing-library/react';
import * as React from 'react';
import { render, screen } from '@testing-library/react';
import { Panel } from '../Panel';
import { PanelMain } from '../PanelMain';
import { PanelMainBody } from '../PanelMainBody';
import { PanelHeader } from '../PanelHeader';
import { PanelFooter } from '../PanelFooter';

describe('Panel', () => {
test('renders content', () => {
const { asFragment } = render(<Panel>Foo</Panel>);
expect(asFragment()).toMatchSnapshot();
});

test('renders content with raised styling', () => {
const { asFragment } = render(<Panel variant="raised">Foo</Panel>);
expect(asFragment()).toMatchSnapshot();
});

test('renders content with bordered styling', () => {
const { asFragment } = render(<Panel variant="bordered">Foo</Panel>);
expect(asFragment()).toMatchSnapshot();
});

test('renders content with scrollable styling', () => {
const { asFragment } = render(<Panel isScrollable>Foo</Panel>);
expect(asFragment()).toMatchSnapshot();
});
});

describe('PanelMain', () => {
test('renders content', () => {
const { asFragment } = render(<PanelMain>Foo</PanelMain>);
expect(asFragment()).toMatchSnapshot();
});

test('renders content with the set maximum height', () => {
const { asFragment } = render(<PanelMain maxHeight={'80px'}>Foo</PanelMain>);
expect(asFragment()).toMatchSnapshot();
});
});

describe('PanelMainBody', () => {
test('renders content', () => {
const { asFragment } = render(<PanelMainBody>Foo</PanelMainBody>);
expect(asFragment()).toMatchSnapshot();
});
});

describe('PanelHeader', () => {
test('renders content', () => {
const { asFragment } = render(<PanelHeader>Foo</PanelHeader>);
expect(asFragment()).toMatchSnapshot();
});
});

describe('PanelFooter', () => {
test('renders content', () => {
const { asFragment } = render(<PanelFooter>Foo</PanelFooter>);
expect(asFragment()).toMatchSnapshot();
});
import userEvent from '@testing-library/user-event';
import { useEffect } from 'react';

test('Renders without children', () => {
render(
<div data-testid="panel">
<Panel />
</div>
);
expect(screen.getByTestId('panel').firstChild).toBeVisible();
});

test('Renders children', () => {
render(<Panel>Test</Panel>);
expect(screen.getByText('Test')).toBeVisible();
});

test('Renders with the class pf-c-panel', () => {
render(<Panel>Test</Panel>);
expect(screen.getByText('Test')).toHaveClass('pf-c-panel');
});

test('Renders with only the class pf-c-panel by default', () => {
render(<Panel>Test</Panel>);
expect(screen.getByText('Test')).toHaveClass('pf-c-panel', { exact: true });
});

test('Renders with custom class name when className prop is passed', () => {
render(<Panel className="test-class">Test</Panel>);
expect(screen.getByText('Test')).toHaveClass('test-class');
});

test('Renders with class name pf-m-raised when variant is raised', () => {
render(<Panel variant="raised">Test</Panel>);
expect(screen.getByText('Test')).toHaveClass('pf-m-raised');
});

test('Renders with class name pf-m-bordered when variant is bordered', () => {
render(<Panel variant="bordered">Test</Panel>);
expect(screen.getByText('Test')).toHaveClass('pf-m-bordered');
});

test('Renders with class name pf-m-scrollable when isScrollable is true', () => {
render(<Panel isScrollable>Test</Panel>);
expect(screen.getByText('Test')).toHaveClass('pf-m-scrollable');
});

test('Renders with ref', async () => {
const user = userEvent.setup();
const panelRef: React.RefObject<HTMLDivElement> = React.createRef();

const BasicPanel = () => {
const [lastClickWasInPanel, setLastClickWasInPanel] = React.useState(false);

const handleClick = event => {
if (panelRef.current && panelRef.current.contains(event.target)) {
setLastClickWasInPanel(true);
} else {
setLastClickWasInPanel(false);
}
};

useEffect(() => {
document.addEventListener('click', handleClick);
}, []);

return (
<div>
<Panel ref={panelRef}>
<PanelMain>
<PanelMainBody>Main content</PanelMainBody>
</PanelMain>
</Panel>
<p>Last click was in panel: {lastClickWasInPanel ? 'true' : 'false'}</p>
</div>
);
};

render(<BasicPanel />);

await user.click(document.body);
expect(screen.getByText('Last click was in panel: false')).toBeVisible;
await user.click(screen.getByText('Main content'));
expect(screen.getByText('Last click was in panel: true')).toBeVisible;
});

test('Renders with the inherited element props spread to the component', () => {
render(<Panel aria-label="this is a simple panel">Test</Panel>);
expect(screen.getByText('Test')).toHaveAccessibleName('this is a simple panel');
});

test('Matches the snapshot', () => {
const { asFragment } = render(<Panel>Test</Panel>);
expect(asFragment()).toMatchSnapshot();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import * as React from 'react';
import { render, screen } from '@testing-library/react';
import { PanelFooter } from '../PanelFooter';

test('Renders without children', () => {
render(
<div data-testid="panelFooter">
<PanelFooter />
</div>
);
expect(screen.getByTestId('panelFooter').firstChild).toBeVisible();
});

test('Renders children', () => {
render(<PanelFooter>Test</PanelFooter>);
expect(screen.getByText('Test')).toBeVisible();
});

test('Renders with the class pf-c-panel__footer', () => {
render(<PanelFooter>Test</PanelFooter>);
expect(screen.getByText('Test')).toHaveClass('pf-c-panel__footer');
});

test('Renders with only the class pf-c-panel__footer by default', () => {
render(<PanelFooter>Test</PanelFooter>);
expect(screen.getByText('Test')).toHaveClass('pf-c-panel__footer', { exact: true });
});

test('Renders with custom class name when className prop is passed', () => {
render(<PanelFooter className="test-class">Test</PanelFooter>);
expect(screen.getByText('Test')).toHaveClass('test-class');
});

test('Renders with the inherited element props spread to the component', () => {
render(<PanelFooter aria-label="this is a simple panel footer">Test</PanelFooter>);
expect(screen.getByText('Test')).toHaveAccessibleName('this is a simple panel footer');
});

test('Matches the snapshot', () => {
const { asFragment } = render(<PanelFooter>Test</PanelFooter>);
expect(asFragment()).toMatchSnapshot();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import * as React from 'react';
import { render, screen } from '@testing-library/react';
import { PanelHeader } from '../PanelHeader';

test('Renders without children', () => {
render(
<div data-testid="panelHeader">
<PanelHeader />
</div>
);
expect(screen.getByTestId('panelHeader').firstChild).toBeVisible();
});

test('Renders children', () => {
render(<PanelHeader>Test</PanelHeader>);
expect(screen.getByText('Test')).toBeVisible();
});

test('Renders with the class pf-c-panel__header', () => {
render(<PanelHeader>Test</PanelHeader>);
expect(screen.getByText('Test')).toHaveClass('pf-c-panel__header');
});

test('Renders with only the class pf-c-panel__header by default', () => {
render(<PanelHeader>Test</PanelHeader>);
expect(screen.getByText('Test')).toHaveClass('pf-c-panel__header', { exact: true });
});

test('Renders with custom class name when className prop is passed', () => {
render(<PanelHeader className="test-class">Test</PanelHeader>);
expect(screen.getByText('Test')).toHaveClass('test-class');
});

test('Renders with the inherited element props spread to the component', () => {
render(<PanelHeader aria-label="this is a simple panel header">Test</PanelHeader>);
expect(screen.getByText('Test')).toHaveAccessibleName('this is a simple panel header');
});

test('Matches the snapshot', () => {
const { asFragment } = render(<PanelHeader>Test</PanelHeader>);
expect(asFragment()).toMatchSnapshot();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import * as React from 'react';
import { render, screen } from '@testing-library/react';
import { PanelMain } from '../PanelMain';

test('Renders without children', () => {
render(
<div data-testid="panelMain">
<PanelMain />
</div>
);
expect(screen.getByTestId('panelMain').firstChild).toBeVisible();
});

test('Renders children', () => {
render(<PanelMain>Test</PanelMain>);
expect(screen.getByText('Test')).toBeVisible();
});

test('Renders with the class pf-c-panel__main', () => {
render(<PanelMain>Test</PanelMain>);
expect(screen.getByText('Test')).toHaveClass('pf-c-panel__main');
});

test('Renders with only the class pf-c-panel__main by default', () => {
render(<PanelMain>Test</PanelMain>);
expect(screen.getByText('Test')).toHaveClass('pf-c-panel__main', { exact: true });
});

test('Renders with custom class name when className prop is passed', () => {
render(<PanelMain className="test-class">Test</PanelMain>);
expect(screen.getByText('Test')).toHaveClass('test-class');
});

test('Renders with custom max height name when maxHeight prop is passed', () => {
render(<PanelMain maxHeight="100px">Test</PanelMain>);
const styles = getComputedStyle(screen.getByText('Test'));
expect(styles.getPropertyValue('--pf-c-panel__main--MaxHeight')).toBe('100px');
});

test('Renders with the inherited element props spread to the component', () => {
render(<PanelMain aria-label="this is a simple panel main">Test</PanelMain>);
expect(screen.getByText('Test')).toHaveAccessibleName('this is a simple panel main');
});

test('Matches the snapshot', () => {
const { asFragment } = render(<PanelMain>Test</PanelMain>);
expect(asFragment()).toMatchSnapshot();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import * as React from 'react';
import { render, screen } from '@testing-library/react';
import { PanelMainBody } from '../PanelMainBody';

test('Renders without children', () => {
render(
<div data-testid="panelMainBody">
<PanelMainBody />
</div>
);
expect(screen.getByTestId('panelMainBody').firstChild).toBeVisible();
});

test('Renders children', () => {
render(<PanelMainBody>Test</PanelMainBody>);
expect(screen.getByText('Test')).toBeVisible();
});

test('Renders with the class pf-c-panel__main-body', () => {
render(<PanelMainBody>Test</PanelMainBody>);
expect(screen.getByText('Test')).toHaveClass('pf-c-panel__main-body');
});

test('Renders with only the class pf-c-panel__main-body by default', () => {
render(<PanelMainBody>Test</PanelMainBody>);
expect(screen.getByText('Test')).toHaveClass('pf-c-panel__main-body', { exact: true });
});

test('Renders with custom class name when className prop is passed', () => {
render(<PanelMainBody className="test-class">Test</PanelMainBody>);
expect(screen.getByText('Test')).toHaveClass('test-class');
});

test('Renders with the inherited element props spread to the component', () => {
render(<PanelMainBody aria-label="this is a simple panel main body">Test</PanelMainBody>);
expect(screen.getByText('Test')).toHaveAccessibleName('this is a simple panel main body');
});

test('Matches the snapshot', () => {
const { asFragment } = render(<PanelMainBody>Test</PanelMainBody>);
expect(asFragment()).toMatchSnapshot();
});
Loading