1
- import { Event , EventProcessor , Hub , Integration } from '@sentry/types' ;
1
+ import { Event , EventProcessor , Hub , Integration , IntegrationClass } from '@sentry/types' ;
2
2
import * as utils from '@sentry/utils' ;
3
3
4
- import { Offline } from '../src/offline' ;
4
+ import { Item , Offline } from '../src/offline' ;
5
5
6
6
// mock localforage methods
7
7
jest . mock ( 'localforage' , ( ) => ( {
8
8
createInstance ( _options : { name : string } ) : any {
9
- let items : { key : string ; value : Event } [ ] = [ ] ;
9
+ let items : Item [ ] = [ ] ;
10
10
11
11
return {
12
- async getItem ( key : string ) : Event {
12
+ async getItem ( key : string ) : Promise < Item | void > {
13
13
return items . find ( item => item . key === key ) ;
14
14
} ,
15
- async iterate ( callback : ( ) => void ) : void {
15
+ async iterate ( callback : ( event : Event , key : string , index : number ) => void ) : Promise < void > {
16
16
items . forEach ( ( item , index ) => {
17
17
callback ( item . value , item . key , index ) ;
18
18
} ) ;
19
19
} ,
20
- async length ( ) : number {
20
+ async length ( ) : Promise < number > {
21
21
return items . length ;
22
22
} ,
23
- async removeItem ( key : string ) : void {
23
+ async removeItem ( key : string ) : Promise < void > {
24
24
items = items . filter ( item => item . key !== key ) ;
25
25
} ,
26
- async setItem ( key : string , value : Event ) : void {
26
+ async setItem ( key : string , value : Event ) : Promise < void > {
27
27
items . push ( {
28
28
key,
29
29
value,
@@ -33,7 +33,7 @@ jest.mock('localforage', () => ({
33
33
} ,
34
34
} ) ) ;
35
35
36
- let integration : Integration ;
36
+ let integration : Offline ;
37
37
let online : boolean ;
38
38
39
39
describe ( 'Offline' , ( ) => {
@@ -52,15 +52,10 @@ describe('Offline', () => {
52
52
} ) ;
53
53
54
54
describe ( 'when there are already events in the cache from a previous offline session' , ( ) => {
55
- beforeEach ( done => {
55
+ beforeEach ( async ( ) => {
56
56
const event = { message : 'previous event' } ;
57
57
58
- integration . offlineEventStore
59
- . setItem ( 'previous' , event )
60
- . then ( ( ) => {
61
- done ( ) ;
62
- } )
63
- . catch ( error => error ) ;
58
+ await integration . offlineEventStore . setItem ( 'previous' , event ) ;
64
59
} ) ;
65
60
66
61
it ( 'sends stored events' , async ( ) => {
@@ -130,16 +125,14 @@ describe('Offline', () => {
130
125
} ) ;
131
126
132
127
describe ( 'when connectivity is restored' , ( ) => {
133
- it ( 'sends stored events' , async done => {
128
+ it ( 'sends stored events' , async ( ) => {
134
129
initIntegration ( ) ;
135
130
setupOnce ( ) ;
136
131
prepopulateEvents ( 1 ) ;
137
132
processEvents ( ) ;
138
133
processEventListeners ( ) ;
139
134
140
135
expect ( await integration . offlineEventStore . length ( ) ) . toEqual ( 0 ) ;
141
-
142
- setImmediate ( done ) ;
143
136
} ) ;
144
137
} ) ;
145
138
} ) ;
@@ -150,7 +143,7 @@ let eventProcessors: EventProcessor[];
150
143
let events : Event [ ] ;
151
144
152
145
/** JSDoc */
153
- function addGlobalEventProcessor ( callback : ( ) => void ) : void {
146
+ function addGlobalEventProcessor ( callback : EventProcessor ) : void {
154
147
eventProcessors . push ( callback ) ;
155
148
}
156
149
@@ -160,11 +153,11 @@ function getCurrentHub(): Hub {
160
153
captureEvent ( _event : Event ) : string {
161
154
return 'an-event-id' ;
162
155
} ,
163
- getIntegration ( _integration : Integration ) : any {
156
+ getIntegration < T extends Integration > ( _integration : IntegrationClass < T > ) : T | null {
164
157
// pretend integration is enabled
165
- return true ;
158
+ return { } as T ;
166
159
} ,
167
- } ;
160
+ } as Hub ;
168
161
}
169
162
170
163
/** JSDoc */
@@ -173,14 +166,17 @@ function initIntegration(options: { maxStoredEvents?: number } = {}): void {
173
166
eventProcessors = [ ] ;
174
167
events = [ ] ;
175
168
176
- utils . getGlobalObject = jest . fn ( ( ) => ( {
177
- addEventListener : ( _windowEvent , callback ) => {
178
- eventListeners . push ( callback ) ;
179
- } ,
180
- navigator : {
181
- onLine : online ,
182
- } ,
183
- } ) ) ;
169
+ jest . spyOn ( utils , 'getGlobalObject' ) . mockImplementation (
170
+ ( ) =>
171
+ ( {
172
+ addEventListener : ( _windowEvent , callback ) => {
173
+ eventListeners . push ( callback ) ;
174
+ } ,
175
+ navigator : {
176
+ onLine : online ,
177
+ } ,
178
+ } as any ) ,
179
+ ) ;
184
180
185
181
integration = new Offline ( options ) ;
186
182
}
0 commit comments