docbrown/app/javascript/crayons/Buttons/__tests__/Buttons.test.jsx
ludwiczakpawel 878c27f9b0
Crayons: buttons followup (#15720)
* init

* whoops

* components update

* components update

* classes

* variables

* focus

* test

* spec

* test

* adjust colors and variables

* buttons colors

* Update app/javascript/crayons/CTAs/CTA.jsx

Co-authored-by: Suzanne Aitchison <suzanne@forem.com>

* proptype

* remove duplicated tests now in Cypress, enhance Cypress nav menu tests

* premove unneeded viewport in test

* remove focus check for now

* classnames

* Update app/javascript/crayons/CTAs/__stories__/CTAs.mdx

Co-authored-by: Nick Taylor <nick@iamdeveloper.com>

* Update app/javascript/crayons/CTAs/__stories__/CTAs.mdx

Co-authored-by: Nick Taylor <nick@iamdeveloper.com>

* better contrast

* classname

* add secondary button

* variants + docs

* docs updates

* radius-full

* variants oneof

* the?

* default values + extra stories

* Apply suggestions from code review

Co-authored-by: Julianna Tetreault <32834804+juliannatetreault@users.noreply.github.com>

Co-authored-by: Suzanne Aitchison <suzanne@forem.com>
Co-authored-by: Nick Taylor <nick@iamdeveloper.com>
Co-authored-by: Julianna Tetreault <32834804+juliannatetreault@users.noreply.github.com>
2021-12-14 14:43:41 +01:00

143 lines
4.3 KiB
JavaScript

import { h } from 'preact';
import { render, fireEvent } from '@testing-library/preact';
import { axe } from 'jest-axe';
import { ButtonNew as Button } from '@crayons';
import CogIcon from '@images/cog.svg';
import '@testing-library/jest-dom';
describe('<Button />', () => {
it('has no accessibility errors in default variant', async () => {
const { container } = render(<Button>Hello world!</Button>);
const results = await axe(container);
expect(results).toHaveNoViolations();
});
it('has no accessibility errors in primary variant', async () => {
const { container } = render(
<Button variant="primary">Hello world!</Button>,
);
const results = await axe(container);
expect(results).toHaveNoViolations();
});
it('has no accessibility errors in secondary variant', async () => {
const { container } = render(
<Button variant="secondary">Hello world!</Button>,
);
const results = await axe(container);
expect(results).toHaveNoViolations();
});
it('has no accessibility errors when props provided', async () => {
const { container } = render(
<Button
variant="primary"
rounded
destructive
icon={CogIcon}
tooltip="tooltip"
>
Hello world!
</Button>,
);
const results = await axe(container);
expect(results).toHaveNoViolations();
});
it('renders a default button', () => {
const { container } = render(<Button>Hello world!</Button>);
expect(container.innerHTML).toMatchSnapshot();
});
it('renders a primary button', () => {
const { container } = render(
<Button variant="primary">Hello world!</Button>,
);
expect(container.innerHTML).toMatchSnapshot();
});
it('renders a secondary button', () => {
const { container } = render(
<Button variant="secondary">Hello world!</Button>,
);
expect(container.innerHTML).toMatchSnapshot();
});
it('renders with an icon and text', () => {
const { container } = render(<Button icon={CogIcon}>Hello world!</Button>);
expect(container.innerHTML).toMatchSnapshot();
});
it('renders with an icon only', () => {
const { container } = render(<Button icon={CogIcon} />);
expect(container.innerHTML).toMatchSnapshot();
});
it('renders a destructive button', () => {
const { container } = render(<Button destructive>Hello world!</Button>);
expect(container.innerHTML).toMatchSnapshot();
});
it('renders a rounded button', () => {
const { container } = render(<Button rounded>Hello world!</Button>);
expect(container.innerHTML).toMatchSnapshot();
});
it('renders with a tooltip', () => {
const { container } = render(
<Button tooltip="tooltip text">Hello world!</Button>,
);
expect(container.innerHTML).toMatchSnapshot();
});
it('renders with additional classnames', () => {
const { container } = render(
<Button className="one two three">Hello world!</Button>,
);
expect(container.innerHTML).toMatchSnapshot();
});
it('should render a button as a specific button type (HTML type attribute) when buttonType is set.', () => {
const { container } = render(<Button type="submit">Hello world!</Button>);
expect(container.innerHTML).toMatchSnapshot();
});
it('should attach additional passed props to the button element', () => {
const mockClickHandler = jest.fn();
const { getByRole } = render(
<Button onClick={mockClickHandler}>Hello world!</Button>,
);
const button = getByRole('button', { name: 'Hello world!' });
button.click();
expect(mockClickHandler).toHaveBeenCalledTimes(1);
});
it('should suppress any tooltip and execute any onKeyUp prop when Escape is pressed', async () => {
const mockKeyUpHandler = jest.fn();
const { getByRole, getByTestId } = render(
<Button tooltip="test tooltip" onKeyUp={mockKeyUpHandler}>
Hello world!
</Button>,
);
const button = getByRole('button', { name: 'Hello world! test tooltip' });
button.focus();
expect(getByTestId('tooltip')).not.toHaveClass(
'crayons-tooltip__suppressed',
);
fireEvent.keyUp(button, { key: 'Escape' });
expect(getByTestId('tooltip')).toHaveClass('crayons-tooltip__suppressed');
expect(mockKeyUpHandler).toHaveBeenCalledTimes(1);
});
});