From 91fe807305e89ecd9df3b6f4f221540fd451659e Mon Sep 17 00:00:00 2001
From: Grégoire Aubert
Date: Thu, 8 Feb 2018 17:43:32 +0100
Subject: SONAR-10347 Create a CodeSnippet component and use it in project
badges and onboarding tutorial
---
.../main/js/apps/overview/badges/BadgeSnippet.tsx | 34 -------
.../main/js/apps/overview/badges/BadgesModal.tsx | 4 +-
.../__snapshots__/BadgesModal-test.tsx.snap | 3 +-
.../src/main/js/apps/overview/badges/styles.css | 35 -------
.../apps/tutorials/onboarding/commands/ClangGCC.js | 6 +-
.../apps/tutorials/onboarding/commands/Command.js | 53 ----------
.../apps/tutorials/onboarding/commands/DotNet.js | 8 +-
.../tutorials/onboarding/commands/JavaGradle.js | 6 +-
.../tutorials/onboarding/commands/JavaMaven.js | 4 +-
.../js/apps/tutorials/onboarding/commands/Msvc.js | 8 +-
.../js/apps/tutorials/onboarding/commands/Other.js | 4 +-
.../onboarding/commands/__tests__/Command-test.js | 27 -----
.../__tests__/__snapshots__/ClangGCC-test.js.snap | 30 +++---
.../__tests__/__snapshots__/Command-test.js.snap | 39 --------
.../__tests__/__snapshots__/DotNet-test.js.snap | 32 +++---
.../__snapshots__/JavaGradle-test.js.snap | 16 +--
.../__tests__/__snapshots__/JavaMaven-test.js.snap | 8 +-
.../__tests__/__snapshots__/Msvc-test.js.snap | 32 +++---
.../__tests__/__snapshots__/Other-test.js.snap | 18 ++--
.../main/js/apps/tutorials/onboarding/styles.css | 42 --------
.../src/main/js/components/common/CodeSnippet.css | 59 +++++++++++
.../src/main/js/components/common/CodeSnippet.tsx | 47 +++++++++
.../common/__tests__/CodeSnippet-test.tsx | 32 ++++++
.../__snapshots__/CodeSnippet-test.tsx.snap | 111 +++++++++++++++++++++
24 files changed, 339 insertions(+), 319 deletions(-)
delete mode 100644 server/sonar-web/src/main/js/apps/overview/badges/BadgeSnippet.tsx
delete mode 100644 server/sonar-web/src/main/js/apps/tutorials/onboarding/commands/Command.js
delete mode 100644 server/sonar-web/src/main/js/apps/tutorials/onboarding/commands/__tests__/Command-test.js
delete mode 100644 server/sonar-web/src/main/js/apps/tutorials/onboarding/commands/__tests__/__snapshots__/Command-test.js.snap
create mode 100644 server/sonar-web/src/main/js/components/common/CodeSnippet.css
create mode 100644 server/sonar-web/src/main/js/components/common/CodeSnippet.tsx
create mode 100644 server/sonar-web/src/main/js/components/common/__tests__/CodeSnippet-test.tsx
create mode 100644 server/sonar-web/src/main/js/components/common/__tests__/__snapshots__/CodeSnippet-test.tsx.snap
diff --git a/server/sonar-web/src/main/js/apps/overview/badges/BadgeSnippet.tsx b/server/sonar-web/src/main/js/apps/overview/badges/BadgeSnippet.tsx
deleted file mode 100644
index 5deaf71cbbd..00000000000
--- a/server/sonar-web/src/main/js/apps/overview/badges/BadgeSnippet.tsx
+++ /dev/null
@@ -1,34 +0,0 @@
-/*
- * SonarQube
- * Copyright (C) 2009-2018 SonarSource SA
- * mailto:info AT sonarsource DOT com
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-import * as React from 'react';
-import ClipboardButton from '../../../components/controls/ClipboardButton';
-
-interface Props {
- snippet: string;
-}
-
-export default function BadgeSnippet({ snippet }: Props) {
- return (
-
- );
-}
diff --git a/server/sonar-web/src/main/js/apps/overview/badges/BadgesModal.tsx b/server/sonar-web/src/main/js/apps/overview/badges/BadgesModal.tsx
index fc98e4eb71a..3592c4daea3 100644
--- a/server/sonar-web/src/main/js/apps/overview/badges/BadgesModal.tsx
+++ b/server/sonar-web/src/main/js/apps/overview/badges/BadgesModal.tsx
@@ -19,10 +19,10 @@
*/
import * as React from 'react';
import BadgeButton from './BadgeButton';
-import BadgeSnippet from './BadgeSnippet';
import BadgeParams from './BadgeParams';
import { BadgeType, BadgeOptions, getBadgeUrl } from './utils';
import { Metric } from '../../../app/types';
+import CodeSnippet from '../../../components/common/CodeSnippet';
import Modal from '../../../components/controls/Modal';
import { translate } from '../../../helpers/l10n';
import './styles.css';
@@ -101,7 +101,7 @@ export default class BadgesModal extends React.PureComponent {
type={selectedType}
updateOptions={this.handleUpdateOptions}
/>
-
+
diff --git a/server/sonar-web/src/main/js/apps/overview/badges/__tests__/__snapshots__/BadgesModal-test.tsx.snap b/server/sonar-web/src/main/js/apps/overview/badges/__tests__/__snapshots__/BadgesModal-test.tsx.snap
index 8366543e432..00776343f26 100644
--- a/server/sonar-web/src/main/js/apps/overview/badges/__tests__/__snapshots__/BadgesModal-test.tsx.snap
+++ b/server/sonar-web/src/main/js/apps/overview/badges/__tests__/__snapshots__/BadgesModal-test.tsx.snap
@@ -75,7 +75,8 @@ exports[`should display the modal after click 2`] = `
type="measure"
updateOptions={[Function]}
/>
-
diff --git a/server/sonar-web/src/main/js/apps/overview/badges/styles.css b/server/sonar-web/src/main/js/apps/overview/badges/styles.css
index 3fe0529fe4c..f3866498378 100644
--- a/server/sonar-web/src/main/js/apps/overview/badges/styles.css
+++ b/server/sonar-web/src/main/js/apps/overview/badges/styles.css
@@ -48,38 +48,3 @@
background-color: var(--lightBlue);
border-color: var(--darkBlue);
}
-
-.badge-snippet {
- position: relative;
- margin: var(--gridSize) 0;
- background: var(--gray40);
- color: var(--snippetFontColor);
- border-radius: 3px;
-}
-
-.badge-snippet pre {
- padding: calc(2 * var(--gridSize));
- padding-bottom: 40px;
- overflow: auto;
-}
-
-.badge-snippet button {
- position: absolute;
- top: auto;
- top: 40px;
- right: calc(2 * var(--gridSize));
- height: var(--smallControlHeight);
- line-height: 18px;
- border: 1px solid #fff;
- color: #fff;
- font-size: 11px;
- font-weight: normal;
- user-select: none;
-}
-
-.badge-snippet button:hover,
-.badge-snippet button:focus,
-.badge-snippet button:active {
- background-color: #fff;
- color: var(--gray40);
-}
diff --git a/server/sonar-web/src/main/js/apps/tutorials/onboarding/commands/ClangGCC.js b/server/sonar-web/src/main/js/apps/tutorials/onboarding/commands/ClangGCC.js
index 249c8cc2976..26d3596d2e9 100644
--- a/server/sonar-web/src/main/js/apps/tutorials/onboarding/commands/ClangGCC.js
+++ b/server/sonar-web/src/main/js/apps/tutorials/onboarding/commands/ClangGCC.js
@@ -19,9 +19,9 @@
*/
// @flow
import React from 'react';
-import Command from './Command';
import SQScanner from './SQScanner';
import BuildWrapper from './BuildWrapper';
+import CodeSnippet from '../../../../components/common/CodeSnippet';
import { translate } from '../../../../helpers/l10n';
/*::
@@ -67,8 +67,8 @@ export default function ClangGCC(props /*: Props */) {
__html: translate('onboarding.analysis.sq_scanner.execute.text')
}}
/>
-
-
+
+
,
- isOneLine?: boolean
-};
-*/
-
-// keep this "useless" concatentation for the readability reason
-// eslint-disable-next-line no-useless-concat
-const s = ' \\' + '\n ';
-
-export default class Command extends React.PureComponent {
- /*:: props: Props; */
-
- render() {
- const { command, isOneLine } = this.props;
- const commandArray = Array.isArray(command) ? command.filter(line => line != null) : [command];
- const finalCommand = isOneLine ? commandArray.join(' ') : commandArray.join(s);
-
- return (
-
- );
- }
-}
diff --git a/server/sonar-web/src/main/js/apps/tutorials/onboarding/commands/DotNet.js b/server/sonar-web/src/main/js/apps/tutorials/onboarding/commands/DotNet.js
index 493eb4f690b..7e5fd4f24e0 100644
--- a/server/sonar-web/src/main/js/apps/tutorials/onboarding/commands/DotNet.js
+++ b/server/sonar-web/src/main/js/apps/tutorials/onboarding/commands/DotNet.js
@@ -19,8 +19,8 @@
*/
// @flow
import React from 'react';
-import Command from './Command';
import MSBuildScanner from './MSBuildScanner';
+import CodeSnippet from '../../../../components/common/CodeSnippet';
import { translate } from '../../../../helpers/l10n';
/*::
@@ -58,9 +58,9 @@ export default function DotNet(props /*: Props */) {
__html: translate('onboarding.analysis.msbuild.execute.text')
}}
/>
-
-
-
+
+
+
-
+
{translate('onboarding.analysis.java.gradle.text.2')}
-
+
{translate('onboarding.analysis.java.maven.header')}
{translate('onboarding.analysis.java.maven.text')}
-
+
-
-
-
+
+
+
-
+
{
- expect(mount( )).toMatchSnapshot();
-});
diff --git a/server/sonar-web/src/main/js/apps/tutorials/onboarding/commands/__tests__/__snapshots__/ClangGCC-test.js.snap b/server/sonar-web/src/main/js/apps/tutorials/onboarding/commands/__tests__/__snapshots__/ClangGCC-test.js.snap
index 42680367bee..4375df814f3 100644
--- a/server/sonar-web/src/main/js/apps/tutorials/onboarding/commands/__tests__/__snapshots__/ClangGCC-test.js.snap
+++ b/server/sonar-web/src/main/js/apps/tutorials/onboarding/commands/__tests__/__snapshots__/ClangGCC-test.js.snap
@@ -22,12 +22,13 @@ exports[`renders correctly 1`] = `
}
}
/>
-
-
-
-
-
-
-
-
- foo
-bar
-
-
-
-
- copy
-
-
-
-
-
-`;
diff --git a/server/sonar-web/src/main/js/apps/tutorials/onboarding/commands/__tests__/__snapshots__/DotNet-test.js.snap b/server/sonar-web/src/main/js/apps/tutorials/onboarding/commands/__tests__/__snapshots__/DotNet-test.js.snap
index 56d2150cae2..e21df31e564 100644
--- a/server/sonar-web/src/main/js/apps/tutorials/onboarding/commands/__tests__/__snapshots__/DotNet-test.js.snap
+++ b/server/sonar-web/src/main/js/apps/tutorials/onboarding/commands/__tests__/__snapshots__/DotNet-test.js.snap
@@ -16,8 +16,9 @@ exports[`renders correctly 1`] = `
}
}
/>
-
-
-
-
-
-
-
@@ -25,8 +25,8 @@ exports[`renders correctly 1`] = `
>
onboarding.analysis.java.gradle.text.2
-
-
@@ -79,8 +79,8 @@ exports[`renders correctly 2`] = `
>
onboarding.analysis.java.gradle.text.2
-
onboarding.analysis.java.maven.text
-
onboarding.analysis.java.maven.text
-
-
-
-
-
-
-
-
-
-
line !== undefined)
+ : [snippet];
+ const finalSnippet = isOneLine ? snippetArray.join(' ') : snippetArray.join(s);
+ return (
+
+
{finalSnippet}
+ {!noCopy &&
}
+
+ );
+}
diff --git a/server/sonar-web/src/main/js/components/common/__tests__/CodeSnippet-test.tsx b/server/sonar-web/src/main/js/components/common/__tests__/CodeSnippet-test.tsx
new file mode 100644
index 00000000000..8956e411a50
--- /dev/null
+++ b/server/sonar-web/src/main/js/components/common/__tests__/CodeSnippet-test.tsx
@@ -0,0 +1,32 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2018 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+import * as React from 'react';
+import { mount } from 'enzyme';
+import CodeSnippet from '../CodeSnippet';
+
+it('renders correctly', () => {
+ expect(mount( )).toMatchSnapshot();
+ expect(mount( )).toMatchSnapshot();
+});
+
+it('renders correctly with array snippet', () => {
+ expect(mount( )).toMatchSnapshot();
+ expect(mount( )).toMatchSnapshot();
+});
diff --git a/server/sonar-web/src/main/js/components/common/__tests__/__snapshots__/CodeSnippet-test.tsx.snap b/server/sonar-web/src/main/js/components/common/__tests__/__snapshots__/CodeSnippet-test.tsx.snap
new file mode 100644
index 00000000000..5e330d13abe
--- /dev/null
+++ b/server/sonar-web/src/main/js/components/common/__tests__/__snapshots__/CodeSnippet-test.tsx.snap
@@ -0,0 +1,111 @@
+// Jest Snapshot v1, https://goo.gl/fbAQLP
+
+exports[`renders correctly 1`] = `
+
+
+
+ foo
+bar
+
+
+
+ copy
+
+
+
+
+`;
+
+exports[`renders correctly 2`] = `
+
+
+
+`;
+
+exports[`renders correctly with array snippet 1`] = `
+
+
+
+ foo \\
+ bar
+
+
+
+ copy
+
+
+
+
+`;
+
+exports[`renders correctly with array snippet 2`] = `
+
+
+
+ foo bar
+
+
+
+ copy
+
+
+
+
+`;
--
cgit v1.2.3