@@ -704,6 +704,117 @@ describe("Cal", () => {
704704 } ) ;
705705 } ) ;
706706
707+ describe ( "__reloadInitiated behavior - ensures correct bookerViewed vs bookerReloaded event firing" , ( ) => {
708+ /**
709+ * These tests verify that __reloadInitiated is sent correctly via doInIframe,
710+ * which determines whether bookerViewed or bookerReloaded fires in the iframe.
711+ *
712+ * - If __reloadInitiated is sent → iframe sets reloadInitiated=true → bookerReloaded fires
713+ * - If __reloadInitiated is NOT sent → iframe has reloadInitiated=false → bookerViewed fires
714+ */
715+ beforeEach ( ( ) => {
716+ calInstance = new CalClass ( "test-namespace" , [ ] ) ;
717+ window . Cal . config = { forwardQueryParams : false } ;
718+ vi . spyOn ( calInstance , "doInIframe" ) ;
719+ } ) ;
720+
721+ it ( "should send __reloadInitiated when fullReload action is taken (bookerReloaded should fire)" , async ( ) => {
722+ const baseModalArgs = {
723+ calLink : "john-doe/meeting" ,
724+ config : { theme : "light" , layout : "modern" } ,
725+ } ;
726+
727+ // 1. First modal open - creates new modal (no __reloadInitiated)
728+ await calInstance . api . modal ( { ...baseModalArgs , __prerender : true } ) ;
729+ expect ( calInstance . doInIframe ) . not . toHaveBeenCalledWith (
730+ expect . objectContaining ( { method : "__reloadInitiated" } )
731+ ) ;
732+
733+ vi . mocked ( calInstance . doInIframe ) . mockClear ( ) ;
734+
735+ // 2. Open modal with DIFFERENT calLink - triggers fullReload
736+ // This should send __reloadInitiated because it's a reload scenario
737+ await calInstance . api . modal ( {
738+ ...baseModalArgs ,
739+ calLink : "jane-doe/meeting" ,
740+ } ) ;
741+
742+ expect ( calInstance . doInIframe ) . toHaveBeenCalledWith ( {
743+ method : "__reloadInitiated" ,
744+ arg : { } ,
745+ } ) ;
746+ } ) ;
747+
748+ it ( "should NOT send __reloadInitiated when creating a new modal (bookerViewed should fire)" , async ( ) => {
749+ const baseModalArgs = {
750+ calLink : "john-doe/meeting" ,
751+ config : { theme : "light" , layout : "modern" } ,
752+ } ;
753+
754+ // Create a new modal - should NOT send __reloadInitiated
755+ await calInstance . api . modal ( baseModalArgs ) ;
756+
757+ expect ( calInstance . doInIframe ) . not . toHaveBeenCalledWith (
758+ expect . objectContaining ( { method : "__reloadInitiated" } )
759+ ) ;
760+ } ) ;
761+
762+ it ( "should NOT send __reloadInitiated when connect action is taken (bookerViewed should fire)" , async ( ) => {
763+ const baseModalArgs = {
764+ calLink : "john-doe/meeting" ,
765+ config : { theme : "light" , layout : "modern" } ,
766+ } ;
767+
768+ // 1. Prerender the modal
769+ await calInstance . api . modal ( { ...baseModalArgs , __prerender : true } ) ;
770+ vi . mocked ( calInstance . doInIframe ) . mockClear ( ) ;
771+
772+ // 2. Open modal with same calLink but different config - triggers connect (not fullReload)
773+ await calInstance . api . modal ( {
774+ ...baseModalArgs ,
775+ config : { ...baseModalArgs . config , name : "John Doe" } ,
776+ } ) ;
777+
778+ // Should call connect, NOT __reloadInitiated
779+ expect ( calInstance . doInIframe ) . toHaveBeenCalledWith (
780+ expect . objectContaining ( { method : "connect" } )
781+ ) ;
782+ expect ( calInstance . doInIframe ) . not . toHaveBeenCalledWith (
783+ expect . objectContaining ( { method : "__reloadInitiated" } )
784+ ) ;
785+ } ) ;
786+
787+ it ( "should clear stale __reloadInitiated from queue when loadInIframe is called again" , ( ) => {
788+ // This tests the queue clearing behavior that prevents stale __reloadInitiated
789+ // from causing bookerReloaded to fire incorrectly
790+
791+ // 1. Create iframe
792+ const iframe = calInstance . createIframe ( {
793+ calLink : "john-doe/meeting" ,
794+ config : { } ,
795+ calOrigin : null ,
796+ } ) ;
797+
798+ // 2. Simulate __reloadInitiated being queued (happens during fullReload)
799+ calInstance . doInIframe ( { method : "__reloadInitiated" , arg : { } } ) ;
800+ expect ( calInstance . iframeDoQueue ) . toHaveLength ( 1 ) ;
801+ expect ( calInstance . iframeDoQueue [ 0 ] . method ) . toBe ( "__reloadInitiated" ) ;
802+
803+ // 3. loadInIframe is called again (e.g., another fullReload before iframe ready)
804+ // This should clear the queue, removing the stale __reloadInitiated
805+ calInstance . loadInIframe ( {
806+ calLink : "jane-doe/meeting" ,
807+ config : { } ,
808+ calOrigin : null ,
809+ iframe,
810+ } ) ;
811+
812+ // 4. Queue should be cleared - stale __reloadInitiated removed
813+ // This ensures the new iframe won't receive the old __reloadInitiated
814+ expect ( calInstance . iframeDoQueue ) . toHaveLength ( 0 ) ;
815+ } ) ;
816+ } ) ;
817+
707818 describe ( "getNextActionForModal" , ( ) => {
708819 const baseArgs = {
709820 pathWithQueryToLoad : "john-doe/meeting" ,
0 commit comments