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

Coachmark (v8): convert tests to use testing-library #22242

Merged
Show file tree
Hide file tree
Changes from 1 commit
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
63 changes: 30 additions & 33 deletions packages/react/src/components/Coachmark/Coachmark.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as React from 'react';
import { Coachmark } from './Coachmark';
import { safeCreate, safeMount } from '@fluentui/test-utilities';
import { Coachmark, ICoachmarkProps } from './index';
import { render, within } from '@testing-library/react';
import { resetIds } from '@fluentui/utilities';
import { isConformant } from '../../common/isConformant';
import * as path from 'path';
Expand Down Expand Up @@ -30,66 +30,63 @@ describe('Coachmark', () => {
it('renders Coachmark (correctly)', () => {
ReactDOM.createPortal = jest.fn(element => element);

safeCreate(
const { container } = render(
<Coachmark className={'test-className'} target="test-target">
This is a test
</Coachmark>,
component => {
const tree = component!.toJSON();
expect(tree).toMatchSnapshot();
},
);
expect(container).toMatchSnapshot();
});

it('renders Coachmark (isCollapsed)', () => {
ReactDOM.createPortal = jest.fn(element => element);

safeCreate(
const { container } = render(
<Coachmark isCollapsed={false} className={'test-className'} target="test-target">
This is a test
</Coachmark>,
component => {
const tree = component!.toJSON();
expect(tree).toMatchSnapshot();
},
);
expect(container).toMatchSnapshot();
});

it('renders Coachmark (color properties)', () => {
ReactDOM.createPortal = jest.fn(element => element);

safeCreate(<Coachmark beaconColorOne="green" beaconColorTwo="blue" color="red" target="test" />, component => {
const tree = component!.toJSON();
expect(tree).toMatchSnapshot();
});
const { container } = render(<Coachmark beaconColorOne="green" beaconColorTwo="blue" color="red" target="test" />);
expect(container).toMatchSnapshot();
});

// Accessibility Tests:
it('correctly applies (ariaAlertText)', () => {
safeMount(<Coachmark ariaAlertText="aria alert " target="test-target" />, component => {
expect(component.find('[role="alert"]').getDOMNode()).toBeDefined();
});
const { getByRole } = render(<Coachmark ariaAlertText="aria alert " target="test-target" />);
expect(getByRole('alert')).toBeTruthy();
});

it('correctly applies (ariaLabelBy)', () => {
safeMount(
<Coachmark ariaLabelledBy="aria label" ariaLabelledByText="aria text" target="test-target" />,
component => {
expect(component.find('[role="dialog"]').getDOMNode().getAttribute('aria-labelledby')).toBe('aria label');
expect(component.find('[id="aria label"]').text()).toBe('aria text');
},
const { getByRole } = render(
<Coachmark
ariaLabelledBy="aria label"
ariaLabelledByText="aria text"
target={document as ICoachmarkProps['target']}
/>,
);

const coachmark = getByRole('dialog', { hidden: true });
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needed hidden:true option since dialog role is inaccessible here because the element containing that role has a parent wrapper with role=presentation when the Coachmark is not collapsed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting, I wonder if that's an actual a11y problem? Regardless would be good to note that in a comment in the file.

expect(coachmark.getAttribute('aria-labelledby')).toBe('aria label');
expect(within(coachmark).queryByText('aria text')).toBeTruthy();
});

it('correctly applies (ariaDescribedBy)', () => {
safeMount(
<Coachmark ariaDescribedBy="aria description" ariaDescribedByText="aria description text" target="test-target" />,
component => {
expect(component.find('[role="dialog"]').getDOMNode().getAttribute('aria-describedby')).toBe(
'aria description',
);
expect(component.find('[id="aria description"]').text()).toBe('aria description text');
},
const { getByRole } = render(
<Coachmark
ariaDescribedBy="aria description"
ariaDescribedByText="aria description text"
target={document as ICoachmarkProps['target']}
/>,
);

const coachmark = getByRole('dialog', { hidden: true });
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

expect(coachmark.getAttribute('aria-describedby')).toBe('aria description');
expect(within(coachmark).queryByText('aria description text')).toBeTruthy();
});
});
Loading