1+ import { PERFORMANCE_ENTRY_TYPE } from '../../../../src/common/vitals/constants'
2+
13beforeEach ( ( ) => {
24 jest . resetModules ( )
35 jest . resetAllMocks ( )
46 jest . clearAllMocks ( )
5- } )
67
7- const fidAttribution = {
8- eventTarget : 'html>body' ,
9- eventType : 'pointerdown' ,
10- eventTime : 1 ,
11- loadState : 'loading'
12- }
13- let triggeronFIDCallback
14- const getFreshFIDImport = async ( codeToRun ) => {
15- jest . doMock ( 'web-vitals/attribution' , ( ) => ( {
16- onFID : jest . fn ( cb => { triggeronFIDCallback = cb ; cb ( { value : 100 , attribution : fidAttribution } ) } )
8+ const mockPerformanceObserver = jest . fn ( cb => ( {
9+ // Note: this is an imperfect mock, as observer.disconnect() is not functional
10+ observe : ( ) => {
11+ const callCb = ( ) => {
12+ cb ( {
13+ getEntries : ( ) => [ {
14+ name : PERFORMANCE_ENTRY_TYPE . FIRST_INPUT ,
15+ startTime : 1
16+ } ]
17+ } )
18+ setTimeout ( callCb , 250 )
19+ }
20+ setTimeout ( callCb , 250 )
21+ } ,
22+ disconnect : jest . fn ( )
1723 } ) )
18- const { firstInputDelay } = await import ( '../../../../src/common/vitals/first-input-delay' )
19- codeToRun ( firstInputDelay )
24+ global . PerformanceObserver = mockPerformanceObserver
25+ global . PerformanceObserver . supportedEntryTypes = [ PERFORMANCE_ENTRY_TYPE . FIRST_INPUT ]
26+ } )
27+
28+ const getFreshImport = async ( codeToRun ) => {
29+ const { firstInteraction } = await import ( '../../../../src/common/vitals/first-interaction' )
30+ codeToRun ( firstInteraction )
2031}
2132
22- describe ( 'fid' , ( ) => {
23- test ( 'reports fcp from web-vitals' , ( done ) => {
24- getFreshFIDImport ( metric => metric . subscribe ( ( { value, attrs } ) => {
25- expect ( value ) . toEqual ( 1 )
26- expect ( attrs . type ) . toEqual ( fidAttribution . eventType )
27- expect ( attrs . fid ) . toEqual ( 100 )
28- expect ( attrs . eventTarget ) . toEqual ( fidAttribution . eventTarget )
29- expect ( attrs . loadState ) . toEqual ( fidAttribution . loadState )
30- done ( )
31- } ) )
33+ describe ( 'fi (first interaction)' , ( ) => {
34+ test ( 'reports fi' , ( done ) => {
35+ getFreshImport ( metric => {
36+ metric . subscribe ( ( { value } ) => {
37+ expect ( value ) . toEqual ( 1 )
38+ done ( )
39+ } )
40+ } )
3241 } )
3342
3443 test ( 'Does NOT report values if initiallyHidden' , ( done ) => {
@@ -38,7 +47,7 @@ describe('fid', () => {
3847 isBrowserScope : true
3948 } ) )
4049
41- getFreshFIDImport ( metric => {
50+ getFreshImport ( metric => {
4251 metric . subscribe ( ( ) => {
4352 console . log ( 'should not have reported' )
4453 expect ( 1 ) . toEqual ( 2 )
@@ -50,11 +59,12 @@ describe('fid', () => {
5059 test ( 'does NOT report if not browser scoped' , ( done ) => {
5160 jest . doMock ( '../../../../src/common/constants/runtime' , ( ) => ( {
5261 __esModule : true ,
62+ initiallyHidden : false ,
5363 isBrowserScope : false
5464 } ) )
5565
56- getFreshFIDImport ( metric => {
57- metric . subscribe ( ( ) => {
66+ getFreshImport ( metric => {
67+ metric . subscribe ( ( { value , attrs } ) => {
5868 console . log ( 'should not have reported...' )
5969 expect ( 1 ) . toEqual ( 2 )
6070 } )
@@ -65,10 +75,11 @@ describe('fid', () => {
6575 test ( 'multiple subs get same value' , done => {
6676 jest . doMock ( '../../../../src/common/constants/runtime' , ( ) => ( {
6777 __esModule : true ,
78+ initiallyHidden : false ,
6879 isBrowserScope : true
6980 } ) )
7081 let witness = 0
71- getFreshFIDImport ( metric => {
82+ getFreshImport ( metric => {
7283 metric . subscribe ( ( { value } ) => {
7384 expect ( value ) . toEqual ( 1 )
7485 witness ++
@@ -88,15 +99,14 @@ describe('fid', () => {
8899 isBrowserScope : true
89100 } ) )
90101 let triggered = 0
91- getFreshFIDImport ( metric => {
92- metric . subscribe ( ( { value } ) => {
93- triggered ++
94- expect ( value ) . toEqual ( 1 )
95- expect ( triggered ) . toEqual ( 1 )
96- } )
97- triggeronFIDCallback ( { value : 'notequal1' } )
102+ getFreshImport ( metric => metric . subscribe ( ( { value } ) => {
103+ triggered ++
104+ expect ( value ) . toEqual ( 1 )
98105 expect ( triggered ) . toEqual ( 1 )
99- done ( )
100- } )
106+ setTimeout ( ( ) => {
107+ expect ( triggered ) . toEqual ( 1 )
108+ done ( )
109+ } , 1000 )
110+ } ) )
101111 } )
102112} )
0 commit comments