@@ -32,19 +32,33 @@ describe('retrieveQueryById - Fetch Query Record by ID from API', () => {
32
32
} ,
33
33
} ;
34
34
35
- it ( 'should make three GET requests to fetch different query records ' , async ( ) => {
35
+ it ( 'should fetch only once if the first call returns a valid query ' , async ( ) => {
36
36
mockCore . http . get . mockResolvedValue ( mockResponse ) ;
37
37
38
38
await retrieveQueryById ( mockCore , undefined , testStart , testEnd , testId ) ;
39
39
40
- expect ( mockCore . http . get ) . toHaveBeenCalledTimes ( 3 ) ;
40
+ expect ( mockCore . http . get ) . toHaveBeenCalledTimes ( 1 ) ;
41
41
expect ( mockCore . http . get ) . toHaveBeenCalledWith ( '/api/top_queries/latency' , {
42
42
query : { from : testStart , to : testEnd , id : testId , dataSourceId : undefined } ,
43
43
} ) ;
44
- expect ( mockCore . http . get ) . toHaveBeenCalledWith ( '/api/top_queries/cpu' , {
44
+ } ) ;
45
+
46
+ it ( 'should try the next endpoints if the first call returns empty' , async ( ) => {
47
+ mockCore . http . get
48
+ . mockResolvedValueOnce ( { response : { top_queries : [ ] } } ) // latency - empty
49
+ . mockResolvedValueOnce ( { response : { top_queries : [ ] } } ) // cpu - empty
50
+ . mockResolvedValueOnce ( mockResponse ) ; // memory - found
51
+
52
+ await retrieveQueryById ( mockCore , undefined , testStart , testEnd , testId ) ;
53
+
54
+ expect ( mockCore . http . get ) . toHaveBeenCalledTimes ( 3 ) ;
55
+ expect ( mockCore . http . get ) . toHaveBeenNthCalledWith ( 1 , '/api/top_queries/latency' , {
56
+ query : { from : testStart , to : testEnd , id : testId , dataSourceId : undefined } ,
57
+ } ) ;
58
+ expect ( mockCore . http . get ) . toHaveBeenNthCalledWith ( 2 , '/api/top_queries/cpu' , {
45
59
query : { from : testStart , to : testEnd , id : testId , dataSourceId : undefined } ,
46
60
} ) ;
47
- expect ( mockCore . http . get ) . toHaveBeenCalledWith ( '/api/top_queries/memory' , {
61
+ expect ( mockCore . http . get ) . toHaveBeenNthCalledWith ( 3 , '/api/top_queries/memory' , {
48
62
query : { from : testStart , to : testEnd , id : testId , dataSourceId : undefined } ,
49
63
} ) ;
50
64
} ) ;
@@ -57,42 +71,39 @@ describe('retrieveQueryById - Fetch Query Record by ID from API', () => {
57
71
expect ( result ) . toEqual ( mockResponse . response . top_queries [ 0 ] ) ;
58
72
} ) ;
59
73
60
- it ( 'should return null if no queries are found ' , async ( ) => {
74
+ it ( 'should return null if all responses are empty ' , async ( ) => {
61
75
mockCore . http . get . mockResolvedValue ( { response : { top_queries : [ ] } } ) ;
62
76
63
77
const result = await retrieveQueryById ( mockCore , undefined , testStart , testEnd , testId ) ;
64
78
65
79
expect ( result ) . toBeNull ( ) ;
80
+ expect ( mockCore . http . get ) . toHaveBeenCalledTimes ( 3 ) ;
66
81
} ) ;
67
- it ( 'should return null if API response is missing the response field' , async ( ) => {
82
+
83
+ it ( 'should return null if all API responses are missing response field' , async ( ) => {
68
84
mockCore . http . get . mockResolvedValue ( { } ) ;
69
85
70
86
const result = await retrieveQueryById ( mockCore , undefined , testStart , testEnd , testId ) ;
71
87
72
88
expect ( result ) . toBeNull ( ) ;
89
+ expect ( mockCore . http . get ) . toHaveBeenCalledTimes ( 3 ) ;
73
90
} ) ;
74
91
75
- it ( 'should return null if API response contains an unexpected structure' , async ( ) => {
92
+ it ( 'should return null if all API responses have unexpected structure' , async ( ) => {
76
93
mockCore . http . get . mockResolvedValue ( { unexpectedKey : { } } ) ;
77
94
78
95
const result = await retrieveQueryById ( mockCore , undefined , testStart , testEnd , testId ) ;
79
96
80
97
expect ( result ) . toBeNull ( ) ;
98
+ expect ( mockCore . http . get ) . toHaveBeenCalledTimes ( 3 ) ;
81
99
} ) ;
82
100
83
- it ( 'should return null if API request fails ' , async ( ) => {
101
+ it ( 'should return null if all API requests fail ' , async ( ) => {
84
102
mockCore . http . get . mockRejectedValue ( new Error ( 'API error' ) ) ;
85
103
86
104
const result = await retrieveQueryById ( mockCore , undefined , testStart , testEnd , testId ) ;
87
105
88
106
expect ( result ) . toBeNull ( ) ;
89
- } ) ;
90
-
91
- it ( 'should handle cases where API returns an empty object instead of expected response structure' , async ( ) => {
92
- mockCore . http . get . mockResolvedValue ( { } ) ;
93
-
94
- const result = await retrieveQueryById ( mockCore , undefined , testStart , testEnd , testId ) ;
95
-
96
- expect ( result ) . toBeNull ( ) ;
107
+ expect ( mockCore . http . get ) . toHaveBeenCalledTimes ( 3 ) ;
97
108
} ) ;
98
109
} ) ;
0 commit comments