* spelling: access Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: additional Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: administrative Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: aggregate Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: assigns Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: attributes Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: autocomplete Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: because Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: between Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: bootstrap Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: calculating Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: captcha Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: character Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: chosen Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: commenter Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: competitor Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: componentize Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: contrast Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: corresponding Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: description Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: destroyed Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: destroys Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: discussion Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: episode Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: escaped Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: evaluates Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: expired Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: explicitly Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: facebook Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: fragment Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: functionality Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: improper Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: incentive Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: interfere Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: latest Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: message Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: minimum Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: moderator Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: mouseover Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: mutual Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: nonexistent Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: notification Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: occasionally Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: occurrence Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: occurs Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: octokit Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: offset Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: omitted Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: opacity Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: organization Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: organizations Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: overridden Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: override Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: overriding Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: prefill Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: previous Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: profile Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: recycling Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: registered Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: repositories Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: rescuing Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: response Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: returns Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: second Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: separator Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: services Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: subscriber Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: subscription Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: success Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: successful Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: successfully Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: suppress Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: test Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: thought Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: uniqueness Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: unknown Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: unproductive Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: unreachable Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: unsuccessful Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: utilities Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: utility Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: valid Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: voluntarily Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: vomited Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: website Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: withholding Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> Co-authored-by: Josh Soref <jsoref@users.noreply.github.com>
191 lines
5.7 KiB
JavaScript
191 lines
5.7 KiB
JavaScript
import { h } from 'preact';
|
|
import { render, fireEvent } from '@testing-library/preact';
|
|
import { axe } from 'jest-axe';
|
|
import { Button } from '@crayons';
|
|
|
|
describe('<Button /> component', () => {
|
|
it('should have no a11y violations when rendered', async () => {
|
|
const { container } = render(<Button>Hello world!</Button>);
|
|
const results = await axe(container);
|
|
|
|
expect(results).toHaveNoViolations();
|
|
});
|
|
|
|
it('should render a primary button when using default values', () => {
|
|
const { container } = render(<Button>Hello world!</Button>);
|
|
expect(container.innerHTML).toMatchSnapshot();
|
|
});
|
|
|
|
it('should render with a tabIndex', () => {
|
|
const { container } = render(<Button tabIndex="0">Hello world!</Button>);
|
|
expect(container.innerHTML).toMatchSnapshot();
|
|
});
|
|
|
|
it('should render a secondary button when using the variant "secondary"', () => {
|
|
const { container } = render(
|
|
<Button variant="secondary">Hello world!</Button>,
|
|
);
|
|
expect(container.innerHTML).toMatchSnapshot();
|
|
});
|
|
|
|
it('should render an outlined button when using the variant "outlined"', () => {
|
|
const { container } = render(
|
|
<Button variant="outlined">Hello world!</Button>,
|
|
);
|
|
expect(container.innerHTML).toMatchSnapshot();
|
|
});
|
|
|
|
it('should render a danger button when using the variant "danger"', () => {
|
|
const { container } = render(
|
|
<Button variant="danger">Hello world!</Button>,
|
|
);
|
|
expect(container.innerHTML).toMatchSnapshot();
|
|
});
|
|
|
|
it('should render an enabled button when using default values', () => {
|
|
const { container } = render(<Button>Hello world!</Button>);
|
|
expect(container.innerHTML).toMatchSnapshot();
|
|
});
|
|
|
|
it('should render a disabled button when disabled is true', () => {
|
|
const { container } = render(<Button disabled>Hello world!</Button>);
|
|
expect(container.innerHTML).toMatchSnapshot();
|
|
});
|
|
|
|
it('should render a button with additional CSS classes when className is set', () => {
|
|
const { container } = render(
|
|
<Button disabled className="some-additional-class-name">
|
|
Hello world!
|
|
</Button>,
|
|
);
|
|
expect(container.innerHTML).toMatchSnapshot();
|
|
});
|
|
|
|
it('should render a button with with an icon when an icon is set and there is button text', () => {
|
|
const Icon = () => (
|
|
<svg
|
|
width="24"
|
|
height="24"
|
|
xmlns="http://www.w3.org/2000/svg"
|
|
className="crayons-icon"
|
|
>
|
|
<path d="M9.99999 15.172L19.192 5.979L20.607 7.393L9.99999 18L3.63599 11.636L5.04999 10.222L9.99999 15.172Z" />
|
|
</svg>
|
|
);
|
|
|
|
const { container } = render(
|
|
<Button icon={Icon} contentType="icon-left">
|
|
Hello world!
|
|
</Button>,
|
|
);
|
|
expect(container.innerHTML).toMatchSnapshot();
|
|
});
|
|
|
|
it('should render a button with with an icon when an icon is set and there is no button text', () => {
|
|
const Icon = () => (
|
|
<svg
|
|
width="24"
|
|
height="24"
|
|
xmlns="http://www.w3.org/2000/svg"
|
|
className="crayons-icon"
|
|
>
|
|
<path d="M9.99999 15.172L19.192 5.979L20.607 7.393L9.99999 18L3.63599 11.636L5.04999 10.222L9.99999 15.172Z" />
|
|
</svg>
|
|
);
|
|
|
|
const { container } = render(<Button icon={Icon} contentType="icon" />);
|
|
expect(container.innerHTML).toMatchSnapshot();
|
|
});
|
|
|
|
it('should render a button as an anchor element if "tagName" is set to "a"', () => {
|
|
const { container } = render(
|
|
<Button tagName="a" url="https://dev.to">
|
|
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 buttonType="submit">Hello world!</Button>,
|
|
);
|
|
expect(container.innerHTML).toMatchSnapshot();
|
|
});
|
|
|
|
it('should fire the onClick event when the button is clicked.', () => {
|
|
const someEvent = jest.fn();
|
|
|
|
const { getByText } = render(
|
|
<Button onClick={someEvent}>Hello world!</Button>,
|
|
);
|
|
|
|
const button = getByText('Hello world!');
|
|
button.click();
|
|
|
|
expect(someEvent).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it('should fire the onMouseOver event when the button is moused over.', () => {
|
|
const someEvent = jest.fn();
|
|
|
|
const { getByText } = render(
|
|
<Button onMouseOver={someEvent} onfocus={jest.fn()}>
|
|
Hello world!
|
|
</Button>,
|
|
);
|
|
|
|
const button = getByText('Hello world!');
|
|
fireEvent.mouseOver(button);
|
|
|
|
expect(someEvent).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it('should fire the onMouseOut event when the button is moused out.', () => {
|
|
const someEvent = jest.fn();
|
|
|
|
const { getByText } = render(
|
|
<Button onMouseOut={someEvent} onBlur={jest.fn()}>
|
|
Hello world!
|
|
</Button>,
|
|
);
|
|
const button = getByText('Hello world!');
|
|
fireEvent.mouseOut(button);
|
|
|
|
expect(someEvent).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it('should fire the onFocus event when the button is given focus.', () => {
|
|
const someEvent = jest.fn();
|
|
|
|
const { getByText } = render(
|
|
<Button onFocus={someEvent}>Hello world!</Button>,
|
|
);
|
|
|
|
const button = getByText('Hello world!');
|
|
button.focus();
|
|
|
|
expect(someEvent).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it('should fire the onBlur event when the button loses focus.', () => {
|
|
const someEvent = jest.fn();
|
|
|
|
const { getByText } = render(
|
|
<Button onBlur={someEvent}>Hello world!</Button>,
|
|
);
|
|
|
|
const button = getByText('Hello world!');
|
|
button.focus();
|
|
button.blur();
|
|
|
|
expect(someEvent).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it('should render with a tooltip', () => {
|
|
const { container } = render(
|
|
<Button tooltip="tooltip text">Hello world!</Button>,
|
|
);
|
|
expect(container.innerHTML).toMatchSnapshot();
|
|
});
|
|
});
|