mirror of
https://github.com/alibaba/lowcode-engine.git
synced 2025-12-12 19:52:51 +00:00
test: add ut file and fix some obvious lint issues
This commit is contained in:
parent
44ad744aac
commit
7eecb39c97
@ -2,7 +2,15 @@ import 'whatwg-fetch';
|
|||||||
import fetchJsonp from 'fetch-jsonp';
|
import fetchJsonp from 'fetch-jsonp';
|
||||||
import { serializeParams } from '.';
|
import { serializeParams } from '.';
|
||||||
|
|
||||||
function buildUrl(dataAPI: any, params: any) {
|
/**
|
||||||
|
* this is a private method, export for testing purposes only.
|
||||||
|
*
|
||||||
|
* @export
|
||||||
|
* @param {*} dataAPI
|
||||||
|
* @param {*} params
|
||||||
|
* @returns
|
||||||
|
*/
|
||||||
|
export function buildUrl(dataAPI: any, params: any) {
|
||||||
const paramStr = serializeParams(params);
|
const paramStr = serializeParams(params);
|
||||||
if (paramStr) {
|
if (paramStr) {
|
||||||
return dataAPI.indexOf('?') > 0 ? `${dataAPI}&${paramStr}` : `${dataAPI}?${paramStr}`;
|
return dataAPI.indexOf('?') > 0 ? `${dataAPI}&${paramStr}` : `${dataAPI}?${paramStr}`;
|
||||||
@ -10,43 +18,75 @@ function buildUrl(dataAPI: any, params: any) {
|
|||||||
return dataAPI;
|
return dataAPI;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function get(dataAPI: any, params = {}, headers = {}, otherProps = {}) {
|
/**
|
||||||
headers = {
|
* do Get request
|
||||||
|
*
|
||||||
|
* @export
|
||||||
|
* @param {*} dataAPI
|
||||||
|
* @param {*} [params={}]
|
||||||
|
* @param {*} [headers={}]
|
||||||
|
* @param {*} [otherProps={}]
|
||||||
|
* @returns
|
||||||
|
*/
|
||||||
|
export function get(dataAPI: any, params = {}, headers = {}, otherProps = {}) {
|
||||||
|
const processedHeaders = {
|
||||||
Accept: 'application/json',
|
Accept: 'application/json',
|
||||||
...headers,
|
...headers,
|
||||||
};
|
};
|
||||||
dataAPI = buildUrl(dataAPI, params);
|
const url = buildUrl(dataAPI, params);
|
||||||
return request(dataAPI, 'GET', null, headers, otherProps);
|
return request(url, 'GET', null, processedHeaders, otherProps);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* do Post request
|
||||||
|
*
|
||||||
|
* @export
|
||||||
|
* @param {*} dataAPI
|
||||||
|
* @param {*} [params={}]
|
||||||
|
* @param {*} [headers={}]
|
||||||
|
* @param {*} [otherProps={}]
|
||||||
|
* @returns
|
||||||
|
*/
|
||||||
export function post(dataAPI: any, params = {}, headers: any = {}, otherProps = {}) {
|
export function post(dataAPI: any, params = {}, headers: any = {}, otherProps = {}) {
|
||||||
headers = {
|
const processedHeaders = {
|
||||||
Accept: 'application/json',
|
Accept: 'application/json',
|
||||||
'Content-Type': 'application/x-www-form-urlencoded',
|
'Content-Type': 'application/x-www-form-urlencoded',
|
||||||
...headers,
|
...headers,
|
||||||
};
|
};
|
||||||
|
const body = processedHeaders['Content-Type'].indexOf('application/json') > -1 || Array.isArray(params)
|
||||||
|
? JSON.stringify(params)
|
||||||
|
: serializeParams(params);
|
||||||
|
|
||||||
return request(
|
return request(
|
||||||
dataAPI,
|
dataAPI,
|
||||||
'POST',
|
'POST',
|
||||||
headers['Content-Type'].indexOf('application/json') > -1 || Array.isArray(params)
|
body,
|
||||||
? JSON.stringify(params)
|
processedHeaders,
|
||||||
: serializeParams(params),
|
|
||||||
headers,
|
|
||||||
otherProps,
|
otherProps,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* do request
|
||||||
|
*
|
||||||
|
* @export
|
||||||
|
* @param {*} dataAPI
|
||||||
|
* @param {string} [method='GET']
|
||||||
|
* @param {*} data
|
||||||
|
* @param {*} [headers={}]
|
||||||
|
* @param {*} [otherProps={}]
|
||||||
|
* @returns
|
||||||
|
*/
|
||||||
export function request(dataAPI: any, method = 'GET', data: any, headers = {}, otherProps: any = {}) {
|
export function request(dataAPI: any, method = 'GET', data: any, headers = {}, otherProps: any = {}) {
|
||||||
switch (method) {
|
let processedHeaders = headers || {};
|
||||||
case 'PUT':
|
let payload = data;
|
||||||
case 'DELETE':
|
if (method === 'PUT' || method === 'DELETE') {
|
||||||
headers = {
|
processedHeaders = {
|
||||||
Accept: 'application/json',
|
Accept: 'application/json',
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
...headers,
|
...processedHeaders,
|
||||||
};
|
};
|
||||||
data = JSON.stringify(data || {});
|
payload = JSON.stringify(payload || {});
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
if (otherProps.timeout) {
|
if (otherProps.timeout) {
|
||||||
@ -57,8 +97,8 @@ export function request(dataAPI: any, method = 'GET', data: any, headers = {}, o
|
|||||||
fetch(dataAPI, {
|
fetch(dataAPI, {
|
||||||
method,
|
method,
|
||||||
credentials: 'include',
|
credentials: 'include',
|
||||||
headers,
|
headers: processedHeaders,
|
||||||
body: data,
|
body: payload,
|
||||||
...otherProps,
|
...otherProps,
|
||||||
})
|
})
|
||||||
.then((response) => {
|
.then((response) => {
|
||||||
@ -101,6 +141,7 @@ export function request(dataAPI: any, method = 'GET', data: any, headers = {}, o
|
|||||||
code: response.status,
|
code: response.status,
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
default:
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
})
|
})
|
||||||
@ -108,6 +149,7 @@ export function request(dataAPI: any, method = 'GET', data: any, headers = {}, o
|
|||||||
if (json && json.__success !== false) {
|
if (json && json.__success !== false) {
|
||||||
resolve(json);
|
resolve(json);
|
||||||
} else {
|
} else {
|
||||||
|
// eslint-disable-next-line no-param-reassign
|
||||||
delete json.__success;
|
delete json.__success;
|
||||||
reject(json);
|
reject(json);
|
||||||
}
|
}
|
||||||
@ -118,13 +160,23 @@ export function request(dataAPI: any, method = 'GET', data: any, headers = {}, o
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* do jsonp request
|
||||||
|
*
|
||||||
|
* @export
|
||||||
|
* @param {*} dataAPI
|
||||||
|
* @param {*} [params={}]
|
||||||
|
* @param {*} [otherProps={}]
|
||||||
|
* @returns
|
||||||
|
*/
|
||||||
export function jsonp(dataAPI: any, params = {}, otherProps = {}) {
|
export function jsonp(dataAPI: any, params = {}, otherProps = {}) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
otherProps = {
|
const processedOtherProps = {
|
||||||
timeout: 5000,
|
timeout: 5000,
|
||||||
...otherProps,
|
...otherProps,
|
||||||
};
|
};
|
||||||
fetchJsonp(buildUrl(dataAPI, params), otherProps)
|
const url = buildUrl(dataAPI, params);
|
||||||
|
fetchJsonp(url, processedOtherProps)
|
||||||
.then((response) => response.json())
|
.then((response) => response.json())
|
||||||
.then((json) => {
|
.then((json) => {
|
||||||
if (json) {
|
if (json) {
|
||||||
|
|||||||
32
packages/renderer-core/tests/utils/request.test.ts
Normal file
32
packages/renderer-core/tests/utils/request.test.ts
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
// @ts-nocheck
|
||||||
|
const mockSerializeParams = jest.fn();
|
||||||
|
jest.mock('../../src/utils/common', () => {
|
||||||
|
return {
|
||||||
|
serializeParams: (params) => {
|
||||||
|
return mockSerializeParams(params);
|
||||||
|
},
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
import { get, post, buildUrl, request, jsonp } from '../../src/utils/request';
|
||||||
|
|
||||||
|
describe('test utils/request.ts ', () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
})
|
||||||
|
it('buildUrl should be working properly', () => {
|
||||||
|
mockSerializeParams.mockImplementation((params) => {
|
||||||
|
return 'serializedParams=serializedParams';
|
||||||
|
});
|
||||||
|
expect(buildUrl('mockDataApi', { a: 1, b: 'a', c: []})).toBe('mockDataApi?serializedParams=serializedParams');
|
||||||
|
expect(buildUrl('mockDataApi?existingParamA=valueA', { a: 1, b: 'a', c: []})).toBe('mockDataApi?existingParamA=valueA&serializedParams=serializedParams');
|
||||||
|
mockSerializeParams.mockClear();
|
||||||
|
|
||||||
|
mockSerializeParams.mockImplementation((params) => {
|
||||||
|
return undefined;
|
||||||
|
});
|
||||||
|
expect(buildUrl('mockDataApi', { a: 1, b: 'a', c: []})).toBe('mockDataApi');
|
||||||
|
mockSerializeParams.mockClear();
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
Loading…
x
Reference in New Issue
Block a user