@@ -3,13 +3,14 @@ import { fetchElasticsearchAction, fetchElasticsearchStatistics, fetchElasticsea
33
44describe ( 'Elasticsearch API' , ( ) => {
55 afterEach ( ( ) => {
6+ vi . useRealTimers ( ) ;
67 vi . restoreAllMocks ( ) ;
78 } ) ;
89
910 describe ( 'fetchElasticsearchAction' , ( ) => {
1011 it ( 'should fetch Elasticsearch action and return JSON response if successful' , async ( ) => {
1112 const mockResponse = { success : true , message : 'Action executed' } ;
12- global . fetch = vi . fn ( ( ) =>
13+ globalThis . fetch = vi . fn ( ( ) =>
1314 Promise . resolve ( {
1415 ok : true ,
1516 json : ( ) => Promise . resolve ( mockResponse ) ,
@@ -20,7 +21,7 @@ describe('Elasticsearch API', () => {
2021 const result = await fetchElasticsearchAction ( action ) ;
2122
2223 expect ( result ) . toEqual ( mockResponse ) ;
23- expect ( global . fetch ) . toHaveBeenCalledWith ( './api/elasticsearch/some-action' , {
24+ expect ( globalThis . fetch ) . toHaveBeenCalledWith ( './api/elasticsearch/some-action' , {
2425 method : 'GET' ,
2526 cache : 'no-cache' ,
2627 headers : {
@@ -33,7 +34,7 @@ describe('Elasticsearch API', () => {
3334
3435 it ( 'should throw an error if fetch fails' , async ( ) => {
3536 const mockError = new Error ( 'Fetch failed' ) ;
36- global . fetch = vi . fn ( ( ) => Promise . reject ( mockError ) ) ;
37+ globalThis . fetch = vi . fn ( ( ) => Promise . reject ( mockError ) ) ;
3738
3839 const action = 'some-action' ;
3940
@@ -60,7 +61,7 @@ describe('Elasticsearch API', () => {
6061 } ,
6162 } ,
6263 } ;
63- global . fetch = vi . fn ( ( ) =>
64+ globalThis . fetch = vi . fn ( ( ) =>
6465 Promise . resolve ( {
6566 ok : true ,
6667 json : ( ) => Promise . resolve ( mockResponse ) ,
@@ -70,7 +71,7 @@ describe('Elasticsearch API', () => {
7071 const result = await fetchElasticsearchStatistics ( ) ;
7172
7273 expect ( result ) . toEqual ( mockResponse ) ;
73- expect ( global . fetch ) . toHaveBeenCalledWith ( './api/elasticsearch/statistics' , {
74+ expect ( globalThis . fetch ) . toHaveBeenCalledWith ( './api/elasticsearch/statistics' , {
7475 method : 'GET' ,
7576 cache : 'no-cache' ,
7677 headers : {
@@ -83,7 +84,7 @@ describe('Elasticsearch API', () => {
8384
8485 it ( 'should throw an error if fetch fails' , async ( ) => {
8586 const mockError = new Error ( 'Fetch failed' ) ;
86- global . fetch = vi . fn ( ( ) => Promise . reject ( mockError ) ) ;
87+ globalThis . fetch = vi . fn ( ( ) => Promise . reject ( mockError ) ) ;
8788
8889 await expect ( fetchElasticsearchStatistics ( ) ) . rejects . toThrow ( mockError ) ;
8990 } ) ;
@@ -92,7 +93,7 @@ describe('Elasticsearch API', () => {
9293 describe ( 'fetchElasticsearchHealthcheck' , ( ) => {
9394 it ( 'should fetch Elasticsearch healthcheck and return JSON response when available' , async ( ) => {
9495 const mockResponse = { available : true , status : 'healthy' } ;
95- global . fetch = vi . fn ( ( ) =>
96+ globalThis . fetch = vi . fn ( ( ) =>
9697 Promise . resolve ( {
9798 ok : true ,
9899 json : ( ) => Promise . resolve ( mockResponse ) ,
@@ -102,20 +103,24 @@ describe('Elasticsearch API', () => {
102103 const result = await fetchElasticsearchHealthcheck ( ) ;
103104
104105 expect ( result ) . toEqual ( mockResponse ) ;
105- expect ( global . fetch ) . toHaveBeenCalledWith ( './api/elasticsearch/healthcheck' , {
106- method : 'GET' ,
107- cache : 'no-cache' ,
108- headers : {
109- 'Content-Type' : 'application/json' ,
110- } ,
111- redirect : 'follow' ,
112- referrerPolicy : 'no-referrer' ,
113- } ) ;
106+ expect ( globalThis . fetch ) . toHaveBeenCalledWith (
107+ './api/elasticsearch/healthcheck' ,
108+ expect . objectContaining ( {
109+ method : 'GET' ,
110+ cache : 'no-cache' ,
111+ headers : {
112+ 'Content-Type' : 'application/json' ,
113+ } ,
114+ redirect : 'follow' ,
115+ referrerPolicy : 'no-referrer' ,
116+ signal : expect . any ( AbortSignal ) ,
117+ } )
118+ ) ;
114119 } ) ;
115120
116121 it ( 'should throw an error when Elasticsearch returns 503 Service Unavailable' , async ( ) => {
117122 const errorResponse = { available : false , status : 'unavailable' } ;
118- global . fetch = vi . fn ( ( ) =>
123+ globalThis . fetch = vi . fn ( ( ) =>
119124 Promise . resolve ( {
120125 ok : false ,
121126 status : 503 ,
@@ -128,7 +133,7 @@ describe('Elasticsearch API', () => {
128133
129134 it ( 'should throw an error with custom message when error data is provided' , async ( ) => {
130135 const errorResponse = { error : 'Connection refused' } ;
131- global . fetch = vi . fn ( ( ) =>
136+ globalThis . fetch = vi . fn ( ( ) =>
132137 Promise . resolve ( {
133138 ok : false ,
134139 status : 503 ,
@@ -141,7 +146,7 @@ describe('Elasticsearch API', () => {
141146
142147 it ( 'should throw an error if fetch fails' , async ( ) => {
143148 const mockError = new Error ( 'Network error' ) ;
144- global . fetch = vi . fn ( ( ) => Promise . reject ( mockError ) ) ;
149+ globalThis . fetch = vi . fn ( ( ) => Promise . reject ( mockError ) ) ;
145150
146151 await expect ( fetchElasticsearchHealthcheck ( ) ) . rejects . toThrow ( mockError ) ;
147152 } ) ;
0 commit comments