@@ -51,12 +51,13 @@ import * as mixpanelInstance from '../mixpanel-service';
5151import * as authInstance from '../auth' ;
5252import * as baseInstance from './base' ;
5353import { MIXPANEL_EVENT } from '../mixpanel-service' ;
54- import * as authService from '../utils/authService/authService ' ;
54+ import * as authService from '../utils/authService' ;
5555import { logger } from '../utils/logger' ;
5656import { version } from '../../package.json' ;
5757import { HiddenActionItemByDefaultForSearchEmbed } from './search' ;
5858import { processTrigger } from '../utils/processTrigger' ;
5959import { UIPassthroughEvent } from './hostEventClient/contracts' ;
60+ import * as sessionInfoService from '../utils/sessionInfoService' ;
6061
6162jest . mock ( '../utils/processTrigger' ) ;
6263
@@ -1147,6 +1148,99 @@ describe('Unit test case for ts embed', () => {
11471148 } ) ;
11481149 } ) ;
11491150
1151+ describe ( 'Trigger infoSuccess event on iframe load' , ( ) => {
1152+ beforeAll ( ( ) => {
1153+ jest . clearAllMocks ( ) ;
1154+ init ( {
1155+ thoughtSpotHost,
1156+ authType : AuthType . None ,
1157+ loginFailedMessage : 'Failed to Login' ,
1158+ } ) ;
1159+ } ) ;
1160+
1161+ const setup = async ( isLoggedIn = false , overrideOrgId : number | undefined = undefined ) => {
1162+ jest . spyOn ( window , 'addEventListener' ) . mockImplementationOnce (
1163+ ( event , handler , options ) => {
1164+ handler ( {
1165+ data : {
1166+ type : 'xyz' ,
1167+ } ,
1168+ ports : [ 3000 ] ,
1169+ source : null ,
1170+ } ) ;
1171+ } ,
1172+ ) ;
1173+ mockProcessTrigger . mockResolvedValueOnce ( { session : 'test' } ) ;
1174+ // resetCachedPreauthInfo();
1175+ let mockGetPreauthInfo = null ;
1176+
1177+ if ( overrideOrgId ) {
1178+ mockGetPreauthInfo = jest . spyOn ( sessionInfoService , 'getPreauthInfo' ) . mockImplementation ( jest . fn ( ) ) ;
1179+ }
1180+
1181+ const mockPreauthInfoFetch = jest . spyOn ( authService , 'fetchPreauthInfoService' ) . mockResolvedValueOnce ( {
1182+ ok : true ,
1183+ headers : new Headers ( { 'content-type' : 'application/json' } ) , // Mock headers correctly
1184+ json : async ( ) => ( {
1185+ info : {
1186+ configInfo : {
1187+ mixpanelConfig : {
1188+ devSdkKey : 'devSdkKey' ,
1189+ } ,
1190+ } ,
1191+ userGUID : 'userGUID' ,
1192+ } ,
1193+ } ) , // Mock JSON response
1194+ } ) ;
1195+ const iFrame : any = document . createElement ( 'div' ) ;
1196+ jest . spyOn ( baseInstance , 'getAuthPromise' ) . mockResolvedValueOnce ( isLoggedIn ) ;
1197+ const tsEmbed = new SearchEmbed ( getRootEl ( ) , {
1198+ overrideOrgId,
1199+ } ) ;
1200+ iFrame . contentWindow = {
1201+ postMessage : jest . fn ( ) ,
1202+ } ;
1203+ tsEmbed . on ( EmbedEvent . CustomAction , jest . fn ( ) ) ;
1204+ jest . spyOn ( iFrame , 'addEventListener' ) . mockImplementationOnce (
1205+ ( event , handler , options ) => {
1206+ handler ( { } ) ;
1207+ } ,
1208+ ) ;
1209+ jest . spyOn ( document , 'createElement' ) . mockReturnValueOnce ( iFrame ) ;
1210+ await tsEmbed . render ( ) ;
1211+
1212+ return {
1213+ mockPreauthInfoFetch,
1214+ mockGetPreauthInfo,
1215+ iFrame,
1216+ } ;
1217+ } ;
1218+
1219+ test ( 'should call InfoSuccess Event on preauth call success' , async ( ) => {
1220+ const {
1221+ mockPreauthInfoFetch,
1222+ iFrame,
1223+ } = await setup ( true ) ;
1224+ expect ( mockPreauthInfoFetch ) . toHaveBeenCalledTimes ( 1 ) ;
1225+
1226+ await executeAfterWait ( ( ) => {
1227+ expect ( mockProcessTrigger ) . toHaveBeenCalledWith (
1228+ iFrame ,
1229+ HostEvent . InfoSuccess ,
1230+ 'http://tshost' ,
1231+ expect . objectContaining ( { info : expect . any ( Object ) } ) ,
1232+ ) ;
1233+ } ) ;
1234+ } ) ;
1235+
1236+ test ( 'should not call InfoSuccess Event if overrideOrgId is true' , async ( ) => {
1237+ const {
1238+ mockGetPreauthInfo,
1239+ } = await setup ( true , 123 ) ;
1240+ expect ( mockGetPreauthInfo ) . toHaveBeenCalledTimes ( 0 ) ;
1241+ } ) ;
1242+ } ) ;
1243+
11501244 describe ( 'when thoughtSpotHost have value and authPromise return error' , ( ) => {
11511245 beforeAll ( ( ) => {
11521246 init ( {
0 commit comments