Skip to content

Commit 346bd91

Browse files
committed
fix: support react native SDK
fix: support react native SDK fix: test fail on node:e2e test: remove duplicate/ unnecessary localStorage.clear() Update events.spec.ts Update events.spec.ts test: inject correct entry point for node environment Update package.json Update package.json fix: lint fail chore: required id-generator for react native environment bump to 2.2.13 fix: missing default user agent in react native environment Update package.json
1 parent d44082a commit 346bd91

19 files changed

+301
-48
lines changed

e2e/BKTClient.spec.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {
33
destroyBKTClient,
44
getBKTClient,
55
initializeBKTClient,
6-
} from '../src/main.browser'
6+
} from './module'
77
import { BKTConfig, defineBKTConfig } from '../src/BKTConfig'
88
import { BKTUser, defineBKTUser } from '../src/BKTUser'
99
import { FEATURE_ID_STRING, USER_ID } from './constants'
@@ -12,14 +12,14 @@ import { BKTClient, BKTClientImpl } from '../src/BKTClient'
1212
import { DefaultComponent } from '../src/internal/di/Component'
1313
import { EvaluationStorageImpl } from '../src/internal/evaluation/EvaluationStorage'
1414
import { evaluation1 } from '../test/mocks/evaluations'
15+
import { fetchLike } from './environment'
1516

