@player-ui/data-change-listener-plugin 0.10.3-next.3 → 0.10.3
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.
- package/package.json +5 -3
- package/src/__tests__/index.test.ts +218 -0
package/package.json
CHANGED
|
@@ -6,15 +6,17 @@
|
|
|
6
6
|
"types"
|
|
7
7
|
],
|
|
8
8
|
"name": "@player-ui/data-change-listener-plugin",
|
|
9
|
-
"version": "0.10.3
|
|
9
|
+
"version": "0.10.3",
|
|
10
10
|
"main": "dist/cjs/index.cjs",
|
|
11
11
|
"peerDependencies": {
|
|
12
|
-
"@player-ui/player": "0.10.3
|
|
12
|
+
"@player-ui/player": "0.10.3"
|
|
13
13
|
},
|
|
14
14
|
"devDependencies": {
|
|
15
15
|
"@player-ui/partial-match-registry": "workspace:*",
|
|
16
16
|
"@player-ui/asset-transform-plugin": "workspace:*",
|
|
17
|
-
"@player-ui/common-types-plugin": "workspace:*"
|
|
17
|
+
"@player-ui/common-types-plugin": "workspace:*",
|
|
18
|
+
"@player-ui/reference-assets-plugin": "workspace:*",
|
|
19
|
+
"@player-ui/common-expressions-plugin": "workspace:*"
|
|
18
20
|
},
|
|
19
21
|
"module": "dist/index.legacy-esm.js",
|
|
20
22
|
"types": "types/index.d.ts",
|
|
@@ -10,6 +10,8 @@ import { CommonTypesPlugin } from "@player-ui/common-types-plugin";
|
|
|
10
10
|
import { AssetTransformPlugin } from "@player-ui/asset-transform-plugin";
|
|
11
11
|
import { Registry } from "@player-ui/partial-match-registry";
|
|
12
12
|
import { DataChangeListenerPlugin } from "../index";
|
|
13
|
+
import { ReferenceAssetsPlugin } from "@player-ui/reference-assets-plugin";
|
|
14
|
+
import { CommonExpressionsPlugin } from "@player-ui/common-expressions-plugin";
|
|
13
15
|
|
|
14
16
|
/** Test transform function to add validation to asset */
|
|
15
17
|
const transform: TransformFunction = (asset: any, options: any) => {
|
|
@@ -431,3 +433,219 @@ describe("Data-Change-Listener that are chained", () => {
|
|
|
431
433
|
});
|
|
432
434
|
});
|
|
433
435
|
});
|
|
436
|
+
|
|
437
|
+
describe("Data-Change-Listener with array modification", () => {
|
|
438
|
+
let player: Player;
|
|
439
|
+
let testExpression: Mock<any, any>;
|
|
440
|
+
|
|
441
|
+
const flow = {
|
|
442
|
+
id: "action-with-expression",
|
|
443
|
+
views: [
|
|
444
|
+
{
|
|
445
|
+
id: "root",
|
|
446
|
+
type: "info",
|
|
447
|
+
listeners: {
|
|
448
|
+
"dataChange.array": ["test('array has changed ' + {{array}})"],
|
|
449
|
+
},
|
|
450
|
+
title: {
|
|
451
|
+
asset: {
|
|
452
|
+
id: "title",
|
|
453
|
+
type: "text",
|
|
454
|
+
value: "Hello",
|
|
455
|
+
},
|
|
456
|
+
},
|
|
457
|
+
primaryInfo: {
|
|
458
|
+
asset: {
|
|
459
|
+
id: "primaryInfo",
|
|
460
|
+
type: "text",
|
|
461
|
+
value: "Collection: [{{array}}]",
|
|
462
|
+
},
|
|
463
|
+
},
|
|
464
|
+
actions: [
|
|
465
|
+
{
|
|
466
|
+
asset: {
|
|
467
|
+
id: "action",
|
|
468
|
+
type: "action",
|
|
469
|
+
exp: "{{array}} = concat({{array}}, [4])",
|
|
470
|
+
label: {
|
|
471
|
+
asset: {
|
|
472
|
+
id: "actions-0-label",
|
|
473
|
+
type: "text",
|
|
474
|
+
value: "Add to collection",
|
|
475
|
+
},
|
|
476
|
+
},
|
|
477
|
+
},
|
|
478
|
+
},
|
|
479
|
+
],
|
|
480
|
+
},
|
|
481
|
+
],
|
|
482
|
+
data: {
|
|
483
|
+
array: [1, 2, 3],
|
|
484
|
+
},
|
|
485
|
+
navigation: {
|
|
486
|
+
BEGIN: "FLOW_1",
|
|
487
|
+
FLOW_1: {
|
|
488
|
+
startState: "VIEW_1",
|
|
489
|
+
VIEW_1: {
|
|
490
|
+
state_type: "VIEW",
|
|
491
|
+
ref: "root",
|
|
492
|
+
transitions: {
|
|
493
|
+
"*": "END_Done",
|
|
494
|
+
},
|
|
495
|
+
},
|
|
496
|
+
END_Done: {
|
|
497
|
+
state_type: "END",
|
|
498
|
+
outcome: "done",
|
|
499
|
+
},
|
|
500
|
+
},
|
|
501
|
+
},
|
|
502
|
+
};
|
|
503
|
+
|
|
504
|
+
const flowNum = {
|
|
505
|
+
id: "action-with-expression",
|
|
506
|
+
views: [
|
|
507
|
+
{
|
|
508
|
+
id: "root",
|
|
509
|
+
type: "info",
|
|
510
|
+
listeners: {
|
|
511
|
+
"dataChange.count": ["test('count has changed ' + {{count}})"],
|
|
512
|
+
},
|
|
513
|
+
title: {
|
|
514
|
+
asset: {
|
|
515
|
+
id: "title",
|
|
516
|
+
type: "text",
|
|
517
|
+
value: "Hello",
|
|
518
|
+
},
|
|
519
|
+
},
|
|
520
|
+
primaryInfo: {
|
|
521
|
+
asset: {
|
|
522
|
+
id: "primaryInfo",
|
|
523
|
+
type: "text",
|
|
524
|
+
value: "Count: {{count}}\\",
|
|
525
|
+
},
|
|
526
|
+
},
|
|
527
|
+
actions: [
|
|
528
|
+
{
|
|
529
|
+
asset: {
|
|
530
|
+
id: "action",
|
|
531
|
+
type: "action",
|
|
532
|
+
exp: "{{count}} = {{count}} + 1",
|
|
533
|
+
label: {
|
|
534
|
+
asset: {
|
|
535
|
+
id: "actions-0-label",
|
|
536
|
+
type: "text",
|
|
537
|
+
value: "Add to collection",
|
|
538
|
+
},
|
|
539
|
+
},
|
|
540
|
+
},
|
|
541
|
+
},
|
|
542
|
+
],
|
|
543
|
+
},
|
|
544
|
+
],
|
|
545
|
+
data: {
|
|
546
|
+
count: 0,
|
|
547
|
+
},
|
|
548
|
+
navigation: {
|
|
549
|
+
BEGIN: "FLOW_1",
|
|
550
|
+
FLOW_1: {
|
|
551
|
+
startState: "VIEW_1",
|
|
552
|
+
VIEW_1: {
|
|
553
|
+
state_type: "VIEW",
|
|
554
|
+
ref: "root",
|
|
555
|
+
transitions: {
|
|
556
|
+
"*": "END_Done",
|
|
557
|
+
},
|
|
558
|
+
},
|
|
559
|
+
END_Done: {
|
|
560
|
+
state_type: "END",
|
|
561
|
+
outcome: "done",
|
|
562
|
+
},
|
|
563
|
+
},
|
|
564
|
+
},
|
|
565
|
+
};
|
|
566
|
+
|
|
567
|
+
/** Helper function to get current Player state */
|
|
568
|
+
function getState() {
|
|
569
|
+
return player.getState() as InProgressState;
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
it("should call expression evaluator and data change listener when an array that is tracked changes", () => {
|
|
573
|
+
player = new Player({
|
|
574
|
+
plugins: [
|
|
575
|
+
new CommonTypesPlugin(),
|
|
576
|
+
new DataChangeListenerPlugin(),
|
|
577
|
+
new ReferenceAssetsPlugin(),
|
|
578
|
+
new CommonExpressionsPlugin(),
|
|
579
|
+
],
|
|
580
|
+
});
|
|
581
|
+
|
|
582
|
+
testExpression = vitest.fn();
|
|
583
|
+
|
|
584
|
+
player.hooks.expressionEvaluator.tap("test", (ev) => {
|
|
585
|
+
ev.addExpressionFunction("test", (context, ...args) => {
|
|
586
|
+
testExpression(...args);
|
|
587
|
+
});
|
|
588
|
+
});
|
|
589
|
+
|
|
590
|
+
player.start(flow);
|
|
591
|
+
|
|
592
|
+
const getCurrent = () => {
|
|
593
|
+
const status = player.getState();
|
|
594
|
+
if (status.status === "in-progress") {
|
|
595
|
+
const view = status.controllers.view.currentView?.lastUpdate;
|
|
596
|
+
if (view) {
|
|
597
|
+
return view;
|
|
598
|
+
}
|
|
599
|
+
}
|
|
600
|
+
};
|
|
601
|
+
|
|
602
|
+
expect(getState().controllers.data.get("array")).toStrictEqual([1, 2, 3]);
|
|
603
|
+
|
|
604
|
+
getCurrent()?.actions[0].asset.run();
|
|
605
|
+
|
|
606
|
+
expect(getState().controllers.data.get("array")).toStrictEqual([
|
|
607
|
+
1, 2, 3, 4,
|
|
608
|
+
]);
|
|
609
|
+
|
|
610
|
+
expect(testExpression).toHaveBeenCalledWith("array has changed 1,2,3,4");
|
|
611
|
+
});
|
|
612
|
+
|
|
613
|
+
it("should call expression evaluator and data change listener when count is changes", () => {
|
|
614
|
+
player = new Player({
|
|
615
|
+
plugins: [
|
|
616
|
+
new CommonTypesPlugin(),
|
|
617
|
+
new DataChangeListenerPlugin(),
|
|
618
|
+
new ReferenceAssetsPlugin(),
|
|
619
|
+
new CommonExpressionsPlugin(),
|
|
620
|
+
],
|
|
621
|
+
});
|
|
622
|
+
|
|
623
|
+
testExpression = vitest.fn();
|
|
624
|
+
|
|
625
|
+
player.hooks.expressionEvaluator.tap("test", (ev) => {
|
|
626
|
+
ev.addExpressionFunction("test", (context, ...args) => {
|
|
627
|
+
testExpression(...args);
|
|
628
|
+
});
|
|
629
|
+
});
|
|
630
|
+
|
|
631
|
+
player.start(flowNum);
|
|
632
|
+
|
|
633
|
+
const getCurrent = () => {
|
|
634
|
+
const status = player.getState();
|
|
635
|
+
if (status.status === "in-progress") {
|
|
636
|
+
const view = status.controllers.view.currentView?.lastUpdate;
|
|
637
|
+
if (view) {
|
|
638
|
+
return view;
|
|
639
|
+
}
|
|
640
|
+
}
|
|
641
|
+
};
|
|
642
|
+
|
|
643
|
+
expect(getState().controllers.data.get("count")).toStrictEqual(0);
|
|
644
|
+
|
|
645
|
+
getCurrent()?.actions[0].asset.run();
|
|
646
|
+
|
|
647
|
+
expect(getState().controllers.data.get("count")).toStrictEqual(1);
|
|
648
|
+
|
|
649
|
+
expect(testExpression).toHaveBeenCalledWith("count has changed 1");
|
|
650
|
+
});
|
|
651
|
+
});
|