File size: 1,892 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
/**
* Testing our Button component
*/
import React from 'react';
import { fireEvent, render } from 'react-testing-library';
import Button from '../index';
const handleRoute = () => {};
const href = 'http://mxstbr.com';
const children = <h1>Test</h1>;
const renderComponent = (props = {}) =>
render(
<Button href={href} {...props}>
{children}
</Button>,
);
describe('<Button />', () => {
it('should render an <a> tag if no route is specified', () => {
const { container } = renderComponent({ href });
expect(container.querySelector('a')).not.toBeNull();
});
it('should render a <button> tag to change route if the handleRoute prop is specified', () => {
const { container } = renderComponent({ handleRoute });
expect(container.querySelector('button')).toBeDefined();
});
it('should have children', () => {
const { container } = renderComponent();
expect(container.querySelector('a').children).toHaveLength(1);
});
it('should handle click events', () => {
const onClickSpy = jest.fn();
const { container } = renderComponent({ onClick: onClickSpy });
fireEvent.click(container.querySelector('a'));
expect(onClickSpy).toHaveBeenCalled();
});
it('should have a class attribute', () => {
const { container } = renderComponent();
expect(container.querySelector('a').hasAttribute('class')).toBe(true);
});
it('should not adopt a type attribute when rendering an <a> tag', () => {
const type = 'text/html';
const { container } = renderComponent({ href, type });
expect(container.querySelector(`a[type="${type}"]`)).toBeNull();
});
it('should not adopt a type attribute when rendering a button', () => {
const type = 'submit';
const { container } = renderComponent({ handleRoute, type });
expect(container.querySelector('button').getAttribute('type')).toBeNull();
});
});
|