Skip to content

Commit

Permalink
adding unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Sean Li <[email protected]>
  • Loading branch information
sejli committed Nov 27, 2024
1 parent e3b8ae8 commit 646cca5
Showing 1 changed file with 58 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,37 @@ jest.mock('./use_index_pattern', () => ({
useIndexPattern: jest.fn(),
}));

const mockQuery = {
query: 'test query',
language: 'test language',
};

const mockDefaultQuery = {
query: 'default query',
language: 'default language',
};

const mockSavedSearch = {
id: 'test-saved-search',
title: 'Test Saved Search',
searchSource: {
setField: jest.fn(),
getField: jest.fn(),
getField: jest.fn().mockReturnValue(mockQuery),
fetch: jest.fn(),
getSearchRequestBody: jest.fn().mockResolvedValue({}),
getOwnField: jest.fn(),
getDataFrame: jest.fn(() => ({ name: 'test-pattern' })),
},
getFullPath: jest.fn(),
getOpenSearchType: jest.fn(),
};

const mockSavedSearchEmptyQuery = {
id: 'test-saved-search',
title: 'Test Saved Search',
searchSource: {
setField: jest.fn(),
getField: jest.fn().mockReturnValue(undefined),
fetch: jest.fn(),
getSearchRequestBody: jest.fn().mockResolvedValue({}),
getOwnField: jest.fn(),
Expand Down Expand Up @@ -215,4 +240,36 @@ describe('useSearch', () => {
expect.objectContaining({ status: ResultStatus.LOADING, rows: [] })
);
});

it('should load saved search', async () => {
const services = createMockServices();
services.data.query.queryString.setQuery = jest.fn();

const { waitForNextUpdate } = renderHook(() => useSearch(services), {
wrapper,
});

await act(async () => {
await waitForNextUpdate();
});

expect(services.data.query.queryString.setQuery).toBeCalledWith(mockQuery);
});

it('if no saved search, use get query', async () => {
const services = createMockServices();
services.getSavedSearchById = jest.fn().mockResolvedValue(mockSavedSearchEmptyQuery);
services.data.query.queryString.getQuery = jest.fn().mockReturnValue(mockDefaultQuery);
services.data.query.queryString.setQuery = jest.fn();

const { waitForNextUpdate } = renderHook(() => useSearch(services), {
wrapper,
});

await act(async () => {
await waitForNextUpdate();
});

expect(services.data.query.queryString.setQuery).toBeCalledWith(mockDefaultQuery);
});
});

0 comments on commit 646cca5

Please sign in to comment.