mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2025-09-17 14:21:29 +00:00
AAE-30882 Separate mock objects
This commit is contained in:
69
lib/js-api/test/mockObjects/httpClient.mock.ts
Normal file
69
lib/js-api/test/mockObjects/httpClient.mock.ts
Normal file
@@ -0,0 +1,69 @@
|
|||||||
|
/*!
|
||||||
|
* @license
|
||||||
|
* Copyright © 2005-2025 Hyland Software, Inc. and its affiliates. All rights reserved.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import { Emitters, RequestOptions, SecurityOptions } from 'lib/js-api/src/api-clients/http-client.interface';
|
||||||
|
|
||||||
|
export const emptyQueryParams = {};
|
||||||
|
export const emptyHeaderParams = {};
|
||||||
|
export const emptyFormParams = {};
|
||||||
|
export const jsonContentType = 'application/json';
|
||||||
|
export const jsonAccept = 'application/json';
|
||||||
|
export const getMethod = 'GET';
|
||||||
|
export const testUrlEnterprise = 'http://fake-api/enterprise/process-instances/';
|
||||||
|
export const testUrlCookie = 'http://fake-api/test-cookie';
|
||||||
|
|
||||||
|
export const buildRequestMockOptions: RequestOptions = {
|
||||||
|
path: '/test',
|
||||||
|
httpMethod: 'GET',
|
||||||
|
queryParams: {},
|
||||||
|
headerParams: {},
|
||||||
|
formParams: {},
|
||||||
|
bodyParam: null,
|
||||||
|
contentType: jsonContentType,
|
||||||
|
accept: jsonAccept,
|
||||||
|
responseType: 'json',
|
||||||
|
returnType: null
|
||||||
|
};
|
||||||
|
|
||||||
|
export const defaultSecurityOptions: SecurityOptions = {
|
||||||
|
isBpmRequest: false,
|
||||||
|
enableCsrf: false,
|
||||||
|
withCredentials: false,
|
||||||
|
authentications: {
|
||||||
|
basicAuth: { ticket: '' },
|
||||||
|
type: 'basic'
|
||||||
|
},
|
||||||
|
defaultHeaders: {}
|
||||||
|
};
|
||||||
|
|
||||||
|
export const defaultRequestOptions: RequestOptions = {
|
||||||
|
path: '/test',
|
||||||
|
httpMethod: 'GET',
|
||||||
|
queryParams: {},
|
||||||
|
headerParams: {},
|
||||||
|
formParams: {},
|
||||||
|
bodyParam: null,
|
||||||
|
contentType: 'application/json',
|
||||||
|
accept: 'application/json',
|
||||||
|
responseType: 'json',
|
||||||
|
returnType: null
|
||||||
|
};
|
||||||
|
|
||||||
|
export const defaultEmitters: Emitters = {
|
||||||
|
eventEmitter: { emit: jest.fn(), on: jest.fn(), off: jest.fn(), once: jest.fn() },
|
||||||
|
apiClientEmitter: { emit: jest.fn(), on: jest.fn(), off: jest.fn(), once: jest.fn() }
|
||||||
|
};
|
@@ -18,46 +18,26 @@
|
|||||||
import { SuperagentHttpClient } from '../src/superagentHttpClient';
|
import { SuperagentHttpClient } from '../src/superagentHttpClient';
|
||||||
import { FetchResponse, ofetch } from 'ofetch';
|
import { FetchResponse, ofetch } from 'ofetch';
|
||||||
import { RequestOptions } from '../src/api-clients/http-client.interface';
|
import { RequestOptions } from '../src/api-clients/http-client.interface';
|
||||||
import { Emitters } from '@alfresco/adf-core/api';
|
|
||||||
import { isBrowser } from '../src/utils';
|
import { isBrowser } from '../src/utils';
|
||||||
|
import { buildRequestMockOptions, defaultEmitters, defaultRequestOptions, defaultSecurityOptions } from './mockObjects/httpClient.mock';
|
||||||
|
|
||||||
jest.mock('ofetch', () => ({
|
jest.mock('ofetch', () => ({
|
||||||
ofetch: jest.fn()
|
ofetch: jest.fn()
|
||||||
}));
|
}));
|
||||||
|
|
||||||
jest.mock('../src/utils', () => ({
|
jest.mock('../src/utils', () => ({
|
||||||
isBrowser: jest.fn(() => true), // default implementation: browser environment
|
isBrowser: jest.fn(() => true),
|
||||||
paramToString: (param: any) => String(param)
|
paramToString: (param: any) => String(param)
|
||||||
}));
|
}));
|
||||||
|
|
||||||
describe('SuperagentHttpClient', () => {
|
describe('SuperagentHttpClient', () => {
|
||||||
describe('request', () => {
|
describe('request', () => {
|
||||||
let client: SuperagentHttpClient;
|
let client: SuperagentHttpClient;
|
||||||
let emitters: Emitters;
|
let emitters: typeof defaultEmitters;
|
||||||
|
|
||||||
const url = 'http://fake-api/test';
|
const url = 'http://fake-api/test';
|
||||||
const options: RequestOptions = {
|
const options: RequestOptions = { ...defaultRequestOptions };
|
||||||
path: '/test',
|
const securityOptions = { ...defaultSecurityOptions };
|
||||||
httpMethod: 'GET',
|
|
||||||
queryParams: {},
|
|
||||||
headerParams: {},
|
|
||||||
formParams: {},
|
|
||||||
bodyParam: null,
|
|
||||||
contentType: 'application/json',
|
|
||||||
accept: 'application/json',
|
|
||||||
responseType: 'json',
|
|
||||||
returnType: null
|
|
||||||
};
|
|
||||||
const securityOptions = {
|
|
||||||
isBpmRequest: false,
|
|
||||||
enableCsrf: false,
|
|
||||||
withCredentials: false,
|
|
||||||
authentications: {
|
|
||||||
basicAuth: { ticket: '' },
|
|
||||||
type: 'basic'
|
|
||||||
},
|
|
||||||
defaultHeaders: {}
|
|
||||||
};
|
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
client = new SuperagentHttpClient();
|
client = new SuperagentHttpClient();
|
||||||
@@ -110,84 +90,58 @@ describe('SuperagentHttpClient', () => {
|
|||||||
describe('buildRequest', () => {
|
describe('buildRequest', () => {
|
||||||
it('should create a request with response type blob', () => {
|
it('should create a request with response type blob', () => {
|
||||||
const client = new SuperagentHttpClient();
|
const client = new SuperagentHttpClient();
|
||||||
const queryParams = {};
|
const options = { ...buildRequestMockOptions };
|
||||||
const headerParams = {};
|
options.contentType = 'application/json';
|
||||||
const formParams = {};
|
options.accept = 'application/json';
|
||||||
|
options.responseType = 'blob';
|
||||||
|
|
||||||
const contentType = 'application/json';
|
|
||||||
const accept = 'application/json';
|
|
||||||
const responseType = 'blob';
|
|
||||||
const url = 'http://fake-api/enterprise/process-instances/';
|
const url = 'http://fake-api/enterprise/process-instances/';
|
||||||
const httpMethod = 'GET';
|
const httpMethod = 'GET';
|
||||||
const securityOptions = {
|
const securityOptions = { ...defaultSecurityOptions };
|
||||||
isBpmRequest: false,
|
|
||||||
enableCsrf: false,
|
|
||||||
withCredentials: false,
|
|
||||||
authentications: {
|
|
||||||
basicAuth: {
|
|
||||||
ticket: ''
|
|
||||||
},
|
|
||||||
type: 'basic'
|
|
||||||
},
|
|
||||||
defaultHeaders: {}
|
|
||||||
};
|
|
||||||
|
|
||||||
const request = client['buildRequest']({
|
const request = client['buildRequest']({
|
||||||
httpMethod,
|
httpMethod,
|
||||||
url,
|
url,
|
||||||
queryParams,
|
queryParams: options.queryParams,
|
||||||
headerParams,
|
headerParams: options.headerParams,
|
||||||
formParams,
|
formParams: options.formParams,
|
||||||
contentType,
|
contentType: options.contentType,
|
||||||
accept,
|
accept: options.accept,
|
||||||
responseType,
|
responseType: options.responseType,
|
||||||
bodyParam: null,
|
bodyParam: null,
|
||||||
returnType: null,
|
returnType: null,
|
||||||
securityOptions
|
securityOptions
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(request.urlWithParams).toEqual('http://fake-api/enterprise/process-instances/');
|
expect(request.urlWithParams).toEqual(url);
|
||||||
const { fetchOptions } = request;
|
const { fetchOptions } = request;
|
||||||
|
expect(fetchOptions.headers['accept']).toEqual(options.accept);
|
||||||
expect(fetchOptions.headers['accept']).toEqual('application/json');
|
expect(fetchOptions.headers['content-type']).toEqual(options.contentType);
|
||||||
expect(fetchOptions.headers['content-type']).toEqual('application/json');
|
|
||||||
expect(fetchOptions.responseType).toEqual('blob');
|
expect(fetchOptions.responseType).toEqual('blob');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should set Cookie header when isBpmRequest is true and cookie exists in non-browser environment', () => {
|
it('should set Cookie header when isBpmRequest is true and cookie exists in non-browser environment', () => {
|
||||||
const client = new SuperagentHttpClient();
|
const client = new SuperagentHttpClient();
|
||||||
const queryParams = {};
|
const options = { ...buildRequestMockOptions };
|
||||||
const headerParams = {};
|
|
||||||
const formParams = {};
|
|
||||||
const contentType = 'application/json';
|
|
||||||
const accept = 'application/json';
|
|
||||||
const responseType = 'json';
|
|
||||||
const url = 'http://fake-api/test-cookie';
|
const url = 'http://fake-api/test-cookie';
|
||||||
const httpMethod = 'GET';
|
const httpMethod = 'GET';
|
||||||
const securityOptions = {
|
const securityOptions = {
|
||||||
|
...defaultSecurityOptions,
|
||||||
isBpmRequest: true,
|
isBpmRequest: true,
|
||||||
enableCsrf: false,
|
authentications: { cookie: 'testCookie', basicAuth: { ticket: '' }, type: 'basic' }
|
||||||
withCredentials: false,
|
|
||||||
authentications: {
|
|
||||||
cookie: 'testCookie',
|
|
||||||
basicAuth: { ticket: '' },
|
|
||||||
type: 'basic'
|
|
||||||
},
|
|
||||||
defaultHeaders: {}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// Override isBrowser to simulate a non-browser environment
|
|
||||||
(isBrowser as jest.Mock).mockReturnValue(false);
|
(isBrowser as jest.Mock).mockReturnValue(false);
|
||||||
|
|
||||||
const request = client['buildRequest']({
|
const request = client['buildRequest']({
|
||||||
httpMethod,
|
httpMethod,
|
||||||
url,
|
url,
|
||||||
queryParams,
|
queryParams: options.queryParams,
|
||||||
headerParams,
|
headerParams: options.headerParams,
|
||||||
formParams,
|
formParams: options.formParams,
|
||||||
contentType,
|
contentType: options.contentType,
|
||||||
accept,
|
accept: options.accept,
|
||||||
responseType,
|
responseType: options.responseType,
|
||||||
bodyParam: null,
|
bodyParam: null,
|
||||||
returnType: null,
|
returnType: null,
|
||||||
securityOptions
|
securityOptions
|
||||||
@@ -199,14 +153,8 @@ describe('SuperagentHttpClient', () => {
|
|||||||
describe('deserialize', () => {
|
describe('deserialize', () => {
|
||||||
it('should deserialize to an array when the response body is an array', async () => {
|
it('should deserialize to an array when the response body is an array', async () => {
|
||||||
const data = [
|
const data = [
|
||||||
{
|
{ id: '1', name: 'test1' },
|
||||||
id: '1',
|
{ id: '2', name: 'test2' }
|
||||||
name: 'test1'
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: '2',
|
|
||||||
name: 'test2'
|
|
||||||
}
|
|
||||||
];
|
];
|
||||||
const response = {
|
const response = {
|
||||||
json() {
|
json() {
|
||||||
@@ -214,7 +162,6 @@ describe('SuperagentHttpClient', () => {
|
|||||||
}
|
}
|
||||||
} as FetchResponse<unknown>;
|
} as FetchResponse<unknown>;
|
||||||
const result = await SuperagentHttpClient['deserialize'](response);
|
const result = await SuperagentHttpClient['deserialize'](response);
|
||||||
|
|
||||||
const isArray = Array.isArray(result);
|
const isArray = Array.isArray(result);
|
||||||
expect(isArray).toEqual(true);
|
expect(isArray).toEqual(true);
|
||||||
});
|
});
|
||||||
@@ -224,7 +171,6 @@ describe('SuperagentHttpClient', () => {
|
|||||||
json: () => Promise.resolve({ id: '1', name: 'test1' })
|
json: () => Promise.resolve({ id: '1', name: 'test1' })
|
||||||
} as FetchResponse<unknown>;
|
} as FetchResponse<unknown>;
|
||||||
const result = SuperagentHttpClient['deserialize'](response);
|
const result = SuperagentHttpClient['deserialize'](response);
|
||||||
|
|
||||||
const isArray = Array.isArray(result);
|
const isArray = Array.isArray(result);
|
||||||
expect(isArray).toEqual(false);
|
expect(isArray).toEqual(false);
|
||||||
});
|
});
|
||||||
@@ -238,9 +184,7 @@ describe('SuperagentHttpClient', () => {
|
|||||||
const data = {
|
const data = {
|
||||||
text: () => Promise.resolve('mock-response-text')
|
text: () => Promise.resolve('mock-response-text')
|
||||||
} as FetchResponse<unknown>;
|
} as FetchResponse<unknown>;
|
||||||
|
|
||||||
const result = await SuperagentHttpClient['deserialize'](data);
|
const result = await SuperagentHttpClient['deserialize'](data);
|
||||||
|
|
||||||
expect(result).toEqual('mock-response-text');
|
expect(result).toEqual('mock-response-text');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user