import { fireEvent, render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import DeviceTypeProvider from './contexts/DeviceTypeProvider';
import Dropdown from './Dropdown';
import SideNavigation from './SideNavigation';
describe('SideNavigation desktop', () => {
function Component({
display,
expanded,
}: {
display?: 'static' | 'expandable';
expanded?: boolean;
}) {
return (
{} }}>
{
event.preventDefault();
},
tooltip: { text: 'More options', accessibilityLabel: 'More options label' },
dropdownItems: [
{}}
option={{ value: 'Edit', label: 'Edit' }}
/>,
{}}
option={{ value: 'Delete', label: 'Delete' }}
/>,
],
}}
>
event.preventDefault()}
/>
event.preventDefault()}
primaryAction={{
onClick: ({ event }: any) => {
event.preventDefault();
},
tooltip: {
text: 'More options nested',
accessibilityLabel: 'More options nested label',
},
dropdownItems: [
{}}
option={{ value: 'Edit', label: 'Edit' }}
/>,
{}}
option={{ value: 'Delete', label: 'Delete' }}
/>,
],
}}
/>
);
}
test('renders correctly on hover', () => {
render();
// BEFORE HOVERING
expect(
screen.getByText('Group item', {
exact: true,
}),
).toBeVisible();
expect(
screen.getByText('123', {
exact: true,
}),
).toBeVisible();
expect(
screen.queryByLabelText('More options label', {
exact: true,
}),
).toBeNull();
expect(
screen.getByText('Top item', {
exact: true,
}),
).toBeVisible();
expect(
screen.getByText('321', {
exact: true,
}),
).toBeVisible();
// ON HOVER
fireEvent.mouseEnter(screen.getByText('Group item'));
expect(
screen.getByText('Group item', {
exact: true,
}),
).toBeVisible();
expect(screen.queryByText('123')).toBeNull();
expect(
screen.getByLabelText('More options label', {
exact: true,
}),
).toBeVisible();
expect(
screen.queryByText('More options', {
exact: true,
}),
).toBeNull();
expect(
screen.getByText('Top item', {
exact: true,
}),
).toBeVisible();
expect(
screen.getByText('321', {
exact: true,
}),
).toBeVisible();
fireEvent.mouseEnter(
screen.getByLabelText('More options label', {
exact: true,
}),
);
expect(
screen.getByText('More options', {
exact: true,
}),
).toBeVisible();
});
test('renders correctly on focus', async () => {
render();
// BEFORE FOCUSING
expect(
screen.getByText('Group item', {
exact: true,
}),
).toBeVisible();
expect(
screen.getByText('123', {
exact: true,
}),
).toBeVisible();
expect(
screen.queryByLabelText('More options label', {
exact: true,
}),
).toBeNull();
expect(document.body).toHaveFocus();
// ON FOCUS
await userEvent.tab();
expect(
screen.getByRole('button', {
exact: true,
}),
).toHaveFocus();
expect(
screen.getByText('Group item', {
exact: true,
}),
).toBeVisible();
expect(screen.queryByText('123')).toBeNull();
expect(
screen.getByLabelText('More options label', {
exact: true,
}),
).toBeVisible();
expect(
screen.queryByText('More options', {
exact: true,
}),
).toBeNull();
await userEvent.tab();
expect(
screen.queryByText('More options', {
exact: true,
}),
).toBeVisible();
});
test('renders display static correctly', () => {
render();
expect(
screen.getByText('test', {
exact: true,
}),
).toBeVisible();
});
test('renders display expandable correctly', () => {
render();
expect(
screen.queryByText('test', {
exact: true,
}),
).toBeNull();
});
test('renders display expandableExpanded correctly', () => {
render();
expect(
screen.getByText('test', {
exact: true,
}),
).toBeVisible();
});
test('renders primaryAction dropdown correctly on click', () => {
render();
fireEvent.mouseEnter(screen.getByText('Group item'));
expect(screen.queryByText('Edit')).toBeNull();
fireEvent.click(screen.getByLabelText('More options label'));
expect(
screen.getByText('Edit', {
exact: true,
}),
).toBeVisible();
});
test('renders primaryAction dropdown correctly on enter', async () => {
render();
expect(document.body).toHaveFocus();
await userEvent.tab();
await userEvent.tab();
expect(
screen.getByLabelText('More options label', {
exact: true,
}),
).toHaveFocus();
expect(screen.queryByText('Edit')).not.toBeInTheDocument();
await userEvent.keyboard('[Enter]');
expect(
screen.getByText('Edit', {
exact: true,
}),
).toBeInTheDocument();
});
});
describe('SideNavigation mobile', () => {
function Component() {
return (
{} }}>
{
event.preventDefault();
},
tooltip: { text: 'More options', accessibilityLabel: 'More options label' },
dropdownItems: [
{}}
option={{ value: 'Edit', label: 'Edit' }}
/>,
{}}
option={{ value: 'Delete', label: 'Delete' }}
/>,
],
}}
>
event.preventDefault()}
/>
event.preventDefault()}
primaryAction={{
onClick: ({ event }: any) => {
event.preventDefault();
},
tooltip: {
text: 'More options nested',
accessibilityLabel: 'More options nested label',
},
dropdownItems: [
{}}
option={{ value: 'Edit', label: 'Edit' }}
/>,
{}}
option={{ value: 'Delete', label: 'Delete' }}
/>,
],
}}
/>
);
}
test('renders correctly on hover', () => {
render();
expect(
screen.getByText('Group item', {
exact: true,
}),
).toBeVisible();
expect(
screen.getByText('123', {
exact: true,
}),
).toBeVisible();
expect(
screen.getByText('Top item', {
exact: true,
}),
).toBeVisible();
expect(
screen.getByText('321', {
exact: true,
}),
).toBeVisible();
expect(screen.queryByText('More options')).toBeNull();
fireEvent.mouseEnter(screen.getByText('Group item'));
expect(
screen.getByText('Group item', {
exact: true,
}),
).toBeVisible();
expect(
screen.getByText('123', {
exact: true,
}),
).toBeVisible();
expect(screen.queryByText('More options')).toBeNull();
fireEvent.mouseEnter(screen.getByLabelText('More options label'));
expect(screen.getByText('More options')).toBeVisible();
});
test('renders correctly on focus', async () => {
render();
expect(
screen.getByText('Group item', {
exact: true,
}),
).toBeVisible();
expect(
screen.getByText('123', {
exact: true,
}),
).toBeVisible();
expect(screen.getByLabelText('More options label')).toBeVisible();
await userEvent.tab();
await userEvent.tab();
expect(
screen.getByText('Group item', {
exact: true,
}),
).toBeVisible();
expect(
screen.getByText('123', {
exact: true,
}),
).toBeVisible();
expect(
screen.getByText('More options', {
exact: true,
}),
).toBeVisible();
});
test('renders primaryAction dropdown correctly on click', () => {
render();
fireEvent.mouseEnter(screen.getByText('Group item'));
expect(screen.queryByText('Edit')).toBeNull();
fireEvent.click(screen.getByLabelText('More options label'));
expect(
screen.getByText('Edit', {
exact: true,
}),
).toBeVisible();
});
test('renders primaryAction dropdown correctly on enter', async () => {
render();
await userEvent.tab();
await userEvent.tab();
expect(
screen.getByLabelText('More options label', {
exact: true,
}),
).toHaveFocus();
expect(screen.queryByText('Edit')).toBeNull();
await userEvent.keyboard('[Enter]');
expect(
screen.getByText('Edit', {
exact: true,
}),
).toBeVisible();
});
});