Skip to content

Commit 386e63c

Browse files
SCAL-236431 corrected related tests
1 parent 4455cbe commit 386e63c

File tree

5 files changed

+168
-120
lines changed

5 files changed

+168
-120
lines changed

src/embed/events.spec.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -272,15 +272,18 @@ describe('test communication between host app and ThoughtSpot', () => {
272272
});
273273

274274
await executeAfterWait(() => {
275-
expect(spy).toHaveBeenCalledTimes(3);
275+
expect(spy).toHaveBeenCalledTimes(4);
276276
expect(spy.mock.calls[0][0]).toMatchObject({
277-
type: EmbedEvent.Init,
277+
type: EmbedEvent.InitStateChange,
278278
});
279279
expect(spy.mock.calls[1][0]).toMatchObject({
280+
type: EmbedEvent.Init,
281+
});
282+
expect(spy.mock.calls[2][0]).toMatchObject({
280283
type: EmbedEvent.CustomAction,
281284
data: PAYLOAD,
282285
});
283-
expect(spy.mock.calls[2][0]).toMatchObject({
286+
expect(spy.mock.calls[3][0]).toMatchObject({
284287
type: EmbedEvent.DialogOpen,
285288
});
286289
}, EVENT_WAIT_TIME);

src/embed/ts-embed.spec.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1926,7 +1926,8 @@ describe('Unit test case for ts embed', () => {
19261926
let mockReject: (error: any) => void;
19271927

19281928
beforeEach(() => {
1929-
mockInitPromise = new Promise<void>((resolve) => {
1929+
mockInitPromise = new Promise<void>((resolve , reject) => {
1930+
mockReject = (error: any) => reject(error);
19301931
mockResolve = () => resolve();
19311932
});
19321933

@@ -1963,6 +1964,7 @@ describe('Unit test case for ts embed', () => {
19631964
// Extract just the first argument (the data)
19641965
const [callData] = initStateChangeCallback.mock.calls[0];
19651966
expect(callData).toEqual({
1967+
type: EmbedEvent.InitStateChange,
19661968
state: InitState.Ready,
19671969
previousState: InitState.Initializing,
19681970
timestamp: expect.any(Number),
@@ -1971,6 +1973,35 @@ describe('Unit test case for ts embed', () => {
19711973
// waitForInit should resolve
19721974
await expect(tsEmbed.waitForInit()).resolves.toBeUndefined();
19731975
});
1976+
1977+
test('Should wait for initialization in renderIFrame when not ready', async () => {
1978+
tsEmbed = new SearchEmbed(getRootEl(), {});
1979+
const renderPromise = tsEmbed['renderIFrame']('http://test-url');
1980+
let renderCompleted = false;
1981+
renderPromise.then(() => { renderCompleted = true; });
1982+
expect(renderCompleted).toBe(false);
1983+
await new Promise(resolve => setTimeout(resolve, 0));
1984+
mockResolve();
1985+
await mockInitPromise;
1986+
await renderPromise;
1987+
expect(renderCompleted).toBe(true);
1988+
});
1989+
1990+
test('Should wait for initialization in ensureInitialized when not ready', async () => {
1991+
tsEmbed = new SearchEmbed(getRootEl(), {});
1992+
const ensurePromise = tsEmbed['ensureInitialized']();
1993+
let ensureCompleted = false;
1994+
ensurePromise.then(() => { ensureCompleted = true; });
1995+
expect(ensureCompleted).toBe(false);
1996+
});
1997+
1998+
test('Should wait for initialization in waitForInit when not ready', async () => {
1999+
tsEmbed = new SearchEmbed(getRootEl(), {});
2000+
const waitForInitPromise = tsEmbed.waitForInit();
2001+
let waitForInitCompleted = false;
2002+
waitForInitPromise.then(() => { waitForInitCompleted = true; });
2003+
expect(waitForInitCompleted).toBe(false);
2004+
});
19742005
});
19752006

19762007
describe('V1Embed ', () => {

src/embed/ts-embed.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,11 @@ export class TsEmbed {
202202
*/
203203
private initPromiseResolve: () => void;
204204

205+
/**
206+
* Reject function for the init promise
207+
*/
208+
private initPromiseReject: (error: any) => void;
209+
205210
/**
206211
* Sets the initialization state and emits events
207212
*/
@@ -210,6 +215,7 @@ export class TsEmbed {
210215
this.initState = newState;
211216

212217
this.executeCallbacks(EmbedEvent.InitStateChange, {
218+
type: EmbedEvent.InitStateChange,
213219
state: newState,
214220
previousState,
215221
timestamp: Date.now(),
@@ -262,8 +268,9 @@ export class TsEmbed {
262268

263269
this.hostEventClient = new HostEventClient(this.iFrame);
264270

265-
this.initPromise = new Promise((resolve) => {
271+
this.initPromise = new Promise((resolve , reject) => {
266272
this.initPromiseResolve = resolve;
273+
this.initPromiseReject = reject;
267274
});
268275
this.setInitState(InitState.Initializing);
269276

@@ -275,6 +282,8 @@ export class TsEmbed {
275282
this.thoughtSpotV2Base = getV2BasePath(embedConfig);
276283
this.shouldEncodeUrlQueryParams = embedConfig.shouldEncodeUrlQueryParams;
277284
this.setInitState(InitState.Ready);
285+
}).catch((error) => {
286+
this.initPromiseReject(error);
278287
});
279288
}
280289

@@ -876,7 +885,7 @@ export class TsEmbed {
876885
try {
877886
await this.ensureInitialized();
878887
} catch (error) {
879-
this.handleError('Cannot render: initialization failed');
888+
this.handleError(ERROR_MESSAGE.EMBED_INITIALIZATION_FAILED);
880889
return null;
881890
}
882891
if (url.length > URL_MAX_LENGTH) {

src/types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5895,6 +5895,7 @@ export enum LogLevel {
58955895
* NotStarted = 'not-started'
58965896
* Initializing = 'initializing'
58975897
* Ready = 'ready'
5898+
* Error = 'error'
58985899
* ```
58995900
*/
59005901
export enum InitState {
@@ -5910,6 +5911,10 @@ export enum InitState {
59105911
* SDK initialization completed successfully
59115912
*/
59125913
Ready = 'ready',
5914+
/**
5915+
* SDK initialization failed
5916+
*/
5917+
Error = 'error',
59135918
}
59145919

59155920
export interface DefaultAppInitData {

0 commit comments

Comments
 (0)