1617
suite('e2e/BKTClientTest', () => {
1718
let config: BKTConfig
1819
let user: BKTUser
1920

2021
afterEach(() => {
2122
destroyBKTClient()
22-
localStorage.clear()
2323
})
2424

2525
suite('get string variation using user attribute when initializing', () => {
@@ -29,7 +29,7 @@ suite('e2e/BKTClientTest', () => {
2929
apiKey: import.meta.env.VITE_BKT_API_KEY,
3030
featureTag: 'javascript',
3131
appVersion: '1.2.3',
32-
fetch: window.fetch,
32+
fetch: fetchLike,
3333
})
3434

3535
user = defineBKTUser({
@@ -54,7 +54,7 @@ suite('e2e/BKTClientTest', () => {
5454
apiKey: import.meta.env.VITE_BKT_API_KEY,
5555
featureTag: 'javascript',
5656
appVersion: '1.2.3',
57-
fetch: window.fetch,
57+
fetch: fetchLike,
5858
})
5959

6060
user = defineBKTUser({
@@ -99,7 +99,7 @@ suite('e2e/BKTClientTest', () => {
9999
apiKey: import.meta.env.VITE_BKT_API_KEY,
100100
featureTag: 'javascript',
101101
appVersion: '1.2.3',
102-
fetch: window.fetch,
102+
fetch: fetchLike,
103103
})
104104

105105
user = defineBKTUser({
@@ -201,7 +201,7 @@ suite('e2e/BKTClientTest', () => {
201201
apiEndpoint: import.meta.env.VITE_BKT_API_ENDPOINT,
202202
apiKey: import.meta.env.VITE_BKT_API_KEY,
203203
appVersion: '1.2.3',
204-
fetch: window.fetch,
204+
fetch: fetchLike,
205205
})
206206

207207
user = defineBKTUser({

e2e/environment.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { FetchLike } from '../src/internal/remote/fetch'
2+
3+
let fetchLike: FetchLike
4+
let isNodeEnvironment = false
5+
6+
function setFetchProvider(fetch: FetchLike) {
7+
fetchLike = fetch
8+
}
9+
10+
function setIsNodeEnvironment(isNode: boolean) {
11+
isNodeEnvironment = isNode
12+
}
13+
14+
export {
15+
setFetchProvider,
16+
setIsNodeEnvironment,
17+
fetchLike,
18+
isNodeEnvironment,
19+
}

e2e/evaluations.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {
33
destroyBKTClient,
44
getBKTClient,
55
initializeBKTClient,
6-
} from '../src/main.browser'
6+
} from './module'
77
import { BKTConfig, defineBKTConfig } from '../src/BKTConfig'
88
import { BKTUser, defineBKTUser } from '../src/BKTUser'
99
import {
@@ -15,6 +15,7 @@ import {
1515
USER_ID,
1616
} from './constants'
1717
import './assertions'
18+
import { fetchLike } from './environment'
1819

1920
suite('e2e/evaluations', () => {
2021
let config: BKTConfig
@@ -26,7 +27,7 @@ suite('e2e/evaluations', () => {
2627
apiKey: import.meta.env.VITE_BKT_API_KEY,
2728
featureTag: 'javascript',
2829
appVersion: '1.2.3',
29-
fetch: window.fetch,
30+
fetch: fetchLike,
3031
})
3132

3233
user = defineBKTUser({
@@ -38,7 +39,6 @@ suite('e2e/evaluations', () => {
3839

3940
afterEach(() => {
4041
destroyBKTClient()
41-
localStorage.clear()
4242
})
4343

4444
suite('stringVariation', () => {

e2e/events.spec.ts

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@ import { suite, test, expect, beforeEach, afterEach, assert } from 'vitest'
22
import {
33
BKTClient,
44
BKTClientImpl,
5-
destroyBKTClient,
6-
getBKTClient,
75
} from '../src/BKTClient'
8-
import { initializeBKTClient } from '../src/main.browser'
6+
import { initializeBKTClient, getBKTClient, destroyBKTClient } from './module'
97
import { BKTConfig, defineBKTConfig } from '../src/BKTConfig'
108
import { BKTUser, defineBKTUser } from '../src/BKTUser'
119
import { DefaultComponent } from '../src/internal/di/Component'
@@ -25,6 +23,7 @@ import { ForbiddenException, TimeoutException } from '../src/BKTExceptions'
2523
import { ApiId, MetricsEventType } from '../src/internal/model/MetricsEventData'
2624
import { SDK_VERSION } from '../src/internal/version'
2725
import { SourceId } from '../src/internal/model/SourceId'
26+
import { fetchLike, isNodeEnvironment } from './environment'
2827

2928
function getDefaultComponent(client: BKTClient): DefaultComponent {
3029
return (client as BKTClientImpl).component as DefaultComponent
@@ -40,7 +39,7 @@ suite('e2e/events', () => {
4039
apiKey: import.meta.env.VITE_BKT_API_KEY,
4140
featureTag: 'javascript',
4241
appVersion: '1.2.3',
43-
fetch: window.fetch,
42+
fetch: fetchLike,
4443
// DO NOT remove this line
4544
// Because the tests are asynchronous and share the same local storage,
4645
// It might fail randomly, having more or fewer events in the storage when checking the test.
@@ -57,7 +56,6 @@ suite('e2e/events', () => {
5756

5857
afterEach(() => {
5958
destroyBKTClient()
60-
localStorage.clear()
6159
})
6260

6361
test('goal event', async () => {
@@ -187,14 +185,13 @@ suite('e2e/events', () => {
187185

188186
test('Using a random string in the api key setting should throw Forbidden', async () => {
189187
destroyBKTClient()
190-
localStorage.clear()
191188

192189
config = defineBKTConfig({
193190
apiEndpoint: import.meta.env.VITE_BKT_API_ENDPOINT,
194191
apiKey: 'some-random-string',
195192
featureTag: 'javascript',
196193
appVersion: '1.2.3',
197-
fetch: window.fetch,
194+
fetch: fetchLike,
198195
})
199196

200197
user = defineBKTUser({
@@ -239,7 +236,7 @@ suite('e2e/events', () => {
239236
apiKey: import.meta.env.VITE_BKT_API_KEY,
240237
featureTag: 'javascript',
241238
appVersion: '1.2.3',
242-
fetch: window.fetch,
239+
fetch: fetchLike,
243240
})
244241

245242
await initializeBKTClient(config, user)
@@ -249,39 +246,45 @@ suite('e2e/events', () => {
249246
const component2 = getDefaultComponent(client)
250247

251248
const events3 = await component2.dataModule.eventStorage().getAll()
252-
// 2 events - latency and response size
253-
expect(events3).toHaveLength(2)
254-
// ForbiddenError should not exist
255-
expect(
256-
events.some((e) => {
257-
return (
249+
250+
if (isNodeEnvironment) {
251+
// on the node environment, no events should be stored after destroying the client
252+
// because it's using in-memory storage
253+
expect(events3).toHaveLength(0)
254+
} else {
255+
// on the browser environment, we should have 2 events - latency and response size
256+
expect(events3).toHaveLength(2)
257+
// ForbiddenError should not exist
258+
expect(
259+
events3.some((e) => {
260+
return (
258261
e.type === EventType.METRICS &&
259262
e.event.event['@type'] === MetricsEventType.ForbiddenError &&
260263
e.event.event.apiId === ApiId.GET_EVALUATIONS &&
261264
e.event.sdkVersion === SDK_VERSION &&
262265
e.event.sourceId === SourceId.JAVASCRIPT
263-
)
264-
}),
265-
).toBe(false)
266+
)
267+
}),
268+
).toBe(false)
266269

267-
await client2.flush()
270+
await client2.flush()
268271

269-
const events4 = await component2.dataModule.eventStorage().getAll()
272+
const events4 = await component2.dataModule.eventStorage().getAll()
270273

271-
// error from /register_events does not get stored
272-
expect(events4).toHaveLength(0)
274+
// error from /register_events does not get stored
275+
expect(events4).toHaveLength(0)
276+
}
273277
})
274278

275279
test('Using a random string in the featureTag setting should not affect api request', async () => {
276280
destroyBKTClient()
277-
localStorage.clear()
278281

279282
config = defineBKTConfig({
280283
apiEndpoint: import.meta.env.VITE_BKT_API_ENDPOINT,
281284
apiKey: import.meta.env.VITE_BKT_API_KEY,
282285
featureTag: 'some-random-feature-tag',
283286
appVersion: '1.2.3',
284-
fetch: window.fetch,
287+
fetch: fetchLike,
285288
})
286289

287290
user = defineBKTUser({
@@ -293,16 +296,14 @@ suite('e2e/events', () => {
293296

294297
test('Timeout', async () => {
295298
// setting a very low value for the timeout
296-
297299
destroyBKTClient()
298-
localStorage.clear()
299300

300301
config = defineBKTConfig({
301302
apiEndpoint: import.meta.env.VITE_BKT_API_ENDPOINT,
302303
apiKey: import.meta.env.VITE_BKT_API_KEY,
303304
featureTag: 'javascript',
304305
appVersion: '1.2.3',
305-
fetch: window.fetch,
306+
fetch: fetchLike,
306307
})
307308

308309
user = defineBKTUser({

e2e/module.browser.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export {
2+
destroyBKTClient,
3+
getBKTClient,
4+
initializeBKTClient,
5+
} from '../src/main.browser'

e2e/module.node.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export {
2+
destroyBKTClient,
3+
getBKTClient,
4+
initializeBKTClient,
5+
} from '../src/main'

e2e/module.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export {
2+
destroyBKTClient,
3+
getBKTClient,
4+
initializeBKTClient,
5+
} from '../src/main'

e2e/setup.browser.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
import { afterEach } from 'vitest'
3+
import { setFetchProvider } from './environment'
4+
5+
setFetchProvider(window.fetch)
6+
7+
afterEach(() => {
8+
localStorage.clear()
9+
})

e2e/setup.node.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { setFetchProvider, setIsNodeEnvironment } from './environment'
2+
3+
setFetchProvider(fetch)
4+
5+
setIsNodeEnvironment(true)

e2e/wrapperSdkSourceId.spec.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import { EventType } from '../src/internal/model/Event'
2424
import { TimeoutException } from '../src/BKTExceptions'
2525
import { ApiId, MetricsEventType } from '../src/internal/model/MetricsEventData'
2626
import { SourceId } from '../src/internal/model/SourceId'
27+
import { fetchLike } from './environment'
2728

2829
function getDefaultComponent(client: BKTClient): DefaultComponent {
2930
return (client as BKTClientImpl).component as DefaultComponent
@@ -39,7 +40,7 @@ suite('e2e/wrapper-sdk-source-id-and-version', () => {
3940
apiKey: import.meta.env.VITE_BKT_API_KEY,
4041
featureTag: 'javascript',
4142
appVersion: '1.2.3',
42-
fetch: window.fetch,
43+
fetch: fetchLike,
4344
// DO NOT remove this line
4445
// Because the tests are asynchronous and share the same local storage,
4546
// It might fail randomly, having more or fewer events in the storage when checking the test.
@@ -58,7 +59,6 @@ suite('e2e/wrapper-sdk-source-id-and-version', () => {
5859

5960
afterEach(() => {
6061
destroyBKTClient()
61-
localStorage.clear()
6262
})
6363

6464
test('goal event', async () => {
@@ -123,14 +123,13 @@ suite('e2e/wrapper-sdk-source-id-and-version', () => {
123123
// setting a very low value for the timeout
124124

125125
destroyBKTClient()
126-
localStorage.clear()
127126

128127
config = defineBKTConfig({
129128
apiEndpoint: import.meta.env.VITE_BKT_API_ENDPOINT,
130129
apiKey: import.meta.env.VITE_BKT_API_KEY,
131130
featureTag: 'javascript',
132131
appVersion: '1.2.3',
133-
fetch: window.fetch,
132+
fetch: fetchLike,
134133
wrapperSdkSourceId: SourceId.OPEN_FEATURE_JAVASCRIPT,
135134
wrapperSdkVersion: '2.2.3',
136135
})

0 commit comments

Comments
 (0)