From e9441729a6fd848aac6ae114e346e15f907493bc Mon Sep 17 00:00:00 2001
From: Paul Slaughter <pslaughter@gitlab.com>
Date: Wed, 21 Oct 2020 19:55:51 -0500
Subject: [PATCH] Fix sourcegraph not loading from asset host

- Adds unit test
---
 app/assets/javascripts/sourcegraph/index.js |  9 ++-
 spec/frontend/sourcegraph/index_spec.js     | 64 +++++++++++++++++++++
 2 files changed, 70 insertions(+), 3 deletions(-)
 create mode 100644 spec/frontend/sourcegraph/index_spec.js

diff --git a/app/assets/javascripts/sourcegraph/index.js b/app/assets/javascripts/sourcegraph/index.js
index 796e90bf08e2..27b63a217d32 100644
--- a/app/assets/javascripts/sourcegraph/index.js
+++ b/app/assets/javascripts/sourcegraph/index.js
@@ -1,3 +1,5 @@
+import { joinPaths } from '~/lib/utils/url_utility';
+
 function loadScript(path) {
   const script = document.createElement('script');
   script.type = 'application/javascript';
@@ -17,10 +19,11 @@ export default function initSourcegraph() {
     return;
   }
 
-  const assetsUrl = new URL('/assets/webpack/sourcegraph/', window.location.href);
-  const scriptPath = new URL('scripts/integration.bundle.js', assetsUrl).href;
+  const base = gon.asset_host || gon.gitlab_url;
+  const assetsUrl = joinPaths(base, '/assets/webpack/sourcegraph/');
+  const scriptPath = joinPaths(assetsUrl, 'scripts/integration.bundle.js');
 
-  window.SOURCEGRAPH_ASSETS_URL = assetsUrl.href;
+  window.SOURCEGRAPH_ASSETS_URL = assetsUrl;
   window.SOURCEGRAPH_URL = url;
   window.SOURCEGRAPH_INTEGRATION = 'gitlab-integration';
 
diff --git a/spec/frontend/sourcegraph/index_spec.js b/spec/frontend/sourcegraph/index_spec.js
new file mode 100644
index 000000000000..0eccdbd39422
--- /dev/null
+++ b/spec/frontend/sourcegraph/index_spec.js
@@ -0,0 +1,64 @@
+import initSourcegraph from '~/sourcegraph';
+
+const TEST_SOURCEGRAPH_URL = 'https://sourcegraph.test:9000';
+const TEST_GITLAB_URL = 'https://gitlab.example.com/test';
+const TEST_ASSET_HOST = 'https://gitlab-assets.example.com/';
+
+describe('~/sourcegraph/index', () => {
+  let origGon;
+
+  beforeEach(() => {
+    origGon = window.gon;
+    window.gon = {
+      sourcegraph: {},
+      gitlab_url: TEST_GITLAB_URL,
+    };
+  });
+
+  afterEach(() => {
+    document.head.innerHTML = '';
+    document.body.innerHTML = '';
+    window.gon = origGon;
+  });
+
+  const findScript = () => document.querySelector('script');
+
+  it('with no sourcegraph url, does nothing', () => {
+    initSourcegraph();
+
+    expect(findScript()).toBeNull();
+  });
+
+  describe.each`
+    assetHost          | assetsUrl                                           | scriptPath
+    ${null}            | ${`${TEST_GITLAB_URL}/assets/webpack/sourcegraph/`} | ${`${TEST_GITLAB_URL}/assets/webpack/sourcegraph/scripts/integration.bundle.js`}
+    ${TEST_ASSET_HOST} | ${`${TEST_ASSET_HOST}assets/webpack/sourcegraph/`}  | ${`${TEST_ASSET_HOST}assets/webpack/sourcegraph/scripts/integration.bundle.js`}
+  `('loads sourcegraph (assetHost=$assetHost)', ({ assetHost, assetsUrl, scriptPath }) => {
+    beforeEach(() => {
+      Object.assign(window.gon, {
+        sourcegraph: {
+          url: TEST_SOURCEGRAPH_URL,
+        },
+        asset_host: assetHost,
+      });
+
+      initSourcegraph();
+    });
+
+    it('should add sourcegraph config constants to window', () => {
+      expect(window).toMatchObject({
+        SOURCEGRAPH_ASSETS_URL: assetsUrl,
+        SOURCEGRAPH_URL: TEST_SOURCEGRAPH_URL,
+        SOURCEGRAPH_INTEGRATION: 'gitlab-integration',
+      });
+    });
+
+    it('should add script tag', () => {
+      expect(findScript()).toMatchObject({
+        src: scriptPath,
+        defer: true,
+        type: 'application/javascript',
+      });
+    });
+  });
+});
-- 
GitLab