Skip to content

Commit

Permalink
Refactored tests
Browse files Browse the repository at this point in the history
Signed-off-by: Kishor Rathva <kishorrathva8298@gmail.com>
  • Loading branch information
kishor82 committed Dec 13, 2023
1 parent 9fb82ab commit 80c83c7
Showing 1 changed file with 37 additions and 19 deletions.
56 changes: 37 additions & 19 deletions src/plugins/console/public/lib/mappings/__tests__/mapping.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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);

Expand All @@ -352,8 +372,8 @@ describe('Auto Complete Info', () => {
settingsService,
{
fields: true,
indices: true,
templates: true,
indices: false,
templates: false,
},
dataSourceId
);
Expand All @@ -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);
});
});

0 comments on commit 80c83c7

Please sign in to comment.