@thoughtspot/ts-chart-sdk 0.0.2-alpha.1 → 0.0.2-alpha.10

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 (69) hide show
  1. package/dist/ts-chart-sdk.d.ts +730 -0
  2. package/lib/index.d.ts +3 -0
  3. package/lib/index.d.ts.map +1 -1
  4. package/lib/index.js +3 -0
  5. package/lib/index.js.map +1 -1
  6. package/lib/main/custom-chart-context.d.ts +9 -4
  7. package/lib/main/custom-chart-context.d.ts.map +1 -1
  8. package/lib/main/custom-chart-context.js +45 -2
  9. package/lib/main/custom-chart-context.js.map +1 -1
  10. package/lib/main/custom-chart-context.spec.js +155 -3
  11. package/lib/main/custom-chart-context.spec.js.map +1 -1
  12. package/lib/main/post-message-event-bridge.js +2 -2
  13. package/lib/main/post-message-event-bridge.js.map +1 -1
  14. package/lib/react/use-custom-chart-context.spec.js +54 -2
  15. package/lib/react/use-custom-chart-context.spec.js.map +1 -1
  16. package/lib/test/test-conditional-formatting-utils.d.ts +5 -0
  17. package/lib/test/test-conditional-formatting-utils.d.ts.map +1 -0
  18. package/lib/test/test-conditional-formatting-utils.js +43 -0
  19. package/lib/test/test-conditional-formatting-utils.js.map +1 -0
  20. package/lib/types/answer-column.types.d.ts +54 -1
  21. package/lib/types/answer-column.types.d.ts.map +1 -1
  22. package/lib/types/answer-column.types.js +47 -0
  23. package/lib/types/answer-column.types.js.map +1 -1
  24. package/lib/types/common.types.d.ts +50 -4
  25. package/lib/types/common.types.d.ts.map +1 -1
  26. package/lib/types/common.types.js +15 -1
  27. package/lib/types/common.types.js.map +1 -1
  28. package/lib/types/conditional-formatting.types.d.ts +155 -0
  29. package/lib/types/conditional-formatting.types.d.ts.map +1 -0
  30. package/lib/types/conditional-formatting.types.js +60 -0
  31. package/lib/types/conditional-formatting.types.js.map +1 -0
  32. package/lib/types/configurator.types.d.ts +5 -0
  33. package/lib/types/configurator.types.d.ts.map +1 -1
  34. package/lib/types/ts-to-chart-event.types.d.ts.map +1 -1
  35. package/lib/types/visual-prop.types.d.ts +3 -0
  36. package/lib/types/visual-prop.types.d.ts.map +1 -1
  37. package/lib/utils/conditional-formatting/conditional-formatting.d.ts +10 -0
  38. package/lib/utils/conditional-formatting/conditional-formatting.d.ts.map +1 -0
  39. package/lib/utils/conditional-formatting/conditional-formatting.js +103 -0
  40. package/lib/utils/conditional-formatting/conditional-formatting.js.map +1 -0
  41. package/lib/utils/conditional-formatting/conditional-formatting.spec.d.ts +2 -0
  42. package/lib/utils/conditional-formatting/conditional-formatting.spec.d.ts.map +1 -0
  43. package/lib/utils/conditional-formatting/conditional-formatting.spec.js +142 -0
  44. package/lib/utils/conditional-formatting/conditional-formatting.spec.js.map +1 -0
  45. package/lib/utils/date-formatting.d.ts +19 -0
  46. package/lib/utils/date-formatting.d.ts.map +1 -0
  47. package/lib/utils/date-formatting.js +56 -0
  48. package/lib/utils/date-formatting.js.map +1 -0
  49. package/lib/utils/date-formatting.spec.d.ts +2 -0
  50. package/lib/utils/date-formatting.spec.d.ts.map +1 -0
  51. package/lib/utils/date-formatting.spec.js +124 -0
  52. package/lib/utils/date-formatting.spec.js.map +1 -0
  53. package/package.json +6 -4
  54. package/src/index.ts +3 -0
  55. package/src/main/custom-chart-context.spec.ts +183 -5
  56. package/src/main/custom-chart-context.ts +115 -8
  57. package/src/main/post-message-event-bridge.ts +2 -2
  58. package/src/react/use-custom-chart-context.spec.tsx +65 -2
  59. package/src/test/test-conditional-formatting-utils.ts +95 -0
  60. package/src/types/answer-column.types.ts +98 -0
  61. package/src/types/common.types.ts +81 -9
  62. package/src/types/conditional-formatting.types.ts +172 -0
  63. package/src/types/configurator.types.ts +21 -0
  64. package/src/types/ts-to-chart-event.types.ts +2 -1
  65. package/src/types/visual-prop.types.ts +7 -0
  66. package/src/utils/conditional-formatting/conditional-formatting.spec.ts +469 -0
  67. package/src/utils/conditional-formatting/conditional-formatting.ts +229 -0
  68. package/src/utils/date-formatting.spec.ts +168 -0
  69. package/src/utils/date-formatting.ts +109 -0
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.2-alpha.1",
4
+ "version": "0.0.2-alpha.10",
5
5
  "module": "lib/index",
