@thoughtspot/ts-chart-sdk 0.0.1-alpha.3 → 0.0.1-alpha.6

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 (116) 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 +2 -1
  6. package/cjs/main/custom-chart-context.d.ts.map +1 -1
  7. package/cjs/main/custom-chart-context.js +10 -2
  8. package/cjs/main/custom-chart-context.js.map +1 -1
  9. package/cjs/main/custom-chart-context.spec.js +77 -0
  10. package/cjs/main/custom-chart-context.spec.js.map +1 -1
  11. package/cjs/react/custom-chart-context-hook.d.ts +22 -0
  12. package/cjs/react/custom-chart-context-hook.d.ts.map +1 -0
  13. package/cjs/react/custom-chart-context-hook.js +116 -0
  14. package/cjs/react/custom-chart-context-hook.js.map +1 -0
  15. package/cjs/react/custom-chart-context-hook.spec.d.ts +2 -0
  16. package/cjs/react/custom-chart-context-hook.spec.d.ts.map +1 -0
  17. package/cjs/react/custom-chart-context-hook.spec.js +343 -0
  18. package/cjs/react/custom-chart-context-hook.spec.js.map +1 -0
  19. package/cjs/react/custom-chart-context-service.d.ts +5 -16
  20. package/cjs/react/custom-chart-context-service.d.ts.map +1 -1
  21. package/cjs/react/custom-chart-context-service.js +34 -35
  22. package/cjs/react/custom-chart-context-service.js.map +1 -1
  23. package/cjs/react/custom-chart.spec.js +282 -53
  24. package/cjs/react/custom-chart.spec.js.map +1 -1
  25. package/cjs/react/mocks/custom-chart-context-hook.d.ts +3 -0
  26. package/cjs/react/mocks/custom-chart-context-hook.d.ts.map +1 -0
  27. package/cjs/react/mocks/custom-chart-context-hook.js +100 -0
  28. package/cjs/react/mocks/custom-chart-context-hook.js.map +1 -0
  29. package/cjs/react/mocks/custom-chart-context-mock.d.ts +3 -0
  30. package/cjs/react/mocks/custom-chart-context-mock.d.ts.map +1 -0
  31. package/cjs/react/mocks/custom-chart-context-mock.js +102 -0
  32. package/cjs/react/mocks/custom-chart-context-mock.js.map +1 -0
  33. package/cjs/react/use-custom-chart-context.d.ts +4 -0
  34. package/cjs/react/use-custom-chart-context.d.ts.map +1 -0
  35. package/cjs/react/use-custom-chart-context.js +95 -0
  36. package/cjs/react/use-custom-chart-context.js.map +1 -0
  37. package/cjs/react/use-custom-chart-context.spec.d.ts +2 -0
  38. package/cjs/react/use-custom-chart-context.spec.d.ts.map +1 -0
  39. package/cjs/react/use-custom-chart-context.spec.js +391 -0
  40. package/cjs/react/use-custom-chart-context.spec.js.map +1 -0
  41. package/cjs/react/use-custom-chart-context.util.d.ts +6 -0
  42. package/cjs/react/use-custom-chart-context.util.d.ts.map +1 -0
  43. package/cjs/react/use-custom-chart-context.util.js +60 -0
  44. package/cjs/react/use-custom-chart-context.util.js.map +1 -0
  45. package/cjs/react/use-custom-chart-types.d.ts +23 -0
  46. package/cjs/react/use-custom-chart-types.d.ts.map +1 -0
  47. package/cjs/react/use-custom-chart-types.js +3 -0
  48. package/cjs/react/use-custom-chart-types.js.map +1 -0
  49. package/cjs/types/chart-to-ts-event.types.d.ts +12 -4
  50. package/cjs/types/chart-to-ts-event.types.d.ts.map +1 -1
  51. package/cjs/types/chart-to-ts-event.types.js +2 -0
  52. package/cjs/types/chart-to-ts-event.types.js.map +1 -1
  53. package/lib/index.d.ts +1 -0
  54. package/lib/index.d.ts.map +1 -1
  55. package/lib/index.js +1 -0
  56. package/lib/index.js.map +1 -1
  57. package/lib/main/custom-chart-context.d.ts +2 -1
  58. package/lib/main/custom-chart-context.d.ts.map +1 -1
  59. package/lib/main/custom-chart-context.js +10 -2
  60. package/lib/main/custom-chart-context.js.map +1 -1
  61. package/lib/main/custom-chart-context.spec.js +77 -0
  62. package/lib/main/custom-chart-context.spec.js.map +1 -1
  63. package/lib/react/custom-chart-context-hook.d.ts +22 -0
  64. package/lib/react/custom-chart-context-hook.d.ts.map +1 -0
  65. package/lib/react/custom-chart-context-hook.js +86 -0
  66. package/lib/react/custom-chart-context-hook.js.map +1 -0
  67. package/lib/react/custom-chart-context-hook.spec.d.ts +2 -0
  68. package/lib/react/custom-chart-context-hook.spec.d.ts.map +1 -0
  69. package/lib/react/custom-chart-context-hook.spec.js +315 -0
  70. package/lib/react/custom-chart-context-hook.spec.js.map +1 -0
  71. package/lib/react/custom-chart-context-service.d.ts +5 -16
  72. package/lib/react/custom-chart-context-service.d.ts.map +1 -1
  73. package/lib/react/custom-chart-context-service.js +34 -35
  74. package/lib/react/custom-chart-context-service.js.map +1 -1
  75. package/lib/react/custom-chart.spec.js +260 -54
  76. package/lib/react/custom-chart.spec.js.map +1 -1
  77. package/lib/react/mocks/custom-chart-context-hook.d.ts +3 -0
  78. package/lib/react/mocks/custom-chart-context-hook.d.ts.map +1 -0
  79. package/lib/react/mocks/custom-chart-context-hook.js +94 -0
  80. package/lib/react/mocks/custom-chart-context-hook.js.map +1 -0
  81. package/lib/react/mocks/custom-chart-context-mock.d.ts +3 -0
  82. package/lib/react/mocks/custom-chart-context-mock.d.ts.map +1 -0
  83. package/lib/react/mocks/custom-chart-context-mock.js +96 -0
  84. package/lib/react/mocks/custom-chart-context-mock.js.map +1 -0
  85. package/lib/react/use-custom-chart-context.d.ts +4 -0
  86. package/lib/react/use-custom-chart-context.d.ts.map +1 -0
  87. package/lib/react/use-custom-chart-context.js +68 -0
  88. package/lib/react/use-custom-chart-context.js.map +1 -0
  89. package/lib/react/use-custom-chart-context.spec.d.ts +2 -0
  90. package/lib/react/use-custom-chart-context.spec.d.ts.map +1 -0
  91. package/lib/react/use-custom-chart-context.spec.js +363 -0
  92. package/lib/react/use-custom-chart-context.spec.js.map +1 -0
  93. package/lib/react/use-custom-chart-context.util.d.ts +6 -0
  94. package/lib/react/use-custom-chart-context.util.d.ts.map +1 -0
  95. package/lib/react/use-custom-chart-context.util.js +51 -0
  96. package/lib/react/use-custom-chart-context.util.js.map +1 -0
  97. package/lib/react/use-custom-chart-types.d.ts +23 -0
  98. package/lib/react/use-custom-chart-types.d.ts.map +1 -0
  99. package/lib/react/use-custom-chart-types.js +2 -0
  100. package/lib/react/use-custom-chart-types.js.map +1 -0
  101. package/lib/types/chart-to-ts-event.types.d.ts +12 -4
  102. package/lib/types/chart-to-ts-event.types.d.ts.map +1 -1
  103. package/lib/types/chart-to-ts-event.types.js +2 -0
  104. package/lib/types/chart-to-ts-event.types.js.map +1 -1
  105. package/package.json +8 -2
  106. package/src/index.ts +1 -0
  107. package/src/main/custom-chart-context.spec.ts +124 -0
  108. package/src/main/custom-chart-context.ts +24 -4
  109. package/src/react/mocks/custom-chart-context-mock.ts +116 -0
  110. package/src/react/use-custom-chart-context.spec.tsx +464 -0
  111. package/src/react/use-custom-chart-context.tsx +180 -0
  112. package/src/react/use-custom-chart-context.util.ts +101 -0
  113. package/src/react/use-custom-chart-types.ts +70 -0
  114. package/src/types/chart-to-ts-event.types.ts +47 -2
  115. package/dist/ts-chart-sdk.d.ts +0 -312
  116. package/lib/dist/ts-chart-sdk.d.ts +0 -312
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@thoughtspot/ts-chart-sdk",
3
3
  "private": false,
