|
1 |
| -import { AutoEnvAttributes, LDLogger } from '@launchdarkly/js-client-sdk-common'; |
| 1 | +import { |
| 2 | + AutoEnvAttributes, |
| 3 | + LDLogger, |
| 4 | + LDSingleKindContext, |
| 5 | +} from '@launchdarkly/js-client-sdk-common'; |
2 | 6 |
|
3 | 7 | import { BrowserClient } from '../src/BrowserClient';
|
4 | 8 | import { makeBasicPlatform } from './BrowserClient.mocks';
|
@@ -184,4 +188,198 @@ describe('given a mock platform for a BrowserClient', () => {
|
184 | 188 | variationIndex: 1,
|
185 | 189 | });
|
186 | 190 | });
|
| 191 | + |
| 192 | + it('can shed intermediate identifyResult calls', async () => { |
| 193 | + const client = new BrowserClient( |
| 194 | + 'client-side-id', |
| 195 | + AutoEnvAttributes.Disabled, |
| 196 | + { streaming: false, logger, diagnosticOptOut: true, sendEvents: false, fetchGoals: false }, |
| 197 | + platform, |
| 198 | + ); |
| 199 | + |
| 200 | + const promise1 = client.identifyResult({ key: 'user-key-1', kind: 'user' }); |
| 201 | + const promise2 = client.identifyResult({ key: 'user-key-2', kind: 'user' }); |
| 202 | + const promise3 = client.identifyResult({ key: 'user-key-3', kind: 'user' }); |
| 203 | + |
| 204 | + const [result1, result2, result3] = await Promise.all([promise1, promise2, promise3]); |
| 205 | + |
| 206 | + expect(result1).toEqual({ status: 'completed' }); |
| 207 | + expect(result2).toEqual({ status: 'shed' }); |
| 208 | + expect(result3).toEqual({ status: 'completed' }); |
| 209 | + // With events and goals disabled the only fetch calls should be for polling requests. |
| 210 | + expect(platform.requests.fetch.mock.calls.length).toBe(2); |
| 211 | + }); |
| 212 | + |
| 213 | + it('calls beforeIdentify in order', async () => { |
| 214 | + const order: string[] = []; |
| 215 | + const client = new BrowserClient( |
| 216 | + 'client-side-id', |
| 217 | + AutoEnvAttributes.Disabled, |
| 218 | + { |
| 219 | + streaming: false, |
| 220 | + logger, |
| 221 | + diagnosticOptOut: true, |
| 222 | + sendEvents: false, |
| 223 | + fetchGoals: false, |
| 224 | + hooks: [ |
| 225 | + { |
| 226 | + beforeIdentify: (hookContext, data) => { |
| 227 | + if ('kind' in hookContext.context && hookContext.context.kind !== 'multi') { |
| 228 | + order.push((hookContext.context as LDSingleKindContext).key); |
| 229 | + } |
| 230 | + |
| 231 | + return data; |
| 232 | + }, |
| 233 | + getMetadata: () => ({ |
| 234 | + name: 'test-hook', |
| 235 | + version: '1.0.0', |
| 236 | + }), |
| 237 | + }, |
| 238 | + ], |
| 239 | + }, |
| 240 | + platform, |
| 241 | + ); |
| 242 | + |
| 243 | + const promise1 = client.identify({ key: 'user-key-1', kind: 'user' }); |
| 244 | + const promise2 = client.identify({ key: 'user-key-2', kind: 'user' }); |
| 245 | + const promise3 = client.identify({ key: 'user-key-3', kind: 'user' }); |
| 246 | + |
| 247 | + await Promise.all([promise1, promise2, promise3]); |
| 248 | + expect(order).toEqual(['user-key-1', 'user-key-2', 'user-key-3']); |
| 249 | + }); |
| 250 | + |
| 251 | + it('completes identify calls in order', async () => { |
| 252 | + const order: string[] = []; |
| 253 | + const client = new BrowserClient( |
| 254 | + 'client-side-id', |
| 255 | + AutoEnvAttributes.Disabled, |
| 256 | + { |
| 257 | + streaming: false, |
| 258 | + logger, |
| 259 | + diagnosticOptOut: true, |
| 260 | + sendEvents: false, |
| 261 | + fetchGoals: false, |
| 262 | + hooks: [ |
| 263 | + { |
| 264 | + afterIdentify: (hookContext, data, result) => { |
| 265 | + if (result.status === 'shed') { |
| 266 | + return data; |
| 267 | + } |
| 268 | + if ('kind' in hookContext.context && hookContext.context.kind !== 'multi') { |
| 269 | + order.push((hookContext.context as LDSingleKindContext).key); |
| 270 | + } |
| 271 | + |
| 272 | + return data; |
| 273 | + }, |
| 274 | + getMetadata: () => ({ |
| 275 | + name: 'test-hook', |
| 276 | + version: '1.0.0', |
| 277 | + }), |
| 278 | + }, |
| 279 | + ], |
| 280 | + }, |
| 281 | + platform, |
| 282 | + ); |
| 283 | + |
| 284 | + const promise1 = client.identify({ key: 'user-key-1', kind: 'user' }); |
| 285 | + const promise2 = client.identify({ key: 'user-key-2', kind: 'user' }); |
| 286 | + const promise3 = client.identify({ key: 'user-key-3', kind: 'user' }); |
| 287 | + |
| 288 | + await Promise.all([promise1, promise2, promise3]); |
| 289 | + // user-key-2 is shed, so it is not included in the order |
| 290 | + expect(order).toEqual(['user-key-1', 'user-key-3']); |
| 291 | + }); |
| 292 | + |
| 293 | + it('completes awaited identify calls in order without shedding', async () => { |
| 294 | + const order: string[] = []; |
| 295 | + const client = new BrowserClient( |
| 296 | + 'client-side-id', |
| 297 | + AutoEnvAttributes.Disabled, |
| 298 | + { |
| 299 | + streaming: false, |
| 300 | + logger, |
| 301 | + diagnosticOptOut: true, |
| 302 | + sendEvents: false, |
| 303 | + fetchGoals: false, |
| 304 | + hooks: [ |
| 305 | + { |
| 306 | + afterIdentify: (hookContext, data, result) => { |
| 307 | + if (result.status === 'shed') { |
| 308 | + return data; |
| 309 | + } |
| 310 | + if ('kind' in hookContext.context && hookContext.context.kind !== 'multi') { |
| 311 | + order.push((hookContext.context as LDSingleKindContext).key); |
| 312 | + } |
| 313 | + |
| 314 | + return data; |
| 315 | + }, |
| 316 | + getMetadata: () => ({ |
| 317 | + name: 'test-hook', |
| 318 | + version: '1.0.0', |
| 319 | + }), |
| 320 | + }, |
| 321 | + ], |
| 322 | + }, |
| 323 | + platform, |
| 324 | + ); |
| 325 | + |
| 326 | + const result1 = await client.identifyResult({ key: 'user-key-1', kind: 'user' }); |
| 327 | + const result2 = await client.identifyResult({ key: 'user-key-2', kind: 'user' }); |
| 328 | + const result3 = await client.identifyResult({ key: 'user-key-3', kind: 'user' }); |
| 329 | + |
| 330 | + expect(result1.status).toEqual('completed'); |
| 331 | + expect(result2.status).toEqual('completed'); |
| 332 | + expect(result3.status).toEqual('completed'); |
| 333 | + |
| 334 | + // user-key-2 is shed, so it is not included in the order |
| 335 | + expect(order).toEqual(['user-key-1', 'user-key-2', 'user-key-3']); |
| 336 | + }); |
| 337 | + |
| 338 | + it('can shed intermediate identify calls', async () => { |
| 339 | + const client = new BrowserClient( |
| 340 | + 'client-side-id', |
| 341 | + AutoEnvAttributes.Disabled, |
| 342 | + { streaming: false, logger, diagnosticOptOut: true, sendEvents: false, fetchGoals: false }, |
| 343 | + platform, |
| 344 | + ); |
| 345 | + |
| 346 | + const promise1 = client.identify({ key: 'user-key-1', kind: 'user' }); |
| 347 | + const promise2 = client.identify({ key: 'user-key-2', kind: 'user' }); |
| 348 | + const promise3 = client.identify({ key: 'user-key-3', kind: 'user' }); |
| 349 | + |
| 350 | + await Promise.all([promise1, promise2, promise3]); |
| 351 | + |
| 352 | + // With events and goals disabled the only fetch calls should be for polling requests. |
| 353 | + expect(platform.requests.fetch.mock.calls.length).toBe(2); |
| 354 | + }); |
| 355 | + |
| 356 | + it('it does not shed non-shedable identify calls', async () => { |
| 357 | + const client = new BrowserClient( |
| 358 | + 'client-side-id', |
| 359 | + AutoEnvAttributes.Disabled, |
| 360 | + { streaming: false, logger, diagnosticOptOut: true, sendEvents: false, fetchGoals: false }, |
| 361 | + platform, |
| 362 | + ); |
| 363 | + |
| 364 | + const promise1 = client.identifyResult( |
| 365 | + { key: 'user-key-1', kind: 'user' }, |
| 366 | + { sheddable: false }, |
| 367 | + ); |
| 368 | + const promise2 = client.identifyResult( |
| 369 | + { key: 'user-key-2', kind: 'user' }, |
| 370 | + { sheddable: false }, |
| 371 | + ); |
| 372 | + const promise3 = client.identifyResult( |
| 373 | + { key: 'user-key-3', kind: 'user' }, |
| 374 | + { sheddable: false }, |
| 375 | + ); |
| 376 | + |
| 377 | + const [result1, result2, result3] = await Promise.all([promise1, promise2, promise3]); |
| 378 | + |
| 379 | + expect(result1).toEqual({ status: 'completed' }); |
| 380 | + expect(result2).toEqual({ status: 'completed' }); |
| 381 | + expect(result3).toEqual({ status: 'completed' }); |
| 382 | + // With events and goals disabled the only fetch calls should be for polling requests. |
| 383 | + expect(platform.requests.fetch.mock.calls.length).toBe(3); |
| 384 | + }); |
187 | 385 | });
|
0 commit comments