1
1
import { describe , expect , it , mock , spyOn } from 'bun:test'
2
- import { loadConfig } from '../src/browser'
2
+ import { isBrowser , loadConfig } from '../src/browser'
3
3
4
4
describe ( 'browser' , ( ) => {
5
5
it ( 'should detect browser environment correctly' , ( ) => {
6
+ // Store original window and fetch
7
+ const originalWindow = globalThis . window
8
+ const originalFetch = globalThis . fetch
9
+
10
+ // Test non-browser environment
11
+ // @ts -expect-error - mocking window
12
+ globalThis . window = undefined
13
+ // @ts -expect-error - mocking fetch
14
+ globalThis . fetch = undefined
15
+ expect ( isBrowser ( ) ) . toBe ( false )
16
+
17
+ // Test browser environment with fetch
18
+ // @ts -expect-error - mocking window
19
+ globalThis . window = { }
20
+ globalThis . fetch = ( ) => Promise . resolve ( new Response ( ) )
21
+ expect ( isBrowser ( ) ) . toBe ( true )
22
+
23
+ // Restore originals
24
+ globalThis . window = originalWindow
25
+ globalThis . fetch = originalFetch
26
+ } )
27
+
28
+ it ( 'should handle browser environment' , async ( ) => {
6
29
// Mock window to simulate browser environment
7
30
const originalWindow = globalThis . window
8
31
// @ts -expect-error - mocking window
@@ -18,13 +41,20 @@ describe('browser', () => {
18
41
globalThis . fetch = mockFetch
19
42
20
43
const defaultConfig = { port : 3000 , host : 'localhost' }
21
- const result = loadConfig ( {
44
+ const result = await loadConfig ( {
22
45
name : 'test-app' ,
23
46
endpoint : '/api/config' ,
24
47
defaultConfig,
25
48
} )
26
49
27
- expect ( result ) . resolves . toEqual ( { port : 3000 , host : 'api-host' } )
50
+ expect ( result ) . toEqual ( { port : 3000 , host : 'api-host' } )
51
+ expect ( mockFetch ) . toHaveBeenCalledWith ( '/api/config' , {
52
+ method : 'GET' ,
53
+ headers : {
54
+ 'Accept' : 'application/json' ,
55
+ 'Content-Type' : 'application/json' ,
56
+ } ,
57
+ } )
28
58
29
59
// Restore window
30
60
globalThis . window = originalWindow
@@ -38,6 +68,7 @@ describe('browser', () => {
38
68
39
69
const consoleSpy = spyOn ( console , 'warn' )
40
70
const defaultConfig = { port : 3000 , host : 'localhost' }
71
+
41
72
const result = await loadConfig ( {
42
73
name : 'test-app' ,
43
74
defaultConfig,
@@ -51,6 +82,62 @@ describe('browser', () => {
51
82
consoleSpy . mockRestore ( )
52
83
} )
53
84
85
+ it ( 'should handle browser fetch errors' , async ( ) => {
86
+ // Mock window to simulate browser environment
87
+ const originalWindow = globalThis . window
88
+ // @ts -expect-error - mocking window
89
+ globalThis . window = { }
90
+
91
+ const consoleSpy = spyOn ( console , 'error' )
92
+ const mockFetch = mock ( ( ) => Promise . reject ( new Error ( 'Network error' ) ) )
93
+ globalThis . fetch = mockFetch
94
+
95
+ const defaultConfig = { port : 3000 , host : 'localhost' }
96
+ const result = await loadConfig ( {
97
+ name : 'test-app' ,
98
+ endpoint : '/api/config' ,
99
+ defaultConfig,
100
+ } )
101
+
102
+ expect ( result ) . toEqual ( defaultConfig )
103
+ expect ( consoleSpy ) . toHaveBeenCalledWith ( 'Failed to load client config:' , expect . any ( Error ) )
104
+
105
+ // Restore window
106
+ globalThis . window = originalWindow
107
+ consoleSpy . mockRestore ( )
108
+ } )
109
+
110
+ it ( 'should handle non-200 responses' , async ( ) => {
111
+ // Mock window to simulate browser environment
112
+ const originalWindow = globalThis . window
113
+ // @ts -expect-error - mocking window
114
+ globalThis . window = { }
115
+
116
+ const consoleSpy = spyOn ( console , 'error' )
117
+ const mockFetch = mock ( ( ) =>
118
+ Promise . resolve ( {
119
+ ok : false ,
120
+ status : 404 ,
121
+ } ) ,
122
+ )
123
+ // @ts -expect-error - mocking fetch
124
+ globalThis . fetch = mockFetch
125
+
126
+ const defaultConfig = { port : 3000 , host : 'localhost' }
127
+ const result = await loadConfig ( {
128
+ name : 'test-app' ,
129
+ endpoint : '/api/config' ,
130
+ defaultConfig,
131
+ } )
132
+
133
+ expect ( result ) . toEqual ( defaultConfig )
134
+ expect ( consoleSpy ) . toHaveBeenCalledWith ( 'Failed to load client config:' , expect . any ( Error ) )
135
+
136
+ // Restore window
137
+ globalThis . window = originalWindow
138
+ consoleSpy . mockRestore ( )
139
+ } )
140
+
54
141
it ( 'should handle custom headers correctly' , async ( ) => {
55
142
// Mock window to simulate browser environment
56
143
const originalWindow = globalThis . window
@@ -99,6 +186,7 @@ describe('browser', () => {
99
186
// @ts -expect-error - mocking window
100
187
globalThis . window = { }
101
188
189
+ const consoleSpy = spyOn ( console , 'error' )
102
190
const mockFetch = mock ( ( ) =>
103
191
Promise . resolve ( {
104
192
ok : true ,
@@ -116,8 +204,10 @@ describe('browser', () => {
116
204
} )
117
205
118
206
expect ( result ) . toEqual ( defaultConfig )
207
+ expect ( consoleSpy ) . toHaveBeenCalledWith ( 'Failed to load client config:' , expect . any ( Error ) )
119
208
120
209
// Restore window
121
210
globalThis . window = originalWindow
211
+ consoleSpy . mockRestore ( )
122
212
} )
123
213
} )
0 commit comments