4
- "version": "0.0.1-alpha.3",
4
+ "version": "0.0.1-alpha.6",
5
5
  "module": "lib/index",
6
6
  "main": "lib/index",
7
7
  "types": "lib/index",
@@ -33,9 +33,13 @@
33
33
  "publish-prod": "npm publish --access public"
34
34
  },
35
35
  "devDependencies": {
36
+ "@testing-library/react": "12.1.5",
37
+ "@testing-library/react-hooks": "^7.0.2",
36
38
  "@types/jest": "^27.0.3",
37
39
  "@types/lodash": "4.14.175",
38
40
  "@types/node": "18.16.3",
41
+ "@types/react": "^17.0.2",
42
+ "@types/react-dom": "^17.0.11",
39
43
  "@typescript-eslint/eslint-plugin": "^5.59.5",
40
44
  "@typescript-eslint/parser": "^5.59.5",
41
45
  "dts-bundle": "^0.7.3",
@@ -69,7 +73,9 @@
69
73
  "vite": "4.2.1"
70
74
  },
71
75
  "dependencies": {
72
- "lodash": "^4.17.21"
76
+ "lodash": "^4.17.21",
77
+ "react": "^17.0.2",
78
+ "react-dom": "^17.0.2"
73
79
  },
74
80
  "prettier": "./prettier-config",
