Repository: test-results-reporter/reporter (js)

Issue:
Enhance quick chart test summary extension
- **Bug**: centre text doesn't include the `%` symbol.
- **Support** - Custom url (defaults to https://quickchart.io)
- **Rename** - _quick-charts-test-summary_ to _quick-chart-test-summary_

Pull request description:
Enhance quickchart extension
Hey @ASaiAnudeep,

This PR Fixes #24 

Change Summary:

* Fixed bug : result percentage not being shown in the quick chart url
* Updated tests for tests and mocks for bug fix and enhancement
* Added QUICK_CHART_URL constant (value: https://quickchart.io)
* Quick chart test extension to support custom url (pass the value in the config to the plugin)
* Renamed all references from `quick-charts-test-summary` to `quick-chart-test-summary`

Patch:
diff --git a/src/extensions/index.js b/src/extensions/index.js
index b0d1a99..9ab19a9 100644
--- a/src/extensions/index.js
+++ b/src/extensions/index.js
@@ -2,7 +2,7 @@ const hyperlinks = require('./hyperlinks');
 const mentions = require('./mentions');
 const rp_analysis = require('./report-portal-analysis');
 const rp_history = require('./report-portal-history');
-const qc_test_summary = require('./quick-charts-test-summary');
+const qc_test_summary = require('./quick-chart-test-summary');
 
 async function run(options) {
   const { target, result, hook } = options;
@@ -30,7 +30,7 @@ function getExtensionRunner(extension) {
       return rp_analysis;
     case 'report-portal-history':
       return rp_history;
-    case 'quick-charts-test-summary':
+    case 'quick-chart-test-summary':
       return qc_test_summary;
     default:
       return require(extension.name);
diff --git a/src/lib/constants.js b/src/lib/constants.js
new file mode 100644
index 0000000..f061557
--- /dev/null
+++ b/src/lib/constants.js
@@ -0,0 +1,3 @@
+module.exports = Object.freeze({
+  QUICK_CHART_URL: 'https://quickchart.io'
+})
\ No newline at end of file


Test patch:
diff --git a/src/extensions/quick-charts-test-summary.js b/src/extensions/quick-chart-test-summary.js
similarity index 67%
rename from src/extensions/quick-charts-test-summary.js
rename to src/extensions/quick-chart-test-summary.js
index 36c451b..be93af9 100644
--- a/src/extensions/quick-charts-test-summary.js
+++ b/src/extensions/quick-chart-test-summary.js
@@ -1,6 +1,7 @@
 const { getPercentage } = require('../helpers/helper');
+const constants = require('../lib/constants');
 
-function getUrl(result) {
+function getUrl(extension, result) {
   const percentage = getPercentage(result.passed, result.total);
   const chart = {
     type: 'radialGauge',
@@ -16,14 +17,14 @@ function getUrl(result) {
       centerPercentage: 80,
       centerArea: {
         fontSize: 80,
-        text: (val) => val + '%',
+        text: `${percentage}%`,
       },
     }
   }
-  return `https://quickchart.io/chart?c=${encodeURIComponent(JSON.stringify(chart))}`;
+  return `${extension.inputs.url}/chart?c=${encodeURIComponent(JSON.stringify(chart))}`;
 }
 
-function attachForTeams({ result, payload }) {
+function attachForTeams({ extension, result, payload }) {
   const main_column = {
     "type": "Column",
     "items": [payload.body[0], payload.body[1]],
@@ -34,7 +35,7 @@ function attachForTeams({ result, payload }) {
     "items": [
       {
         "type": "Image",
-        "url": getUrl(result),
+        "url": getUrl(extension, result),
         "altText": "overall-results-summary",
         "size": "large"
       }
@@ -51,16 +52,18 @@ function attachForTeams({ result, payload }) {
   payload.body = [column_set];
 }
 
-function attachForSlack({ result, payload }) {
+function attachForSlack({ extension, result, payload }) {
   payload.blocks[0]["accessory"] = {
     "type": "image",
     "alt_text": "overall-results-summary",
-    "image_url": getUrl(result)
+    "image_url": getUrl(extension, result)
   }
 }
 
 function run(params) {
-  const { target } = params;
+  const { extension, target } = params;  
+  params.extension.inputs = extension.inputs || {};
+  params.extension.inputs["url"] = (extension.inputs.url && extension.inputs.url.trim()) || constants.QUICK_CHART_URL;
   if (target.name === 'teams') {
     attachForTeams(params);
   } else {
diff --git a/test/ext.quick-chart-test-summary.spec.js b/test/ext.quick-chart-test-summary.spec.js
new file mode 100644
index 0000000..0a86fb0
--- /dev/null
+++ b/test/ext.quick-chart-test-summary.spec.js
@@ -0,0 +1,191 @@
+const { mock } = require('pactum');
+const assert = require('assert');
+const { publish } = require('../src');
+
+describe('extensions - quick-chart-test-summary', () => {
+
+  it('should send test-summary with links to teams', async () => {
+    const id = mock.addInteraction('post test-summary to teams with qc-test-summary', {quickChartUrl: "https://quickchart.io"});
+    await publish({
+      config: {
+        "reports": [
+          {
+            "targets": [
+              {
+                "name": "teams",
+                "inputs": {
+                  "url": "http://localhost:9393/message"
+                },
+                "extensions": [
+                  {
+                    "name": "quick-chart-test-summary"
+                  }
+                ]
+              }
+            ],
+            "results": [
+              {
+                "type": "testng",
+                "files": [
+                  "test/data/testng/single-suite-failures.xml"
+                ]
+              }
+            ]
+          }
+        ]
+      }
+    });
+    assert.equal(mock.getInteraction(id).exercised, true);
+  });
+
+  it('should send test-summary with links to slack', async () => {
+    const id = mock.addInteraction('post test-summary to slack with qc-test-summary', {quickChartUrl: "https://quickchart.io"});
+    await publish({
+      config: {
+        "reports": [
+          {
+            "targets": [
+              {
+                "name": "slack",
+                "inputs": {
+                  "url": "http://localhost:9393/message"
+                },
+                "extensions": [
+                  {
+                    "name": "quick-chart-test-summary"
+                  }
+                ]
+              }
+            ],
+            "results": [
+              {
+                "type": "testng",
+                "files": [
+                  "test/data/testng/single-suite-failures.xml"
+                ]
+              }
+            ]
+          }
+        ]
+      }
+    });
+    assert.equal(mock.getInteraction(id).exercised, true);
+  });
+
+  it('should send test-summary with links to teams with custom qc-test server', async () => {
+    const id = mock.addInteraction('post test-summary to teams with qc-test-summary', {quickChartUrl: "https://demo.quickchart.example.com"});
+    await publish({
+      config: {
+        "reports": [
+          {
+            "targets": [
+              {
+                "name": "teams",
+                "inputs": {
+                  "url": "http://localhost:9393/message"
+                },
+                "extensions": [
+                  {
+                    "name": "quick-chart-test-summary",
+                    "inputs": {
+                      "url": "https://demo.quickchart.example.com"
+                    }
+                  }
+                ]
+              }
+            ],
+            "results": [
+              {
+                "type": "testng",
+                "files": [
+                  "test/data/testng/single-suite-failures.xml"
+                ]
+              }
+            ]
+          }
+        ]
+      }
+    });
+    assert.equal(mock.getInteraction(id).exercised, true);
+  });
+
+  it('should send test-summary with links to slack with custom qc-test server', async () => {
+    const id = mock.addInteraction('post test-summary to slack with qc-test-summary', {quickChartUrl: "https://demo.quickchart.example.com"});
+    await publish({
+      config: {
+        "reports": [
+          {
+            "targets": [
+              {
+                "name": "slack",
+                "inputs": {
+                  "url": "http://localhost:9393/message"
+                },
+                "extensions": [
+                  {
+                    "name": "quick-chart-test-summary",
+                    "inputs": {
+                      "url": "https://demo.quickchart.example.com"
+                    }
+                  }
+                ]
+              }
+            ],
+            "results": [
+              {
+                "type": "testng",
+                "files": [
+                  "test/data/testng/single-suite-failures.xml"
+                ]
+              }
+            ]
+          }
+        ]
+      }
+    });
+    assert.equal(mock.getInteraction(id).exercised, true);
+  });
+
+  it('should send test-summary with links to teams and default qc-test server if url empty', async () => {
+    const id = mock.addInteraction('post test-summary to teams with qc-test-summary', {quickChartUrl: "https://quickchart.io"});
+    await publish({
+      config: {
+        "reports": [
+          {
+            "targets": [
+              {
+                "name": "teams",
+                "inputs": {
+                  "url": "http://localhost:9393/message"
+                },
+                "extensions": [
+                  {
+                    "name": "quick-chart-test-summary",
+                    "inputs": {
+                      "url": " "
+                    }
+                  }
+                ]
+              }
+            ],
+            "results": [
+              {
+                "type": "testng",
+                "files": [
+                  "test/data/testng/single-suite-failures.xml"
+                ]
+              }
+            ]
+          }
+        ]
+      }
+    });
+    assert.equal(mock.getInteraction(id).exercised, true);
+  });
+
+
+  afterEach(() => {
+    mock.clearInteractions();
+  });
+
+});
\ No newline at end of file
diff --git a/test/ext.quick-charts-test-summary.spec.js b/test/ext.quick-charts-test-summary.spec.js
deleted file mode 100644
index 7ce735e..0000000
--- a/test/ext.quick-charts-test-summary.spec.js
+++ /dev/null
@@ -1,79 +0,0 @@
-const { mock } = require('pactum');
-const assert = require('assert');
-const { publish } = require('../src');
-
-describe('extensions - quick-charts-test-summary', () => {
-
-  it('should send test-summary with links to teams', async () => {
-    const id = mock.addInteraction('post test-summary to teams with qc-test-summary');
-    await publish({
-      config: {
-        "reports": [
-          {
-            "targets": [
-              {
-                "name": "teams",
-                "inputs": {
-                  "url": "http://localhost:9393/message"
-                },
-                "extensions": [
-                  {
-                    "name": "quick-charts-test-summary"
-                  }
-                ]
-              }
-            ],
-            "results": [
-              {
-                "type": "testng",
-                "files": [
-                  "test/data/testng/single-suite-failures.xml"
-                ]
-              }
-            ]
-          }
-        ]
-      }
-    });
-    assert.equal(mock.getInteraction(id).exercised, true);
-  });
-
-  it('should send test-summary with links to slack', async () => {
-    const id = mock.addInteraction('post test-summary to slack with qc-test-summary');
-    await publish({
-      config: {
-        "reports": [
-          {
-            "targets": [
-              {
-                "name": "slack",
-                "inputs": {
-                  "url": "http://localhost:9393/message"
-                },
-                "extensions": [
-                  {
-                    "name": "quick-charts-test-summary"
-                  }
-                ]
-              }
-            ],
-            "results": [
-              {
-                "type": "testng",
-                "files": [
-                  "test/data/testng/single-suite-failures.xml"
-                ]
-              }
-            ]
-          }
-        ]
-      }
-    });
-    assert.equal(mock.getInteraction(id).exercised, true);
-  });
-
-  afterEach(() => {
-    mock.clearInteractions();
-  });
-
-});
\ No newline at end of file
diff --git a/test/mocks/slack.mock.js b/test/mocks/slack.mock.js
index 964c2b4..e7513b9 100644
--- a/test/mocks/slack.mock.js
+++ b/test/mocks/slack.mock.js
@@ -296,7 +296,7 @@ addInteractionHandler('post test-summary with mentions to slack', () => {
   }
 });
 
-addInteractionHandler('post test-summary to slack with qc-test-summary', () => {
+addInteractionHandler('post test-summary to slack with qc-test-summary', (ctx) => {
   return {
     request: {
       method: 'POST',
@@ -314,7 +314,7 @@ addInteractionHandler('post test-summary to slack with qc-test-summary', () => {
                 },
                 "accessory": {
                   "type": "image",
-                  "image_url": "https://quickchart.io/chart?c=%7B%22type%22%3A%22radialGauge%22%2C%22data%22%3A%7B%22datasets%22%3A%5B%7B%22data%22%3A%5B75%5D%2C%22backgroundColor%22%3A%22green%22%7D%5D%7D%2C%22options%22%3A%7B%22trackColor%22%3A%22%23FF0000%22%2C%22roundedCorners%22%3Afalse%2C%22centerPercentage%22%3A80%2C%22centerArea%22%3A%7B%22fontSize%22%3A80%7D%7D%7D",
+                  "image_url": `${ctx.data.quickChartUrl}/chart?c=%7B%22type%22%3A%22radialGauge%22%2C%22data%22%3A%7B%22datasets%22%3A%5B%7B%22data%22%3A%5B75%5D%2C%22backgroundColor%22%3A%22green%22%7D%5D%7D%2C%22options%22%3A%7B%22trackColor%22%3A%22%23FF0000%22%2C%22roundedCorners%22%3Afalse%2C%22centerPercentage%22%3A80%2C%22centerArea%22%3A%7B%22fontSize%22%3A80%2C%22text%22%3A%2275%25%22%7D%7D%7D`,
                   "alt_text": "overall-results-summary"
                 }
               }
diff --git a/test/mocks/teams.mock.js b/test/mocks/teams.mock.js
index 2289e86..0ab91a4 100644
--- a/test/mocks/teams.mock.js
+++ b/test/mocks/teams.mock.js
@@ -584,7 +584,7 @@ addInteractionHandler('post test-summary to teams with mentions', () => {
   }
 });
 
-addInteractionHandler('post test-summary to teams with qc-test-summary', () => {
+addInteractionHandler('post test-summary to teams with qc-test-summary', (ctx) => {
   return {
     request: {
       method: 'POST',
@@ -632,7 +632,7 @@ addInteractionHandler('post test-summary to teams with qc-test-summary', () => {
                       "items": [
                         {
                           "type": "Image",
-                          "url": "https://quickchart.io/chart?c=%7B%22type%22%3A%22radialGauge%22%2C%22data%22%3A%7B%22datasets%22%3A%5B%7B%22data%22%3A%5B75%5D%2C%22backgroundColor%22%3A%22green%22%7D%5D%7D%2C%22options%22%3A%7B%22trackColor%22%3A%22%23FF0000%22%2C%22roundedCorners%22%3Afalse%2C%22centerPercentage%22%3A80%2C%22centerArea%22%3A%7B%22fontSize%22%3A80%7D%7D%7D",
+                          "url": `${ctx.data.quickChartUrl}/chart?c=%7B%22type%22%3A%22radialGauge%22%2C%22data%22%3A%7B%22datasets%22%3A%5B%7B%22data%22%3A%5B75%5D%2C%22backgroundColor%22%3A%22green%22%7D%5D%7D%2C%22options%22%3A%7B%22trackColor%22%3A%22%23FF0000%22%2C%22roundedCorners%22%3Afalse%2C%22centerPercentage%22%3A80%2C%22centerArea%22%3A%7B%22fontSize%22%3A80%2C%22text%22%3A%2275%25%22%7D%7D%7D`,
                           "altText": "overall-results-summary",
                           "size": "large"
                         }