|
| 1 | +import { describe, expect, it, vi } from 'vitest'; |
| 2 | + |
| 3 | +import { ReactNativeRemote } from '../../src/sync/stream/ReactNativeRemote'; |
| 4 | + |
| 5 | +const connector = { |
| 6 | + fetchCredentials: async () => ({ |
| 7 | + endpoint: 'https://example.com', |
| 8 | + token: 'token' |
| 9 | + }) |
| 10 | +}; |
| 11 | + |
| 12 | +function jsonResponse(body: unknown): Response { |
| 13 | + return { |
| 14 | + ok: true, |
| 15 | + status: 200, |
| 16 | + statusText: 'OK', |
| 17 | + json: async () => body, |
| 18 | + text: async () => JSON.stringify(body) |
| 19 | + } as Response; |
| 20 | +} |
| 21 | + |
| 22 | +describe('ReactNativeRemote', () => { |
| 23 | + it('uses text streaming for non-streaming GET requests', async () => { |
| 24 | + const fetchImplementation = vi.fn(async () => jsonResponse({ ok: true })); |
| 25 | + const remote = new ReactNativeRemote(connector, undefined, { fetchImplementation }); |
| 26 | + |
| 27 | + await remote.get('/write-checkpoint2.json'); |
| 28 | + |
| 29 | + expect(fetchImplementation).toHaveBeenCalledWith( |
| 30 | + 'https://example.com/write-checkpoint2.json', |
| 31 | + expect.objectContaining({ |
| 32 | + method: 'GET', |
| 33 | + reactNative: expect.objectContaining({ |
| 34 | + textStreaming: true |
| 35 | + }) |
| 36 | + }) |
| 37 | + ); |
| 38 | + }); |
| 39 | + |
| 40 | + it('uses text streaming for non-streaming POST requests', async () => { |
| 41 | + const fetchImplementation = vi.fn(async () => jsonResponse({ ok: true })); |
| 42 | + const remote = new ReactNativeRemote(connector, undefined, { fetchImplementation }); |
| 43 | + |
| 44 | + await remote.post('/crud', { op: 'put' }); |
| 45 | + |
| 46 | + expect(fetchImplementation).toHaveBeenCalledWith( |
| 47 | + 'https://example.com/crud', |
| 48 | + expect.objectContaining({ |
| 49 | + method: 'POST', |
| 50 | + reactNative: expect.objectContaining({ |
| 51 | + textStreaming: true |
| 52 | + }) |
| 53 | + }) |
| 54 | + ); |
| 55 | + }); |
| 56 | +}); |
0 commit comments