Skip to content

Commit

Permalink
move response mocks to mocks.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
pgayvallet committed Jul 3, 2020
1 parent 0a09aee commit 1a68ea1
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 26 deletions.
24 changes: 0 additions & 24 deletions src/core/server/elasticsearch/client/client_facade.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,8 @@
* under the License.
*/

import { ApiResponse } from '@elastic/elasticsearch';
import { TransportRequestPromise } from '@elastic/elasticsearch/lib/Transport';
import { ClientFacade } from './client_facade';

const createApiResponse = <T>(body: T): TransportRequestPromise<ApiResponse<T>> => {
const response: ApiResponse<T> = {
body,
statusCode: 200,
warnings: [],
headers: {},
meta: {} as any,
};
const promise = Promise.resolve(response);
(promise as TransportRequestPromise<ApiResponse<T>>).abort = () => undefined;

return promise as TransportRequestPromise<ApiResponse<T>>;
};

const createApiError = (err: any): TransportRequestPromise<never> => {
const promise = Promise.reject(err);
(promise as TransportRequestPromise<never>).abort = () => undefined;
return promise as TransportRequestPromise<never>;
};

const createFacadeMock = () => {
const mock: DeeplyMockedKeys<ClientFacade> = {
transport: {
Expand Down Expand Up @@ -435,6 +413,4 @@ const createFacadeMock = () => {

export const clientFacadeMock = {
create: createFacadeMock,
createApiResponse,
createApiError,
};
30 changes: 28 additions & 2 deletions src/core/server/elasticsearch/client/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
* under the License.
*/

import { ApiResponse } from '@elastic/elasticsearch';
import { TransportRequestPromise } from '@elastic/elasticsearch/lib/Transport';
import { clientFacadeMock } from './client_facade.mock';
import { IScopedClusterClient } from './scoped_cluster_client';
import { IClusterClient, ICustomClusterClient } from './cluster_client';
Expand Down Expand Up @@ -58,11 +60,35 @@ const createCustomClusterClientMock = () => {
return mock;
};

type MockedTransportRequestPromise<T> = TransportRequestPromise<T> & {
abort: jest.MockedFunction<() => undefined>;
};

const createMockedClientResponse = <T>(body: T): MockedTransportRequestPromise<ApiResponse<T>> => {
const response: ApiResponse<T> = {
body,
statusCode: 200,
warnings: [],
headers: {},
meta: {} as any,
};
const promise = Promise.resolve(response);
(promise as MockedTransportRequestPromise<ApiResponse<T>>).abort = jest.fn();

return promise as MockedTransportRequestPromise<ApiResponse<T>>;
};

const createMockedClientError = (err: any): MockedTransportRequestPromise<never> => {
const promise = Promise.reject(err);
(promise as MockedTransportRequestPromise<never>).abort = jest.fn();
return promise as MockedTransportRequestPromise<never>;
};

export const elasticsearchClientMock = {
createClusterClient: createClusterClientMock,
createCustomClusterClient: createCustomClusterClientMock,
createScopedClusterClient: createScopedClusterClientMock,
createFacade: clientFacadeMock.create,
createClientResponse: clientFacadeMock.createApiResponse,
createClientError: clientFacadeMock.createApiError,
createClientResponse: createMockedClientResponse,
createClientError: createMockedClientError,
};

0 comments on commit 1a68ea1

Please sign in to comment.