Skip to content

Commit

Permalink
test: add unit tests for SwitcherItem component to improve coverage (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
mariat189 authored Nov 12, 2024
1 parent 8713091 commit 4e5ffb2
Showing 1 changed file with 97 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
/**
* Copyright IBM Corp. 2023
* Copyright IBM Corp. 2024
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/

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

import React from 'react';
import SwitcherItem from '../SwitcherItem';
import { render, screen, fireEvent } from '@testing-library/react';

describe('SwitcherItem', () => {
describe('renders as expected - Component API', () => {
Expand Down Expand Up @@ -92,5 +93,99 @@ describe('SwitcherItem', () => {

expect(dummyHandler).toHaveBeenCalledTimes(1);
});

it('should handle onKeyDown event', () => {
const dummyHandler = jest.fn();

render(
<SwitcherItem aria-label="dummy-aria-label" onKeyDown={dummyHandler}>
Dummy child
</SwitcherItem>
);

fireEvent.keyDown(screen.getByRole('listitem').firstChild, {
key: 'Enter',
code: 'Enter',
});

expect(dummyHandler).toHaveBeenCalledTimes(1);
});

it('should handle keyboard navigation with ArrowUp and ArrowDown keys', () => {
const mockHandleSwitcherItemFocus = jest.fn();

render(
<SwitcherItem
aria-label="dummy-aria-label"
index={0}
handleSwitcherItemFocus={mockHandleSwitcherItemFocus}>
Dummy child
</SwitcherItem>
);
fireEvent.keyDown(screen.getByRole('listitem').firstChild, {
key: 'ArrowDown',
code: 'ArrowDown',
});
expect(mockHandleSwitcherItemFocus).toHaveBeenCalledWith({
currentIndex: -1,
direction: 1,
});

fireEvent.keyDown(screen.getByRole('listitem').firstChild, {
key: 'ArrowUp',
code: 'ArrowUp',
});
expect(mockHandleSwitcherItemFocus).toHaveBeenCalledWith({
currentIndex: -1,
direction: -1,
});
});

it('should apply selected class when isSelected prop is true', () => {
const { container } = render(
<SwitcherItem isSelected aria-label="dummy-aria-label">
Dummy child
</SwitcherItem>
);
const link = container.querySelector('a');
expect(link).toHaveClass('cds--switcher__item-link--selected');
});

it('should have tabIndex 0 when expanded is true', () => {
const { container } = render(
<SwitcherItem expanded={true} aria-label="dummy-aria-label">
Dummy child
</SwitcherItem>
);
const link = container.querySelector('a');
expect(link).toHaveAttribute('tabIndex', '0');
});

it('should have tabIndex -1 when expanded is false', () => {
const { container } = render(
<SwitcherItem expanded={false} aria-label="dummy-aria-label">
Dummy child
</SwitcherItem>
);
const link = container.querySelector('a');
expect(link).toHaveAttribute('tabIndex', '-1');
});

it('should not call handleSwitcherItemFocus when not provided', () => {
const { container } = render(
<SwitcherItem aria-label="dummy-aria-label">Dummy child</SwitcherItem>
);
const link = container.querySelector('a');
fireEvent.keyDown(link, { key: 'ArrowDown' });
expect(container).toBeTruthy();
});

it('should not throw an error when onClick is not provided', () => {
expect(() => {
render(
<SwitcherItem aria-label="dummy-aria-label">Dummy child</SwitcherItem>
);
}).not.toThrow();
});
});
});

0 comments on commit 4e5ffb2

Please sign in to comment.