@@ -92,7 +92,7 @@ describe('handleUpdateInformation', () => {
9292
9393 it ( 'should do nothing when URL does not end with /update' , async ( ) => {
9494 Object . defineProperty ( window , 'location' , {
95- value : { href : 'http://localhost/admin' } ,
95+ value : { href : 'http://localhost/admin' , pathname : '/admin' } ,
9696 writable : true ,
9797 } ) ;
9898
@@ -105,7 +105,7 @@ describe('handleUpdateInformation', () => {
105105
106106 it ( 'should do nothing when installed version input is missing' , async ( ) => {
107107 Object . defineProperty ( window , 'location' , {
108- value : { href : 'http://localhost/update' } ,
108+ value : { href : 'http://localhost/update' , pathname : '/update' } ,
109109 writable : true ,
110110 } ) ;
111111
@@ -119,7 +119,7 @@ describe('handleUpdateInformation', () => {
119119
120120 it ( 'should show success alert and enable button on successful check' , async ( ) => {
121121 Object . defineProperty ( window , 'location' , {
122- value : { href : 'http://localhost/update' } ,
122+ value : { href : 'http://localhost/update' , pathname : '/update' } ,
123123 writable : true ,
124124 } ) ;
125125
@@ -153,9 +153,9 @@ describe('handleUpdateInformation', () => {
153153 expect ( button . disabled ) . toBe ( false ) ;
154154 } ) ;
155155
156- it ( 'should show error alert on failed check response' , async ( ) => {
156+ it ( 'should show error alert on failed check response with JSON content type ' , async ( ) => {
157157 Object . defineProperty ( window , 'location' , {
158- value : { href : 'http://localhost/update' } ,
158+ value : { href : 'http://localhost/update' , pathname : '/update' } ,
159159 writable : true ,
160160 } ) ;
161161
@@ -167,6 +167,7 @@ describe('handleUpdateInformation', () => {
167167
168168 global . fetch = vi . fn ( ) . mockResolvedValue ( {
169169 ok : false ,
170+ headers : { get : ( name : string ) => ( name === 'content-type' ? 'application/json' : null ) } ,
170171 json : ( ) => Promise . resolve ( { message : 'Version mismatch' } ) ,
171172 } ) ;
172173
@@ -179,9 +180,9 @@ describe('handleUpdateInformation', () => {
179180 expect ( result ?. innerText ) . toBe ( 'Version mismatch' ) ;
180181 } ) ;
181182
182- it ( 'should show default error message when response has no message' , async ( ) => {
183+ it ( 'should show default error message when JSON response has no message' , async ( ) => {
183184 Object . defineProperty ( window , 'location' , {
184- value : { href : 'http://localhost/update' } ,
185+ value : { href : 'http://localhost/update' , pathname : '/update' } ,
185186 writable : true ,
186187 } ) ;
187188
@@ -193,6 +194,7 @@ describe('handleUpdateInformation', () => {
193194
194195 global . fetch = vi . fn ( ) . mockResolvedValue ( {
195196 ok : false ,
197+ headers : { get : ( name : string ) => ( name === 'content-type' ? 'application/json' : null ) } ,
196198 json : ( ) => Promise . resolve ( { } ) ,
197199 } ) ;
198200
@@ -202,9 +204,9 @@ describe('handleUpdateInformation', () => {
202204 expect ( result ?. innerText ) . toBe ( 'Update check failed' ) ;
203205 } ) ;
204206
205- it ( 'should show server config error on SyntaxError ' , async ( ) => {
207+ it ( 'should show server config error on non-JSON Not Found response ' , async ( ) => {
206208 Object . defineProperty ( window , 'location' , {
207- value : { href : 'http://localhost/update' } ,
209+ value : { href : 'http://localhost/update' , pathname : '/update' } ,
208210 writable : true ,
209211 } ) ;
210212
@@ -214,7 +216,11 @@ describe('handleUpdateInformation', () => {
214216 <div id="phpmyfaq-update-check-result"></div>
215217 ` ;
216218
217- global . fetch = vi . fn ( ) . mockRejectedValue ( new SyntaxError ( 'Unexpected token' ) ) ;
219+ global . fetch = vi . fn ( ) . mockResolvedValue ( {
220+ ok : false ,
221+ headers : { get : ( ) => 'text/html' } ,
222+ text : ( ) => Promise . resolve ( 'Not Found' ) ,
223+ } ) ;
218224
219225 await handleUpdateInformation ( ) ;
220226
@@ -223,9 +229,9 @@ describe('handleUpdateInformation', () => {
223229 expect ( result ?. innerText ) . toContain ( 'RewriteBase' ) ;
224230 } ) ;
225231
226- it ( 'should show generic error message on other errors ' , async ( ) => {
232+ it ( 'should show connection error message on network failure ' , async ( ) => {
227233 Object . defineProperty ( window , 'location' , {
228- value : { href : 'http://localhost/update' } ,
234+ value : { href : 'http://localhost/update' , pathname : '/update' } ,
229235 writable : true ,
230236 } ) ;
231237
@@ -240,12 +246,12 @@ describe('handleUpdateInformation', () => {
240246 await handleUpdateInformation ( ) ;
241247
242248 const result = document . getElementById ( 'phpmyfaq-update-check-result' ) ;
243- expect ( result ?. innerText ) . toBe ( 'Network failure ') ;
249+ expect ( result ?. innerText ) . toContain ( 'Could not connect to the update API ') ;
244250 } ) ;
245251
246252 it ( 'should work with URL ending in /update/' , async ( ) => {
247253 Object . defineProperty ( window , 'location' , {
248- value : { href : 'http://localhost/update/' } ,
254+ value : { href : 'http://localhost/update/' , pathname : '/update/' } ,
249255 writable : true ,
250256 } ) ;
251257
0 commit comments