@@ -18,6 +18,11 @@ import { getCDN, setGlobalCDNUrl } from '../../lib/parse-cdn'
1818import { clearAjsBrowserStorage } from '../../test-helpers/browser-storage'
1919import { parseFetchCall } from '../../test-helpers/fetch-parse'
2020import { ActionDestination } from '../../plugins/remote-loader'
21+ import {
22+ highEntropyTestData ,
23+ lowEntropyTestData ,
24+ } from '../../lib/client-hints/__tests__/index.test'
25+ import { UADataValues } from '../../lib/client-hints/interfaces'
2126
2227let fetchCalls : ReturnType < typeof parseFetchCall > [ ] = [ ]
2328
@@ -207,76 +212,112 @@ describe('Initialization', () => {
207212 } )
208213 } )
209214
210- it ( 'calls page if initialpageview is set' , async ( ) => {
211- jest . mock ( '../../core/analytics' )
212- const mockPage = jest . fn ( ) . mockImplementation ( ( ) => Promise . resolve ( ) )
213- Analytics . prototype . page = mockPage
214-
215- await AnalyticsBrowser . load ( { writeKey } , { initialPageview : true } )
215+ describe ( 'Load options' , ( ) => {
216+ it ( 'gets high entropy client hints if set' , async ( ) => {
217+ ; ( window . navigator as any ) . userAgentData = {
218+ ...lowEntropyTestData ,
219+ getHighEntropyValues : jest
220+ . fn ( )
221+ . mockImplementation ( ( hints : string [ ] ) : Promise < UADataValues > => {
222+ let result = { }
223+ Object . entries ( highEntropyTestData ) . forEach ( ( [ k , v ] ) => {
224+ if ( hints . includes ( k ) ) {
225+ result = {
226+ ...result ,
227+ [ k ] : v ,
228+ }
229+ }
230+ } )
231+ return Promise . resolve ( {
232+ ...lowEntropyTestData ,
233+ ...result ,
234+ } )
235+ } ) ,
236+ toJSON : jest . fn ( ( ) => lowEntropyTestData ) ,
237+ }
216238
217- expect ( mockPage ) . toHaveBeenCalled ( )
218- } )
239+ const [ ajs ] = await AnalyticsBrowser . load (
240+ { writeKey } ,
241+ { highEntropyValuesClientHints : [ 'architecture' ] }
242+ )
219243
220- it ( 'does not call page if initialpageview is not set' , async ( ) => {
221- jest . mock ( '../../core/analytics' )
222- const mockPage = jest . fn ( )
223- Analytics . prototype . page = mockPage
224- await AnalyticsBrowser . load ( { writeKey } , { initialPageview : false } )
225- expect ( mockPage ) . not . toHaveBeenCalled ( )
226- } )
244+ const evt = await ajs . track ( 'foo' )
245+ expect ( evt . event . context ?. userAgentData ) . toEqual ( {
246+ ...lowEntropyTestData ,
247+ architecture : 'x86' ,
248+ } )
249+ } )
250+ it ( 'calls page if initialpageview is set' , async ( ) => {
251+ jest . mock ( '../../core/analytics' )
252+ const mockPage = jest . fn ( ) . mockImplementation ( ( ) => Promise . resolve ( ) )
253+ Analytics . prototype . page = mockPage
227254
228- it ( 'does not use a persisted queue when disableClientPersistence is true' , async ( ) => {
229- const [ ajs ] = await AnalyticsBrowser . load (
230- {
231- writeKey,
232- } ,
233- {
234- disableClientPersistence : true ,
235- }
236- )
255+ await AnalyticsBrowser . load ( { writeKey } , { initialPageview : true } )
237256
238- expect ( ajs . queue . queue instanceof PriorityQueue ) . toBe ( true )
239- expect ( ajs . queue . queue instanceof PersistedPriorityQueue ) . toBe ( false )
240- } )
257+ expect ( mockPage ) . toHaveBeenCalled ( )
258+ } )
241259
242- it ( 'uses a persisted queue by default' , async ( ) => {
243- const [ ajs ] = await AnalyticsBrowser . load ( {
244- writeKey,
260+ it ( 'does not call page if initialpageview is not set' , async ( ) => {
261+ jest . mock ( '../../core/analytics' )
262+ const mockPage = jest . fn ( )
263+ Analytics . prototype . page = mockPage
264+ await AnalyticsBrowser . load ( { writeKey } , { initialPageview : false } )
265+ expect ( mockPage ) . not . toHaveBeenCalled ( )
245266 } )
246267
247- expect ( ajs . queue . queue instanceof PersistedPriorityQueue ) . toBe ( true )
248- } )
268+ it ( 'does not use a persisted queue when disableClientPersistence is true' , async ( ) => {
269+ const [ ajs ] = await AnalyticsBrowser . load (
270+ {
271+ writeKey,
272+ } ,
273+ {
274+ disableClientPersistence : true ,
275+ }
276+ )
249277
250- it ( 'disables identity persistance when disableClientPersistence is true' , async ( ) => {
251- const [ ajs ] = await AnalyticsBrowser . load (
252- {
278+ expect ( ajs . queue . queue instanceof PriorityQueue ) . toBe ( true )
279+ expect ( ajs . queue . queue instanceof PersistedPriorityQueue ) . toBe ( false )
280+ } )
281+
282+ it ( 'uses a persisted queue by default' , async ( ) => {
283+ const [ ajs ] = await AnalyticsBrowser . load ( {
253284 writeKey,
254- } ,
255- {
256- disableClientPersistence : true ,
257- }
258- )
285+ } )
259286
260- expect ( ajs . user ( ) . options . persist ) . toBe ( false )
261- expect ( ajs . group ( ) . options . persist ) . toBe ( false )
262- } )
287+ expect ( ajs . queue . queue instanceof PersistedPriorityQueue ) . toBe ( true )
288+ } )
263289
264- it ( 'fetch remote source settings by default' , async ( ) => {
265- await AnalyticsBrowser . load ( {
266- writeKey,
290+ it ( 'disables identity persistance when disableClientPersistence is true' , async ( ) => {
291+ const [ ajs ] = await AnalyticsBrowser . load (
292+ {
293+ writeKey,
294+ } ,
295+ {
296+ disableClientPersistence : true ,
297+ }
298+ )
299+
300+ expect ( ajs . user ( ) . options . persist ) . toBe ( false )
301+ expect ( ajs . group ( ) . options . persist ) . toBe ( false )
267302 } )
268303
269- expect ( fetchCalls . length ) . toBeGreaterThan ( 0 )
270- expect ( fetchCalls [ 0 ] . url ) . toMatch ( / \/ s e t t i n g s $ / )
271- } )
304+ it ( 'fetch remote source settings by default' , async ( ) => {
305+ await AnalyticsBrowser . load ( {
306+ writeKey,
307+ } )
272308
273- it ( 'does not fetch source settings if cdnSettings is set' , async ( ) => {
274- await AnalyticsBrowser . load ( {
275- writeKey,
276- cdnSettings : { integrations : { } } ,
309+ expect ( fetchCalls . length ) . toBeGreaterThan ( 0 )
310+ expect ( fetchCalls [ 0 ] . url ) . toMatch ( / \/ s e t t i n g s $ / )
277311 } )
278312
279- expect ( fetchCalls . length ) . toBe ( 0 )
313+ it ( 'does not fetch source settings if cdnSettings is set' , async ( ) => {
314+ await AnalyticsBrowser . load ( {
315+ writeKey,
316+ cdnSettings : { integrations : { } } ,
317+ } )
318+
319+ expect ( fetchCalls . length ) . toBe ( 0 )
320+ } )
280321 } )
281322
282323 describe ( 'options.integrations permutations' , ( ) => {
0 commit comments