import React from 'react';
import { fireEvent, render } from '@testing-library/react';
import 'jest-styled-components';
import 'jest-axe/extend-expect';
import 'regenerator-runtime/runtime';
import { axe } from 'jest-axe';
import { Grommet } from '../../Grommet';
import { Anchor } from '..';
import { Box } from '../../Box';
import { Paragraph } from '../../Paragraph';
import { Text } from '../../Text';
import { LinkNext } from 'grommet-icons';
describe('Anchor', () => {
test('should have no accessibility violations', async () => {
const { container } = render(
Link
,
);
const results = await axe(container);
expect(results).toHaveNoViolations();
expect(container).toMatchSnapshot();
});
test('renders', () => {
const { container } = render(
,
);
expect(container).toMatchSnapshot();
});
test('renders with children', () => {
const { container } = render(
children
,
);
expect(container.firstChild).toMatchSnapshot();
});
test('warns about invalid label render', () => {
const warnSpy = jest.spyOn(console, 'warn').mockImplementation();
const { container } = render(
invalid
,
);
expect(container.firstChild).toMatchSnapshot();
expect(warnSpy).toHaveBeenCalledWith(
'Anchor should not have children if icon or label is provided',
);
warnSpy.mockReset();
warnSpy.mockRestore();
});
test('warns about invalid icon render', () => {
const warnSpy = jest.spyOn(console, 'warn').mockImplementation();
const { container } = render(
}>
invalid
,
);
expect(container.firstChild).toMatchSnapshot();
expect(warnSpy).toHaveBeenCalledWith(
'Anchor should not have children if icon or label is provided',
);
warnSpy.mockReset();
warnSpy.mockRestore();
});
test('shows no error for component used in as prop', () => {
const errorSpy = jest.spyOn(console, 'error').mockImplementation();
const { container } = render(
,
);
expect(container.firstChild).toMatchSnapshot();
expect(errorSpy).not.toHaveBeenCalled();
errorSpy.mockReset();
errorSpy.mockRestore();
});
test('focus renders', () => {
const onFocus = jest.fn();
const { container, getByText } = render(
,
);
fireEvent.focus(getByText('Test'));
expect(container.firstChild).toMatchSnapshot();
expect(onFocus).toHaveBeenCalledTimes(1);
});
test('blur renders', () => {
const onBlur = jest.fn();
const { container, getByText } = render(
,
);
fireEvent.blur(getByText('Test'));
expect(container.firstChild).toMatchSnapshot();
expect(onBlur).toHaveBeenCalledTimes(1);
});
test('disabled renders', () => {
const { container } = render(
,
);
expect(container).toMatchSnapshot();
});
test('icon label renders', () => {
const { container } = render(
} label="Test" onClick={() => {}} />
,
);
expect(container).toMatchSnapshot();
});
test('reverse icon label renders', () => {
const { container } = render(
} label="Test" onClick={() => {}} />
,
);
expect(container).toMatchSnapshot();
});
test('size renders', () => {
const { container } = render(
,
);
expect(container).toMatchSnapshot();
});
test('is clickable', () => {
const onClick = jest.fn();
const { container, getByText } = render(
,
);
const anchor = getByText('Test');
fireEvent.click(anchor);
expect(container.firstChild).toMatchSnapshot();
expect(onClick).toBeCalled();
});
test('renders tag', () => {
const { container } = render(
,
);
expect(container).toMatchSnapshot();
});
test('weight renders', () => {
const { container } = render(
,
);
expect(container).toMatchSnapshot();
});
test('gap renders', () => {
const { container } = render(
} label="Theme Gap" href="#" />
} label="Small Gap" href="#" gap="small" />
} label="Medium Gap" href="#" gap="medium" />
} label="Large Gap" href="#" gap="large" />
} label="5px Gap" href="#" gap="5px" />
,
);
expect(container).toMatchSnapshot();
});
test('renders a11yTitle and aria-label', () => {
const { container, getByLabelText } = render(
,
);
expect(getByLabelText('test')).toBeTruthy();
expect(getByLabelText('test-2')).toBeTruthy();
expect(container).toMatchSnapshot();
});
test('renders size specific theming', () => {
const theme = {
anchor: {
color: 'text-strong',
textDecoration: 'underline',
fontWeight: 700,
size: {
large: {
color: 'brand',
textDecoration: 'none',
fontWeight: 500,
},
xlarge: {
color: 'brand',
textDecoration: 'none',
fontWeight: 500,
},
xxlarge: {
color: 'brand',
textDecoration: 'none',
fontWeight: 500,
},
},
},
};
const { asFragment } = render(
{['xsmall', 'small', 'medium', 'large', 'xlarge', 'xxlarge'].map(
(size) => (
Anchor should inherit from text.
),
)}
Anchor should inherit from Paragraph.
} />
}
size="large"
/>
,
);
expect(asFragment()).toMatchSnapshot();
});
test('matches icon size to size prop when theme.icon.matchSize is true', () => {
const theme = {
icon: {
matchSize: true,
},
};
const { asFragment } = render(
} />
} />
} />
} />
} />
,
);
expect(asFragment()).toMatchSnapshot();
});
});