@thoughtspot/visual-embed-sdk 1.24.0-preRender.4 → 1.24.0-preRender.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.
@@ -26,6 +26,7 @@ import {
26
26
  defaultParamsForPinboardEmbed,
27
27
  waitFor,
28
28
  expectUrlMatchesWithParams,
29
+ mockMessageChannel,
29
30
  } from '../test/test-utils';
30
31
  import * as config from '../config';
31
32
  import * as tsEmbedInstance from './ts-embed';
@@ -1321,7 +1322,9 @@ describe('Unit test case for ts embed', () => {
1321
1322
  });
1322
1323
 
1323
1324
  // show preRender
1325
+ const warnSpy = spyOn(console, 'warn');
1324
1326
  libEmbed.showPreRender();
1327
+ expect(warnSpy).toHaveBeenCalledTimes(0);
1325
1328
 
1326
1329
  resizeObserverCb([{
1327
1330
  target: tsEmbedDiv,
@@ -1384,5 +1387,34 @@ describe('Unit test case for ts embed', () => {
1384
1387
  libEmbed.hidePreRender();
1385
1388
  expect(libEmbed.preRender).toHaveBeenCalledTimes(0);
1386
1389
  });
1390
+
1391
+ it('it should connect with another object', async () => {
1392
+ createRootEleForEmbed();
1393
+ mockMessageChannel();
1394
+ (window as any).ResizeObserver = window.ResizeObserver
1395
+ || jest.fn().mockImplementation(() => ({
1396
+ disconnect: jest.fn(),
1397
+ observe: jest.fn(),
1398
+ unobserve: jest.fn(),
1399
+ }));
1400
+ const libEmbed = new LiveboardEmbed('#tsEmbedDiv', {
1401
+ preRenderId: 'i-am-preRendered',
1402
+ liveboardId: 'myLiveboardId',
1403
+ });
1404
+
1405
+ libEmbed.preRender();
1406
+ await waitFor(() => !!getIFrameEl());
1407
+ const warnSpy = jest.spyOn(console, 'warn');
1408
+ const newEmbed = new LiveboardEmbed('#tsEmbedDiv', {
1409
+ preRenderId: 'i-am-preRendered',
1410
+ liveboardId: 'awdawda',
1411
+ hiddenActions: [Action.AddFilter],
1412
+ frameParams: { height: 90 },
1413
+ });
1414
+
1415
+ newEmbed.showPreRender();
1416
+
1417
+ expect(warnSpy).toHaveBeenCalledTimes(2);
1418
+ });
1387
1419
  });
1388
1420
  });
@@ -7,6 +7,7 @@
7
7
  * @author Ayon Ghosh <ayon.ghosh@thoughtspot.com>
8
8
  */
9
9
 
10
+ import _ from 'lodash';
10
11
  import {
11
12
  getEncodedQueryParamsString,
12
13
  getCssDimension,
@@ -98,7 +99,7 @@ export class TsEmbed {
98
99
  /**
99
100
  * The key to store the embed instance in the DOM node
100
101
  */
101
- protected embedNodeKey = '__tsEmbed'
102
+ protected embedNodeKey = '__tsEmbed';
102
103
 
103
104
  protected isAppInitialized = false;
104
105
 
@@ -1062,6 +1063,37 @@ export class TsEmbed {
1062
1063
  // Override in subclass
1063
1064
  }
1064
1065
 
1066
+ private validatePreRenderViewConfig = (viewConfig: ViewConfig) => {
1067
+ const preRenderAllowedKeys = [
1068
+ 'preRenderId',
1069
+ 'vizId',
1070
+ 'liveboardId',
1071
+ ];
1072
+ const preRenderedObject = this.insertedDomEl?.[this.embedNodeKey] as TsEmbed;
1073
+ if (!preRenderedObject) return;
1074
+ if (viewConfig.preRenderId) {
1075
+ const allOtherKeys = Object.keys(viewConfig).filter(
1076
+ (key) => !preRenderAllowedKeys.includes(key) && !key.startsWith('on'),
1077
+ );
1078
+
1079
+ allOtherKeys.forEach((key) => {
1080
+ if (
1081
+ !_.isUndefined(viewConfig[key])
1082
+ && !_.isEqual(viewConfig[key], preRenderedObject.viewConfig[key])
1083
+ ) {
1084
+ console.warn(
1085
+ `${this.embedComponentType} was pre-rendered with `
1086
+ + `"${key}" as "${JSON.stringify(preRenderedObject.viewConfig[key])}" `
1087
+ + `but a different value "${JSON.stringify(viewConfig[key])}" `
1088
+ + 'was passed to the Embed component. '
1089
+ + 'The new value provided is ignored, the value provided during '
1090
+ + 'preRender is used.',
1091
+ );
1092
+ }
1093
+ });
1094
+ }
1095
+ };
1096
+
1065
1097
  /**
1066
1098
  * Displays the PreRender component.
1067
1099
  * If the component is not preRendered, it attempts to create and render it.
@@ -1071,11 +1103,13 @@ export class TsEmbed {
1071
1103
  public showPreRender(): void {
1072
1104
  if (!this.isPreRenderAvailable()) {
1073
1105
  const isAvailable = this.connectPreRendered();
1106
+
1074
1107
  if (!isAvailable) {
1075
1108
  // if the Embed component is not preRendered , Render it now and
1076
1109
  this.preRender(true);
1077
1110
  return;
1078
1111
  }
1112
+ this.validatePreRenderViewConfig(this.viewConfig);
1079
1113
  }
1080
1114
 
1081
1115
  if (this.el) {
package/src/utils.spec.ts CHANGED
@@ -179,7 +179,6 @@ describe('unit test for utils', () => {
179
179
  it('should remove specified style properties from an HTML element', () => {
180
180
  const element = document.createElement('div');
181
181
 
182
- // Set initial styles for testing
183
182
  element.style.backgroundColor = 'blue';
184
183
  element.style.fontSize = '14px';
185
184
 
@@ -187,8 +186,8 @@ describe('unit test for utils', () => {
187
186
 
188
187
  removeStyleProperties(element, propertiesToRemove);
189
188
 
190
- expect(element.style.backgroundColor).toBe(''); // Check that the property is removed
191
- expect(element.style.fontSize).toBe(''); // Check that the property is removed
189
+ expect(element.style.backgroundColor).toBe('');
190
+ expect(element.style.fontSize).toBe('');
192
191
  });
193
192
 
194
193
  it('should handle undefined param', () => {
@@ -200,18 +199,15 @@ describe('unit test for utils', () => {
200
199
  it('should handle removing non-existent style properties', () => {
201
200
  const element = document.createElement('div');
202
201
 
203
- // Set initial styles for testing
204
202
  element.style.backgroundColor = 'blue';
205
203
  element.style.fontSize = '14px';
206
204
 
207
205
  const propertiesToRemove = ['color', 'border'];
208
206
 
209
- // Removing non-existent properties should not throw an error and
210
- // should not change existing styles
211
207
  removeStyleProperties(element, propertiesToRemove);
212
208
 
213
- expect(element.style.backgroundColor).toBe('blue'); // Style should remain unchanged
214
- expect(element.style.fontSize).toBe('14px'); // Style should remain unchanged
209
+ expect(element.style.backgroundColor).toBe('blue');
210
+ expect(element.style.fontSize).toBe('14px');
215
211
  });
216
212
  });
217
213