@thoughtspot/ts-chart-sdk 0.0.1-alpha.2 → 0.0.1-alpha.5

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 (83) 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 +1 -0
  6. package/cjs/main/custom-chart-context.d.ts.map +1 -1
  7. package/cjs/main/custom-chart-context.js +8 -0
  8. package/cjs/main/custom-chart-context.js.map +1 -1
  9. package/cjs/main/custom-chart-context.spec.js +69 -0
  10. package/cjs/main/custom-chart-context.spec.js.map +1 -1
  11. package/cjs/react/mocks/custom-chart-context-mock.d.ts +3 -0
  12. package/cjs/react/mocks/custom-chart-context-mock.d.ts.map +1 -0
  13. package/cjs/react/mocks/custom-chart-context-mock.js +100 -0
  14. package/cjs/react/mocks/custom-chart-context-mock.js.map +1 -0
  15. package/cjs/react/use-custom-chart-context.d.ts +4 -0
  16. package/cjs/react/use-custom-chart-context.d.ts.map +1 -0
  17. package/cjs/react/use-custom-chart-context.js +95 -0
  18. package/cjs/react/use-custom-chart-context.js.map +1 -0
  19. package/cjs/react/use-custom-chart-context.spec.d.ts +2 -0
  20. package/cjs/react/use-custom-chart-context.spec.d.ts.map +1 -0
  21. package/cjs/react/use-custom-chart-context.spec.js +391 -0
  22. package/cjs/react/use-custom-chart-context.spec.js.map +1 -0
  23. package/cjs/react/use-custom-chart-context.util.d.ts +6 -0
  24. package/cjs/react/use-custom-chart-context.util.d.ts.map +1 -0
  25. package/cjs/react/use-custom-chart-context.util.js +60 -0
  26. package/cjs/react/use-custom-chart-context.util.js.map +1 -0
  27. package/cjs/react/use-custom-chart-types.d.ts +23 -0
  28. package/cjs/react/use-custom-chart-types.d.ts.map +1 -0
  29. package/cjs/react/use-custom-chart-types.js +3 -0
  30. package/cjs/react/use-custom-chart-types.js.map +1 -0
  31. package/cjs/types/chart-to-ts-event.types.d.ts +12 -4
  32. package/cjs/types/chart-to-ts-event.types.d.ts.map +1 -1
  33. package/cjs/types/chart-to-ts-event.types.js +2 -0
  34. package/cjs/types/chart-to-ts-event.types.js.map +1 -1
  35. package/cjs/types/visual-prop.types.d.ts +12 -0
  36. package/cjs/types/visual-prop.types.d.ts.map +1 -1
  37. package/lib/index.d.ts +1 -0
  38. package/lib/index.d.ts.map +1 -1
  39. package/lib/index.js +1 -0
  40. package/lib/index.js.map +1 -1
  41. package/lib/main/custom-chart-context.d.ts +1 -0
  42. package/lib/main/custom-chart-context.d.ts.map +1 -1
  43. package/lib/main/custom-chart-context.js +8 -0
  44. package/lib/main/custom-chart-context.js.map +1 -1
  45. package/lib/main/custom-chart-context.spec.js +69 -0
  46. package/lib/main/custom-chart-context.spec.js.map +1 -1
  47. package/lib/react/mocks/custom-chart-context-mock.d.ts +3 -0
  48. package/lib/react/mocks/custom-chart-context-mock.d.ts.map +1 -0
  49. package/lib/react/mocks/custom-chart-context-mock.js +94 -0
  50. package/lib/react/mocks/custom-chart-context-mock.js.map +1 -0
  51. package/lib/react/use-custom-chart-context.d.ts +4 -0
  52. package/lib/react/use-custom-chart-context.d.ts.map +1 -0
  53. package/lib/react/use-custom-chart-context.js +68 -0
  54. package/lib/react/use-custom-chart-context.js.map +1 -0
  55. package/lib/react/use-custom-chart-context.spec.d.ts +2 -0
  56. package/lib/react/use-custom-chart-context.spec.d.ts.map +1 -0
  57. package/lib/react/use-custom-chart-context.spec.js +363 -0
  58. package/lib/react/use-custom-chart-context.spec.js.map +1 -0
  59. package/lib/react/use-custom-chart-context.util.d.ts +6 -0
  60. package/lib/react/use-custom-chart-context.util.d.ts.map +1 -0
  61. package/lib/react/use-custom-chart-context.util.js +51 -0
  62. package/lib/react/use-custom-chart-context.util.js.map +1 -0
  63. package/lib/react/use-custom-chart-types.d.ts +23 -0
  64. package/lib/react/use-custom-chart-types.d.ts.map +1 -0
  65. package/lib/react/use-custom-chart-types.js +2 -0
  66. package/lib/react/use-custom-chart-types.js.map +1 -0
  67. package/lib/types/chart-to-ts-event.types.d.ts +12 -4
  68. package/lib/types/chart-to-ts-event.types.d.ts.map +1 -1
  69. package/lib/types/chart-to-ts-event.types.js +2 -0
  70. package/lib/types/chart-to-ts-event.types.js.map +1 -1
  71. package/lib/types/visual-prop.types.d.ts +12 -0
  72. package/lib/types/visual-prop.types.d.ts.map +1 -1
  73. package/package.json +8 -2
  74. package/src/index.ts +1 -0
  75. package/src/main/custom-chart-context.spec.ts +116 -0
  76. package/src/main/custom-chart-context.ts +15 -0
  77. package/src/react/mocks/custom-chart-context-mock.ts +114 -0
  78. package/src/react/use-custom-chart-context.spec.tsx +464 -0
  79. package/src/react/use-custom-chart-context.tsx +180 -0
  80. package/src/react/use-custom-chart-context.util.ts +101 -0
  81. package/src/react/use-custom-chart-types.ts +70 -0
  82. package/src/types/chart-to-ts-event.types.ts +47 -2
  83. package/src/types/visual-prop.types.ts +68 -0
