@thoughtspot/ts-chart-sdk 0.0.1-alpha.1 → 0.0.1-alpha.11

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.
Files changed (109) hide show
  1. package/cjs/index.d.ts +1 -0
  2. package/cjs/index.d.ts.map +1 -1
  3. package/cjs/index.js +1 -0
  4. package/cjs/index.js.map +1 -1
  5. package/cjs/main/custom-chart-context.d.ts +10 -2
  6. package/cjs/main/custom-chart-context.d.ts.map +1 -1
  7. package/cjs/main/custom-chart-context.js +117 -2
  8. package/cjs/main/custom-chart-context.js.map +1 -1
  9. package/cjs/main/custom-chart-context.spec.js +320 -0
  10. package/cjs/main/custom-chart-context.spec.js.map +1 -1
  11. package/cjs/main/post-message-event-bridge.js +1 -1
  12. package/cjs/main/post-message-event-bridge.js.map +1 -1
  13. package/cjs/main/post-message-event-bridge.spec.js +5 -3
  14. package/cjs/main/post-message-event-bridge.spec.js.map +1 -1
  15. package/cjs/react/mocks/custom-chart-context-mock.d.ts +3 -0
  16. package/cjs/react/mocks/custom-chart-context-mock.d.ts.map +1 -0
  17. package/cjs/react/mocks/custom-chart-context-mock.js +100 -0
  18. package/cjs/react/mocks/custom-chart-context-mock.js.map +1 -0
  19. package/cjs/react/use-custom-chart-context.d.ts +4 -0
  20. package/cjs/react/use-custom-chart-context.d.ts.map +1 -0
  21. package/cjs/react/use-custom-chart-context.js +95 -0
  22. package/cjs/react/use-custom-chart-context.js.map +1 -0
  23. package/cjs/react/use-custom-chart-context.spec.d.ts +2 -0
  24. package/cjs/react/use-custom-chart-context.spec.d.ts.map +1 -0
  25. package/cjs/react/use-custom-chart-context.spec.js +391 -0
  26. package/cjs/react/use-custom-chart-context.spec.js.map +1 -0
  27. package/cjs/react/use-custom-chart-context.util.d.ts +6 -0
  28. package/cjs/react/use-custom-chart-context.util.d.ts.map +1 -0
  29. package/cjs/react/use-custom-chart-context.util.js +60 -0
  30. package/cjs/react/use-custom-chart-context.util.js.map +1 -0
  31. package/cjs/react/use-custom-chart-types.d.ts +23 -0
  32. package/cjs/react/use-custom-chart-types.d.ts.map +1 -0
  33. package/cjs/react/use-custom-chart-types.js +3 -0
  34. package/cjs/react/use-custom-chart-types.js.map +1 -0
  35. package/cjs/types/chart-to-ts-event.types.d.ts +54 -4
  36. package/cjs/types/chart-to-ts-event.types.d.ts.map +1 -1
  37. package/cjs/types/chart-to-ts-event.types.js +14 -1
  38. package/cjs/types/chart-to-ts-event.types.js.map +1 -1
  39. package/cjs/types/common.types.d.ts +2 -0
  40. package/cjs/types/common.types.d.ts.map +1 -1
  41. package/cjs/types/ts-to-chart-event.types.d.ts +23 -1
  42. package/cjs/types/ts-to-chart-event.types.d.ts.map +1 -1
  43. package/cjs/types/ts-to-chart-event.types.js +2 -0
  44. package/cjs/types/ts-to-chart-event.types.js.map +1 -1
  45. package/cjs/types/visual-prop.types.d.ts +27 -1
  46. package/cjs/types/visual-prop.types.d.ts.map +1 -1
  47. package/lib/index.d.ts +1 -0
  48. package/lib/index.d.ts.map +1 -1
  49. package/lib/index.js +1 -0
  50. package/lib/index.js.map +1 -1
  51. package/lib/main/custom-chart-context.d.ts +12 -4
  52. package/lib/main/custom-chart-context.d.ts.map +1 -1
  53. package/lib/main/custom-chart-context.js +119 -4
  54. package/lib/main/custom-chart-context.js.map +1 -1
  55. package/lib/main/custom-chart-context.spec.js +317 -0
  56. package/lib/main/custom-chart-context.spec.js.map +1 -1
  57. package/lib/main/post-message-event-bridge.js +1 -1
  58. package/lib/main/post-message-event-bridge.js.map +1 -1
  59. package/lib/main/post-message-event-bridge.spec.js +5 -3
  60. package/lib/main/post-message-event-bridge.spec.js.map +1 -1
  61. package/lib/react/mocks/custom-chart-context-mock.d.ts +3 -0
  62. package/lib/react/mocks/custom-chart-context-mock.d.ts.map +1 -0
  63. package/lib/react/mocks/custom-chart-context-mock.js +94 -0
  64. package/lib/react/mocks/custom-chart-context-mock.js.map +1 -0
  65. package/lib/react/use-custom-chart-context.d.ts +4 -0
  66. package/lib/react/use-custom-chart-context.d.ts.map +1 -0
  67. package/lib/react/use-custom-chart-context.js +68 -0
  68. package/lib/react/use-custom-chart-context.js.map +1 -0
  69. package/lib/react/use-custom-chart-context.spec.d.ts +2 -0
  70. package/lib/react/use-custom-chart-context.spec.d.ts.map +1 -0
  71. package/lib/react/use-custom-chart-context.spec.js +363 -0
  72. package/lib/react/use-custom-chart-context.spec.js.map +1 -0
  73. package/lib/react/use-custom-chart-context.util.d.ts +6 -0
  74. package/lib/react/use-custom-chart-context.util.d.ts.map +1 -0
  75. package/lib/react/use-custom-chart-context.util.js +51 -0
  76. package/lib/react/use-custom-chart-context.util.js.map +1 -0
  77. package/lib/react/use-custom-chart-types.d.ts +23 -0
  78. package/lib/react/use-custom-chart-types.d.ts.map +1 -0
  79. package/lib/react/use-custom-chart-types.js +2 -0
  80. package/lib/react/use-custom-chart-types.js.map +1 -0
  81. package/lib/types/chart-to-ts-event.types.d.ts +54 -4
  82. package/lib/types/chart-to-ts-event.types.d.ts.map +1 -1
  83. package/lib/types/chart-to-ts-event.types.js +13 -0
  84. package/lib/types/chart-to-ts-event.types.js.map +1 -1
  85. package/lib/types/common.types.d.ts +7 -5
  86. package/lib/types/common.types.d.ts.map +1 -1
  87. package/lib/types/ts-to-chart-event.types.d.ts +23 -1
  88. package/lib/types/ts-to-chart-event.types.d.ts.map +1 -1
  89. package/lib/types/ts-to-chart-event.types.js +2 -0
  90. package/lib/types/ts-to-chart-event.types.js.map +1 -1
  91. package/lib/types/visual-prop.types.d.ts +27 -1
  92. package/lib/types/visual-prop.types.d.ts.map +1 -1
  93. package/package.json +8 -2
  94. package/src/index.ts +1 -0
  95. package/src/main/custom-chart-context.spec.ts +426 -0
  96. package/src/main/custom-chart-context.ts +249 -4
  97. package/src/main/post-message-event-bridge.spec.ts +5 -3
  98. package/src/main/post-message-event-bridge.ts +1 -1
  99. package/src/react/mocks/custom-chart-context-mock.ts +114 -0
  100. package/src/react/use-custom-chart-context.spec.tsx +464 -0
  101. package/src/react/use-custom-chart-context.tsx +180 -0
  102. package/src/react/use-custom-chart-context.util.ts +101 -0
  103. package/src/react/use-custom-chart-types.ts +70 -0
  104. package/src/types/chart-to-ts-event.types.ts +215 -3
  105. package/src/types/common.types.ts +15 -1
  106. package/src/types/ts-to-chart-event.types.ts +70 -0
  107. package/src/types/visual-prop.types.ts +137 -0
  108. package/dist/ts-chart-sdk.d.ts +0 -312
  109. package/lib/dist/ts-chart-sdk.d.ts +0 -312
