Skip to content

Commit 5aa9dd3

Browse files
committed
test(realtime): add more testing
1 parent 1343f18 commit 5aa9dd3

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

packages/core/realtime-js/test/RealtimeClient.test.ts

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,70 @@ describe('Additional Coverage Tests', () => {
146146
global.fetch = originalFetch
147147
})
148148

149+
test('should use native fetch by default', () => {
150+
// Verify fetch exists (Node 20+ requirement)
151+
expect(typeof global.fetch).toBe('function')
152+
153+
const socket = new RealtimeClient(testSetup.url, {
154+
params: { apikey: '123456789' },
155+
})
156+
157+
const resolvedFetch = socket._resolveFetch()
158+
expect(typeof resolvedFetch).toBe('function')
159+
})
160+
161+
test('should use global fetch when available and no custom fetch provided', () => {
162+
// Mock global fetch
163+
const mockGlobalFetch = vi.fn().mockResolvedValue({ ok: true })
164+
global.fetch = mockGlobalFetch
165+
166+
const socket = new RealtimeClient(testSetup.url, {
167+
params: { apikey: '123456789' },
168+
})
169+
170+
// The fetch property should be a function that wraps global fetch
171+
expect(typeof socket.fetch).toBe('function')
172+
173+
// Call the fetch function
174+
socket.fetch('https://example.com', { method: 'POST' })
175+
176+
// Verify global fetch was called
177+
expect(mockGlobalFetch).toHaveBeenCalledWith('https://example.com', { method: 'POST' })
178+
179+
// Test _resolveFetch without custom fetch
180+
const resolvedFetch = socket._resolveFetch()
181+
resolvedFetch('https://test.com')
182+
expect(mockGlobalFetch).toHaveBeenCalledWith('https://test.com')
183+
})
184+
185+
test('should prioritize custom fetch over global fetch', () => {
186+
// Mock both global and custom fetch
187+
const mockGlobalFetch = vi.fn().mockResolvedValue({ ok: false })
188+
const mockCustomFetch = vi.fn().mockResolvedValue({ ok: true })
189+
global.fetch = mockGlobalFetch
190+
191+
const socket = new RealtimeClient(testSetup.url, {
192+
params: { apikey: '123456789' },
193+
fetch: mockCustomFetch,
194+
})
195+
196+
// The fetch property should use custom fetch
197+
socket.fetch('https://example.com')
198+
199+
// Verify custom fetch was called, not global
200+
expect(mockCustomFetch).toHaveBeenCalledWith('https://example.com')
201+
expect(mockGlobalFetch).not.toHaveBeenCalled()
202+
203+
// Test _resolveFetch with custom fetch parameter
204+
const anotherCustomFetch = vi.fn().mockResolvedValue({ ok: true })
205+
const resolvedFetch = socket._resolveFetch(anotherCustomFetch)
206+
resolvedFetch('https://test.com')
207+
208+
// Should use the fetch passed to _resolveFetch
209+
expect(anotherCustomFetch).toHaveBeenCalledWith('https://test.com')
210+
expect(mockCustomFetch).toHaveBeenCalledTimes(1) // Only from earlier call
211+
expect(mockGlobalFetch).not.toHaveBeenCalled()
212+
})
149213
})
150214

151215
describe('_leaveOpenTopic', () => {

0 commit comments

Comments
 (0)