Skip to content

Commit

Permalink
Fix mock import (#7922)
Browse files Browse the repository at this point in the history
Signed-off-by: Huy Nguyen <[email protected]>
  • Loading branch information
huyaboo authored Sep 2, 2024
1 parent 0798865 commit b370871
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 28 deletions.
2 changes: 0 additions & 2 deletions src/core/server/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@

export { coreDeprecationProvider } from './deprecation';

export { dynamicConfigServiceMock } from './mocks';

export { config } from './dynamic_config_service_config';

export {
Expand Down
5 changes: 5 additions & 0 deletions src/core/server/config/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,8 @@ export {
} from '@osd/config/target/mocks';

export { dynamicConfigServiceMock } from './dynamic_config_service.mock';

export {
internalDynamicConfigurationClientMock,
dynamicConfigurationClientMock,
} from './service/configuration_client.mock';
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import { DummyConfigStoreClient } from './dummy_config_store_client';

describe('DummyConfigStoreClient', () => {
const expectedResponse = {
body: {},
statusCode: 200,
headers: {},
warnings: [],
meta: {},
};
const dummyStoreConfigClient = new DummyConfigStoreClient();

it('should return empty map for listConfigs', async () => {
const response = await dummyStoreConfigClient.listConfigs();
expect(response).toMatchObject(new Map());
});

it('should return expectedResponse for bulkCreateConfigs', async () => {
const response = await dummyStoreConfigClient.bulkCreateConfigs({
configs: [],
});
expect(response).toMatchObject(expectedResponse);
});

it('should return expectedResponse for createConfigs', async () => {
const response = await dummyStoreConfigClient.createConfig({
config: {
name: 'foo',
updatedConfig: {},
},
});
expect(response).toMatchObject(expectedResponse);
});

it('should return expectedResponse for bulkDeleteConfigs', async () => {
const response = await dummyStoreConfigClient.bulkDeleteConfigs({
paths: [],
});
expect(response).toMatchObject(expectedResponse);
});

it('should return expectedResponse for deleteConfig', async () => {
const response = await dummyStoreConfigClient.deleteConfig({
pluginConfigPath: 'foo',
});
expect(response).toMatchObject(expectedResponse);
});

it('should return undefined for getConfig', async () => {
const response = await dummyStoreConfigClient.getConfig('foo');
expect(response).toBeUndefined();
});

it('should return empty for bulkGetConfigs', async () => {
const response = await dummyStoreConfigClient.bulkGetConfigs([]);
expect(response).toMatchObject(new Map());
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
DynamicConfigurationClientOptions,
IDynamicConfigStoreClient,
} from '../../types';
import { dynamicConfigurationClientMock } from '../configuration_client.mock';
import { createApiResponse } from '../../utils/utils';

/**
* The DummyConfigStoreClient is the client DAO that will used when dynamic config service is "disabled".
Expand All @@ -27,28 +27,28 @@ export class DummyConfigStoreClient implements IDynamicConfigStoreClient {
bulkCreateConfigProps: BulkCreateConfigProps,
options?: DynamicConfigurationClientOptions | undefined
) {
return Promise.resolve(dynamicConfigurationClientMock.createApiResponse());
return Promise.resolve(createApiResponse());
}

public async createConfig(
createConfigProps: CreateConfigProps,
options?: DynamicConfigurationClientOptions | undefined
) {
return Promise.resolve(dynamicConfigurationClientMock.createApiResponse());
return Promise.resolve(createApiResponse());
}

public async bulkDeleteConfigs(
bulkDeleteConfigs: BulkDeleteConfigProps,
options?: DynamicConfigurationClientOptions | undefined
) {
return Promise.resolve(dynamicConfigurationClientMock.createApiResponse());
return Promise.resolve(createApiResponse());
}

public async deleteConfig(
deleteConfigs: ConfigIdentifier,
options?: DynamicConfigurationClientOptions | undefined
) {
return Promise.resolve(dynamicConfigurationClientMock.createApiResponse());
return Promise.resolve(createApiResponse());
}

public async getConfig(
Expand Down
14 changes: 1 addition & 13 deletions src/core/server/config/service/configuration_client.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,7 @@

import { ApiResponse } from '@opensearch-project/opensearch/.';
import { IDynamicConfigurationClient, IInternalDynamicConfigurationClient } from '../types';

const createApiResponse = <TResponse = Record<string, any>>(
opts: Partial<ApiResponse> = {}
): ApiResponse<TResponse> => {
return {
body: {} as any,
statusCode: 200,
headers: {},
warnings: [],
meta: {} as any,
...opts,
};
};
import { createApiResponse } from '../utils/utils';

const createInternalDynamicConfigurationClientMock = (
props: InternalDynamicConfigurationClientMockProps = {
Expand Down
14 changes: 14 additions & 0 deletions src/core/server/config/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import _ from 'lodash';
import { Logger } from '@osd/logging';
import { Request } from 'hapi__hapi';
import { ApiResponse } from '@opensearch-project/opensearch/.';
import { ConfigIdentifier } from '../types';
import { DYNAMIC_APP_CONFIG_INDEX_PREFIX } from './constants';
import { OpenSearchDashboardsRequest } from '../../http';
Expand All @@ -25,6 +26,19 @@ export const pathToString = (configIdentifier: ConfigIdentifier) => {
return _.snakeCase(name);
};

export const createApiResponse = <TResponse = Record<string, any>>(
opts: Partial<ApiResponse> = {}
): ApiResponse<TResponse> => {
return {
body: {} as any,
statusCode: 200,
headers: {},
warnings: [],
meta: {} as any,
...opts,
};
};

/**
* Given the config from the config file and the config store, merge the two configs.
*
Expand Down
1 change: 1 addition & 0 deletions src/core/server/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ import { crossCompatibilityServiceMock } from './cross_compatibility/cross_compa
import { dynamicConfigServiceMock } from './config/dynamic_config_service.mock';

export { configServiceMock } from './config/mocks';
export { dynamicConfigServiceMock } from './config/mocks';
export { httpServerMock } from './http/http_server.mocks';
export { httpResourcesMock } from './http_resources/http_resources_service.mock';
export { sessionStorageMock } from './http/cookie_session_storage.mocks';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ import { registerFetchDataSourceMetaDataRoute } from './fetch_data_source_metada
import { AuthType } from '../../common/data_sources';
// eslint-disable-next-line @osd/eslint/no-restricted-paths
import { opensearchClientMock } from '../../../../../src/core/server/opensearch/client/mocks';
// eslint-disable-next-line @osd/eslint/no-restricted-paths
import { dynamicConfigServiceMock } from '../../../../../src/core/server/config';
import { dynamicConfigServiceMock } from '../../../../../src/core/server/mocks';

type SetupServerReturn = UnwrapPromise<ReturnType<typeof setupServer>>;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ import { registerTestConnectionRoute } from './test_connection';
import { AuthType } from '../../common/data_sources';
// eslint-disable-next-line @osd/eslint/no-restricted-paths
import { opensearchClientMock } from '../../../../../src/core/server/opensearch/client/mocks';
// eslint-disable-next-line @osd/eslint/no-restricted-paths
import { dynamicConfigServiceMock } from '../../../../../src/core/server/config';
import { dynamicConfigServiceMock } from '../../../../../src/core/server/mocks';

type SetupServerReturn = UnwrapPromise<ReturnType<typeof setupServer>>;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ import { createHttpServer } from '../../../../../core/server/test_utils';
import { registerStatsRoute } from '../stats';
import supertest from 'supertest';
import { CollectorSet } from '../../collector';
// eslint-disable-next-line @osd/eslint/no-restricted-paths
import { dynamicConfigServiceMock } from '../../../../../core/server/config';
import { dynamicConfigServiceMock } from '../../../../../core/server/mocks';

type HttpService = ReturnType<typeof createHttpServer>;
type HttpSetup = UnwrapPromise<ReturnType<HttpService['setup']>>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ import { setupServer } from '../../../../core/server/test_utils';
import { registerDuplicateRoute } from '../routes/duplicate';
import { createListStream } from '../../../../core/server/utils/streams';
import Boom from '@hapi/boom';
// eslint-disable-next-line @osd/eslint/no-restricted-paths
import { dynamicConfigServiceMock } from '../../../../core/server/config';
import { dynamicConfigServiceMock } from '../../../../core/server/mocks';

jest.mock('../../../../core/server/saved_objects/export', () => ({
exportSavedObjectsToStream: jest.fn(),
Expand Down

0 comments on commit b370871

Please sign in to comment.