@@ -4,6 +4,7 @@
4
4
  * @author Chetan Agrawal <chetan.agrawal@thoughtspot.com>
5
5
  */
6
6
 
7
+ import _ from 'lodash';
7
8
  import { mockInitializeContextPayload } from '../test/test-utils';
8
9
  import { ChartToTSEvent, ErrorType } from '../types/chart-to-ts-event.types';
9
10
  import { TSToChartEvent } from '../types/ts-to-chart-event.types';
@@ -330,6 +331,71 @@ describe('CustomChartContext', () => {
330
331
  expect(customChartContext.getChartModel().visualProps).toBe(
331
332
  'random data',
332
333
  );
334
+ mockPostMessage.mockReset();
335
+
336
+ // mock the event trigger for AxisMenuActionClick
337
+ customChartContext.axisMenuCustomActionPreProcessor([
338
+ {
339
+ customActions: [
340
+ {
341
+ icon: '',
342
+ id: 'custom-action-1',
343
+ label: 'Custom user action 1',
344
+ onClick: () => _.noop(),
345
+ },
346
+ ],
347
+ },
348
+ ] as any);
349
+ eventProcessor({
350
+ data: {
351
+ payload: {
352
+ customAction: {
353
+ id: 'custom-action-1',
354
+ columnIds: ['9f96b5b0-f7e4-4a5e-aa11-4c77fdf42125'],
355
+ },
356
+ },
357
+ eventType: TSToChartEvent.AxisMenuActionClick,
358
+ source: 'ts-host-app',
359
+ },
360
+ ports: [{ postMessage: mockPostMessage }],
361
+ });
362
+ // Check that the response was valid which means action gets
363
+ // executed successfully
364
+ expect(mockPostMessage).toHaveBeenCalledWith({
365
+ isValid: true,
366
+ });
367
+
368
+ // mock the event trigger for ContextMenuActionClick
369
+ customChartContext.contextMenuCustomActionPreProcessor([
370
+ {
371
+ customActions: [
372
+ {
373
+ icon: '',
374
+ id: 'custom-action-1',
375
+ label: 'Custom user action 1',
376
+ onClick: () => _.noop(),
377
+ },
378
+ ],
379
+ },
380
+ ] as any);
381
+ eventProcessor({
382
+ data: {
383
+ payload: {
384
+ customAction: {
385
+ id: 'custom-action-1',
386
+ columnIds: ['9f96b5b0-f7e4-4a5e-aa11-4c77fdf42125'],
387
+ },
388
+ },
389
+ eventType: TSToChartEvent.ContextMenuActionClick,
390
+ source: 'ts-host-app',
391
+ },
392
+ ports: [{ postMessage: mockPostMessage }],
393
+ });
394
+ // Check that the response was valid which means action gets
395
+ // executed successfully
396
+ expect(mockPostMessage).toHaveBeenCalledWith({
397
+ isValid: true,
398
+ });
333
399
  });
