Parcourir la source

[frontend] Fix jest warning when an actual API call happens from unit tests

This also takes care of some type issues in our api utils.
Johan Åhlén il y a 1 an
Parent
commit
30a1693b35

+ 5 - 2
desktop/core/src/desktop/js/api/utils.ts

@@ -16,9 +16,10 @@
 
 import axios, {
   AxiosError,
+  AxiosInstance,
   AxiosRequestConfig,
   AxiosResponse,
-  AxiosTransformer,
+  AxiosResponseTransformer,
   CancelToken
 } from 'axios';
 import qs from 'qs';
@@ -42,7 +43,7 @@ export interface DefaultApiResponse {
 export interface ApiFetchOptions<T, E = string> extends AxiosRequestConfig {
   silenceErrors?: boolean;
   ignoreSuccessErrors?: boolean;
-  transformResponse?: AxiosTransformer;
+  transformResponse?: AxiosResponseTransformer;
   qsEncodeData?: boolean;
   handleSuccess?: (
     response: T & DefaultApiResponse,
@@ -78,6 +79,8 @@ axiosInstance.interceptors.response.use(response => {
   return response;
 });
 
+export const getAxiosInstance = (): AxiosInstance => axiosInstance;
+
 export const setBaseUrl = (newBaseUrl: string): void => {
   baseUrl = newBaseUrl;
 };

+ 2 - 3
desktop/core/src/desktop/js/apps/editor/execution/api.ts

@@ -14,6 +14,7 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
+import { AxiosResponseTransformer } from 'axios';
 import KnockoutObservable from '@types/knockout';
 
 import {
@@ -423,9 +424,7 @@ export const fetchResults = async (options: {
   data.rows = options.rows;
   data.startOver = options.startOver;
 
-  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
-  // @ts-ignore
-  const transformResponse = (response: unknown) => JSON.bigdataParse(response);
+  const transformResponse: AxiosResponseTransformer = response => JSON.bigdataParse(response);
 
   const response = await post<DefaultApiResponse & { result?: ResultApiResponse }>(
     FETCH_RESULT_DATA_API,

+ 3 - 7
desktop/core/src/desktop/js/catalog/api.ts

@@ -14,6 +14,8 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
+import { AxiosResponseTransformer } from 'axios';
+
 import { Tags } from './GeneralDataCatalog';
 import { Cancellable, CancellablePromise } from 'api/cancellablePromise';
 import {
@@ -506,13 +508,7 @@ export const fetchSample = ({
           queryResult.result.handle.has_result_set = resultStatus.has_result_set;
         }
         snippetJson = JSON.stringify(queryResult);
-        // eslint-disable-next-line @typescript-eslint/ban-ts-comment
-        // @ts-ignore
-        const transformResponse = (response: unknown) => {
-          // eslint-disable-next-line @typescript-eslint/ban-ts-comment
-          // @ts-ignore
-          return JSON.bigdataParse(response);
-        };
+        const transformResponse: AxiosResponseTransformer = response => JSON.bigdataParse(response);
         const resultPromise = post<SampleResponse>(
           FETCH_RESULT_DATA_URL,
           {

+ 6 - 2
desktop/core/src/desktop/js/jest/jest.init.js

@@ -27,6 +27,7 @@ import axios from 'axios';
 import $ from 'jquery';
 import * as ko from 'knockout';
 import komapping from 'knockout.mapping';
+import { getAxiosInstance } from 'api/utils';
 
 ko.mapping = komapping;
 
@@ -111,11 +112,14 @@ $.ajaxSetup({
   }
 });
 
-axios.interceptors.request.use(config => {
+const axiosConfigInterceptor = config => {
   console.warn('Actual axios ajax request made to url: ' + config.url);
   console.trace();
   return config;
-});
+};
+
+axios.interceptors.request.use(axiosConfigInterceptor);
+getAxiosInstance().interceptors.request.use(axiosConfigInterceptor);
 
 process.on('unhandledRejection', err => {
   fail(err);