1
1
describe ( 'service workers' , ( ) => {
2
+ let sessionId
3
+
4
+ const getSessionId = async ( ) => {
5
+ if ( ! sessionId ) {
6
+ const targets = ( await Cypress . automation ( 'remote:debugger:protocol' , { command : 'Target.getTargets' , params : { } } ) ) . targetInfos
7
+ const serviceWorkerTarget = targets . reverse ( ) . find ( ( target ) => target . type === 'service_worker' && target . url === 'http://localhost:3500/fixtures/service-worker.js' )
8
+
9
+ ; ( { sessionId } = await Cypress . automation ( 'remote:debugger:protocol' , { command : 'Target.attachToTarget' , params : { targetId : serviceWorkerTarget . targetId , flatten : true } } ) )
10
+ }
11
+
12
+ return sessionId
13
+ }
14
+
15
+ const getEventListenersLength = async ( ) => {
16
+ const sessionId = await getSessionId ( )
17
+ let result = await Cypress . automation ( 'remote:debugger:protocol' , { command : 'Runtime.evaluate' , params : { expression : 'getEventListeners(self).fetch' , includeCommandLineAPI : true } , sessionId } )
18
+
19
+ if ( result . result . type === 'undefined' ) return 0
20
+
21
+ result = await Cypress . automation ( 'remote:debugger:protocol' , { command : 'Runtime.getProperties' , params : { objectId : result . result . objectId } , sessionId } )
22
+
23
+ const length = result . result . find ( ( prop ) => prop . name === 'length' ) . value . value
24
+
25
+ return length
26
+ }
27
+
28
+ const getOnFetchHandlerType = async ( ) => {
29
+ const sessionId = await getSessionId ( )
30
+
31
+ const result = await Cypress . automation ( 'remote:debugger:protocol' , { command : 'Runtime.evaluate' , params : { expression : 'self.onfetch' , includeCommandLineAPI : true } , sessionId } )
32
+
33
+ return result . result . type
34
+ }
35
+
2
36
beforeEach ( async ( ) => {
37
+ sessionId = null
38
+
3
39
// unregister the service worker to ensure it does not affect other tests
4
40
const registrations = await navigator . serviceWorker . getRegistrations ( )
5
41
@@ -21,6 +57,9 @@ describe('service workers', () => {
21
57
22
58
cy . visit ( 'fixtures/service-worker.html' )
23
59
cy . get ( '#output' ) . should ( 'have.text' , 'done' )
60
+ cy . then ( async ( ) => {
61
+ expect ( await getEventListenersLength ( ) ) . to . equal ( 1 )
62
+ } )
24
63
} )
25
64
26
65
it ( 'supports using addEventListener with object' , ( ) => {
@@ -41,6 +80,9 @@ describe('service workers', () => {
41
80
42
81
cy . visit ( 'fixtures/service-worker.html' )
43
82
cy . get ( '#output' ) . should ( 'have.text' , 'done' )
83
+ cy . then ( async ( ) => {
84
+ expect ( await getEventListenersLength ( ) ) . to . equal ( 1 )
85
+ } )
44
86
} )
45
87
46
88
it ( 'supports using addEventListener with delayed handleEvent' , ( ) => {
@@ -60,6 +102,9 @@ describe('service workers', () => {
60
102
61
103
cy . visit ( 'fixtures/service-worker.html' )
62
104
cy . get ( '#output' ) . should ( 'have.text' , 'done' )
105
+ cy . then ( async ( ) => {
106
+ expect ( await getEventListenersLength ( ) ) . to . equal ( 1 )
107
+ } )
63
108
} )
64
109
65
110
it ( 'supports using onfetch' , ( ) => {
@@ -76,6 +121,11 @@ describe('service workers', () => {
76
121
77
122
cy . visit ( 'fixtures/service-worker.html' )
78
123
cy . get ( '#output' ) . should ( 'have.text' , 'done' )
124
+ cy . then ( async ( ) => {
125
+ // onfetch will add an event listener
126
+ expect ( await getEventListenersLength ( ) ) . to . equal ( 1 )
127
+ expect ( await getOnFetchHandlerType ( ) ) . to . equal ( 'function' )
128
+ } )
79
129
} )
80
130
} )
81
131
@@ -94,6 +144,9 @@ describe('service workers', () => {
94
144
95
145
cy . visit ( 'fixtures/service-worker.html' )
96
146
cy . get ( '#output' ) . should ( 'have.text' , 'done' )
147
+ cy . then ( async ( ) => {
148
+ expect ( await getEventListenersLength ( ) ) . to . equal ( 1 )
149
+ } )
97
150
} )
98
151
99
152
it ( 'supports using onfetch' , ( ) => {
@@ -110,6 +163,11 @@ describe('service workers', () => {
110
163
111
164
cy . visit ( 'fixtures/service-worker.html' )
112
165
cy . get ( '#output' ) . should ( 'have.text' , 'done' )
166
+ cy . then ( async ( ) => {
167
+ // onfetch will add an event listener
168
+ expect ( await getEventListenersLength ( ) ) . to . equal ( 1 )
169
+ expect ( await getOnFetchHandlerType ( ) ) . to . equal ( 'function' )
170
+ } )
113
171
} )
114
172
} )
115
173
@@ -138,6 +196,9 @@ describe('service workers', () => {
138
196
139
197
cy . visit ( 'fixtures/service-worker.html' )
140
198
cy . get ( '#output' ) . should ( 'have.text' , 'done' )
199
+ cy . then ( async ( ) => {
200
+ expect ( await getEventListenersLength ( ) ) . to . equal ( 1 )
201
+ } )
141
202
} )
142
203
143
204
it ( 'supports using onfetch' , ( ) => {
@@ -163,6 +224,11 @@ describe('service workers', () => {
163
224
164
225
cy . visit ( 'fixtures/service-worker.html' )
165
226
cy . get ( '#output' ) . should ( 'have.text' , 'done' )
227
+ cy . then ( async ( ) => {
228
+ // onfetch will add an event listener
229
+ expect ( await getEventListenersLength ( ) ) . to . equal ( 1 )
230
+ expect ( await getOnFetchHandlerType ( ) ) . to . equal ( 'function' )
231
+ } )
166
232
} )
167
233
} )
168
234
@@ -185,6 +251,41 @@ describe('service workers', () => {
185
251
186
252
cy . visit ( 'fixtures/service-worker.html' )
187
253
cy . get ( '#output' ) . should ( 'have.text' , 'done' )
254
+ cy . then ( async ( ) => {
255
+ // onfetch will also add an event listener
256
+ expect ( await getEventListenersLength ( ) ) . to . equal ( 2 )
257
+ expect ( await getOnFetchHandlerType ( ) ) . to . equal ( 'function' )
258
+ } )
259
+ } )
260
+
261
+ it ( 'supports other options' , ( ) => {
262
+ const script = ( ) => {
263
+ const handler = function ( event ) {
264
+ event . respondWith ( fetch ( event . request ) )
265
+ }
266
+
267
+ self . addEventListener ( 'fetch' , handler )
268
+
269
+ // this one does not get added because capture is the same
270
+ self . addEventListener ( 'fetch' , handler , { capture : false } )
271
+
272
+ // this one gets added because capture is different
273
+ self . addEventListener ( 'fetch' , handler , { capture : true } )
274
+
275
+ // this one does not get added because capture is the same
276
+ self . addEventListener ( 'fetch' , handler , { once : true } )
277
+ }
278
+
279
+ cy . intercept ( '/fixtures/service-worker.js' , ( req ) => {
280
+ req . reply ( `(${ script } )()` ,
281
+ { 'Content-Type' : 'application/javascript' } )
282
+ } )
283
+
284
+ cy . visit ( 'fixtures/service-worker.html' )
285
+ cy . get ( '#output' ) . should ( 'have.text' , 'done' )
286
+ cy . then ( async ( ) => {
287
+ expect ( await getEventListenersLength ( ) ) . to . equal ( 2 )
288
+ } )
188
289
} )
189
290
} )
190
291
} )
0 commit comments