@usertour/helpers 0.0.18 → 0.0.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,12 @@
1
1
  import {
2
+ activedRulesConditions,
2
3
  filterConditionsByType,
3
4
  isConditionsActived
4
- } from "../chunk-YOFQHQ7D.js";
5
+ } from "../chunk-UNXDVBM3.js";
6
+ import "../chunk-YYIGUZNZ.js";
7
+ import "../chunk-PAESAL23.js";
8
+ import "../chunk-PBZSPV5R.js";
9
+ import "../chunk-CEK3SCQO.js";
5
10
  import "../chunk-XEO3YXBM.js";
6
11
 
7
12
  // src/__tests__/condition.test.ts
@@ -202,3 +207,339 @@ describe("isConditionsActived", () => {
202
207
  expect(result).toBe(true);
203
208
  });
204
209
  });
210
+ describe("activedRulesConditions", () => {
211
+ const mockConditions = [
212
+ {
213
+ id: "rule-1",
214
+ type: "current-page",
215
+ operators: "and",
216
+ actived: false,
217
+ data: {
218
+ includes: ["https://example.com"],
219
+ excludes: []
220
+ }
221
+ },
222
+ {
223
+ id: "rule-2",
224
+ type: "time",
225
+ operators: "and",
226
+ actived: false,
227
+ data: {
228
+ startDate: "2024-01-01",
229
+ startDateHour: "00",
230
+ startDateMinute: "00"
231
+ }
232
+ },
233
+ {
234
+ id: "rule-3",
235
+ type: "user-attr",
236
+ operators: "and",
237
+ actived: false,
238
+ data: {
239
+ attrId: "email",
240
+ logic: "is",
241
+ value: "test@example.com"
242
+ }
243
+ },
244
+ {
245
+ id: "rule-4",
246
+ type: "element",
247
+ operators: "and",
248
+ actived: true,
249
+ data: {}
250
+ }
251
+ ];
252
+ const mockOptions = {
253
+ clientContext: {
254
+ page_url: "https://example.com",
255
+ viewport_width: 1920,
256
+ viewport_height: 1080
257
+ },
258
+ attributes: [
259
+ {
260
+ id: "email",
261
+ codeName: "email",
262
+ dataType: 2
263
+ // String
264
+ }
265
+ ],
266
+ userAttributes: {
267
+ email: "test@example.com"
268
+ }
269
+ };
270
+ test("should return conditions with default actived state when no options provided", () => {
271
+ const conditions = [
272
+ {
273
+ id: "rule-1",
274
+ type: "element",
275
+ operators: "and",
276
+ actived: true,
277
+ data: {}
278
+ },
279
+ {
280
+ id: "rule-2",
281
+ type: "element",
282
+ operators: "and",
283
+ actived: false,
284
+ data: {}
285
+ }
286
+ ];
287
+ const result = activedRulesConditions(conditions);
288
+ expect(result).toHaveLength(2);
289
+ expect(result[0].actived).toBe(true);
290
+ expect(result[1].actived).toBe(false);
291
+ });
292
+ test("should force activate rules by ID", () => {
293
+ const result = activedRulesConditions(mockConditions, {
294
+ ...mockOptions,
295
+ activatedIds: ["rule-1", "rule-2"]
296
+ });
297
+ expect(result[0].actived).toBe(true);
298
+ expect(result[1].actived).toBe(true);
299
+ expect(result[2].actived).toBe(true);
300
+ expect(result[3].actived).toBe(true);
301
+ });
302
+ test("should force deactivate rules by ID", () => {
303
+ const result = activedRulesConditions(mockConditions, {
304
+ ...mockOptions,
305
+ deactivatedIds: ["rule-1", "rule-4"]
306
+ });
307
+ expect(result[0].actived).toBe(false);
308
+ expect(result[1].actived).toBe(true);
309
+ expect(result[2].actived).toBe(true);
310
+ expect(result[3].actived).toBe(false);
311
+ });
312
+ test("should prioritize activatedIds over deactivatedIds", () => {
313
+ const result = activedRulesConditions(mockConditions, {
314
+ ...mockOptions,
315
+ activatedIds: ["rule-1"],
316
+ deactivatedIds: ["rule-1"]
317
+ });
318
+ expect(result[0].actived).toBe(true);
319
+ });
320
+ test("should disable evaluation for specific rule types", () => {
321
+ const result = activedRulesConditions(mockConditions, {
322
+ ...mockOptions,
323
+ typeControl: {
324
+ [RulesType.CURRENT_PAGE]: false,
325
+ [RulesType.TIME]: false
326
+ }
327
+ });
328
+ expect(result[0].actived).toBe(false);
329
+ expect(result[1].actived).toBe(false);
330
+ expect(result[2].actived).toBe(true);
331
+ expect(result[3].actived).toBe(true);
332
+ });
333
+ test("should evaluate URL conditions correctly", () => {
334
+ const result = activedRulesConditions(mockConditions, mockOptions);
335
+ expect(result[0].actived).toBe(false);
336
+ });
337
+ test("should evaluate time conditions correctly", () => {
338
+ const result = activedRulesConditions(mockConditions, mockOptions);
339
+ expect(typeof result[1].actived).toBe("boolean");
340
+ });
341
+ test("should evaluate attribute conditions correctly", () => {
342
+ const result = activedRulesConditions(mockConditions, mockOptions);
343
+ expect(result[2].actived).toBe(true);
344
+ });
345
+ test("should handle rules without ID", () => {
346
+ const conditions = [
347
+ {
348
+ id: "rule-without-id",
349
+ type: "element",
350
+ operators: "and",
351
+ actived: true,
352
+ data: {}
353
+ }
354
+ ];
355
+ const result = activedRulesConditions(conditions, {
356
+ activatedIds: ["non-existent"],
357
+ deactivatedIds: ["non-existent"]
358
+ });
359
+ expect(result[0].actived).toBe(true);
360
+ });
361
+ test("should handle nested group conditions", () => {
362
+ const nestedConditions = [
363
+ {
364
+ id: "group-1",
365
+ type: "group",
366
+ operators: "and",
367
+ actived: false,
368
+ data: {},
369
+ conditions: [
370
+ {
371
+ id: "rule-1",
372
+ type: "current-page",
373
+ operators: "and",
374
+ actived: false,
375
+ data: {
376
+ includes: ["https://example.com"],
377
+ excludes: []
378
+ }
379
+ },
380
+ {
381
+ id: "rule-2",
382
+ type: "element",
383
+ operators: "and",
384
+ actived: true,
385
+ data: {}
386
+ }
387
+ ]
388
+ }
389
+ ];
390
+ const result = activedRulesConditions(nestedConditions, {
391
+ ...mockOptions,
392
+ activatedIds: ["rule-1"]
393
+ });
394
+ expect(result[0].type).toBe("group");
395
+ expect(result[0].conditions).toBeDefined();
396
+ expect(result[0].conditions[0].actived).toBe(true);
397
+ expect(result[0].conditions[1].actived).toBe(true);
398
+ });
399
+ test("should handle deep nested group conditions", () => {
400
+ const deepNestedConditions = [
401
+ {
402
+ id: "group-1",
403
+ type: "group",
404
+ operators: "and",
405
+ actived: false,
406
+ data: {},
407
+ conditions: [
408
+ {
409
+ id: "group-2",
410
+ type: "group",
411
+ operators: "and",
412
+ actived: false,
413
+ data: {},
414
+ conditions: [
415
+ {
416
+ id: "rule-1",
417
+ type: "current-page",
418
+ operators: "and",
419
+ actived: false,
420
+ data: {
421
+ includes: ["https://example.com"],
422
+ excludes: []
423
+ }
424
+ }
425
+ ]
426
+ }
427
+ ]
428
+ }
429
+ ];
430
+ const result = activedRulesConditions(deepNestedConditions, {
431
+ ...mockOptions,
432
+ activatedIds: ["rule-1"]
433
+ });
434
+ expect(result[0].conditions[0].conditions[0].actived).toBe(true);
435
+ });
436
+ test("should handle empty conditions array", () => {
437
+ const result = activedRulesConditions([]);
438
+ expect(result).toEqual([]);
439
+ });
440
+ test("should handle conditions with missing data", () => {
441
+ const conditions = [
442
+ {
443
+ id: "rule-1",
444
+ type: "current-page",
445
+ operators: "and",
446
+ actived: false,
447
+ data: void 0
448
+ }
449
+ ];
450
+ const result = activedRulesConditions(conditions, mockOptions);
451
+ expect(result[0].actived).toBe(false);
452
+ });
453
+ test("should handle mixed rule types with different evaluation results", () => {
454
+ const mixedConditions = [
455
+ {
456
+ id: "url-rule",
457
+ type: "current-page",
458
+ operators: "and",
459
+ actived: false,
460
+ data: {
461
+ includes: ["https://wrong-url.com"],
462
+ excludes: []
463
+ }
464
+ },
465
+ {
466
+ id: "attr-rule",
467
+ type: "user-attr",
468
+ operators: "and",
469
+ actived: false,
470
+ data: {
471
+ attrId: "email",
472
+ logic: "is",
473
+ value: "test@example.com"
474
+ }
475
+ },
476
+ {
477
+ id: "element-rule",
478
+ type: "element",
479
+ operators: "and",
480
+ actived: false,
481
+ data: {}
482
+ }
483
+ ];
484
+ const result = activedRulesConditions(mixedConditions, mockOptions);
485
+ expect(result[0].actived).toBe(false);
486
+ expect(result[1].actived).toBe(true);
487
+ expect(result[2].actived).toBe(false);
488
+ });
489
+ test("should handle type control with partial configuration", () => {
490
+ const result = activedRulesConditions(mockConditions, {
491
+ ...mockOptions,
492
+ typeControl: {
493
+ [RulesType.CURRENT_PAGE]: false
494
+ // Other types not specified, should evaluate normally
495
+ }
496
+ });
497
+ expect(result[0].actived).toBe(false);
498
+ expect(result[1].actived).toBe(true);
499
+ expect(result[2].actived).toBe(true);
500
+ expect(result[3].actived).toBe(true);
501
+ });
502
+ test("should handle missing clientContext gracefully", () => {
503
+ const result = activedRulesConditions(mockConditions, {
504
+ attributes: mockOptions.attributes,
505
+ userAttributes: mockOptions.userAttributes
506
+ });
507
+ expect(result).toHaveLength(4);
508
+ expect(typeof result[0].actived).toBe("boolean");
509
+ });
510
+ test("should handle missing attributes gracefully", () => {
511
+ const result = activedRulesConditions(mockConditions, {
512
+ clientContext: mockOptions.clientContext,
513
+ userAttributes: mockOptions.userAttributes
514
+ });
515
+ expect(result[2].actived).toBe(false);
516
+ });
517
+ test("should handle missing userAttributes gracefully", () => {
518
+ const result = activedRulesConditions(mockConditions, {
519
+ clientContext: mockOptions.clientContext,
520
+ attributes: mockOptions.attributes
521
+ });
522
+ expect(result[2].actived).toBe(false);
523
+ });
524
+ test("debug: should test URL evaluation directly", () => {
525
+ const urlCondition = {
526
+ id: "debug-rule",
527
+ type: "current-page",
528
+ operators: "and",
529
+ actived: false,
530
+ data: {
531
+ includes: ["https://example.com"],
532
+ excludes: []
533
+ }
534
+ };
535
+ const options = {
536
+ clientContext: {
537
+ page_url: "https://example.com",
538
+ viewport_width: 1920,
539
+ viewport_height: 1080
540
+ }
541
+ };
542
+ const result = activedRulesConditions([urlCondition], options);
543
+ expect(result[0].actived).toBe(false);
544
+ });
545
+ });