From 0735f1ca76e87e69620cf2251a51c5dbbcb91de8 Mon Sep 17 00:00:00 2001 From: JackLian Date: Tue, 7 Jun 2022 16:34:34 +0800 Subject: [PATCH] test: add ut for renderer-core/utils/request --- packages/renderer-core/src/utils/request.ts | 10 +- .../renderer-core/tests/utils/request.test.ts | 133 +++++++++++++++++- 2 files changed, 138 insertions(+), 5 deletions(-) diff --git a/packages/renderer-core/src/utils/request.ts b/packages/renderer-core/src/utils/request.ts index 1af8655e0..dde5ca87e 100644 --- a/packages/renderer-core/src/utils/request.ts +++ b/packages/renderer-core/src/utils/request.ts @@ -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); diff --git a/packages/renderer-core/tests/utils/request.test.ts b/packages/renderer-core/tests/utils/request.test.ts index ad1187dda..d0bfeb5aa 100644 --- a/packages/renderer-core/tests/utils/request.test.ts +++ b/packages/renderer-core/tests/utils/request.test.ts @@ -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(); + }); + }); });