334
400
 
335
401
  test('should add an event listener to the specified event type', () => {
@@ -380,6 +446,122 @@ describe('CustomChartContext', () => {
380
446
  });
381
447
  });
382
448
 
449
+ describe('off', () => {
450
+ let customChartContext: CustomChartContext;
451
+
452
+ beforeEach(() => {
453
+ customChartContext = new CustomChartContext({
454
+ getDefaultChartConfig,
455
+ getQueriesFromChartConfig,
456
+ renderChart,
457
+ });
458
+ });
459
+
460
+ afterEach(() => {
461
+ // Destroy the chart context after each test
462
+ customChartContext.destroy();
463
+ eventProcessor = null;
464
+ jest.resetAllMocks();
465
+ });
466
+
467
+ test('should remove the event listener for the specified event type', () => {
468
+ // Define a test event type and callback function
469
+ const TEST_EVENT_TYPE = 'testEventType' as any;
470
+ const testCallbackFn = jest.fn();
471
+ // Call the on function with the test event type and callback
472
+ // function to add the event listener
473
+ customChartContext.on(TEST_EVENT_TYPE, testCallbackFn);
474
+
475
+ // mock the event trigger for the test event type
476
+ const mockPostMessage = jest.fn();
477
+ eventProcessor({
478
+ data: {
479
+ payload: mockInitializeContextPayload,
480
+ eventType: TEST_EVENT_TYPE,
481
+ source: 'ts-host-app',
482
+ },
483
+ ports: [{ postMessage: mockPostMessage }],
484
+ });
485
+
486
+ // Call the off function to remove the event listener
487
+ customChartContext.off(TEST_EVENT_TYPE);
488
+
489
+ // Check that the event listener was not called
490
+ expect(testCallbackFn).toHaveBeenCalled();
491
+ expect(
492
+ (customChartContext as any).eventListeners[TEST_EVENT_TYPE],
493
+ ).toHaveLength(0);
494
+ });
495
+
496
+ test('should remove a specific event listener without affecting others', () => {
497
+ // Define two test event types and callback functions
498
+ const TEST_EVENT_TYPE_1: any = 'testEventType1';
499
+ const TEST_EVENT_TYPE_2: any = 'testEventType2';
500
+ const testCallbackFn1 = jest.fn();
501
+ const testCallbackFn2 = jest.fn();
502
+
503
+ // Add event listeners with the test event types and callback
504
+ // functions
505
+ customChartContext.on(TEST_EVENT_TYPE_1, testCallbackFn1);
506
+ customChartContext.on(TEST_EVENT_TYPE_2, testCallbackFn2);
507
+
508
+ // Verify that both event listeners are in the eventListeners array
509
+ expect(
510
+ (customChartContext as any).eventListeners[TEST_EVENT_TYPE_1],
511
+ ).toHaveLength(1);
512
+ expect(
513
+ (customChartContext as any).eventListeners[TEST_EVENT_TYPE_2],
514
+ ).toHaveLength(1);
515
+
516
+ // Emit events for both event types
517
+ const mockPostMessage = jest.fn();
518
+ eventProcessor({
519
+ data: {
520
+ payload: {},
521
+ eventType: TEST_EVENT_TYPE_1,
522
+ source: 'ts-host-app',
523
+ },
524
+ ports: [{ postMessage: mockPostMessage }],
525
+ });
526
+ eventProcessor({
527
+ data: {
528
+ payload: {},
529
+ eventType: TEST_EVENT_TYPE_2,
530
+ source: 'ts-host-app',
531
+ },
532
+ ports: [{ postMessage: mockPostMessage }],
533
+ });
534
+
535
+ // Check that both callback functions were called
536
+ expect(testCallbackFn1).toHaveBeenCalled();
537
+ expect(testCallbackFn2).toHaveBeenCalled();
538
+
539
+ // Remove the first event listener (TEST_EVENT_TYPE_1)
540
+ customChartContext.off(TEST_EVENT_TYPE_1);
541
+
542
+ // Verify that the first event listener is removed while the second
543
+ // is still present
544
+ expect(
545
+ (customChartContext as any).eventListeners[TEST_EVENT_TYPE_1],
546
+ ).toHaveLength(0);
547
+ expect(
548
+ (customChartContext as any).eventListeners[TEST_EVENT_TYPE_2],
549
+ ).toHaveLength(1);
550
+ });
551
+
552
+ test('should make sure not to throw error when off is called before', () => {
553
+ // Define two test event types and callback functions
554
+ const TEST_EVENT_TYPE_1: any = 'testEventType1';
555
+ customChartContext.off(TEST_EVENT_TYPE_1);
556
+
557
+ // Verify that the first event listener is removed while the second
558
+ // is still present
559
+ expect(
560
+ (customChartContext as any).eventListeners[TEST_EVENT_TYPE_1],
561
+ ).toHaveLength(0);
562
+ });
563
+ });
564
+
383
565
  describe('emitEvent', () => {
384
566
  let customChartContext: CustomChartContext;
385
567
 
@@ -489,5 +671,249 @@ describe('CustomChartContext', () => {
489
671
  ChartToTSEvent.RenderStart,
490
672
  );
491
673
  });
674
+ test('should process the event payload for context menu custom actions', async () => {
675
+ const mockPostMessage = jest.fn();
676
+ const mockCustomAction = jest.fn();
677
+ let resolve: any;
678
+ eventProcessor({
679
+ data: {
680
+ payload: mockInitializeContextPayload,
681
+ eventType: TSToChartEvent.Initialize,
682
+ source: 'ts-host-app',
683
+ },
684
+ ports: [{ postMessage: mockPostMessage }],
685
+ });
686
+ eventProcessor({
687
+ data: {
688
+ payload: {},
689
+ eventType: TSToChartEvent.InitializeComplete,
690
+ source: 'ts-host-app',
691
+ },
692
+ ports: [{ postMessage: mockPostMessage }],
693
+ });
694
+ customChartContext.initialize();
695
+ mockPostMessageToHost.mockImplementation(
696
+ () =>
697
+ new global.Promise<any>((res) => {
698
+ resolve = res;
699
+ }),
700
+ );
701
+
702
+ // Define a test event type and payload
703
+ const TEST_EVENT_TYPE = ChartToTSEvent.OpenContextMenu as any;
704
+ const testPayload = {
705
+ customActions: [
706
+ {
707
+ id: 'custom-action-1',
708
+ label: 'Custom user action 1',
709
+ icon: '',
710
+ onClick: mockCustomAction,
711
+ },
712
+ ],
713
+ };
714
+
715
+ // Call the emitEvent function and wait for it to resolve
716
+ customChartContext.emitEvent(TEST_EVENT_TYPE, testPayload);
717
+
718
+ // Check that the result is defined
719
+ expect(mockPostMessageToHost).toHaveBeenCalledWith(
720
+ mockInitializeContextPayload.componentId,
721
+ mockInitializeContextPayload.hostUrl,
722
+ {
723
+ customActions: [
724
+ {
725
+ icon: '',
726
+ id: 'custom-action-1',
727
+ label: 'Custom user action 1',
728
+ },
729
+ ],
730
+ },
731
+ TEST_EVENT_TYPE,
732
+ );
733
+ eventProcessor({
734
+ data: {
735
+ payload: {
736
+ customAction: {
737
+ id: 'custom-action-1',
738
+ clickedPoint: {},
739
+ event: {},
740
+ selectedPoints: [{}],
741
+ },
742
+ },
743
+ eventType: TSToChartEvent.ContextMenuActionClick,
744
+ source: 'ts-host-app',
745
+ },
746
+ ports: [{ postMessage: mockPostMessage }],
747
+ });
748
+ expect(mockCustomAction).toHaveBeenCalled();
749
+ expect(mockCustomAction).toHaveBeenCalledWith({
750
+ clickedPoint: {},
751
+ event: {},
752
+ id: 'custom-action-1',
753
+ selectedPoints: [{}],
754
+ });
755
+ });
756
+ test('should process the event payload for axis menu custom actions', async () => {
757
+ const mockPostMessage = jest.fn();
758
+ const mockCustomAction = jest.fn();
759
+ let resolve: any;
760
+ eventProcessor({
761
+ data: {
762
+ payload: mockInitializeContextPayload,
763
+ eventType: TSToChartEvent.Initialize,
764
+ source: 'ts-host-app',
765
+ },
766
+ ports: [{ postMessage: mockPostMessage }],
767
+ });
768
+ eventProcessor({
769
+ data: {
770
+ payload: {},
771
+ eventType: TSToChartEvent.InitializeComplete,
772
+ source: 'ts-host-app',
773
+ },
774
+ ports: [{ postMessage: mockPostMessage }],
775
+ });
776
+ customChartContext.initialize();
777
+ mockPostMessageToHost.mockImplementation(
778
+ () =>
779
+ new global.Promise<any>((res) => {
780
+ resolve = res;
781
+ }),
782
+ );
783
+
784
+ // Define a test event type and payload
785
+ const TEST_EVENT_TYPE = ChartToTSEvent.OpenAxisMenu as any;
786
+ const testPayload = {
787
+ customActions: [
788
+ {
789
+ id: 'custom-action-1',
790
+ label: 'Custom user action 1',
791
+ icon: '',
792
+ onClick: mockCustomAction,
793
+ },
794
+ ],
795
+ };
796
+
797
+ // Call the emitEvent function and wait for it to resolve
798
+ customChartContext.emitEvent(TEST_EVENT_TYPE, testPayload);
799
+ // Check that the result is defined
800
+ expect(mockPostMessageToHost).toHaveBeenCalledWith(
801
+ mockInitializeContextPayload.componentId,
802
+ mockInitializeContextPayload.hostUrl,
803
+ {
804
+ customActions: [
805
+ {
806
+ icon: '',
807
+ id: 'custom-action-1',
808
+ label: 'Custom user action 1',
809
+ },
810
+ ],
811
+ },
812
+ TEST_EVENT_TYPE,
813
+ );
814
+
815
+ eventProcessor({
816
+ data: {
817
+ payload: {
818
+ customAction: {
819
+ id: 'custom-action-1',
820
+ columnIds: ['123'],
821
+ event: {},
822
+ },
823
+ },
824
+ eventType: TSToChartEvent.AxisMenuActionClick,
825
+ source: 'ts-host-app',
826
+ },
827
+ ports: [{ postMessage: mockPostMessage }],
828
+ });
829
+ expect(mockCustomAction).toHaveBeenCalled();
830
+ expect(mockCustomAction).toHaveBeenCalledWith({
831
+ id: 'custom-action-1',
832
+ columnIds: ['123'],
833
+ event: {},
834
+ });
835
+ });
836
+
837
+ test('should process the event payload for axis menu when custom actions are not defined', async () => {
838
+ const mockPostMessage = jest.fn();
839
+ let resolve: any;
840
+ eventProcessor({
841
+ data: {
842
+ payload: mockInitializeContextPayload,
843
+ eventType: TSToChartEvent.Initialize,
844
+ source: 'ts-host-app',
845
+ },
846
+ ports: [{ postMessage: mockPostMessage }],
847
+ });
848
+ eventProcessor({
849
+ data: {
850
+ payload: {},
851
+ eventType: TSToChartEvent.InitializeComplete,
852
+ source: 'ts-host-app',
853
+ },
854
+ ports: [{ postMessage: mockPostMessage }],
855
+ });
856
+ customChartContext.initialize();
857
+ mockPostMessageToHost.mockImplementation(
858
+ () =>
859
+ new global.Promise<any>((res) => {
860
+ resolve = res;
861
+ }),
862
+ );
863
+
864
+ // Define a test event type and payload
865
+ const TEST_EVENT_TYPE = ChartToTSEvent.OpenAxisMenu as any;
866
+ const testPayload = {};
867
+ // Call the emitEvent function and wait for it to resolve
868
+ customChartContext.emitEvent(TEST_EVENT_TYPE, testPayload);
869
+ // Check that the result is defined
870
+ expect(mockPostMessageToHost).toHaveBeenCalledWith(
871
+ mockInitializeContextPayload.componentId,
872
+ mockInitializeContextPayload.hostUrl,
873
+ {},
874
+ TEST_EVENT_TYPE,
875
+ );
876
+ });
877
+
878
+ test('should process the event payload for context menu when custom actions are not defined', async () => {
879
+ const mockPostMessage = jest.fn();
880
+ let resolve: any;
881
+ eventProcessor({
882
+ data: {
883
+ payload: mockInitializeContextPayload,
884
+ eventType: TSToChartEvent.Initialize,
885
+ source: 'ts-host-app',
886
+ },
887
+ ports: [{ postMessage: mockPostMessage }],
888
+ });
889
+ eventProcessor({
890
+ data: {
891
+ payload: {},
892
+ eventType: TSToChartEvent.InitializeComplete,
893
+ source: 'ts-host-app',
894
+ },
895
+ ports: [{ postMessage: mockPostMessage }],
896
+ });
897
+ customChartContext.initialize();
898
+ mockPostMessageToHost.mockImplementation(
899
+ () =>
900
+ new global.Promise<any>((res) => {
901
+ resolve = res;
902
+ }),
903
+ );
904
+
905
+ // Define a test event type and payload
906
+ const TEST_EVENT_TYPE = ChartToTSEvent.OpenContextMenu as any;
907
+ const testPayload = {};
908
+ // Call the emitEvent function and wait for it to resolve
909
+ customChartContext.emitEvent(TEST_EVENT_TYPE, testPayload);
910
+ // Check that the result is defined
911
+ expect(mockPostMessageToHost).toHaveBeenCalledWith(
912
+ mockInitializeContextPayload.componentId,
913
+ mockInitializeContextPayload.hostUrl,
914
+ {},
915
+ TEST_EVENT_TYPE,
916
+ );
917
+ });
492
918
  });
493
919
  });