From f6a4bddd9e43c776ad46f18921dfe324bbba53e9 Mon Sep 17 00:00:00 2001 From: Kishor Rathva <kishorrathva8298@gmail.com> Date: Thu, 7 Dec 2023 00:34:17 +0530 Subject: [PATCH] Refactored tests Signed-off-by: kishor82 <kishorrathva8298@gmail.com> --- .../lib/mappings/__tests__/mapping.test.ts | 56 ++++++++++++------- 1 file changed, 37 insertions(+), 19 deletions(-) diff --git a/src/plugins/console/public/lib/mappings/__tests__/mapping.test.ts b/src/plugins/console/public/lib/mappings/__tests__/mapping.test.ts index fc977c32c7e5..8e023df6c1ce 100644 --- a/src/plugins/console/public/lib/mappings/__tests__/mapping.test.ts +++ b/src/plugins/console/public/lib/mappings/__tests__/mapping.test.ts @@ -294,18 +294,17 @@ describe('Mappings', () => { expect(mappings.getTypes()).toEqual(['properties']); }); }); -const tick = () => new Promise((res) => setImmediate(res)); - -export const advanceTimersByTime = async (time: number) => - jest.advanceTimersByTime(time) && (await tick()); - -export const runOnlyPendingTimers = async () => jest.runOnlyPendingTimers() && (await tick()); - -export const runAllTimers = async () => jest.runAllTimers() && (await tick()); describe('Auto Complete Info', () => { let response = {}; + const mockHttpResponse = createMockHttpResponse( + 200, + 'ok', + [['Content-Type', 'application/json, utf-8']], + response + ); + beforeEach(() => { mappings.clear(); response = { @@ -331,15 +330,36 @@ describe('Auto Complete Info', () => { }); test('Retrieve AutoComplete Info for Mappings, Aliases and Templates', async () => { - jest.useFakeTimers(); // Ensure fake timers are used + const dataSourceId = 'mock-data-source-id'; + const sendSpy = jest.spyOn(opensearch, 'send').mockResolvedValue(mockHttpResponse); + + const { + services: { http, settings: settingsService }, + } = serviceContextMock.create(); - const mockHttpResponse = createMockHttpResponse( - 200, - 'ok', - [['Content-Type', 'application/json, utf-8']], - response + mappings.retrieveAutoCompleteInfo( + http, + settingsService, + { + fields: true, + indices: true, + templates: true, + }, + dataSourceId ); + // Fast-forward until all timers have been executed + jest.runAllTimers(); + + expect(sendSpy).toHaveBeenCalledTimes(3); + + // Ensure send is called with different arguments + expect(sendSpy).toHaveBeenCalledWith(http, 'GET', '_mapping', null, dataSourceId); + expect(sendSpy).toHaveBeenCalledWith(http, 'GET', '_aliases', null, dataSourceId); + expect(sendSpy).toHaveBeenCalledWith(http, 'GET', '_template', null, dataSourceId); + }); + + test('Retrieve AutoComplete Info for Specified Fields from the Settings', async () => { const dataSourceId = 'mock-data-source-id'; const sendSpy = jest.spyOn(opensearch, 'send').mockResolvedValue(mockHttpResponse); @@ -352,8 +372,8 @@ describe('Auto Complete Info', () => { settingsService, { fields: true, - indices: true, - templates: true, + indices: false, + templates: false, }, dataSourceId ); @@ -364,11 +384,9 @@ describe('Auto Complete Info', () => { // Resolve the promise chain await Promise.resolve(); - expect(sendSpy).toHaveBeenCalledTimes(3); + expect(sendSpy).toHaveBeenCalledTimes(1); // Ensure send is called with different arguments expect(sendSpy).toHaveBeenCalledWith(http, 'GET', '_mapping', null, dataSourceId); - expect(sendSpy).toHaveBeenCalledWith(http, 'GET', '_aliases', null, dataSourceId); - expect(sendSpy).toHaveBeenCalledWith(http, 'GET', '_template', null, dataSourceId); }); });