Skip to content

Commit f80b41b

Browse files
committed
Add test cases
Signed-off-by: Luna Liu <lunalzm@amazon.com>
1 parent d7eba71 commit f80b41b

File tree

1 file changed

+104
-6
lines changed

1 file changed

+104
-6
lines changed

src/plugins/explore/public/application/utils/state_management/actions/query_actions.test.ts

Lines changed: 104 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import {
1616
import { QueryExecutionStatus } from '../types';
1717
import { setResults } from '../slices';
1818
import { Query, DataView } from 'src/plugins/data/common';
19-
import { DataPublicPluginStart } from '../../../../../../data/public';
2019
import { ExploreServices } from '../../../../types';
2120
import { SAMPLE_SIZE_SETTING } from '../../../../../common';
2221

@@ -178,6 +177,9 @@ describe('Query Actions - Comprehensive Test Suite', () => {
178177
query: {
179178
queryString: {
180179
getQuery: jest.fn().mockReturnValue({ query: '', language: 'PPL' }),
180+
getLanguageService: jest.fn().mockReturnValue({
181+
getLanguage: jest.fn().mockReturnValue({}),
182+
}),
181183
},
182184
filterManager: {
183185
getFilters: jest.fn().mockReturnValue([]),
@@ -488,7 +490,6 @@ describe('Query Actions - Comprehensive Test Suite', () => {
488490
mockBuildPointSeriesData.mockReturnValue([{ x: 1609459200000, y: 5 }] as any);
489491
mockTabifyAggResponse.mockReturnValue({ rows: [], columns: [] } as any);
490492
});
491-
492493
it('should process histogram data when timeFieldName exists', () => {
493494
const rawResults = {
494495
hits: {
@@ -793,7 +794,34 @@ describe('Query Actions - Comprehensive Test Suite', () => {
793794
// Using mockCreateHistogramConfigs from module scope
794795
mockCreateHistogramConfigs.mockReturnValue({
795796
toDsl: jest.fn().mockReturnValue({}),
797+
aggs: [
798+
{} as any,
799+
{
800+
buckets: {
801+
getInterval: jest.fn(() => ({ interval: '1h', scale: 1 })),
802+
},
803+
} as any,
804+
],
796805
} as any);
806+
mockSearchSource.fetch.mockReset();
807+
mockSearchSource.fetch.mockResolvedValue({
808+
hits: {
809+
hits: [
810+
{ _id: '1', _source: { field1: 'value1' } },
811+
{ _id: '2', _source: { field2: 'value2' } },
812+
],
813+
total: 2,
814+
},
815+
took: 5,
816+
aggregations: {
817+
histogram: {
818+
buckets: [
819+
{ key: 1609459200000, doc_count: 5 },
820+
{ key: 1609462800000, doc_count: 3 },
821+
],
822+
},
823+
},
824+
});
797825
});
798826

799827
it('should execute histogram query with custom interval', async () => {
@@ -808,6 +836,17 @@ describe('Query Actions - Comprehensive Test Suite', () => {
808836

809837
expect(mockServices.data.dataViews.get).toHaveBeenCalled();
810838
expect(mockSearchSource.fetch).toHaveBeenCalled();
839+
expect(mockDispatch).toHaveBeenCalledWith(
840+
expect.objectContaining({
841+
type: 'queryEditor/setIndividualQueryStatus',
842+
payload: expect.objectContaining({
843+
cacheKey: 'test-cache-key',
844+
status: expect.objectContaining({
845+
status: QueryExecutionStatus.READY,
846+
}),
847+
}),
848+
})
849+
);
811850
});
812851

813852
it('should handle missing interval gracefully', async () => {
@@ -820,6 +859,18 @@ describe('Query Actions - Comprehensive Test Suite', () => {
820859
await thunk(mockDispatch, mockGetState, undefined);
821860

822861
expect(mockServices.data.dataViews.get).toHaveBeenCalled();
862+
expect(mockSearchSource.fetch).toHaveBeenCalled();
863+
expect(mockDispatch).toHaveBeenCalledWith(
864+
expect.objectContaining({
865+
type: 'queryEditor/setIndividualQueryStatus',
866+
payload: expect.objectContaining({
867+
cacheKey: 'test-cache-key',
868+
status: expect.objectContaining({
869+
status: QueryExecutionStatus.READY,
870+
}),
871+
}),
872+
})
873+
);
823874
});
824875

825876
it('should handle dataView without timeFieldName', async () => {
@@ -834,16 +885,32 @@ describe('Query Actions - Comprehensive Test Suite', () => {
834885

835886
const thunk = executeHistogramQuery(params);
836887
await thunk(mockDispatch, mockGetState, undefined);
837-
888+
expect(mockServices.data.dataViews.get).toHaveBeenCalled();
838889
expect(mockSearchSource.fetch).toHaveBeenCalled();
890+
expect(mockDispatch).toHaveBeenCalledWith(
891+
expect.objectContaining({
892+
type: 'queryEditor/setIndividualQueryStatus',
893+
payload: expect.objectContaining({
894+
cacheKey: 'test-cache-key',
895+
status: expect.objectContaining({
896+
status: QueryExecutionStatus.READY,
897+
}),
898+
}),
899+
})
900+
);
839901
});
840902

841903
it('should handle search errors gracefully', async () => {
842904
const error = {
843905
body: {
844906
error: 'Search failed',
845-
message:
846-
'{"error":{"details":"Query syntax error","reason":"Invalid query","type":"parsing_exception"}}',
907+
message: JSON.stringify({
908+
error: {
909+
details: 'Query syntax error',
910+
reason: 'Invalid query',
911+
type: 'parsing_exception',
912+
},
913+
}),
847914
statusCode: 400,
848915
},
849916
};
@@ -866,11 +933,20 @@ describe('Query Actions - Comprehensive Test Suite', () => {
866933
// Verify error status is set in Redux
867934
expect(mockDispatch).toHaveBeenCalledWith(
868935
expect.objectContaining({
869-
type: expect.stringContaining('setIndividualQueryStatus'),
936+
type: 'queryEditor/setIndividualQueryStatus',
870937
payload: expect.objectContaining({
871938
cacheKey: 'test-cache-key',
872939
status: expect.objectContaining({
873940
status: QueryExecutionStatus.ERROR,
941+
error: expect.objectContaining({
942+
error: 'Search failed',
943+
message: {
944+
details: 'Query syntax error',
945+
reason: 'Invalid query',
946+
type: 'parsing_exception',
947+
},
948+
statusCode: 400,
949+
}),
874950
}),
875951
}),
876952
})
@@ -917,6 +993,17 @@ describe('Query Actions - Comprehensive Test Suite', () => {
917993

918994
const mockIndexPatterns = dataPublicModule.indexPatterns as any;
919995
mockIndexPatterns.isDefault.mockReturnValue(true);
996+
mockSearchSource.fetch.mockReset();
997+
mockSearchSource.fetch.mockResolvedValue({
998+
hits: {
999+
hits: [
1000+
{ _id: '1', _source: { field1: 'value1' } },
1001+
{ _id: '2', _source: { field2: 'value2' } },
1002+
],
1003+
total: 2,
1004+
},
1005+
took: 5,
1006+
});
9201007
});
9211008

9221009
it('should execute tab query successfully', async () => {
@@ -931,6 +1018,17 @@ describe('Query Actions - Comprehensive Test Suite', () => {
9311018
expect(mockServices.data.dataViews.get).toHaveBeenCalled();
9321019
expect(mockSearchSource.fetch).toHaveBeenCalled();
9331020
expect(setResults).toHaveBeenCalled();
1021+
expect(mockDispatch).toHaveBeenCalledWith(
1022+
expect.objectContaining({
1023+
type: 'queryEditor/setIndividualQueryStatus',
1024+
payload: expect.objectContaining({
1025+
cacheKey: 'test-cache-key',
1026+
status: expect.objectContaining({
1027+
status: QueryExecutionStatus.READY,
1028+
}),
1029+
}),
1030+
})
1031+
);
9341032
});
9351033

9361034
it('should handle missing services gracefully', async () => {

0 commit comments

Comments
 (0)