Explorar o código

HUE-9274 [ui] Prevent real ajax requests from the jest tests

Johan Ahlen %!s(int64=5) %!d(string=hai) anos
pai
achega
ca0a5a5e9c

+ 8 - 0
desktop/core/src/desktop/js/jest/jest.init.js

@@ -22,6 +22,7 @@ import 'apps/notebook2/execution/sessionManager';
 import './jquery.setup';
 import './sqlTestUtils';
 
+import $ from 'jquery';
 import * as ko from 'knockout';
 import komapping from 'knockout.mapping';
 
@@ -100,3 +101,10 @@ Object.keys(globalVars).forEach(key => {
   global[key] = globalVars[key];
   global.window[key] = globalVars[key];
 });
+
+$.ajaxSetup({
+  beforeSend: function() {
+    console.warn('actual jQuery ajax called');
+    console.trace();
+  }
+});

+ 52 - 2
desktop/core/src/desktop/js/ko/components/contextPopover/__snapshots__/ko.quickQueryContext.test.js.snap

@@ -2,7 +2,57 @@
 
 exports[`ko.quickQueryContext.js should render component 1`] = `
 "<div data-bind=\\"descendantsComplete: descendantsComplete, component: { name: &quot;quick-query-context&quot;, params: params }\\"><div class=\\"context-popover-flex-fill\\" style=\\"overflow: auto;\\">
-  <!-- ko hueSpinner: { spin: loadingConfig, center: true, size: 'xlarge' } --><!-- /ko -->
-  <!-- ko ifnot: loadingConfig --><!-- /ko -->
+  <div style=\\"display: inline-block\\" data-bind=\\"
+    component: {
+      name: 'hue-drop-down',
+      params: {
+        value: interpreter,
+        labelAttribute: 'displayName',
+        entries: availableInterpreters,
+        linkTitle: 'Active connector'
+      }
+    }
+  \\"><a class=\\"inactive-action hue-drop-down-active\\" href=\\"javascript:void(0)\\" data-bind=\\"toggle: dropDownVisible, css: { 'blue': dropDownVisible }\\">
+    <!-- ko if: icon --><!-- /ko -->
+    <!-- ko if: !noLabel && value -->
+    <span class=\\"hue-drop-down-selected\\" data-bind=\\"text: value() &amp;&amp; typeof value()[labelAttribute] !== 'undefined' ? value()[labelAttribute] : value(), visible: ! dropDownVisible() || !searchable, attr: { 'title': value() &amp;&amp; typeof value()[labelAttribute] !== 'undefined' ? value()[labelAttribute] : value() }\\" title=\\"[object Object]\\">[object Object]</span>
+    <!-- /ko -->
+    <i class=\\"fa fa-caret-down\\"></i>
+  </a><input class=\\"hue-drop-down-input\\" type=\\"text\\" data-bind=\\"textInput: filter, attr: { 'placeHolder': inputPlaceHolder }, visible: dropDownVisible, style: { color: filterEdited() ? '#000' : '#AAA', 'min-height': '22px', 'margin-left': '10px' }\\" style=\\"display: none; color: rgb(170, 170, 170); min-height: 22px; margin-left: 10px;\\"><i class=\\"fa fa-caret-down\\"></i><div class=\\"hue-drop-down-container\\" data-bind=\\"css: { 'open' : dropDownVisible, 'hue-drop-down-fixed': fixedPosition, 'hue-drop-down-container-searchable': searchable }, dropDownKeyUp: { onEsc: onEsc, onEnter: onEnter, dropDownVisible: dropDownVisible }\\">
+    <div style=\\"overflow-y: auto;\\" class=\\"dropdown-menu\\" data-bind=\\"visible: filteredEntries().length > 0\\">
+      <!-- ko if: foreachVisible --><!-- /ko -->
+      <!-- ko ifnot: foreachVisible -->
+      <ul class=\\"hue-inner-drop-down\\" data-bind=\\"foreach: filteredEntries\\">
+        <!-- ko if: typeof $data.divider !== 'undefined' && $data.divider --><!-- /ko -->
+        <!-- ko if: typeof $data.divider === 'undefined' || !$data.divider -->
+        <li><a href=\\"javascript:void(0)\\" data-bind=\\"text: $data &amp;&amp; typeof $data[$parent.labelAttribute] !== 'undefined' ? $data[$parent.labelAttribute] : $data, click: function () { let previous = $parent.value(); $parent.value($data); $parent.onSelect($data, previous); }\\">[object Object]</a></li>
+        <!-- /ko -->
+      </ul>
+      <!-- /ko -->
+    </div>
+  </div></div>
+  <!-- ko if: interpreter() -->
+    <div class=\\"margin-left-10\\" style=\\"display: inline-block\\" data-bind=\\"
+      component: {
+        name: 'hue-context-selector',
+        params: {
+          sourceType: interpreter().type,
+          compute: compute,
+          namespace: namespace,
+          availableDatabases: availableDatabases,
+          database: database,
+          hideLabels: true
+        }
+      }
+    \\"><i class=\\"fa fa-spinner fa-spin muted\\"></i><div class=\\"inline-block\\" style=\\"display: none;\\" data-bind=\\"visible: !loadingContext()\\">
+    <!-- ko if: window.HAS_MULTI_CLUSTER --><!-- /ko -->
+
+    <!-- ko if: availableDatabases().length > 0 && !hideDatabases--><!-- /ko -->
+    <!-- ko if: availableDatabases().length === 0  && !hideDatabases -->
+    <span class=\\"editor-header-title\\"><i class=\\"fa fa-warning\\"></i> No databases found</span>
+    <!-- /ko -->
+  </div></div>
+  <!-- /ko -->
+  <!-- ko ifnot: loadingContext --><!-- /ko -->
 </div></div>"
 `;

