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

test(Coachmark): increases test coverage #6496

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
100 changes: 99 additions & 1 deletion packages/ibm-products/src/components/Coachmark/Coachmark.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@
*/

import React from 'react';
import { render, screen, act } from '@testing-library/react'; // https://testing-library.com/docs/react-testing-library/intro
import {
render,
screen,
act,
waitFor,
fireEvent,
} from '@testing-library/react'; // https://testing-library.com/docs/react-testing-library/intro
import userEvent from '@testing-library/user-event';
import { pkg } from '../../settings';
import uuidv4 from '../../global/js/utils/uuidv4';
Expand All @@ -18,6 +24,7 @@ import {
CoachmarkOverlayElements,
} from '..';
import { BEACON_KIND } from './utils/enums';
import { CoachmarkDragbar } from './CoachmarkDragbar';
const blockClass = `${pkg.prefix}--coachmark`;
const componentName = Coachmark.displayName;

Expand Down Expand Up @@ -105,4 +112,95 @@ describe(componentName, () => {
componentName
);
});

it('renders the closeIconDescription text ', async () => {
const a11yKeyboardHandler = jest.fn();
const onClose = jest.fn();
const closeIconDescription = 'Close';
render(
<CoachmarkDragbar
a11yKeyboardHandler={a11yKeyboardHandler}
closeIconDescription={closeIconDescription}
onClose={onClose}
showCloseButton
/>
);

const tooltip = screen.getByText(closeIconDescription);

expect(tooltip).toBeInTheDocument();
});

it('calls the onClose prop when close is clicked', async () => {
const a11yKeyboardHandler = jest.fn();
const onClose = jest.fn();
render(
<CoachmarkDragbar
a11yKeyboardHandler={a11yKeyboardHandler}
onClose={onClose}
/>
);

const closeButton = screen.getAllByRole('button')[1];

expect(closeButton).toBeInTheDocument();
await waitFor(() => userEvent.click(closeButton));
expect(onClose).toHaveBeenCalled();
});

it('showCloseButton prop works as expected', async () => {
const a11yKeyboardHandler = jest.fn();

const { rerender } = render(
<CoachmarkDragbar
a11yKeyboardHandler={a11yKeyboardHandler}
showCloseButton
/>
);

expect(screen.queryAllByRole('button').length).toBe(2);

rerender(
<CoachmarkDragbar
a11yKeyboardHandler={a11yKeyboardHandler}
showCloseButton={false}
/>
);

expect(screen.queryAllByRole('button').length).toBe(1);
});

it('calls the onDrag prop', async () => {
const a11yKeyboardHandler = jest.fn();
const onDrag = jest.fn();

render(
<CoachmarkDragbar
a11yKeyboardHandler={a11yKeyboardHandler}
showCloseButton={false}
onDrag={onDrag}
/>
);

const dragbar = screen.getByRole('button');

await waitFor(() => {
fireEvent.mouseDown(dragbar, { clientX: 0, clientY: 0 });
fireEvent.mouseMove(dragbar, { clientX: 100, clientY: 100 });
fireEvent.mouseUp(dragbar);
});

expect(onDrag).toHaveBeenCalled();
});

it('renders the theme prop', async () => {
renderCoachmark({
'data-testid': dataTestId,
theme: 'dark',
});

await expect(screen.getByTestId(dataTestId)).toHaveClass(
`${pkg.prefix}--coachmark__dark`
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ describe(componentName, () => {
renderCoachmarkWithBeacon({
label: testingLabel,
});
screen.getByLabelText(testingLabel);
screen.getAllByLabelText(testingLabel)[1];
});

it('forwards a ref to an appropriate node', () => {
Expand Down
Loading