1
- import { PERFORMANCE_ENTRY_TYPE } from '../../../../src/common/vitals/constants'
2
-
3
1
beforeEach ( ( ) => {
4
2
jest . resetModules ( )
5
3
jest . resetAllMocks ( )
6
4
jest . clearAllMocks ( )
7
-
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 ( )
23
- } ) )
24
- global . PerformanceObserver = mockPerformanceObserver
25
- global . PerformanceObserver . supportedEntryTypes = [ PERFORMANCE_ENTRY_TYPE . FIRST_INPUT ]
26
5
} )
27
6
28
- const getFreshImport = async ( codeToRun ) => {
29
- const { firstInteraction } = await import ( '../../../../src/common/vitals/first-interaction' )
30
- codeToRun ( firstInteraction )
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 } ) } )
17
+ } ) )
18
+ const { firstInputDelay } = await import ( '../../../../src/common/vitals/first-input-delay' )
19
+ codeToRun ( firstInputDelay )
31
20
}
32
21
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
- } )
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
+ } ) )
41
32
} )
42
33
43
34
test ( 'Does NOT report values if initiallyHidden' , ( done ) => {
@@ -47,7 +38,7 @@ describe('fi (first interaction)', () => {
47
38
isBrowserScope : true
48
39
} ) )
49
40
50
- getFreshImport ( metric => {
41
+ getFreshFIDImport ( metric => {
51
42
metric . subscribe ( ( ) => {
52
43
console . log ( 'should not have reported' )
53
44
expect ( 1 ) . toEqual ( 2 )
@@ -63,7 +54,7 @@ describe('fi (first interaction)', () => {
63
54
isBrowserScope : false
64
55
} ) )
65
56
66
- getFreshImport ( metric => {
57
+ getFreshFIDImport ( metric => {
67
58
metric . subscribe ( ( { value, attrs } ) => {
68
59
console . log ( 'should not have reported...' )
69
60
expect ( 1 ) . toEqual ( 2 )
@@ -79,7 +70,7 @@ describe('fi (first interaction)', () => {
79
70
isBrowserScope : true
80
71
} ) )
81
72
let witness = 0
82
- getFreshImport ( metric => {
73
+ getFreshFIDImport ( metric => {
83
74
metric . subscribe ( ( { value } ) => {
84
75
expect ( value ) . toEqual ( 1 )
85
76
witness ++
@@ -99,14 +90,15 @@ describe('fi (first interaction)', () => {
99
90
isBrowserScope : true
100
91
} ) )
101
92
let triggered = 0
102
- getFreshImport ( metric => metric . subscribe ( ( { value } ) => {
103
- triggered ++
104
- expect ( value ) . toEqual ( 1 )
105
- expect ( triggered ) . toEqual ( 1 )
106
- setTimeout ( ( ) => {
93
+ getFreshFIDImport ( metric => {
94
+ metric . subscribe ( ( { value } ) => {
95
+ triggered ++
96
+ expect ( value ) . toEqual ( 1 )
107
97
expect ( triggered ) . toEqual ( 1 )
108
- done ( )
109
- } , 1000 )
110
- } ) )
98
+ } )
99
+ triggeronFIDCallback ( { value : 'notequal1' } )
100
+ expect ( triggered ) . toEqual ( 1 )
101
+ done ( )
102
+ } )
111
103
} )
112
104
} )
0 commit comments