+ 21 - 0
desktop/core/src/desktop/js/ko/components/contextPopover/ko.quickQueryContext.test.js

@@ -16,15 +16,36 @@
 
 import { koSetup } from 'jest/koTestUtils';
 import { NAME } from './ko.quickQueryContext';
+import * as hueConfig from 'utils/hueConfig';
+import apiHelper from 'api/apiHelper';
+
+import $ from 'jquery';
 
 describe('ko.quickQueryContext.js', () => {
   const setup = koSetup();
 
   it('should render component', async () => {
+    const connectorsSpy = jest
+      .spyOn(hueConfig, 'filterConnectors')
+      .mockImplementation(() => [{ type: 'impala' }]);
+    const computeSpy = jest
+      .spyOn(apiHelper, 'fetchContextComputes')
+      .mockImplementation(() => $.Deferred().resolve({}));
+    const fetchContextNamespaces = jest
+      .spyOn(apiHelper, 'fetchContextNamespaces')
+      .mockImplementation(() => $.Deferred().resolve({}));
+
     const element = await setup.renderComponent(NAME, {});
 
+    expect(connectorsSpy).toHaveBeenCalled();
+    expect(computeSpy).toHaveBeenCalled();
+    expect(fetchContextNamespaces).toHaveBeenCalled();
+
     expect(
       element.innerHTML.replace(/[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}/gi, 'uuid-uuid')
     ).toMatchSnapshot();
+
+    connectorsSpy.mockRestore();
+    connectorsSpy.mockClear();
   });
 });

+ 8 - 1
desktop/core/src/desktop/js/ko/components/ko.historyPanel.test.js

@@ -14,6 +14,7 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
+import $ from 'jquery';
 import { koSetup } from 'jest/koTestUtils';
 import { NAME } from './ko.historyPanel';
 
@@ -21,8 +22,14 @@ describe('ko.historyPanel.js', () => {
   const setup = koSetup();
 
   it('should render component', async () => {
-    const element = await setup.renderComponent(NAME, {});
+    const spy = jest.spyOn($, 'post').mockImplementation(() => $.Deferred().reject());
 
+    const element = await setup.renderComponent(NAME, {});
     expect(element.innerHTML).toMatchSnapshot();
+
+    expect(spy).toHaveBeenCalled();
+
+    spy.mockRestore();
+    spy.mockClear();
   });
 });

+ 6 - 0
desktop/core/src/desktop/js/ko/components/ko.sentryPrivileges.test.js

@@ -14,6 +14,7 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
+import $ from 'jquery';
 import { koSetup } from 'jest/koTestUtils';
 import { NAME } from './ko.sentryPrivileges';
 
@@ -21,10 +22,15 @@ describe('ko.sentryPrivileges.js', () => {
   const setup = koSetup();
 
   it('should render component', async () => {
+    const ajaxSpy = jest
+      .spyOn($, 'ajax')
+      .mockImplementation(() => $.Deferred().reject({ error: 'Not implemented' }));
+
     const element = await setup.renderComponent(NAME, {
       path: 'foo'
     });
 
+    expect(ajaxSpy).toHaveBeenCalled();
     expect(element.innerHTML).toMatchSnapshot();
   });
 });