*/
import { shallow } from 'enzyme';
import * as React from 'react';
+import { ClearButton } from 'sonar-ui-common/components/controls/buttons';
import { click } from 'sonar-ui-common/helpers/testUtils';
import MetaLink from '../MetaLink';
-it('should match snapshot', () => {
- const link = {
- id: '1',
- name: 'Foo',
- url: 'http://example.com',
- type: 'foo'
- };
-
- expect(shallow(<MetaLink link={link} />)).toMatchSnapshot();
- expect(shallow(<MetaLink iconOnly={true} link={link} />)).toMatchSnapshot();
-});
-
-it('should render dangerous links as plaintext', () => {
- const link = {
- id: '1',
- name: 'Dangerous',
- url: 'javascript:alert("hi")',
- type: 'dangerous'
- };
+const DANGEROUS_LINK = {
+ id: '1',
+ name: 'Dangerous',
+ url: 'javascript:alert("hi")',
+ type: 'dangerous'
+};
- expect(shallow(<MetaLink link={link} />)).toMatchSnapshot();
+it('should match snapshot', () => {
+ expect(shallowRender()).toMatchSnapshot('default');
+ expect(shallowRender({ iconOnly: true })).toMatchSnapshot('icon only');
+ const wrapper = shallowRender({ link: DANGEROUS_LINK });
+ expect(wrapper).toMatchSnapshot('dangerous link, collapsed');
+ wrapper.setState({ expanded: true });
+ expect(wrapper).toMatchSnapshot('dangerous link, expanded');
});
-it('should expand and collapse dangerous link', () => {
- const link = {
- id: '1',
- name: 'Dangerous',
- url: 'javascript:alert("hi")',
- type: 'dangerous'
- };
-
- const wrapper = shallow(<MetaLink link={link} />);
- expect(wrapper).toMatchSnapshot();
+it('should expand and collapse dangerous links', () => {
+ const wrapper = shallowRender({ link: DANGEROUS_LINK });
+ expect(wrapper.state().expanded).toBe(false);
// expand
click(wrapper.find('a'));
- expect(wrapper).toMatchSnapshot();
+ expect(wrapper.state().expanded).toBe(true);
// collapse
click(wrapper.find('a'));
- expect(wrapper).toMatchSnapshot();
+ expect(wrapper.state().expanded).toBe(false);
// collapse with button
- click(wrapper.find('a'));
- expect(wrapper.state('expanded')).toBe(true);
- click(wrapper.find('ClearButton'));
- expect(wrapper.state('expanded')).toBe(false);
+ wrapper.setState({ expanded: true });
+ click(wrapper.find(ClearButton));
+ expect(wrapper.state().expanded).toBe(false);
});
+
+function shallowRender(props: Partial<MetaLink['props']> = {}) {
+ return shallow<MetaLink>(
+ <MetaLink
+ link={{
+ id: '1',
+ name: 'Foo',
+ url: 'http://example.com',
+ type: 'foo'
+ }}
+ {...props}
+ />
+ );
+}
// Jest Snapshot v1, https://goo.gl/fbAQLP
-exports[`should expand and collapse dangerous link 1`] = `
+exports[`should match snapshot: dangerous link, collapsed 1`] = `
<li>
<a
className="link-with-icon"
- href="javascript:alert(\\"hi\\")"
onClick={[Function]}
rel="nofollow noreferrer noopener"
target="_blank"
</li>
`;
-exports[`should expand and collapse dangerous link 2`] = `
+exports[`should match snapshot: dangerous link, expanded 1`] = `
<li>
<a
className="link-with-icon"
- href="javascript:alert(\\"hi\\")"
onClick={[Function]}
rel="nofollow noreferrer noopener"
target="_blank"
</li>
`;
-exports[`should expand and collapse dangerous link 3`] = `
-<li>
- <a
- className="link-with-icon"
- href="javascript:alert(\\"hi\\")"
- onClick={[Function]}
- rel="nofollow noreferrer noopener"
- target="_blank"
- title="Dangerous"
- >
- <ProjectLinkIcon
- className="little-spacer-right"
- type="dangerous"
- />
- Dangerous
- </a>
-</li>
-`;
-
-exports[`should match snapshot 1`] = `
+exports[`should match snapshot: default 1`] = `
<li>
<a
className="link-with-icon"
</li>
`;
-exports[`should match snapshot 2`] = `
+exports[`should match snapshot: icon only 1`] = `
<li>
<a
className="link-with-icon"
</a>
</li>
`;
-
-exports[`should render dangerous links as plaintext 1`] = `
-<li>
- <a
- className="link-with-icon"
- href="javascript:alert(\\"hi\\")"
- onClick={[Function]}
- rel="nofollow noreferrer noopener"
- target="_blank"
- title="Dangerous"
- >
- <ProjectLinkIcon
- className="little-spacer-right"
- type="dangerous"
- />
- Dangerous
- </a>
-</li>
-`;