-
Notifications
You must be signed in to change notification settings - Fork 190
Expand file tree
/
Copy pathremoteConfigurationFetch.spec.ts
More file actions
77 lines (61 loc) · 2.79 KB
/
Copy pathremoteConfigurationFetch.spec.ts
File metadata and controls
77 lines (61 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import { interceptRequests } from '@datadog/browser-core/test'
import { fetchRemoteConfiguration } from './remoteConfigurationFetch'
describe('fetchRemoteConfiguration', () => {
const options = { site: 'datadoghq.com', remoteConfigurationId: 'test-id' }
let interceptor: ReturnType<typeof interceptRequests>
beforeEach(() => {
interceptor = interceptRequests()
})
it('returns ok:true with the parsed config on success', async () => {
const config = { rum: { applicationId: 'abc', sessionSampleRate: 50 } }
interceptor.withFetch(() =>
Promise.resolve({
ok: true,
json: () => Promise.resolve(config),
})
)
const result = await fetchRemoteConfiguration(options)
expect(result).toEqual({ ok: true, value: config })
})
it('returns ok:false on HTTP error (non-ok response)', async () => {
interceptor.withFetch(() => Promise.resolve({ ok: false, status: 404 }))
const result = await fetchRemoteConfiguration(options)
expect(result.ok).toBeFalse()
expect((result as { ok: false; error: Error }).error).toBeInstanceOf(Error)
})
it('returns ok:false on network failure (fetch throws)', async () => {
interceptor.withFetch(() => Promise.reject(new Error('Network error')))
const result = await fetchRemoteConfiguration(options)
expect(result.ok).toBeFalse()
expect((result as { ok: false; error: Error }).error).toBeInstanceOf(Error)
})
it('returns ok:false when response body is not valid JSON', async () => {
interceptor.withFetch(() =>
Promise.resolve({
ok: true,
json: () => Promise.reject(new SyntaxError('Unexpected end of JSON input')),
})
)
const result = await fetchRemoteConfiguration(options)
expect(result.ok).toBeFalse()
expect((result as { ok: false; error: Error }).error).toBeInstanceOf(Error)
})
it('removes the window registry entry after all fetches settle', async () => {
const config = { rum: { applicationId: 'abc', sessionSampleRate: 50 } }
interceptor.withFetch(() => Promise.resolve({ ok: true, json: () => Promise.resolve(config) }))
await fetchRemoteConfiguration(options)
expect((window as unknown as Record<string, unknown>).__ddRcInflight).toBeUndefined()
})
it('deduplicates concurrent calls for the same endpoint', async () => {
let fetchCount = 0
const config = { rum: { applicationId: 'abc', sessionSampleRate: 50 } }
interceptor.withFetch(() => {
fetchCount++
return Promise.resolve({ ok: true, json: () => Promise.resolve(config) })
})
const [result1, result2] = await Promise.all([fetchRemoteConfiguration(options), fetchRemoteConfiguration(options)])
expect(fetchCount).toBe(1)
expect(result1).toEqual({ ok: true, value: config })
expect(result2).toEqual({ ok: true, value: config })
})
})