@@ -92,4 +92,65 @@ describe('Hubspot', () => {
92
92
expect ( spy ) . toHaveBeenCalledTimes ( 2 ) ;
93
93
} ) ;
94
94
} ) ;
95
+
96
+ describe ( 'openFromMetaTag' , ( ) => {
97
+ beforeEach ( ( ) => {
98
+ document . head . innerHTML = '' ;
99
+ jest . spyOn ( window . history , 'pushState' ) . mockImplementation ( ) ;
100
+ global . window . HubSpotConversations = {
101
+ widget : {
102
+ load : jest . fn ( ) ,
103
+ open : jest . fn ( ) ,
104
+ } ,
105
+ } ;
106
+ } ) ;
107
+
108
+ afterEach ( ( ) => {
109
+ jest . restoreAllMocks ( ) ;
110
+ } ) ;
111
+
112
+ it ( 'opens chat widget when conversation meta tag exists' , ( ) => {
113
+ // Add a meta tag to the document
114
+ const metaTag = document . createElement ( 'meta' ) ;
115
+ metaTag . name = 'conversation' ;
116
+ metaTag . content = 'sales' ;
117
+ document . head . appendChild ( metaTag ) ;
118
+
119
+ hubspot ( '12345' ) ;
120
+
121
+ // Simulate the HubSpot script loading by calling the registered callback
122
+ global . hsConversationsOnReady [ 0 ] ( ) ;
123
+
124
+ expect ( window . history . pushState ) . toHaveBeenCalledWith ( { } , '' , '?chat-type=sales' ) ;
125
+ expect ( global . window . HubSpotConversations . widget . load ) . toHaveBeenCalled ( ) ;
126
+ expect ( global . window . HubSpotConversations . widget . open ) . toHaveBeenCalled ( ) ;
127
+ } ) ;
128
+
129
+ it ( 'does not open chat widget when no conversation meta tag exists' , ( ) => {
130
+ document . head . innerHTML = '<meta name="other-tag" content="something">' ;
131
+
132
+ hubspot ( '12345' ) ;
133
+
134
+ // Simulate the HubSpot script loading
135
+ global . hsConversationsOnReady [ 0 ] ( ) ;
136
+
137
+ expect ( window . history . pushState ) . not . toHaveBeenCalled ( ) ;
138
+ expect ( global . window . HubSpotConversations . widget . load ) . not . toHaveBeenCalled ( ) ;
139
+ expect ( global . window . HubSpotConversations . widget . open ) . not . toHaveBeenCalled ( ) ;
140
+ } ) ;
141
+
142
+ it ( 'correctly sets URL query parameter from meta tag content' , ( ) => {
143
+ const metaTag = document . createElement ( 'meta' ) ;
144
+ metaTag . name = 'conversation' ;
145
+ metaTag . content = 'support' ;
146
+ document . head . appendChild ( metaTag ) ;
147
+
148
+ hubspot ( '12345' ) ;
149
+
150
+ // Simulate the HubSpot script loading
151
+ global . hsConversationsOnReady [ 0 ] ( ) ;
152
+
153
+ expect ( window . history . pushState ) . toHaveBeenCalledWith ( { } , '' , '?chat-type=support' ) ;
154
+ } ) ;
155
+ } ) ;
95
156
} ) ;
0 commit comments