@@ -103,16 +103,16 @@ describe('insights', () => {
103
103
} ) ;
104
104
} ) ;
105
105
106
- it ( 'does not throw when an event is sent right after the creation in UMD ' , ( ) => {
106
+ it ( 'does not throw without userToken in UMD with the library loaded after the event ' , ( ) => {
107
107
const {
108
- analytics,
109
108
insightsClient,
110
109
libraryLoadedAndProcessQueue,
111
110
instantSearchInstance,
112
111
} = createUmdTestEnvironment ( ) ;
113
112
114
113
const middleware = createInsightsMiddleware ( {
115
114
insightsClient,
115
+ insightsInitParams : { useCookie : false } ,
116
116
} ) ( { instantSearchInstance } ) ;
117
117
middleware . subscribe ( ) ;
118
118
@@ -127,22 +127,98 @@ describe('insights', () => {
127
127
} ,
128
128
widgetType : 'ais.hits' ,
129
129
} ) ;
130
- expect ( analytics . viewedObjectIDs ) . toHaveBeenCalledTimes ( 0 ) ;
131
-
132
- // But, the library hasn't been loaded yet, so the event stays in the queue.
133
- expect ( insightsClient . queue [ insightsClient . queue . length - 1 ] ) . toEqual ( [
134
- 'viewedObjectIDs' ,
135
- { eventName : 'Hits Viewed' , index : '' , objectIDs : [ '1' , '2' ] } ,
136
- ] ) ;
137
130
138
131
// When the library is loaded later, it consumes the queue and sends the event.
132
+ expect ( ( ) => {
133
+ libraryLoadedAndProcessQueue ( ) ;
134
+ } ) . not . toThrow (
135
+ "Before calling any methods on the analytics, you first need to call 'setUserToken' function or include 'userToken' in the event payload."
136
+ ) ;
137
+ } ) ;
138
+
139
+ it ( 'does not throw without userToken in UMD with the library loaded before the event' , ( ) => {
140
+ const {
141
+ insightsClient,
142
+ libraryLoadedAndProcessQueue,
143
+ instantSearchInstance,
144
+ } = createUmdTestEnvironment ( ) ;
145
+
146
+ const middleware = createInsightsMiddleware ( {
147
+ insightsClient,
148
+ insightsInitParams : { useCookie : false } ,
149
+ } ) ( { instantSearchInstance } ) ;
150
+ middleware . subscribe ( ) ;
151
+
139
152
libraryLoadedAndProcessQueue ( ) ;
140
- expect ( analytics . viewedObjectIDs ) . toHaveBeenCalledTimes ( 1 ) ;
141
- expect ( analytics . viewedObjectIDs ) . toHaveBeenCalledWith ( {
142
- eventName : 'Hits Viewed' ,
143
- index : '' ,
144
- objectIDs : [ '1' , '2' ] ,
145
- } ) ;
153
+
154
+ expect ( ( ) => {
155
+ // It tries to send an event.
156
+ instantSearchInstance . sendEventToInsights ( {
157
+ eventType : 'view' ,
158
+ insightsMethod : 'viewedObjectIDs' ,
159
+ payload : {
160
+ eventName : 'Hits Viewed' ,
161
+ index : '' ,
162
+ objectIDs : [ '1' , '2' ] ,
163
+ } ,
164
+ widgetType : 'ais.hits' ,
165
+ } ) ;
166
+ } ) . not . toThrow (
167
+ "Before calling any methods on the analytics, you first need to call 'setUserToken' function or include 'userToken' in the event payload."
168
+ ) ;
169
+ } ) ;
170
+
171
+ it ( 'does not throw without userToken in CJS' , ( ) => {
172
+ const { insightsClient, instantSearchInstance } = createTestEnvironment ( ) ;
173
+
174
+ const middleware = createInsightsMiddleware ( {
175
+ insightsClient,
176
+ insightsInitParams : { useCookie : false } ,
177
+ } ) ( { instantSearchInstance } ) ;
178
+ middleware . subscribe ( ) ;
179
+
180
+ expect ( ( ) => {
181
+ // It tries to send an event.
182
+ instantSearchInstance . sendEventToInsights ( {
183
+ eventType : 'view' ,
184
+ insightsMethod : 'viewedObjectIDs' ,
185
+ payload : {
186
+ eventName : 'Hits Viewed' ,
187
+ index : '' ,
188
+ objectIDs : [ '1' , '2' ] ,
189
+ } ,
190
+ widgetType : 'ais.hits' ,
191
+ } ) ;
192
+ } ) . not . toThrow (
193
+ "Before calling any methods on the analytics, you first need to call 'setUserToken' function or include 'userToken' in the event payload."
194
+ ) ;
195
+ } ) ;
196
+
197
+ it ( 'warns when userToken is not set' , ( ) => {
198
+ const { insightsClient, instantSearchInstance } = createTestEnvironment ( ) ;
199
+
200
+ const middleware = createInsightsMiddleware ( {
201
+ insightsClient,
202
+ insightsInitParams : { useCookie : false } ,
203
+ } ) ( { instantSearchInstance } ) ;
204
+ middleware . subscribe ( ) ;
205
+
206
+ expect ( ( ) =>
207
+ instantSearchInstance . sendEventToInsights ( {
208
+ eventType : 'view' ,
209
+ insightsMethod : 'viewedObjectIDs' ,
210
+ payload : {
211
+ eventName : 'Hits Viewed' ,
212
+ index : '' ,
213
+ objectIDs : [ '1' , '2' ] ,
214
+ } ,
215
+ widgetType : 'ais.hits' ,
216
+ } )
217
+ ) . toWarnDev (
218
+ `[InstantSearch.js]: Cannot send event to Algolia Insights because \`userToken\` is not set.
219
+
220
+ See documentation: https://www.algolia.com/doc/guides/building-search-ui/going-further/send-insights-events/js/#setting-the-usertoken`
221
+ ) ;
146
222
} ) ;
147
223
148
224
it ( 'applies clickAnalytics' , ( ) => {
0 commit comments