@@ -380,6 +380,122 @@ describe('CustomChartContext', () => {
380
380
  });
381
381
  });
382
382
 
383
+ describe('off', () => {
384
+ let customChartContext: CustomChartContext;
385
+
386
+ beforeEach(() => {
387
+ customChartContext = new CustomChartContext({
388
+ getDefaultChartConfig,
389
+ getQueriesFromChartConfig,
390
+ renderChart,
391
+ });
392
+ });
393
+
394
+ afterEach(() => {
395
+ // Destroy the chart context after each test
396
+ customChartContext.destroy();
397
+ eventProcessor = null;
398
+ jest.resetAllMocks();
399
+ });
400
+
401
+ test('should remove the event listener for the specified event type', () => {
402
+ // Define a test event type and callback function
403
+ const TEST_EVENT_TYPE = 'testEventType' as any;
404
+ const testCallbackFn = jest.fn();
405
+ // Call the on function with the test event type and callback
406
+ // function to add the event listener
407
+ customChartContext.on(TEST_EVENT_TYPE, testCallbackFn);
408
+
409
+ // mock the event trigger for the test event type
410
+ const mockPostMessage = jest.fn();
411
+ eventProcessor({
412
+ data: {
413
+ payload: mockInitializeContextPayload,
414
+ eventType: TEST_EVENT_TYPE,
415
+ source: 'ts-host-app',
416
+ },
417
+ ports: [{ postMessage: mockPostMessage }],
418
+ });
419
+
420
+ // Call the off function to remove the event listener
421
+ customChartContext.off(TEST_EVENT_TYPE);
422
+
423
+ // Check that the event listener was not called
424
+ expect(testCallbackFn).toHaveBeenCalled();
425
+ expect(
426
+ (customChartContext as any).eventListeners[TEST_EVENT_TYPE],
427
+ ).toHaveLength(0);
428
+ });
429
+
430
+ test('should remove a specific event listener without affecting others', () => {
431
+ // Define two test event types and callback functions
432
+ const TEST_EVENT_TYPE_1: any = 'testEventType1';
433
+ const TEST_EVENT_TYPE_2: any = 'testEventType2';
434
+ const testCallbackFn1 = jest.fn();
435
+ const testCallbackFn2 = jest.fn();
436
+
437
+ // Add event listeners with the test event types and callback
438
+ // functions
439
+ customChartContext.on(TEST_EVENT_TYPE_1, testCallbackFn1);
440
+ customChartContext.on(TEST_EVENT_TYPE_2, testCallbackFn2);
441
+
442
+ // Verify that both event listeners are in the eventListeners array
443
+ expect(
444
+ (customChartContext as any).eventListeners[TEST_EVENT_TYPE_1],
445
+ ).toHaveLength(1);
446
+ expect(
447
+ (customChartContext as any).eventListeners[TEST_EVENT_TYPE_2],
448
+ ).toHaveLength(1);
449
+
450
+ // Emit events for both event types
451
+ const mockPostMessage = jest.fn();
452
+ eventProcessor({
453
+ data: {
454
+ payload: {},
455
+ eventType: TEST_EVENT_TYPE_1,
456
+ source: 'ts-host-app',
457
+ },
458
+ ports: [{ postMessage: mockPostMessage }],
459
+ });
460
+ eventProcessor({
461
+ data: {
462
+ payload: {},
463
+ eventType: TEST_EVENT_TYPE_2,
464
+ source: 'ts-host-app',
465
+ },
466
+ ports: [{ postMessage: mockPostMessage }],
467
+ });
468
+
469
+ // Check that both callback functions were called
470
+ expect(testCallbackFn1).toHaveBeenCalled();
471
+ expect(testCallbackFn2).toHaveBeenCalled();
472
+
473
+ // Remove the first event listener (TEST_EVENT_TYPE_1)
474
+ customChartContext.off(TEST_EVENT_TYPE_1);
475
+
476
+ // Verify that the first event listener is removed while the second
477
+ // is still present
478
+ expect(
479
+ (customChartContext as any).eventListeners[TEST_EVENT_TYPE_1],
480
+ ).toHaveLength(0);
481
+ expect(
482
+ (customChartContext as any).eventListeners[TEST_EVENT_TYPE_2],
483
+ ).toHaveLength(1);
484
+ });
485
+
486
+ test('should make sure not to throw error when off is called before', () => {
487
+ // Define two test event types and callback functions
488
+ const TEST_EVENT_TYPE_1: any = 'testEventType1';
489
+ customChartContext.off(TEST_EVENT_TYPE_1);
490
+
491
+ // Verify that the first event listener is removed while the second
492
+ // is still present
493
+ expect(
494
+ (customChartContext as any).eventListeners[TEST_EVENT_TYPE_1],
495
+ ).toHaveLength(0);
496
+ });
497
+ });
498
+
383
499
  describe('emitEvent', () => {
384
500
  let customChartContext: CustomChartContext;
385
501
 
@@ -232,6 +232,21 @@ export class CustomChartContext {
232
232
  this.eventListeners[eventType].push(callbackFn);
233
233
  }
234
234
 
235
+ /**
236
+ * Removes specific event listeners on the chart context.
237
+ *
238
+ * @param {T} eventType
239
+ * @returns void
240
+ */
241
+ public off<T extends keyof TSToChartEventsPayloadMap>(eventType: T): void {
242
+ if (_.isNil(this.eventListeners[eventType])) {
243
+ console.log('No event listener found to remove');
244
+ this.eventListeners[eventType] = [];
245
+ return;
246
+ }
247
+ this.eventListeners[eventType].splice(0, 1);
248
+ }
249
+
235
250
  /**
236
251
  * Add internal event listeners on the chart context with the required callback.
237
252
  *
@@ -0,0 +1,114 @@
1
+ import _ from 'lodash';
2
+ import { CustomChartContextProps } from '../../main/custom-chart-context';
3
+ import { ColumnType } from '../../types/answer-column.types';
4
+ import { ChartConfig, ChartModel } from '../../types/common.types';
5
+ import { Query } from '../../types/ts-to-chart-event.types';
6
+
7
+ export const contextChartProps: CustomChartContextProps = {
8
+ getDefaultChartConfig: (chartModel: ChartModel): ChartConfig[] => {
9
+ const cols = chartModel.columns;
10
+
11
+ const measureColumns = _.filter(
12
+ cols,
13
+ (col) => col.type === ColumnType.MEASURE,
14
+ );
15
+
16
+ const attributeColumns = _.filter(
17
+ cols,
18
+ (col) => col.type === ColumnType.ATTRIBUTE,
19
+ );
20
+
21
+ const axisConfig: ChartConfig = {
22
+ key: 'column',
23
+ dimensions: [
24
+ {
25
+ key: 'x',
26
+ columns: [attributeColumns[0]],
27
+ },
28
+ {
29
+ key: 'y',
30
+ columns: measureColumns.slice(0, 2),
31
+ },
32
+ ],
33
+ };
34
+ return [axisConfig];
35
+ },
36
+ getQueriesFromChartConfig: (chartConfig: ChartConfig[]): Array<Query> => {
37
+ const queries = chartConfig.map(
38
+ (config: ChartConfig): Query =>
39
+ _.reduce(
40
+ config.dimensions,
41
+ (acc: Query, dimension) => ({
42
+ queryColumns: [
43
+ ...acc.queryColumns,
44
+ ...dimension.columns,
45
+ ],
46
+ }),
47
+ {
48
+ queryColumns: [],
49
+ } as Query,
50
+ ),
51
+ );
52
+ return queries;
53
+ },
54
+ chartConfigEditorDefinition: [
55
+ {
56
+ key: 'column',
57
+ label: 'Custom Column',
58
+ descriptionText:
59
+ 'X Axis can only have attributes, Y Axis can only have measures, Color can only have attributes. ' +
60
+ 'Should have just 1 column in Y axis with colors columns.',
61
+ columnSections: [
62
+ {
63
+ key: 'x',
64
+ label: 'Custom X Axis',
65
+ allowAttributeColumns: true,
66
+ allowMeasureColumns: false,
67
+ allowTimeSeriesColumns: true,
68
+ maxColumnCount: 1,
69
+ },
70
+ {
71
+ key: 'y',
72
+ label: 'Custom Y Axis',
73
+ allowAttributeColumns: false,
74
+ allowMeasureColumns: true,
75
+ allowTimeSeriesColumns: false,
76
+ },
77
+ ],
78
+ },
79
+ ],
80
+ visualPropEditorDefinition: {
81
+ elements: [
82
+ {
83
+ key: 'color',
84
+ type: 'radio',
85
+ defaultValue: 'red',
86
+ values: ['red', 'green', 'yellow'],
87
+ label: 'Colors',
88
+ },
89
+ {
90
+ type: 'section',
91
+ key: 'accordion',
92
+ label: 'Accordion',
93
+ children: [
94
+ {
95
+ key: 'Color2',
96
+ type: 'radio',
97
+ defaultValue: 'blue',
98
+ values: ['blue', 'white', 'red'],
99
+ label: 'Color2',
100
+ },
101
+ {
102
+ key: 'datalabels',
103
+ type: 'toggle',
104
+ defaultValue: false,
105
+ label: 'Data Labels',
106
+ },
107
+ ],
108
+ },
109
+ ],
110
+ },
111
+ renderChart: (cx) => {
112
+ return Promise.resolve();
113
+ },
114
+ };