6
6
  "main": "lib/index",
7
7
  "types": "lib/index",
@@ -23,26 +23,27 @@
23
23
  "dev:bar": "vite serve example/custom-bar-chart --port 3000",
24
24
  "about:bundle-dts": "echo '===== add ts types bundle in dist ====='",
25
25
  "bundle-dts": "npx dts-bundle --name ../dist/ts-chart-sdk --main lib/index.d.ts --outputAsModuleFolder=true",
26
- "prePublish": "npm run ci; npm run build; npm run bundle-dts;",
26
+ "prepublishOnly": "npm run ci; npm run build; npm run bundle-dts;",
27
27
  "about:publish-dev": "echo '===== patch dev workflow ====='",
28
28
  "patch-dev": "npm version prerelease --no-git-tag-version",
29
29
  "publish-dev": "npm publish --tag dev --access public",
30
30
  "about:publish-prod": "echo '===== patch prod workflow ====='",
31
31
  "patch-prod": "npm version prepatch --no-git-tag-version",
32
- "publish-prod": "npm publish --access public"
32
+ "publish-prod": "npm publish --tag latest --access public"
33
33
  },
34
34
  "devDependencies": {
35
35
  "@testing-library/react": "12.1.5",
36
36
  "@testing-library/react-hooks": "^7.0.2",
37
37
  "@types/jest": "^27.0.3",
38
38
  "@types/lodash": "4.14.175",
39
+ "@types/luxon": "^3.4.2",
39
40
  "@types/node": "18.16.3",
40
41
  "@types/react": "^17.0.2",
41
42
  "@types/react-dom": "^17.0.11",
42
43
  "@typescript-eslint/eslint-plugin": "^5.59.5",
43
44
  "@typescript-eslint/parser": "^5.59.5",
44
45
  "dts-bundle": "^0.7.3",
45
- "eslint": "7.32.0",
46
+ "eslint": "^8.56.0",
46
47
  "eslint-config-airbnb-base": "15.0.0",
47
48
  "eslint-config-prettier": "^6.9.0",
48
49
  "eslint-import-resolver-typescript": "^2.0.0",
@@ -74,6 +75,7 @@
74
75
  },
75
76
  "dependencies": {
76
77
  "lodash": "^4.17.21",
78
+ "luxon": "^3.4.4",
77
79
  "promise-postmessage": "^3.5.0",
78
80
  "react": "^17.0.2",
79
81
  "react-dom": "^17.0.2"
package/src/index.ts CHANGED
@@ -6,3 +6,6 @@ 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
8
  export * from './react/use-custom-chart-context';
9
+ export * from './types/conditional-formatting.types';
10
+ export * from './utils/date-formatting';
11
+ export * from './utils/conditional-formatting/conditional-formatting';
@@ -107,28 +107,89 @@ describe('CustomChartContext', () => {
107
107
  allowedConfigurations: {
108
108
  allowColumnNumberFormatting: false,
109
109
  allowColumnConditionalFormatting: false,
110
+ allowMeasureNamesAndValues: false,
110
111
  },
111
112
  });
112
113
  expect(mockPostMessageToHost).not.toHaveBeenCalled();
113
114
  });
114
115
 
