@@ -5,20 +5,11 @@ jest.mock('unfetch', () => {
55
66import { segmentio , SegmentioSettings } from '..'
77import { Analytics } from '../../../core/analytics'
8- // @ts -ignore isOffline mocked dependency is accused as unused
9- import { isOffline } from '../../../core/connection'
108import { Plugin } from '../../../core/plugin'
119import { envEnrichment } from '../../env-enrichment'
12- import { scheduleFlush } from '../schedule-flush'
13- import * as PPQ from '../../../lib/priority-queue/persisted'
1410import * as PQ from '../../../lib/priority-queue'
15- import { Context } from '../../../core/context'
1611import { createError , createSuccess } from '../../../test-helpers/factories'
1712
18- //jest.mock('../schedule-flush')
19-
20- type QueueType = 'priority' | 'persisted'
21-
2213describe ( 'Segment.io retries 500s and 429' , ( ) => {
2314 let options : SegmentioSettings
2415 let analytics : Analytics
@@ -29,20 +20,14 @@ describe('Segment.io retries 500s and 429', () => {
2920 jest . restoreAllMocks ( )
3021
3122 options = { apiKey : 'foo' }
32- analytics = new Analytics (
33- { writeKey : options . apiKey } ,
34- {
35- retryQueue : true ,
36- }
37- )
23+ analytics = new Analytics ( { writeKey : options . apiKey } )
3824 segment = await segmentio ( analytics , options , { } )
3925 await analytics . register ( segment , envEnrichment )
4026 } )
4127
4228 test ( 'retries on 500' , async ( ) => {
4329 jest . useFakeTimers ( { advanceTimers : true } )
4430 fetch . mockReturnValue ( createError ( { status : 500 } ) )
45- // .mockReturnValue(createSuccess({}))
4631 const ctx = await analytics . track ( 'event' )
4732 jest . runAllTimers ( )
4833
@@ -88,12 +73,7 @@ describe('Batches retry 500s and 429', () => {
8873 config : { size : 3 , timeout : 1 , maxRetries : 2 } ,
8974 } ,
9075 }
91- analytics = new Analytics (
92- { writeKey : options . apiKey } ,
93- {
94- retryQueue : true ,
95- }
96- )
76+ analytics = new Analytics ( { writeKey : options . apiKey } )
9777 segment = await segmentio ( analytics , options , { } )
9878 await analytics . register ( segment , envEnrichment )
9979 } )
@@ -142,73 +122,3 @@ describe('Batches retry 500s and 429', () => {
142122 expect ( fetch . mock . lastCall [ 1 ] . body ) . toContain ( '"retryCount":2' )
143123 } )
144124} )
145-
146- describe ( 'Segment.io retries' , ( ) => {
147- let options : SegmentioSettings
148- let analytics : Analytics
149- let segment : Plugin
150- let queue : ( PPQ . PersistedPriorityQueue | PQ . PriorityQueue < Context > ) & {
151- __type ?: QueueType
152- }
153- ; [ false , true ] . forEach ( ( persistenceIsDisabled ) => {
154- describe ( `disableClientPersistence: ${ persistenceIsDisabled } ` , ( ) => {
155- beforeEach ( async ( ) => {
156- jest . useRealTimers ( )
157- jest . resetAllMocks ( )
158- jest . restoreAllMocks ( )
159-
160- // @ts -expect-error reassign import
161- isOffline = jest . fn ( ) . mockImplementation ( ( ) => true )
162- // @ts -expect-error reassign import
163- scheduleFlush = jest . fn ( ) . mockImplementation ( ( ) => { } )
164-
165- options = { apiKey : 'foo' }
166- analytics = new Analytics (
167- { writeKey : options . apiKey } ,
168- {
169- retryQueue : true ,
170- disableClientPersistence : persistenceIsDisabled ,
171- }
172- )
173-
174- if ( persistenceIsDisabled ) {
175- queue = new PQ . PriorityQueue ( 3 , [ ] )
176- queue [ '__type' ] = 'priority'
177- Object . defineProperty ( PQ , 'PriorityQueue' , {
178- writable : true ,
179- value : jest . fn ( ) . mockImplementation ( ( ) => queue ) ,
180- } )
181- } else {
182- queue = new PPQ . PersistedPriorityQueue (
183- 3 ,
184- `${ options . apiKey } :test-Segment.io`
185- )
186- queue [ '__type' ] = 'persisted'
187- Object . defineProperty ( PPQ , 'PersistedPriorityQueue' , {
188- writable : true ,
189- value : jest . fn ( ) . mockImplementation ( ( ) => queue ) ,
190- } )
191- }
192-
193- segment = await segmentio ( analytics , options , { } )
194- await analytics . register ( segment , envEnrichment )
195- } )
196-
197- test ( `add events to the queue` , async ( ) => {
198- jest . spyOn ( queue , 'push' )
199-
200- const ctx = await analytics . track ( 'event' )
201-
202- expect ( scheduleFlush ) . toHaveBeenCalled ( )
203- /* eslint-disable @typescript-eslint/unbound-method */
204- expect ( queue . push ) . toHaveBeenCalled ( )
205- expect ( queue . length ) . toBe ( 1 )
206- expect ( ctx . attempts ) . toBe ( 1 )
207- expect ( isOffline ) . toHaveBeenCalledTimes ( 2 )
208- expect ( queue . __type ) . toBe < QueueType > (
209- persistenceIsDisabled ? 'priority' : 'persisted'
210- )
211- } )
212- } )
213- } )
214- } )
0 commit comments