1+ import { AxiosError } from 'axios' ;
12import { describe , expect , test , vi } from 'vitest' ;
23
34import Instance from '@/services/instance' ;
@@ -30,6 +31,8 @@ describe('Instance', () => {
3031
3132 const instance = new Instance ( {
3233 id : 'id' ,
34+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
35+ // @ts -expect-error
3336 registration : {
3437 metadata : {
3538 [ 'hide-url' ] : metadataHideUrl ,
@@ -40,4 +43,142 @@ describe('Instance', () => {
4043 expect ( instance . showUrl ( ) ) . toEqual ( expectUrlToBeShownOnUI ) ;
4144 } ,
4245 ) ;
46+
47+ describe ( 'fetchMetric' , ( ) => {
48+ const instance = new Instance ( {
49+ id : 'test-id' ,
50+ registration : {
51+ name : 'test' ,
52+ healthUrl : '' ,
53+ source : '' ,
54+ } ,
55+ availableMetrics : [ 'test.metric' , 'cache.size' , 'cache.gets' ] ,
56+ } ) ;
57+ test ( 'should pass suppressToast option to axios config' , async ( ) => {
58+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
59+ // @ts -expect-error
60+ // Spy on axios.get
61+ const axiosGetSpy = vi . spyOn ( instance . axios , 'get' ) ;
62+
63+ // Mock the axios request
64+ axiosGetSpy . mockResolvedValue ( {
65+ data : {
66+ measurements : [ { value : 42 } ] ,
67+ } ,
68+ } ) ;
69+
70+ await instance . fetchMetric (
71+ 'test.metric' ,
72+ { tag : 'value' } ,
73+ {
74+ suppressToast : true ,
75+ } ,
76+ ) ;
77+
78+ // Verify suppressToast was passed in config
79+ expect ( axiosGetSpy ) . toHaveBeenCalledWith (
80+ expect . stringContaining ( 'actuator/metrics/test.metric' ) ,
81+ expect . objectContaining ( {
82+ suppressToast : true ,
83+ } ) ,
84+ ) ;
85+ } ) ;
86+
87+ test ( 'should work without options parameter for backward compatibility' , async ( ) => {
88+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
89+ // @ts -expect-error
90+ const axiosGetSpy = vi . spyOn ( instance . axios , 'get' ) ;
91+
92+ axiosGetSpy . mockResolvedValue ( {
93+ data : {
94+ measurements : [ { value : 42 } ] ,
95+ } ,
96+ } ) ;
97+
98+ await instance . fetchMetric ( 'test.metric' , { tag : 'value' } ) ;
99+
100+ // Verify it was called without suppressToast
101+ expect ( axiosGetSpy ) . toHaveBeenCalledWith (
102+ expect . stringContaining ( 'actuator/metrics/test.metric' ) ,
103+ expect . objectContaining ( {
104+ suppressToast : undefined ,
105+ } ) ,
106+ ) ;
107+ } ) ;
108+
109+ test ( 'should pass suppressToast=false when explicitly set to false' , async ( ) => {
110+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
111+ // @ts -expect-error
112+ const axiosGetSpy = vi . spyOn ( instance . axios , 'get' ) ;
113+
114+ axiosGetSpy . mockResolvedValue ( {
115+ data : {
116+ measurements : [ { value : 42 } ] ,
117+ } ,
118+ } ) ;
119+
120+ await instance . fetchMetric (
121+ 'test.metric' ,
122+ { tag : 'value' } ,
123+ {
124+ suppressToast : false ,
125+ } ,
126+ ) ;
127+
128+ expect ( axiosGetSpy ) . toHaveBeenCalledWith (
129+ expect . stringContaining ( 'actuator/metrics/test.metric' ) ,
130+ expect . objectContaining ( {
131+ suppressToast : false ,
132+ } ) ,
133+ ) ;
134+ } ) ;
135+
136+ test ( 'should include tags in request parameters' , async ( ) => {
137+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
138+ // @ts -expect-error
139+ const axiosGetSpy = vi . spyOn ( instance . axios , 'get' ) ;
140+
141+ axiosGetSpy . mockResolvedValue ( {
142+ data : {
143+ measurements : [ { value : 42 } ] ,
144+ } ,
145+ } ) ;
146+
147+ await instance . fetchMetric ( 'cache.gets' , {
148+ name : 'my-cache' ,
149+ result : 'hit' ,
150+ } ) ;
151+
152+ const callArgs = axiosGetSpy . mock . calls [ 0 ] ;
153+ const params = callArgs [ 1 ] ?. params as URLSearchParams ;
154+
155+ expect ( params ) . toBeInstanceOf ( URLSearchParams ) ;
156+ expect ( params . getAll ( 'tag' ) ) . toContain ( 'name:my-cache' ) ;
157+ expect ( params . getAll ( 'tag' ) ) . toContain ( 'result:hit' ) ;
158+ } ) ;
159+
160+ test ( 'should pass suppressToast function to axios config' , async ( ) => {
161+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
162+ // @ts -expect-error
163+ const axiosGetSpy = vi . spyOn ( instance . axios , 'get' ) ;
164+
165+ axiosGetSpy . mockResolvedValue ( {
166+ data : {
167+ measurements : [ { value : 42 } ] ,
168+ } ,
169+ } ) ;
170+
171+ const suppressFn = ( err : AxiosError ) => err . response ?. status === 404 ;
172+ await instance . fetchMetric (
173+ 'cache.size' ,
174+ { } ,
175+ { suppressToast : suppressFn } ,
176+ ) ;
177+
178+ expect ( axiosGetSpy ) . toHaveBeenCalledWith (
179+ expect . any ( String ) ,
180+ expect . objectContaining ( { suppressToast : suppressFn } ) ,
181+ ) ;
182+ } ) ;
183+ } ) ;
43184} ) ;
0 commit comments