From 4f5f2cbf2a54c6c0664c882962dcee26de00a469 Mon Sep 17 00:00:00 2001 From: Wouter Admiraal Date: Fri, 27 Nov 2020 11:48:41 +0100 Subject: [PATCH] SONAR-14181 Fix SSF-136 --- .../projectInformation/meta/MetaLink.tsx | 5 +- .../meta/__tests__/MetaLink-test.tsx | 72 +++++++++---------- .../__snapshots__/MetaLink-test.tsx.snap | 48 ++----------- 3 files changed, 43 insertions(+), 82 deletions(-) diff --git a/server/sonar-web/src/main/js/app/components/nav/component/projectInformation/meta/MetaLink.tsx b/server/sonar-web/src/main/js/app/components/nav/component/projectInformation/meta/MetaLink.tsx index 64c8a30d246..352c1274917 100644 --- a/server/sonar-web/src/main/js/app/components/nav/component/projectInformation/meta/MetaLink.tsx +++ b/server/sonar-web/src/main/js/app/components/nav/component/projectInformation/meta/MetaLink.tsx @@ -51,12 +51,13 @@ export default class MetaLink extends React.PureComponent { render() { const { iconOnly, link } = this.props; const linkTitle = getLinkName(link); + const isValid = isValidUri(link.url); return (
  • diff --git a/server/sonar-web/src/main/js/app/components/nav/component/projectInformation/meta/__tests__/MetaLink-test.tsx b/server/sonar-web/src/main/js/app/components/nav/component/projectInformation/meta/__tests__/MetaLink-test.tsx index 3af9088b62b..b1a971bc4f6 100644 --- a/server/sonar-web/src/main/js/app/components/nav/component/projectInformation/meta/__tests__/MetaLink-test.tsx +++ b/server/sonar-web/src/main/js/app/components/nav/component/projectInformation/meta/__tests__/MetaLink-test.tsx @@ -19,54 +19,54 @@ */ 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()).toMatchSnapshot(); - expect(shallow()).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()).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(); - 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 = {}) { + return shallow( + + ); +} diff --git a/server/sonar-web/src/main/js/app/components/nav/component/projectInformation/meta/__tests__/__snapshots__/MetaLink-test.tsx.snap b/server/sonar-web/src/main/js/app/components/nav/component/projectInformation/meta/__tests__/__snapshots__/MetaLink-test.tsx.snap index 773b22685c1..31c9deade9b 100644 --- a/server/sonar-web/src/main/js/app/components/nav/component/projectInformation/meta/__tests__/__snapshots__/MetaLink-test.tsx.snap +++ b/server/sonar-web/src/main/js/app/components/nav/component/projectInformation/meta/__tests__/__snapshots__/MetaLink-test.tsx.snap @@ -1,10 +1,9 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`should expand and collapse dangerous link 1`] = ` +exports[`should match snapshot: dangerous link, collapsed 1`] = `
  • `; -exports[`should expand and collapse dangerous link 2`] = ` +exports[`should match snapshot: dangerous link, expanded 1`] = `
  • `; -exports[`should expand and collapse dangerous link 3`] = ` -
  • - - - Dangerous - -
  • -`; - -exports[`should match snapshot 1`] = ` +exports[`should match snapshot: default 1`] = `
  • `; -exports[`should match snapshot 2`] = ` +exports[`should match snapshot: icon only 1`] = `
  • `; - -exports[`should render dangerous links as plaintext 1`] = ` -
  • - - - Dangerous - -
  • -`; -- 2.39.5