75
81
  "browserslist": [
package/src/index.ts CHANGED
@@ -5,3 +5,4 @@ export * from './types/answer-column.types';
5
5
  export * from './types/chart-to-ts-event.types';
6
6
  export * from './types/ts-to-chart-event.types';
7
7
  export * from './main/custom-chart-context';
8
+ export * from './react/use-custom-chart-context';
@@ -20,6 +20,7 @@ describe('CustomChartContext', () => {
20
20
  let eventProcessor: any;
21
21
 
22
22
  let getDefaultChartConfig = jest.fn();
23
+ let getVisualPropEditorDefinition = jest.fn();
23
24
  let getQueriesFromChartConfig = jest.fn();
24
25
  let renderChart = jest.fn();
25
26
  const mockInitMessage = jest.spyOn(
@@ -33,6 +34,7 @@ describe('CustomChartContext', () => {
33
34
 
34
35
  beforeEach(() => {
35
36
  getDefaultChartConfig = jest.fn();
37
+ getVisualPropEditorDefinition = jest.fn();
36
38
  getQueriesFromChartConfig = jest.fn();
37
39
  renderChart = jest.fn();
38
40
 
@@ -52,6 +54,7 @@ describe('CustomChartContext', () => {
52
54
  getDefaultChartConfig,
53
55
  getQueriesFromChartConfig,
54
56
  renderChart,
57
+ getVisualPropEditorDefinition,
55
58
  });
56
59
  });
57
60
 
@@ -147,6 +150,7 @@ describe('CustomChartContext', () => {
147
150
  customChartContext = new CustomChartContext({
148
151
  getDefaultChartConfig,
149
152
  getQueriesFromChartConfig,
153
+ getVisualPropEditorDefinition,
150
154
  renderChart,
151
155
  });
152
156
  } catch (err: any) {
@@ -158,6 +162,7 @@ describe('CustomChartContext', () => {
158
162
  await getChartContext({
159
163
  getDefaultChartConfig,
160
164
  getQueriesFromChartConfig,
165
+ getVisualPropEditorDefinition,
161
166
  renderChart,
162
167
  });
163
168
  } catch (err: any) {
@@ -173,6 +178,7 @@ describe('CustomChartContext', () => {
173
178
  customChartContext = new CustomChartContext({
174
179
  getDefaultChartConfig,
175
180
  getQueriesFromChartConfig,
181
+ getVisualPropEditorDefinition,
176
182
  renderChart,
177
183
  });
178
184
  });
@@ -380,6 +386,123 @@ describe('CustomChartContext', () => {
380
386
  });
381
387
  });
382
388
 
389
+ describe('off', () => {
390
+ let customChartContext: CustomChartContext;
391
+
392
+ beforeEach(() => {
393
+ customChartContext = new CustomChartContext({
394
+ getDefaultChartConfig,
395
+ getQueriesFromChartConfig,
396
+ getVisualPropEditorDefinition,
397
+ renderChart,
398
+ });
399
+ });
400
+
401
+ afterEach(() => {
402
+ // Destroy the chart context after each test
403
+ customChartContext.destroy();
404
+ eventProcessor = null;
405
+ jest.resetAllMocks();
406
+ });
407
+
408
+ test('should remove the event listener for the specified event type', () => {
409
+ // Define a test event type and callback function
410
+ const TEST_EVENT_TYPE = 'testEventType' as any;
411
+ const testCallbackFn = jest.fn();
412
+ // Call the on function with the test event type and callback
413
+ // function to add the event listener
414
+ customChartContext.on(TEST_EVENT_TYPE, testCallbackFn);
415
+
416
+ // mock the event trigger for the test event type
417
+ const mockPostMessage = jest.fn();
418
+ eventProcessor({
419
+ data: {
420
+ payload: mockInitializeContextPayload,
421
+ eventType: TEST_EVENT_TYPE,
422
+ source: 'ts-host-app',
423
+ },
424
+ ports: [{ postMessage: mockPostMessage }],
425
+ });
426
+
427
+ // Call the off function to remove the event listener
428
+ customChartContext.off(TEST_EVENT_TYPE);
429
+
430
+ // Check that the event listener was not called
431
+ expect(testCallbackFn).toHaveBeenCalled();
432
+ expect(
433
+ (customChartContext as any).eventListeners[TEST_EVENT_TYPE],
434
+ ).toHaveLength(0);
435
+ });
436
+
437
+ test('should remove a specific event listener without affecting others', () => {
438
+ // Define two test event types and callback functions
439
+ const TEST_EVENT_TYPE_1: any = 'testEventType1';
440
+ const TEST_EVENT_TYPE_2: any = 'testEventType2';
441
+ const testCallbackFn1 = jest.fn();
442
+ const testCallbackFn2 = jest.fn();
443
+
444
+ // Add event listeners with the test event types and callback
445
+ // functions
446
+ customChartContext.on(TEST_EVENT_TYPE_1, testCallbackFn1);
447
+ customChartContext.on(TEST_EVENT_TYPE_2, testCallbackFn2);
448
+
449
+ // Verify that both event listeners are in the eventListeners array
450
+ expect(
451
+ (customChartContext as any).eventListeners[TEST_EVENT_TYPE_1],
452
+ ).toHaveLength(1);
453
+ expect(
454
+ (customChartContext as any).eventListeners[TEST_EVENT_TYPE_2],
455
+ ).toHaveLength(1);
456
+
457
+ // Emit events for both event types
458
+ const mockPostMessage = jest.fn();
459
+ eventProcessor({
460
+ data: {
461
+ payload: {},
462
+ eventType: TEST_EVENT_TYPE_1,
463
+ source: 'ts-host-app',
464
+ },
465
+ ports: [{ postMessage: mockPostMessage }],
466
+ });
467
+ eventProcessor({
468
+ data: {
469
+ payload: {},
470
+ eventType: TEST_EVENT_TYPE_2,
471
+ source: 'ts-host-app',
472
+ },
473
+ ports: [{ postMessage: mockPostMessage }],
474
+ });
475
+
476
+ // Check that both callback functions were called
477
+ expect(testCallbackFn1).toHaveBeenCalled();
478
+ expect(testCallbackFn2).toHaveBeenCalled();
479
+
480
+ // Remove the first event listener (TEST_EVENT_TYPE_1)
481
+ customChartContext.off(TEST_EVENT_TYPE_1);
482
+
483
+ // Verify that the first event listener is removed while the second
484
+ // is still present
485
+ expect(
486
+ (customChartContext as any).eventListeners[TEST_EVENT_TYPE_1],
487
+ ).toHaveLength(0);
488
+ expect(
489
+ (customChartContext as any).eventListeners[TEST_EVENT_TYPE_2],
490
+ ).toHaveLength(1);
491
+ });
492
+
493
+ test('should make sure not to throw error when off is called before', () => {
494
+ // Define two test event types and callback functions
495
+ const TEST_EVENT_TYPE_1: any = 'testEventType1';
496
+ customChartContext.off(TEST_EVENT_TYPE_1);
497
+
498
+ // Verify that the first event listener is removed while the second
499
+ // is still present
500
+ expect(
501
+ (customChartContext as any).eventListeners[TEST_EVENT_TYPE_1],
502
+ ).toHaveLength(0);
503
+ });
504
+ });
505
+
383
506
  describe('emitEvent', () => {
384
507
  let customChartContext: CustomChartContext;
385
508
 
@@ -387,6 +510,7 @@ describe('CustomChartContext', () => {
387
510
  customChartContext = new CustomChartContext({
388
511
  getDefaultChartConfig,
389
512
  getQueriesFromChartConfig,
513
+ getVisualPropEditorDefinition,
390
514
  renderChart,
391
515
  });
392
516
  });
@@ -104,9 +104,13 @@ export type CustomChartContextProps = {
104
104
  * on the TS app. If not provided, visual properties will not be configurable in
105
105
  * editor
106
106
  *
107
- * @version SDK: 0.1 | ThoughtSpot:
107
+ * @param chartModel
108
+ * @returns {@link VisualPropEditorDefinition}
109
+ * @version SDK: 0.0.1-alpha.6 | ThoughtSpot:
108
110
  */
109
- visualPropEditorDefinition?: VisualPropEditorDefinition;
111
+ getVisualPropEditorDefinition: (
112
+ chartModel: ChartModel,
113
+ ) => VisualPropEditorDefinition;
110
114
  };
111
115
 
112
116
  /**
@@ -116,7 +120,6 @@ const DEFAULT_CHART_CONTEXT_PROPS: Partial<CustomChartContextProps> = {
116
120
  validateConfig: () => ({ isValid: true }),
117
121
  validateVisualProps: () => ({ isValid: true }),
118
122
  chartConfigEditorDefinition: undefined,
119
- visualPropEditorDefinition: undefined,
120
123
  };
121
124
 
122
125
  export class CustomChartContext {
@@ -232,6 +235,21 @@ export class CustomChartContext {
232
235
  this.eventListeners[eventType].push(callbackFn);
233
236
  }
234
237
 
238
+ /**
239
+ * Removes specific event listeners on the chart context.
240
+ *
241
+ * @param {T} eventType
242
+ * @returns void
243
+ */
244
+ public off<T extends keyof TSToChartEventsPayloadMap>(eventType: T): void {
245
+ if (_.isNil(this.eventListeners[eventType])) {
246
+ console.log('No event listener found to remove');
247
+ this.eventListeners[eventType] = [];
248
+ return;
249
+ }
250
+ this.eventListeners[eventType].splice(0, 1);
251
+ }
252
+
235
253
  /**
236
254
  * Add internal event listeners on the chart context with the required callback.
237
255
  *
@@ -541,7 +559,9 @@ export class CustomChartContext {
541
559
  chartConfigEditorDefinition:
542
560
  this.chartContextProps.chartConfigEditorDefinition,
543
561
  visualPropEditorDefinition:
544
- this.chartContextProps.visualPropEditorDefinition,
562
+ this.chartContextProps.getVisualPropEditorDefinition(
563
+ this.chartModel,
564
+ ),
545
565
  };
546
566
  };
547
567
 
@@ -0,0 +1,116 @@
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
+ getVisualPropEditorDefinition: (chartModel: ChartModel) => {
81
+ return {
82
+ elements: [
83
+ {
84
+ key: 'color',
85
+ type: 'radio',
86
+ defaultValue: 'red',
87
+ values: ['red', 'green', 'yellow'],
88
+ label: 'Colors',
89
+ },
90
+ {
91
+ type: 'section',
92
+ key: 'accordion',
93
+ label: 'Accordion',
94
+ children: [
95
+ {
96
+ key: 'Color2',
97
+ type: 'radio',
98
+ defaultValue: 'blue',
99
+ values: ['blue', 'white', 'red'],
100
+ label: 'Color2',
101
+ },
102
+ {
103
+ key: 'datalabels',
104
+ type: 'toggle',
105
+ defaultValue: false,
106
+ label: 'Data Labels',
107
+ },
108
+ ],
109
+ },
110
+ ],
111
+ };
112
+ },
113
+ renderChart: (cx) => {
114
+ return Promise.resolve();
115
+ },
116
+ };