@thoughtspot/ts-chart-sdk 0.0.1-alpha.0
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/dist/ts-chart-sdk.d.ts +312 -0
- package/lib/dist/ts-chart-sdk.d.ts +312 -0
- package/lib/index.d.ts +8 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +8 -0
- package/lib/index.js.map +1 -0
- package/lib/main/custom-chart-context.d.ts +38 -0
- package/lib/main/custom-chart-context.d.ts.map +1 -0
- package/lib/main/custom-chart-context.js +176 -0
- package/lib/main/custom-chart-context.js.map +1 -0
- package/lib/main/custom-chart-context.spec.d.ts +2 -0
- package/lib/main/custom-chart-context.spec.d.ts.map +1 -0
- package/lib/main/custom-chart-context.spec.js +315 -0
- package/lib/main/custom-chart-context.spec.js.map +1 -0
- package/lib/main/post-message-event-bridge.d.ts +6 -0
- package/lib/main/post-message-event-bridge.d.ts.map +1 -0
- package/lib/main/post-message-event-bridge.js +41 -0
- package/lib/main/post-message-event-bridge.js.map +1 -0
- package/lib/main/post-message-event-bridge.spec.d.ts +2 -0
- package/lib/main/post-message-event-bridge.spec.d.ts.map +1 -0
- package/lib/main/post-message-event-bridge.spec.js +117 -0
- package/lib/main/post-message-event-bridge.spec.js.map +1 -0
- package/lib/test/test-utils.d.ts +7 -0
- package/lib/test/test-utils.d.ts.map +1 -0
- package/lib/test/test-utils.js +11 -0
- package/lib/test/test-utils.js.map +1 -0
- package/lib/types/answer-column.types.d.ts +69 -0
- package/lib/types/answer-column.types.d.ts.map +1 -0
- package/lib/types/answer-column.types.js +56 -0
- package/lib/types/answer-column.types.js.map +1 -0
- package/lib/types/chart-to-ts-event.types.d.ts +48 -0
- package/lib/types/chart-to-ts-event.types.d.ts.map +1 -0
- package/lib/types/chart-to-ts-event.types.js +16 -0
- package/lib/types/chart-to-ts-event.types.js.map +1 -0
- package/lib/types/common.types.d.ts +51 -0
- package/lib/types/common.types.d.ts.map +1 -0
- package/lib/types/common.types.js +2 -0
- package/lib/types/common.types.js.map +1 -0
- package/lib/types/configurator.types.d.ts +15 -0
- package/lib/types/configurator.types.d.ts.map +1 -0
- package/lib/types/configurator.types.js +2 -0
- package/lib/types/configurator.types.js.map +1 -0
- package/lib/types/ts-to-chart-event.types.d.ts +67 -0
- package/lib/types/ts-to-chart-event.types.d.ts.map +1 -0
- package/lib/types/ts-to-chart-event.types.js +12 -0
- package/lib/types/ts-to-chart-event.types.js.map +1 -0
- package/lib/types/visual-prop.types.d.ts +47 -0
- package/lib/types/visual-prop.types.d.ts.map +1 -0
- package/lib/types/visual-prop.types.js +2 -0
- package/lib/types/visual-prop.types.js.map +1 -0
- package/package.json +88 -0
- package/src/index.ts +7 -0
- package/src/main/custom-chart-context.spec.ts +432 -0
- package/src/main/custom-chart-context.ts +587 -0
- package/src/main/post-message-event-bridge.spec.ts +215 -0
- package/src/main/post-message-event-bridge.ts +88 -0
- package/src/test/test-utils.ts +13 -0
- package/src/types/answer-column.types.ts +174 -0
- package/src/types/chart-to-ts-event.types.ts +159 -0
- package/src/types/common.types.ts +187 -0
- package/src/types/configurator.types.ts +102 -0
- package/src/types/ts-to-chart-event.types.ts +257 -0
- package/src/types/visual-prop.types.ts +271 -0
package/package.json
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@thoughtspot/ts-chart-sdk",
|
|
3
|
+
"private": false,
|
|
4
|
+
"version": "0.0.1-alpha.0",
|
|
5
|
+
"module": "lib/index",
|
|
6
|
+
"main": "lib/index",
|
|
7
|
+
"types": "lib/index",
|
|
8
|
+
"files": [
|
|
9
|
+
"dist/**",
|
|
10
|
+
"lib/**",
|
|
11
|
+
"src/**"
|
|
12
|
+
],
|
|
13
|
+
"scripts": {
|
|
14
|
+
"build": "tsc -p .; tsc -p . --module commonjs --outDir cjs",
|
|
15
|
+
"tsc": "tsc -p .; tsc -p . --module commonjs --outDir cjs",
|
|
16
|
+
"test-sdk": "npx jest -c jest.config.sdk.js --runInBand",
|
|
17
|
+
"test": "npm run test-sdk",
|
|
18
|
+
"lint": "eslint 'src/**'",
|
|
19
|
+
"lint:fix": "eslint 'src/**' --fix",
|
|
20
|
+
"ci": "npm-run-all lint test",
|
|
21
|
+
"docgen": "typedoc --tsconfig tsconfig.json",
|
|
22
|
+
"dev": "npm run dev:bar",
|
|
23
|
+
"dev:bar": "vite serve example/custom-bar-chart --port 3000",
|
|
24
|
+
"about:bundle-dts": "echo '===== add ts types bundle in dist ====='",
|
|
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;",
|
|
27
|
+
"about:publish-dev": "echo '===== patch dev workflow ====='",
|
|
28
|
+
"patch-dev": "npm version prerelease --no-git-tag-version",
|
|
29
|
+
"publish-dev": "npm publish --tag dev --access public",
|
|
30
|
+
"about:publish-prod": "echo '===== patch prod workflow ====='",
|
|
31
|
+
"patch-prod": "npm version prepatch --no-git-tag-version",
|
|
32
|
+
"publish-prod": "npm publish --access public"
|
|
33
|
+
},
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"@types/jest": "^27.0.3",
|
|
36
|
+
"@types/lodash": "4.14.175",
|
|
37
|
+
"@types/node": "18.16.3",
|
|
38
|
+
"@typescript-eslint/eslint-plugin": "^5.59.5",
|
|
39
|
+
"@typescript-eslint/parser": "^5.59.5",
|
|
40
|
+
"dts-bundle": "^0.7.3",
|
|
41
|
+
"eslint": "7.32.0",
|
|
42
|
+
"eslint-config-airbnb-base": "15.0.0",
|
|
43
|
+
"eslint-config-prettier": "^6.9.0",
|
|
44
|
+
"eslint-import-resolver-typescript": "^2.0.0",
|
|
45
|
+
"eslint-plugin-anti-trojan-source": "^1.1.1",
|
|
46
|
+
"eslint-plugin-ban": "^1.6.0",
|
|
47
|
+
"eslint-plugin-comment-length": "0.9.2",
|
|
48
|
+
"eslint-plugin-compat": "^4.1.4",
|
|
49
|
+
"eslint-plugin-import": "^2.20.0",
|
|
50
|
+
"eslint-plugin-jest": "^27.2.1",
|
|
51
|
+
"eslint-plugin-jest-dom": "^4.0.3",
|
|
52
|
+
"eslint-plugin-jsdoc": "41.1.1",
|
|
53
|
+
"eslint-plugin-no-unsanitized": "^4.0.2",
|
|
54
|
+
"eslint-plugin-prettier": "^3.1.2",
|
|
55
|
+
"eslint-plugin-promise": "^6.1.1",
|
|
56
|
+
"eslint-plugin-prototype-pollution-security-rules": "^1.0.6",
|
|
57
|
+
"eslint-plugin-react": "^7.32.2",
|
|
58
|
+
"eslint-plugin-security": "^1.7.1",
|
|
59
|
+
"eslint-plugin-simple-import-sort": "^10.0.0",
|
|
60
|
+
"eslint-plugin-testing-library": "^5.11.0",
|
|
61
|
+
"jest": "^27.4.6",
|
|
62
|
+
"jsdom": "^19.0.0",
|
|
63
|
+
"npm-run-all": "^4.1.5",
|
|
64
|
+
"prettier": "^2.8.8",
|
|
65
|
+
"ts-jest": "^27.1.1",
|
|
66
|
+
"typedoc": "^0.24.4",
|
|
67
|
+
"typescript": "^4.9.5",
|
|
68
|
+
"vite": "4.2.1"
|
|
69
|
+
},
|
|
70
|
+
"dependencies": {
|
|
71
|
+
"chart.js": "4.2.1",
|
|
72
|
+
"lodash": "^4.17.21"
|
|
73
|
+
},
|
|
74
|
+
"prettier": "./prettier-config",
|
|
75
|
+
"browserslist": [
|
|
76
|
+
"chrome >=77, edge >= 81, firefox >=69, safari > 13"
|
|
77
|
+
],
|
|
78
|
+
"author": "ThoughtSpot",
|
|
79
|
+
"email": "support@thoughtspot.com",
|
|
80
|
+
"license": "ThoughtSpot Development Tools End User License Agreement",
|
|
81
|
+
"repository": {
|
|
82
|
+
"type": "git",
|
|
83
|
+
"url": "git+https://github.com/thoughtspot/ts-chart-sdk.git"
|
|
84
|
+
},
|
|
85
|
+
"publishConfig": {
|
|
86
|
+
"registry": "https://registry.npmjs.org"
|
|
87
|
+
}
|
|
88
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export * from './types/common.types';
|
|
2
|
+
export * from './types/configurator.types';
|
|
3
|
+
export * from './types/visual-prop.types';
|
|
4
|
+
export * from './types/answer-column.types';
|
|
5
|
+
export * from './types/chart-to-ts-event.types';
|
|
6
|
+
export * from './types/ts-to-chart-event.types';
|
|
7
|
+
export * from './main/custom-chart-context';
|
|
@@ -0,0 +1,432 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Custom chart context spec file
|
|
3
|
+
*
|
|
4
|
+
* @author Chetan Agrawal <chetan.agrawal@thoughtspot.com>
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { mockInitializeContextPayload } from '../test/test-utils';
|
|
8
|
+
import { ErrorType } from '../types/chart-to-ts-event.types';
|
|
9
|
+
import { TSToChartEvent } from '../types/ts-to-chart-event.types';
|
|
10
|
+
import { CustomChartContext, getChartContext } from './custom-chart-context';
|
|
11
|
+
import * as PostMessageEventBridge from './post-message-event-bridge';
|
|
12
|
+
|
|
13
|
+
jest.mock('./post-message-event-bridge');
|
|
14
|
+
|
|
15
|
+
jest.spyOn(console, 'log').mockImplementation(() => {
|
|
16
|
+
// do nothing.
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
describe('CustomChartContext', () => {
|
|
20
|
+
let eventProcesor: any;
|
|
21
|
+
|
|
22
|
+
let getDefaultChartConfig = jest.fn();
|
|
23
|
+
let getQueriesFromChartConfig = jest.fn();
|
|
24
|
+
let renderChart = jest.fn();
|
|
25
|
+
const mockInitMessage = jest.spyOn(
|
|
26
|
+
PostMessageEventBridge,
|
|
27
|
+
'initMessageListener',
|
|
28
|
+
);
|
|
29
|
+
const mockPostMessageToHost: any = jest.spyOn(
|
|
30
|
+
PostMessageEventBridge,
|
|
31
|
+
'postMessageToHostApp',
|
|
32
|
+
);
|
|
33
|
+
|
|
34
|
+
beforeEach(() => {
|
|
35
|
+
getDefaultChartConfig = jest.fn();
|
|
36
|
+
getQueriesFromChartConfig = jest.fn();
|
|
37
|
+
renderChart = jest.fn();
|
|
38
|
+
|
|
39
|
+
mockInitMessage.mockImplementation((fn: any) => {
|
|
40
|
+
eventProcesor = fn;
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
describe('initialize', () => {
|
|
45
|
+
let customChartContext: CustomChartContext;
|
|
46
|
+
beforeEach(() => {
|
|
47
|
+
customChartContext = new CustomChartContext({
|
|
48
|
+
getDefaultChartConfig,
|
|
49
|
+
getQueriesFromChartConfig,
|
|
50
|
+
renderChart,
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
afterEach(() => {
|
|
55
|
+
// Destroy the chart context after each test
|
|
56
|
+
customChartContext.destroy();
|
|
57
|
+
jest.resetAllMocks();
|
|
58
|
+
eventProcesor = null;
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
test('should wait till the parent calls initialize', async () => {
|
|
62
|
+
expect(mockInitMessage).toHaveBeenCalled();
|
|
63
|
+
// initialize function should not resolve
|
|
64
|
+
const promise = customChartContext.initialize();
|
|
65
|
+
const rej = jest.fn();
|
|
66
|
+
const res = jest.fn();
|
|
67
|
+
promise.then(() => rej()).catch(() => res());
|
|
68
|
+
await new global.Promise((resolve) => {
|
|
69
|
+
setTimeout(() => resolve(null), 3000);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
expect(rej).not.toHaveBeenCalled();
|
|
73
|
+
expect(res).not.toHaveBeenCalled();
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
test('should return a promise that resolves when the chart context is initialized', async () => {
|
|
77
|
+
expect(mockInitMessage).toHaveBeenCalled();
|
|
78
|
+
|
|
79
|
+
// Call the initialize function and wait for it to resolve
|
|
80
|
+
const promise = customChartContext.initialize();
|
|
81
|
+
|
|
82
|
+
// Check that the hasInitializedPromise has resolved
|
|
83
|
+
const mockPostMessage = jest.fn();
|
|
84
|
+
eventProcesor({
|
|
85
|
+
data: {
|
|
86
|
+
payload: mockInitializeContextPayload,
|
|
87
|
+
eventType: TSToChartEvent.Initialize,
|
|
88
|
+
source: 'ts-host-app',
|
|
89
|
+
},
|
|
90
|
+
ports: [{ postMessage: mockPostMessage }],
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
await expect(promise).resolves.toBeUndefined();
|
|
94
|
+
expect(mockPostMessage).toHaveBeenCalledWith({
|
|
95
|
+
isConfigValid: false,
|
|
96
|
+
defaultChartConfig: undefined,
|
|
97
|
+
chartConfigEditorDefinition: undefined,
|
|
98
|
+
visualPropEditorDefinition: undefined,
|
|
99
|
+
});
|
|
100
|
+
expect(mockPostMessage).toHaveBeenCalled();
|
|
101
|
+
expect(mockPostMessageToHost).not.toHaveBeenCalled();
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
test('multiple intializations should throw an error', async () => {
|
|
105
|
+
// Call the initialize function and wait for it to resolve
|
|
106
|
+
const promise = customChartContext.initialize();
|
|
107
|
+
|
|
108
|
+
// Check that the hasInitializedPromise has resolved
|
|
109
|
+
const mockPostMessage = jest.fn();
|
|
110
|
+
eventProcesor({
|
|
111
|
+
data: {
|
|
112
|
+
payload: mockInitializeContextPayload,
|
|
113
|
+
eventType: TSToChartEvent.Initialize,
|
|
114
|
+
source: 'ts-host-app',
|
|
115
|
+
},
|
|
116
|
+
ports: [{ postMessage: mockPostMessage }],
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
await expect(promise).resolves.toBeUndefined();
|
|
120
|
+
|
|
121
|
+
let error;
|
|
122
|
+
try {
|
|
123
|
+
customChartContext = new CustomChartContext({
|
|
124
|
+
getDefaultChartConfig,
|
|
125
|
+
getQueriesFromChartConfig,
|
|
126
|
+
renderChart,
|
|
127
|
+
});
|
|
128
|
+
} catch (err: any) {
|
|
129
|
+
error = err.message;
|
|
130
|
+
}
|
|
131
|
+
expect(error).toBe(ErrorType.MultipleContextsNotSupported);
|
|
132
|
+
|
|
133
|
+
try {
|
|
134
|
+
await getChartContext({
|
|
135
|
+
getDefaultChartConfig,
|
|
136
|
+
getQueriesFromChartConfig,
|
|
137
|
+
renderChart,
|
|
138
|
+
});
|
|
139
|
+
} catch (err: any) {
|
|
140
|
+
error = err.message;
|
|
141
|
+
}
|
|
142
|
+
expect(error).toBe(ErrorType.MultipleContextsNotSupported);
|
|
143
|
+
});
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
describe('on', () => {
|
|
147
|
+
let customChartContext: CustomChartContext;
|
|
148
|
+
beforeEach(() => {
|
|
149
|
+
customChartContext = new CustomChartContext({
|
|
150
|
+
getDefaultChartConfig,
|
|
151
|
+
getQueriesFromChartConfig,
|
|
152
|
+
renderChart,
|
|
153
|
+
});
|
|
154
|
+
});
|
|
155
|
+
afterEach(() => {
|
|
156
|
+
// Destroy the chart context after each test
|
|
157
|
+
jest.resetAllMocks();
|
|
158
|
+
customChartContext.destroy();
|
|
159
|
+
eventProcesor = null;
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
test('should not trigger post message if host is not accurate', () => {
|
|
163
|
+
expect(mockInitMessage).toHaveBeenCalled();
|
|
164
|
+
|
|
165
|
+
// mock the event trigger for ChartConfigValidate
|
|
166
|
+
const mockPostMessage = jest.fn();
|
|
167
|
+
eventProcesor({
|
|
168
|
+
data: {
|
|
169
|
+
payload: mockInitializeContextPayload,
|
|
170
|
+
eventType: TSToChartEvent.ChartConfigValidate,
|
|
171
|
+
source: 'incorrect-source',
|
|
172
|
+
},
|
|
173
|
+
ports: [{ postMessage: mockPostMessage }],
|
|
174
|
+
});
|
|
175
|
+
// Check that the event listener was added to the eventListeners
|
|
176
|
+
// object
|
|
177
|
+
expect(mockPostMessage).not.toHaveBeenCalled();
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
test('default internal function testing', () => {
|
|
181
|
+
expect(mockInitMessage).toHaveBeenCalled();
|
|
182
|
+
|
|
183
|
+
// mock the event trigger for ChartConfigValidate
|
|
184
|
+
const mockPostMessage = jest.fn();
|
|
185
|
+
eventProcesor({
|
|
186
|
+
data: {
|
|
187
|
+
payload: mockInitializeContextPayload,
|
|
188
|
+
eventType: TSToChartEvent.ChartConfigValidate,
|
|
189
|
+
source: 'ts-host-app',
|
|
190
|
+
},
|
|
191
|
+
ports: [{ postMessage: mockPostMessage }],
|
|
192
|
+
});
|
|
193
|
+
// Check that the response was received
|
|
194
|
+
expect(mockPostMessage).toHaveBeenCalledWith({
|
|
195
|
+
isValid: true,
|
|
196
|
+
});
|
|
197
|
+
mockPostMessage.mockReset();
|
|
198
|
+
|
|
199
|
+
// mock the event trigger for VisualPropsValidate
|
|
200
|
+
eventProcesor({
|
|
201
|
+
data: {
|
|
202
|
+
payload: mockInitializeContextPayload,
|
|
203
|
+
eventType: TSToChartEvent.VisualPropsValidate,
|
|
204
|
+
source: 'ts-host-app',
|
|
205
|
+
},
|
|
206
|
+
ports: [{ postMessage: mockPostMessage }],
|
|
207
|
+
});
|
|
208
|
+
// Check that the response was received
|
|
209
|
+
expect(mockPostMessage).toHaveBeenCalledWith({
|
|
210
|
+
isValid: true,
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
mockPostMessage.mockReset();
|
|
214
|
+
|
|
215
|
+
// mock the event trigger for TriggerRenderChart
|
|
216
|
+
eventProcesor({
|
|
217
|
+
data: {
|
|
218
|
+
payload: mockInitializeContextPayload,
|
|
219
|
+
eventType: TSToChartEvent.TriggerRenderChart,
|
|
220
|
+
source: 'ts-host-app',
|
|
221
|
+
},
|
|
222
|
+
ports: [{ postMessage: mockPostMessage }],
|
|
223
|
+
});
|
|
224
|
+
// Check that the response was received
|
|
225
|
+
expect(mockPostMessage).toHaveBeenCalledWith({});
|
|
226
|
+
expect(renderChart).toHaveBeenCalled();
|
|
227
|
+
|
|
228
|
+
mockPostMessage.mockReset();
|
|
229
|
+
|
|
230
|
+
// mock the event trigger for getQueriesFromChartConfig
|
|
231
|
+
eventProcesor({
|
|
232
|
+
data: {
|
|
233
|
+
payload: mockInitializeContextPayload,
|
|
234
|
+
eventType: TSToChartEvent.GetDataQuery,
|
|
235
|
+
source: 'ts-host-app',
|
|
236
|
+
},
|
|
237
|
+
ports: [{ postMessage: mockPostMessage }],
|
|
238
|
+
});
|
|
239
|
+
// Check that the response was received
|
|
240
|
+
expect(mockPostMessage).toHaveBeenCalledWith({});
|
|
241
|
+
expect(getQueriesFromChartConfig).toHaveBeenCalled();
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
test('default external function testing', () => {
|
|
245
|
+
expect(mockInitMessage).toHaveBeenCalled();
|
|
246
|
+
|
|
247
|
+
// mock the event trigger for DataUpdate
|
|
248
|
+
const mockPostMessage = jest.fn();
|
|
249
|
+
eventProcesor({
|
|
250
|
+
data: {
|
|
251
|
+
payload: {
|
|
252
|
+
data: 'random data',
|
|
253
|
+
},
|
|
254
|
+
eventType: TSToChartEvent.DataUpdate,
|
|
255
|
+
source: 'ts-host-app',
|
|
256
|
+
},
|
|
257
|
+
ports: [{ postMessage: mockPostMessage }],
|
|
258
|
+
});
|
|
259
|
+
// Check that the response was received
|
|
260
|
+
expect(mockPostMessage).toHaveBeenCalledWith({
|
|
261
|
+
triggerRenderChart: true,
|
|
262
|
+
});
|
|
263
|
+
expect(customChartContext.getChartModel().data).toBe('random data');
|
|
264
|
+
mockPostMessage.mockReset();
|
|
265
|
+
|
|
266
|
+
// mock the event trigger for VisualPropsValidate
|
|
267
|
+
eventProcesor({
|
|
268
|
+
data: {
|
|
269
|
+
payload: {
|
|
270
|
+
chartModel: {
|
|
271
|
+
data: 'random data2',
|
|
272
|
+
visualProps: null,
|
|
273
|
+
},
|
|
274
|
+
},
|
|
275
|
+
eventType: TSToChartEvent.ChartModelUpdate,
|
|
276
|
+
source: 'ts-host-app',
|
|
277
|
+
},
|
|
278
|
+
ports: [{ postMessage: mockPostMessage }],
|
|
279
|
+
});
|
|
280
|
+
// Check that the response was received
|
|
281
|
+
expect(mockPostMessage).toHaveBeenCalledWith({
|
|
282
|
+
triggerRenderChart: true,
|
|
283
|
+
});
|
|
284
|
+
expect(customChartContext.getChartModel().data).toBe(
|
|
285
|
+
'random data2',
|
|
286
|
+
);
|
|
287
|
+
expect(customChartContext.getChartModel().visualProps).toBe(null);
|
|
288
|
+
|
|
289
|
+
mockPostMessage.mockReset();
|
|
290
|
+
|
|
291
|
+
// mock the event trigger for TriggerRenderChart
|
|
292
|
+
eventProcesor({
|
|
293
|
+
data: {
|
|
294
|
+
payload: {
|
|
295
|
+
visualProps: 'random data',
|
|
296
|
+
},
|
|
297
|
+
eventType: TSToChartEvent.VisualPropsUpdate,
|
|
298
|
+
source: 'ts-host-app',
|
|
299
|
+
},
|
|
300
|
+
ports: [{ postMessage: mockPostMessage }],
|
|
301
|
+
});
|
|
302
|
+
// Check that the response was received
|
|
303
|
+
expect(mockPostMessage).toHaveBeenCalledWith({
|
|
304
|
+
triggerRenderChart: true,
|
|
305
|
+
});
|
|
306
|
+
expect(customChartContext.getChartModel().visualProps).toBe(
|
|
307
|
+
'random data',
|
|
308
|
+
);
|
|
309
|
+
});
|
|
310
|
+
|
|
311
|
+
test('should add an event listener to the specified event type', () => {
|
|
312
|
+
expect(mockInitMessage).toHaveBeenCalled();
|
|
313
|
+
// Define a test event type and callback function
|
|
314
|
+
const TEST_EVENT_TYPE = 'testEventType' as any;
|
|
315
|
+
const testCallbackFn = jest.fn();
|
|
316
|
+
// Call the on function with the test event type and callback
|
|
317
|
+
// function
|
|
318
|
+
customChartContext.on(TEST_EVENT_TYPE, testCallbackFn);
|
|
319
|
+
|
|
320
|
+
// mock the event trigger
|
|
321
|
+
const mockPostMessage = jest.fn();
|
|
322
|
+
eventProcesor({
|
|
323
|
+
data: {
|
|
324
|
+
payload: mockInitializeContextPayload,
|
|
325
|
+
eventType: TEST_EVENT_TYPE,
|
|
326
|
+
source: 'ts-host-app',
|
|
327
|
+
},
|
|
328
|
+
ports: [{ postMessage: mockPostMessage }],
|
|
329
|
+
});
|
|
330
|
+
// Check that the event listener was added to the eventListeners
|
|
331
|
+
// object
|
|
332
|
+
expect(testCallbackFn).toHaveBeenCalled();
|
|
333
|
+
});
|
|
334
|
+
|
|
335
|
+
test('should respond with an error to an unspecified event type', () => {
|
|
336
|
+
expect(mockInitMessage).toHaveBeenCalled();
|
|
337
|
+
// Define a test event type and callback function
|
|
338
|
+
const TEST_EVENT_TYPE = 'testEventType' as any;
|
|
339
|
+
|
|
340
|
+
// mock the event trigger
|
|
341
|
+
const mockPostMessage = jest.fn();
|
|
342
|
+
eventProcesor({
|
|
343
|
+
data: {
|
|
344
|
+
payload: mockInitializeContextPayload,
|
|
345
|
+
eventType: TEST_EVENT_TYPE,
|
|
346
|
+
source: 'ts-host-app',
|
|
347
|
+
},
|
|
348
|
+
ports: [{ postMessage: mockPostMessage }],
|
|
349
|
+
});
|
|
350
|
+
// Check that the event listener was added to the eventListeners
|
|
351
|
+
// object
|
|
352
|
+
expect(mockPostMessage).toHaveBeenCalledWith({
|
|
353
|
+
hasError: true,
|
|
354
|
+
error: `Event type not recognised or processed: ${TEST_EVENT_TYPE}`,
|
|
355
|
+
});
|
|
356
|
+
});
|
|
357
|
+
});
|
|
358
|
+
|
|
359
|
+
describe('emitEvent', () => {
|
|
360
|
+
let customChartContext: CustomChartContext;
|
|
361
|
+
beforeEach(() => {
|
|
362
|
+
customChartContext = new CustomChartContext({
|
|
363
|
+
getDefaultChartConfig,
|
|
364
|
+
getQueriesFromChartConfig,
|
|
365
|
+
renderChart,
|
|
366
|
+
});
|
|
367
|
+
});
|
|
368
|
+
afterEach(() => {
|
|
369
|
+
// Destroy the chart context after each test
|
|
370
|
+
jest.resetAllMocks();
|
|
371
|
+
customChartContext.destroy();
|
|
372
|
+
eventProcesor = null;
|
|
373
|
+
});
|
|
374
|
+
|
|
375
|
+
test('should reject the promise if the chart context is not initialized', async () => {
|
|
376
|
+
// Define a test event type and payload
|
|
377
|
+
const TEST_EVENT_TYPE = 'testEventType' as any;
|
|
378
|
+
const testPayload = { value: 'testPayload' };
|
|
379
|
+
// Call the emitEvent function and wait for it to resolve
|
|
380
|
+
const result = customChartContext.emitEvent(
|
|
381
|
+
TEST_EVENT_TYPE,
|
|
382
|
+
testPayload,
|
|
383
|
+
);
|
|
384
|
+
|
|
385
|
+
// Check that the result is defined
|
|
386
|
+
expect(mockPostMessageToHost).not.toHaveBeenCalled();
|
|
387
|
+
await expect(result).rejects.not.toBeDefined();
|
|
388
|
+
});
|
|
389
|
+
|
|
390
|
+
test('should resolve the promise if the chart context is initialized', async () => {
|
|
391
|
+
// Check that the hasInitializedPromise has resolved
|
|
392
|
+
const mockPostMessage = jest.fn();
|
|
393
|
+
eventProcesor({
|
|
394
|
+
data: {
|
|
395
|
+
payload: mockInitializeContextPayload,
|
|
396
|
+
eventType: TSToChartEvent.Initialize,
|
|
397
|
+
source: 'ts-host-app',
|
|
398
|
+
},
|
|
399
|
+
ports: [{ postMessage: mockPostMessage }],
|
|
400
|
+
});
|
|
401
|
+
|
|
402
|
+
// define mock response for the postMessage response promise
|
|
403
|
+
let resolve: any;
|
|
404
|
+
mockPostMessageToHost.mockImplementation(
|
|
405
|
+
() =>
|
|
406
|
+
new global.Promise<any>((res) => {
|
|
407
|
+
resolve = res;
|
|
408
|
+
}),
|
|
409
|
+
);
|
|
410
|
+
|
|
411
|
+
// Define a test event type and payload
|
|
412
|
+
const TEST_EVENT_TYPE = 'testEventType' as any;
|
|
413
|
+
const testPayload = { value: 'testPayload' };
|
|
414
|
+
|
|
415
|
+
// Call the emitEvent function and wait for it to resolve
|
|
416
|
+
const result = customChartContext.emitEvent(
|
|
417
|
+
TEST_EVENT_TYPE,
|
|
418
|
+
testPayload,
|
|
419
|
+
);
|
|
420
|
+
global.setTimeout(() => resolve('helloworld'), 3000);
|
|
421
|
+
|
|
422
|
+
// Check that the result is defined
|
|
423
|
+
expect(mockPostMessageToHost).toHaveBeenCalledWith(
|
|
424
|
+
mockInitializeContextPayload.componentId,
|
|
425
|
+
mockInitializeContextPayload.hostUrl,
|
|
426
|
+
testPayload,
|
|
427
|
+
TEST_EVENT_TYPE,
|
|
428
|
+
);
|
|
429
|
+
await expect(result).resolves.toBe('helloworld');
|
|
430
|
+
});
|
|
431
|
+
});
|
|
432
|
+
});
|