115
- test('multiple intializations should throw an error', async () => {
116
+ test('type check string for visualProps on initialize payload should not throw an error.', async () => {
117
+ expect(mockInitMessage).toHaveBeenCalled();
118
+
116
119
  // Call the initialize function and wait for it to resolve
117
120
  const promise = customChartContext.initialize();
118
121
 
119
122
  // Check that the hasInitializedPromise has resolved
120
- eventProcessor({
121
- payload: mockInitializeContextPayload,
123
+
124
+ const mockInitializeContextPayloadWithVisualProps = {
125
+ ...mockInitializeContextPayload,
126
+ chartModel: {
127
+ ...mockInitializeContextPayload.chartModel,
128
+ visualProps: 'visualPropStringPayload',
129
+ },
130
+ };
131
+
132
+ await eventProcessor({
133
+ payload: mockInitializeContextPayloadWithVisualProps,
122
134
  eventType: TSToChartEvent.Initialize,
123
135
  });
124
-
125
136
  eventProcessor({
126
137
  payload: {},
127
138
  eventType: TSToChartEvent.InitializeComplete,
128
139
  });
140
+ await expect(promise).resolves.toBeUndefined();
141
+ const chartModel = customChartContext.getChartModel();
142
+ expect(typeof chartModel.visualProps).toEqual('string');
143
+ expect(chartModel.visualProps).toEqual('visualPropStringPayload');
144
+ expect(mockPostMessageToHost).not.toHaveBeenCalled();
145
+ });
129
146
 
147
+ test('type check object for visualProps on initialize payload should not throw an error.', async () => {
148
+ expect(mockInitMessage).toHaveBeenCalled();
149
+
150
+ // Call the initialize function and wait for it to resolve
151
+ const promise = customChartContext.initialize();
152
+
153
+ // Check that the hasInitializedPromise has resolved
154
+
155
+ const mockInitializeContextPayloadWithVisualProps = {
156
+ ...mockInitializeContextPayload,
157
+ chartModel: {
158
+ ...mockInitializeContextPayload.chartModel,
159
+ visualProps: {
160
+ data: 'sample data',
161
+ },
162
+ },
163
+ };
164
+
165
+ await eventProcessor({
166
+ payload: mockInitializeContextPayloadWithVisualProps,
167
+ eventType: TSToChartEvent.Initialize,
168
+ });
169
+ eventProcessor({
170
+ payload: {},
171
+ eventType: TSToChartEvent.InitializeComplete,
172
+ });
130
173
  await expect(promise).resolves.toBeUndefined();
174
+ const chartModel = customChartContext.getChartModel();
175
+ expect(typeof chartModel.visualProps).toEqual('object');
176
+ expect(chartModel.visualProps).toEqual({
177
+ data: 'sample data',
178
+ });
179
+ expect(mockPostMessageToHost).not.toHaveBeenCalled();
180
+ });
131
181
 
182
+ test('multiple intializations should throw an error', async () => {
183
+ // Call the initialize function and wait for it to resolve
184
+ getChartContext({
185
+ getDefaultChartConfig,
186
+ getQueriesFromChartConfig,
187
+ renderChart,
188
+ });
189
+ eventProcessor({
190
+ payload: {},
191
+ eventType: TSToChartEvent.InitializeComplete,
192
+ });
132
193
  let error;
133
194
  try {
134
195
  customChartContext = new CustomChartContext({
@@ -170,7 +231,124 @@ describe('CustomChartContext', () => {
170
231
  eventProcessor = null;
171
232
  });
172
233
 
173
- test('default internal function testing', async () => {
234
+ test('TSToChartEvent.ChartConfigValidate validation response testing', async () => {
235
+ // Define initial context with object definitions
236
+ customChartContext = new CustomChartContext({
237
+ getDefaultChartConfig,
238
+ getQueriesFromChartConfig,
239
+ renderChart,
240
+ visualPropEditorDefinition: { elements: [] },
241
+ chartConfigEditorDefinition: [],
242
+ });
243
+
244
+ // Trigger event processor with initial context
245
+ const responseWithInitialContext = await eventProcessor({
246
+ payload: mockInitializeContextPayload,
247
+ eventType: TSToChartEvent.ChartConfigValidate,
248
+ source: 'ts-host-app',
249
+ });
250
+ // Verify response with object definitions
251
+ expect(responseWithInitialContext).toStrictEqual({
252
+ isValid: true,
253
+ visualPropEditorDefinition: { elements: [] },
254
+ chartConfigEditorDefinition: [],
255
+ });
256
+
257
+ // Redefine context with function-returned definitions
258
+ customChartContext = new CustomChartContext({
259
+ getDefaultChartConfig,
260
+ getQueriesFromChartConfig,
261
+ renderChart,
262
+ visualPropEditorDefinition: () => {
263
+ return { elements: [] };
264
+ },
265
+ chartConfigEditorDefinition: () => {
266
+ return [
267
+ {
268
+ key: 'x',
269
+ columnSections: [{ label: 'x-axis', key: 'x' }],
270
+ },
271
+ ];
272
+ },
273
+ });
274
+ // Trigger event processor with updated context
275
+ const responseWithUpdatedContext = await eventProcessor({
276
+ payload: mockInitializeContextPayload,
277
+ eventType: TSToChartEvent.ChartConfigValidate,
278
+ source: 'ts-host-app',
279
+ });
280
+ // Verify response with function-returned definitions
281
+ expect(responseWithUpdatedContext).toStrictEqual({
282
+ isValid: true,
283
+ visualPropEditorDefinition: { elements: [] },
284
+ chartConfigEditorDefinition: [
285
+ {
286
+ key: 'x',
287
+ columnSections: [{ label: 'x-axis', key: 'x' }],
288
+ },
289
+ ],
290
+ });
291
+ });
292
+
293
+ test('TSToChartEvent.validateVisualProps validation response testing', () => {
294
+ // Define initial context with object definitions
295
+ customChartContext = new CustomChartContext({
296
+ getDefaultChartConfig,
297
+ getQueriesFromChartConfig,
298
+ renderChart,
299
+ visualPropEditorDefinition: { elements: [] },
300
+ chartConfigEditorDefinition: [],
301
+ });
302
+
303
+ // Trigger event processor with initial context
304
+ const responseWithInitialContext = eventProcessor({
305
+ payload: mockInitializeContextPayload,
306
+ eventType: TSToChartEvent.VisualPropsValidate,
307
+ source: 'ts-host-app',
308
+ });
309
+ // Verify response with object definitions
310
+ expect(responseWithInitialContext).toStrictEqual({
311
+ isValid: true,
312
+ visualPropEditorDefinition: { elements: [] },
313
+ chartConfigEditorDefinition: [],
314
+ });
315
+ // Redefine context with function-returned definitions
316
+ customChartContext = new CustomChartContext({
317
+ getDefaultChartConfig,
318
+ getQueriesFromChartConfig,
319
+ renderChart,
320
+ visualPropEditorDefinition: () => {
321
+ return { elements: [] };
322
+ },
323
+ chartConfigEditorDefinition: () => {
324
+ return [
325
+ {
326
+ key: 'x',
327
+ columnSections: [{ label: 'x-axis', key: 'x' }],
328
+ },
329
+ ];
330
+ },
331
+ });
332
+ // Trigger event processor with updated context
333
+ const responseWithFunctionReturnedValues = eventProcessor({
334
+ payload: mockInitializeContextPayload,
335
+ eventType: TSToChartEvent.VisualPropsValidate,
336
+ source: 'ts-host-app',
337
+ });
338
+ // Verify response with function-returned definitions
339
+ expect(responseWithFunctionReturnedValues).toStrictEqual({
340
+ isValid: true,
341
+ visualPropEditorDefinition: { elements: [] },
342
+ chartConfigEditorDefinition: [
343
+ {
344
+ key: 'x',
345
+ columnSections: [{ label: 'x-axis', key: 'x' }],
346
+ },
347
+ ],
348
+ });
349
+ });
350
+
351
+ test('should not trigger post message if host is not accurate', async () => {
174
352
  expect(mockInitMessage).toHaveBeenCalled();
175
353
 
176
354
  // mock the event trigger for ChartConfigValidate
@@ -21,10 +21,14 @@ import {
21
21
  AppConfig,
22
22
  ChartConfig,
23
23
  ChartModel,
24
+ SuccessValidationResponse,
24
25
  ValidationResponse,
25
26
  VisualProps,
26
27
  } from '../types/common.types';
27
- import { ChartConfigEditorDefinition } from '../types/configurator.types';
28
+ import {
29
+ ChartConfigEditorDefinition,
30
+ ConfigEditorDefinitionSetter,
31
+ } from '../types/configurator.types';
28
32
  import {
29
33
  AxisMenuCustomActionPayload,
30
34
  ChartConfigValidateEventPayload,
@@ -42,7 +46,10 @@ import {
42
46
  VisualPropsUpdateEventPayload,
43
47
  VisualPropsValidateEventPayload,
44
48
  } from '../types/ts-to-chart-event.types';
45
- import { VisualPropEditorDefinition } from '../types/visual-prop.types';
49
+ import {
50
+ VisualEditorDefinitionSetter,
51
+ VisualPropEditorDefinition,
52
+ } from '../types/visual-prop.types';
46
53
  import {
47
54
  globalThis,
48
55
  initMessageListener,
@@ -52,6 +59,7 @@ import {
52
59
  export type AllowedConfigurations = {
53
60
  allowColumnNumberFormatting: boolean;
54
61
  allowColumnConditionalFormatting: boolean;
62
+ allowMeasureNamesAndValues: boolean;
55
63
  };
56
64
 
57
65
  export type CustomChartContextProps = {
@@ -116,7 +124,9 @@ export type CustomChartContextProps = {
116
124
  *
117
125
  * @version SDK: 0.1 | ThoughtSpot:
118
126
  */
119
- chartConfigEditorDefinition?: ChartConfigEditorDefinition[];
127
+ chartConfigEditorDefinition?:
128
+ | ConfigEditorDefinitionSetter
129
+ | ChartConfigEditorDefinition[];
120
130
 
121
131
  /**
122
132
  * Definition to help edit/customize the visual properties from chart settings editor
@@ -127,12 +137,18 @@ export type CustomChartContextProps = {
127
137
  * @returns {@link VisualPropEditorDefinition}
128
138
  * @version SDK: 0.1 | ThoughtSpot:
129
139
  */
130
- visualPropEditorDefinition?: VisualPropEditorDefinition;
140
+ visualPropEditorDefinition?:
141
+ | VisualEditorDefinitionSetter
142
+ | VisualPropEditorDefinition;
131
143
 
132
144
  // Whether user wants thoughtspot default number and conditional formatting
133
145
  allowedConfigurations?: AllowedConfigurations;
134
146
  };
135
147
 
148
+ export type ValidationFunctions =
149
+ | CustomChartContextProps['validateVisualProps']
150
+ | CustomChartContextProps['validateConfig'];
151
+
136
152
  /**
137
153
  * Default configuration options for all the chart context properties
138
154
  */
@@ -143,6 +159,7 @@ const DEFAULT_CHART_CONTEXT_PROPS: Partial<CustomChartContextProps> = {
143
159
  allowedConfigurations: {
144
160
  allowColumnNumberFormatting: false,
145
161
  allowColumnConditionalFormatting: false,
162
+ allowMeasureNamesAndValues: false,
146
163
  },
147
164
  };
148
165
 
@@ -379,6 +396,48 @@ export class CustomChartContext {
379
396
  return processedPayload;
380
397
  }
381
398
 
399
+ /**
400
+ * Funtions return the chart config editor definition
401
+ * @param {ChartConfig[]} currentChartConfig
402
+ * @param {VisualProps}
403
+ * @returns {ChartConfigEditorDefinition[]}
404
+ */
405
+ private getChartConfigEditorDefinition = (
406
+ currentState: Partial<ChartModel> = {},
407
+ ) => {
408
+ if (_.isFunction(this.chartContextProps.chartConfigEditorDefinition)) {
409
+ return this.chartContextProps.chartConfigEditorDefinition(
410
+ {
411
+ ...this.chartModel,
412
+ ...currentState,
413
+ },
414
+ this,
415
+ );
416
+ }
417
+ return this.chartContextProps.chartConfigEditorDefinition;
418
+ };
419
+
420
+ /**
421
+ * Funtions returns the visual prop editor definition
422
+ * @param {ChartConfig[]} currentChartConfig
423
+ * @param {VisualProps}
424
+ * @returns {VisualPropEditorDefinition}
425
+ */
426
+ private getVisualPropEditorDefinition = (
427
+ currentState: Partial<ChartModel> = {},
428
+ ) => {
429
+ if (_.isFunction(this.chartContextProps.visualPropEditorDefinition)) {
430
+ return this.chartContextProps.visualPropEditorDefinition(
431
+ {
432
+ ...this.chartModel,
433
+ ...currentState,
434
+ },
435
+ this,
436
+ );
437
+ }
438
+ return this.chartContextProps.visualPropEditorDefinition;
439
+ };
440
+
382
441
  /**
383
442
  * Function to store the axis menu custom action callback mapped with action id
384
443
  * @param {[OpenAxisMenuEventPayload]} eventPayload Event payload bound
@@ -438,6 +497,24 @@ export class CustomChartContext {
438
497
  }
439
498
  }
440
499
 
500
+ private validationsResponseProcessor(
501
+ currentValidationState: Partial<ChartModel>,
502
+ validationResponse: ValidationResponse,
503
+ ) {
504
+ const visualPropEditorDefinition = this.getVisualPropEditorDefinition(
505
+ currentValidationState,
506
+ );
507
+ const chartConfigEditorDefinition = this.getChartConfigEditorDefinition(
508
+ currentValidationState,
509
+ );
510
+
511
+ return {
512
+ ...validationResponse,
513
+ visualPropEditorDefinition,
514
+ chartConfigEditorDefinition,
515
+ };
516
+ }
517
+
441
518
  /**
442
519
  * Function to emit Chart to TS Events to the TS application.
443
520
 
@@ -530,13 +607,27 @@ export class CustomChartContext {
530
607
  */
531
608
  this.onInternal(
532
609
  TSToChartEvent.VisualPropsValidate,
533
- (payload: VisualPropsValidateEventPayload): ValidationResponse => {
610
+ (
611
+ payload: VisualPropsValidateEventPayload,
612
+ ):
613
+ | (ValidationResponse & SuccessValidationResponse)
614
+ | ValidationResponse => {
534
615
  if (this.chartContextProps.validateVisualProps) {
535
616
  const validationResponse =
536
617
  this.chartContextProps.validateVisualProps(
537
618
  payload.visualProps,
538
619
  this.chartModel,
539
620
  );
621
+ if (validationResponse.isValid) {
622
+ const currentVisualState = {
623
+ visualProps: payload.visualProps,
624
+ };
625
+
626
+ return this.validationsResponseProcessor(
627
+ currentVisualState,
628
+ validationResponse,
629
+ );
630
+ }
540
631
  return validationResponse;
541
632
  }
542
633
  // this will never be true
@@ -551,13 +642,29 @@ export class CustomChartContext {
551
642
  */
552
643
  this.onInternal(
553
644
  TSToChartEvent.ChartConfigValidate,
554
- (payload: ChartConfigValidateEventPayload): ValidationResponse => {
645
+ (
646
+ payload: ChartConfigValidateEventPayload,
647
+ ):
648
+ | (ValidationResponse & SuccessValidationResponse)
649
+ | ValidationResponse => {
555
650
  if (this.chartContextProps.validateConfig) {
556
651
  const validationResponse =
557
652
  this.chartContextProps.validateConfig(
558
653
  payload.chartConfig,
559
654
  this.chartModel,
560
655
  );
656
+ if (validationResponse.isValid) {
657
+ const currentConfigState = {
658
+ config: {
659
+ ...this.chartModel.config,
660
+ chartConfig: payload.chartConfig,
661
+ },
662
+ };
663
+ return this.validationsResponseProcessor(
664
+ currentConfigState,
665
+ validationResponse,
666
+ );
667
+ }
561
668
  return validationResponse;
562
669
  }
563
670
  // this will never be true
@@ -789,9 +896,9 @@ export class CustomChartContext {
789
896
  isConfigValid: isValid,
790
897
  defaultChartConfig,
791
898
  chartConfigEditorDefinition:
792
- this.chartContextProps.chartConfigEditorDefinition,
899
+ this.getChartConfigEditorDefinition(),
793
900
  visualPropEditorDefinition:
794
- this.chartContextProps.visualPropEditorDefinition,
901
+ this.getVisualPropEditorDefinition(),
795
902
  allowedConfigurations:
796
903
  this.chartContextProps.allowedConfigurations,
797
904
  };
@@ -9,7 +9,7 @@ const target =
9
9
  (elSelector && (document.querySelector(elSelector) as HTMLElement)) ||
10
10
  window.parent;
11
11
 
12
- const globalThis = (target instanceof Window ? window : target) as any;
12
+ const globalThis = (target === window.parent ? window : target) as any;
13
13
 
14
14
  /**
15
15
  * method to listen to messages using postMessage from the parent.
@@ -53,7 +53,7 @@ const postMessageToHostApp = async (
53
53
  TIMEOUT_THRESHOLD,
54
54
  'ChartContext: postMessage operation timed out.',
55
55
  );
56
- if (resp.hasError) {
56
+ if (resp?.hasError) {
57
57
  throw new Error(resp.error);
58
58
  }
59
59
  return resp;
@@ -3,6 +3,7 @@ import { renderHook } from '@testing-library/react-hooks';
3
3
  import React from 'react';
4
4
  import * as PostMessageEventBridge from '../main/post-message-event-bridge';
5
5
  import { mockInitializeContextPayload } from '../test/test-utils';
6
+ import { ColumnType } from '../types/answer-column.types';
6
7
  import { ChartToTSEvent } from '../types/chart-to-ts-event.types';
7
8
  import { TSToChartEvent } from '../types/ts-to-chart-event.types';
8
9
  import { contextChartProps } from './mocks/custom-chart-context-mock';
@@ -12,7 +13,10 @@ describe('useChartContext initialization', () => {
12
13
  let eventProcessor: any;
13
14
  let mockInitMessage;
14
15
  let mockPostMessageToHost;
15
- const mockedChartModel = { columns: [], config: {} };
16
+ const mockedChartModel = {
17
+ columns: [{ type: ColumnType.MEASURE }, { type: ColumnType.ATTRIBUTE }],
18
+ config: {},
19
+ };
16
20
  beforeEach(() => {
17
21
  mockInitMessage = jest.spyOn(
18
22
  PostMessageEventBridge,
@@ -67,6 +71,66 @@ describe('useChartContext initialization', () => {
67
71
  });
68
72
  });
69
73
 
74
+ test('should trigger getDataQuery and fetch correct query response', async () => {
75
+ // Render the hook with the custom chart context props
76
+ const { result, waitFor } = renderHook(() =>
77
+ useChartContext(contextChartProps),
78
+ );
79
+
80
+ // Assert that the context is not initialized initially
81
+ expect(result.current.hasInitialized).toBe(false);
82
+ expect(result.current.chartModel).toBeUndefined();
83
+ await eventProcessor({
84
+ payload: {
85
+ componentId: 'COMPONENT_ID',
86
+ hostUrl: 'https://some.chart.app',
87
+ chartModel: mockedChartModel,
88
+ },
89
+ eventType: TSToChartEvent.Initialize,
90
+ });
91
+ const response = await eventProcessor({
92
+ payload: {
93
+ config: [
94
+ {
95
+ key: 'column',
96
+ dimensions: [
97
+ {
98
+ key: 'x',
99
+ columns: [mockedChartModel.columns[0]],
100
+ },
101
+ {
102
+ key: 'y',
103
+ columns: [mockedChartModel.columns[1]],
104
+ },
105
+ ],
106
+ },
107
+ ],
108
+ },
109
+ eventType: TSToChartEvent.GetDataQuery,
110
+ source: 'ts-host-app',
111
+ });
112
+ await eventProcessor({
113
+ payload: {},
114
+ eventType: TSToChartEvent.TriggerRenderChart,
115
+ source: 'ts-host-app',
116
+ });
117
+ await eventProcessor({
118
+ payload: {},
119
+ eventType: TSToChartEvent.InitializeComplete,
120
+ source: 'ts-host-app',
121
+ });
122
+
123
+ await waitFor(() => {
124
+ expect(response.queries[0].queryColumns[0]).toBe(
125
+ mockedChartModel.columns[0],
126
+ );
127
+ expect(response.queries[0].queryColumns[1]).toBe(
128
+ mockedChartModel.columns[1],
129
+ );
130
+ expect(result.current.hasInitialized).toBeTruthy();
131
+ });
132
+ });
133
+
70
134
  test('should make sure hasInitialized to remain false when context initialization failed', async () => {
71
135
  jest.mock('../main/custom-chart-context', () => ({
72
136
  CustomChartContext: jest.fn().mockImplementation(() => ({
@@ -76,7 +140,6 @@ describe('useChartContext initialization', () => {
76
140
  emitEvent: jest.fn(),
77
141
  on: jest.fn(),
78
142
  getChartModel: jest.fn(),
79
- renderChart: jest.fn(),
80
143
  destroy: jest.fn(),
81
144
  })),
82
145
  }));