mirror of
https://github.com/alibaba/lowcode-engine.git
synced 2025-12-12 19:52:51 +00:00
test: add ut for renderer-core/utils/request
This commit is contained in:
parent
988719d72f
commit
0735f1ca76
@ -146,7 +146,11 @@ export function request(dataAPI: any, method = 'GET', data: any, headers = {}, o
|
||||
return null;
|
||||
})
|
||||
.then((json) => {
|
||||
if (json && json.__success !== false) {
|
||||
if (!json) {
|
||||
reject(json);
|
||||
return;
|
||||
}
|
||||
if (json.__success !== false) {
|
||||
resolve(json);
|
||||
} else {
|
||||
// eslint-disable-next-line no-param-reassign
|
||||
@ -177,7 +181,9 @@ export function jsonp(dataAPI: any, params = {}, otherProps = {}) {
|
||||
};
|
||||
const url = buildUrl(dataAPI, params);
|
||||
fetchJsonp(url, processedOtherProps)
|
||||
.then((response) => response.json())
|
||||
.then((response) => {
|
||||
response.json();
|
||||
})
|
||||
.then((json) => {
|
||||
if (json) {
|
||||
resolve(json);
|
||||
|
||||
@ -7,13 +7,24 @@ jest.mock('../../src/utils/common', () => {
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
const mockFetchJsonp = jest.fn();
|
||||
jest.mock('fetch-jsonp', () => {
|
||||
return (uri, otherProps) => {
|
||||
mockFetchJsonp(uri, otherProps);
|
||||
return Promise.resolve({
|
||||
json: () => {
|
||||
return Promise.resolve({ data: [1, 2, 3]});
|
||||
} ,
|
||||
ok: true,
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
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';
|
||||
@ -29,4 +40,120 @@ describe('test utils/request.ts ', () => {
|
||||
mockSerializeParams.mockClear();
|
||||
});
|
||||
|
||||
it('request should be working properly', () => {
|
||||
const fetchMock = jest
|
||||
.spyOn(global, 'fetch')
|
||||
.mockImplementation(() =>
|
||||
Promise.resolve({
|
||||
json: () => Promise.resolve([]) ,
|
||||
status: 200,
|
||||
})
|
||||
);
|
||||
|
||||
request('https://someradomurl/api/list', 'GET', {}, {}, {}).then((response) => {
|
||||
expect(fetchMock).toBeCalledWith('https://someradomurl/api/list', { body: {}, credentials: 'include', headers: {}, method: 'GET'});
|
||||
}).catch((error) => {
|
||||
console.error(error);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
it('get should be working properly', () => {
|
||||
const fetchMock = jest
|
||||
.spyOn(global, 'fetch')
|
||||
.mockImplementation(() =>
|
||||
Promise.resolve({
|
||||
json: () => Promise.resolve([]) ,
|
||||
status: 200,
|
||||
})
|
||||
);
|
||||
|
||||
get('https://someradomurl/api/list', {}, {}, {}).then((response) => {
|
||||
expect(fetchMock).toBeCalledWith(
|
||||
'https://someradomurl/api/list',
|
||||
{
|
||||
body: null,
|
||||
headers: { Accept: 'application/json' },
|
||||
method: 'GET',
|
||||
credentials: 'include',
|
||||
});
|
||||
}).catch((error) => {
|
||||
console.error(error);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
it('post should be working properly', () => {
|
||||
const fetchMock = jest
|
||||
.spyOn(global, 'fetch')
|
||||
.mockImplementation(() =>
|
||||
Promise.resolve({
|
||||
json: () => Promise.resolve([]) ,
|
||||
status: 200,
|
||||
})
|
||||
);
|
||||
|
||||
post('https://someradomurl/api/list', { a: 1, b: 'a', c: [] }, { 'Content-Type': 'application/json' }, {}).then((response) => {
|
||||
expect(fetchMock).toBeCalledWith(
|
||||
'https://someradomurl/api/list',
|
||||
{
|
||||
body: '{"a":1,"b":"a","c":[]}',
|
||||
headers: {
|
||||
Accept: 'application/json',
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
method: 'POST',
|
||||
credentials: 'include',
|
||||
});
|
||||
}).catch((error) => {
|
||||
console.error(error);
|
||||
});
|
||||
|
||||
|
||||
post('https://someradomurl/api/list', [ 1, 2, 3, 4 ], {}, {}).then((response) => {
|
||||
expect(fetchMock).toBeCalledWith(
|
||||
'https://someradomurl/api/list',
|
||||
{
|
||||
body: '[1,2,3,4]',
|
||||
headers: {
|
||||
Accept: 'application/json',
|
||||
'Content-Type': 'application/x-www-form-urlencoded',
|
||||
},
|
||||
method: 'POST',
|
||||
credentials: 'include',
|
||||
});
|
||||
}).catch((error) => {
|
||||
console.error(error);
|
||||
});
|
||||
|
||||
mockSerializeParams.mockImplementation((params) => {
|
||||
return 'serializedParams=serializedParams';
|
||||
});
|
||||
post('https://someradomurl/api/list', { a: 1, b: 'a', c: [] }, {}, {}).then((response) => {
|
||||
expect(fetchMock).toBeCalledWith(
|
||||
'https://someradomurl/api/list',
|
||||
{
|
||||
body: 'serializedParams=serializedParams',
|
||||
headers: {
|
||||
Accept: 'application/json',
|
||||
'Content-Type': 'application/x-www-form-urlencoded',
|
||||
},
|
||||
method: 'POST',
|
||||
credentials: 'include',
|
||||
});
|
||||
mockSerializeParams.mockClear();
|
||||
}).catch((error) => {
|
||||
console.error(error);
|
||||
});
|
||||
|
||||
});
|
||||
it('jsonp should be working properly', () => {
|
||||
mockSerializeParams.mockImplementation((params) => {
|
||||
return 'params';
|
||||
});
|
||||
jsonp('https://someradomurl/api/list', {}, { otherParam1: '123'}).catch(() => {
|
||||
expect(mockFetchJsonp).toBeCalledWith('https://someradomurl/api/list?params', { timeout: 5000, otherParam1: '123' });
|
||||
mockSerializeParams.mockClear();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user