1
+ import makeServiceWorkerEnv = require( "service-worker-mock" )
2
+ const makeFetchMock = require ( "service-worker-mock/fetch" )
3
+
1
4
describe ( "serviceWorker" , ( ) => {
2
- it ( "should add the proper eventListeners" , ( ) => {
3
- // make sure install, active and fetch were added as event listeners
5
+ let spy : jest . SpyInstance
6
+ beforeEach ( ( ) => {
7
+ Object . assign (
8
+ global ,
9
+ makeServiceWorkerEnv ( ) ,
10
+ makeFetchMock ( ) ,
11
+ // If you're using sinon ur similar you'd probably use below instead of makeFetchMock
12
+ // fetch: sinon.stub().returns(Promise.resolve())
13
+ )
14
+ jest . resetModules ( )
15
+
16
+ spy = jest . spyOn ( console , "log" )
17
+ } )
18
+
19
+ afterEach ( ( ) => {
20
+ jest . restoreAllMocks ( )
21
+ spy . mockRestore ( )
22
+ } )
23
+
24
+ it ( "should add listeners" , ( ) => {
25
+ require ( "../src/browser/serviceWorker.ts" )
26
+ const _self = ( self as unknown ) as WorkerGlobalScope
27
+ expect ( _self . listeners . get ( "install" ) ) . toBeDefined ( )
28
+ expect ( _self . listeners . get ( "activate" ) ) . toBeDefined ( )
29
+ expect ( _self . listeners . get ( "fetch" ) ) . toBeDefined ( )
30
+ } )
31
+
32
+ it ( "should call the proper callbacks for 'install'" , async ( ) => {
33
+ require ( "../src/browser/serviceWorker.ts" )
34
+ await self . trigger ( "install" )
35
+ expect ( spy ) . toHaveBeenCalledWith ( "[Service Worker] installed" )
4
36
} )
37
+ it ( "should call the proper callbacks for 'activate'" , async ( ) => {
38
+ require ( "../src/browser/serviceWorker.ts" )
39
+ await self . trigger ( "activate" )
5
40
6
- it ( "should call the proper callbacks" , ( ) => {
7
- // somehow test Line 8 with the events waitUntil..
41
+ // Activate serviceWorker
42
+ expect ( spy ) . toHaveBeenCalledWith ( "[Service Worker] activated" )
8
43
} )
9
- } )
44
+ } )
0 commit comments