19
19
import * as React from 'react' ;
20
20
import { act } from 'react-dom/test-utils' ;
21
21
import { render , renderHook , screen , waitFor } from '@testing-library/react' ;
22
- import '@testing-library/jest-dom/extend-expect ' ;
22
+ import '@testing-library/jest-dom' ;
23
23
24
24
import { OptimizelyProvider } from './Provider' ;
25
25
import { OnReadyResult , ReactSDKClient , VariableValuesObject } from './client' ;
@@ -71,6 +71,7 @@ describe('hooks', () => {
71
71
let notificationListenerCallbacks : Array < ( ) => void > ;
72
72
let optimizelyMock : ReactSDKClient ;
73
73
let readySuccess : boolean ;
74
+ // let reason: NotReadyReason;
74
75
let userUpdateCallbacks : Array < ( ) => void > ;
75
76
let UseExperimentLoggingComponent : React . FunctionComponent < any > ;
76
77
let UseFeatureLoggingComponent : React . FunctionComponent < any > ;
@@ -84,15 +85,15 @@ describe('hooks', () => {
84
85
85
86
beforeEach ( ( ) => {
86
87
getOnReadyPromise = ( { timeout = 0 } : any ) : Promise < OnReadyResult > =>
87
- new Promise ( resolve => {
88
- setTimeout ( function ( ) {
88
+ new Promise ( ( resolve ) => {
89
+ setTimeout ( function ( ) {
89
90
resolve (
90
91
Object . assign (
91
92
{
92
93
success : readySuccess ,
93
94
} ,
94
95
! readySuccess && {
95
- dataReadyPromise : new Promise ( r => setTimeout ( r , mockDelay ) ) ,
96
+ dataReadyPromise : new Promise ( ( r ) => setTimeout ( r , mockDelay ) ) ,
96
97
}
97
98
)
98
99
) ;
@@ -109,21 +110,21 @@ describe('hooks', () => {
109
110
decideMock = jest . fn ( ) ;
110
111
setForcedDecisionMock = jest . fn ( ) ;
111
112
hooksLoggerErrorSpy = jest . spyOn ( hooksLogger , 'error' ) ;
112
- optimizelyMock = ( {
113
+ optimizelyMock = {
113
114
activate : activateMock ,
114
- onReady : jest . fn ( ) . mockImplementation ( config => getOnReadyPromise ( config || { } ) ) ,
115
+ onReady : jest . fn ( ) . mockImplementation ( ( config ) => getOnReadyPromise ( config || { } ) ) ,
115
116
getFeatureVariables : jest . fn ( ) . mockImplementation ( ( ) => featureVariables ) ,
116
117
isFeatureEnabled : isFeatureEnabledMock ,
117
118
getVuid : jest . fn ( ) . mockReturnValue ( 'vuid_95bf72cebc774dfd8e8e580a5a1' ) ,
118
- onUserUpdate : jest . fn ( ) . mockImplementation ( handler => {
119
+ onUserUpdate : jest . fn ( ) . mockImplementation ( ( handler ) => {
119
120
userUpdateCallbacks . push ( handler ) ;
120
121
return ( ) => { } ;
121
122
} ) ,
122
123
notificationCenter : {
123
124
addNotificationListener : jest . fn ( ) . mockImplementation ( ( type , handler ) => {
124
125
notificationListenerCallbacks . push ( handler ) ;
125
126
} ) ,
126
- removeNotificationListener : jest . fn ( ) . mockImplementation ( id => { } ) ,
127
+ removeNotificationListener : jest . fn ( ) . mockImplementation ( ( id ) => { } ) ,
127
128
} ,
128
129
user : {
129
130
id : 'testuser' ,
@@ -132,15 +133,16 @@ describe('hooks', () => {
132
133
isReady : ( ) => readySuccess ,
133
134
getIsReadyPromiseFulfilled : ( ) => true ,
134
135
getIsUsingSdkKey : ( ) => true ,
135
- onForcedVariationsUpdate : jest . fn ( ) . mockImplementation ( handler => {
136
+ onForcedVariationsUpdate : jest . fn ( ) . mockImplementation ( ( handler ) => {
136
137
forcedVariationUpdateCallbacks . push ( handler ) ;
137
138
return ( ) => { } ;
138
139
} ) ,
139
140
getForcedVariations : jest . fn ( ) . mockReturnValue ( { } ) ,
140
141
decide : decideMock ,
141
142
setForcedDecision : setForcedDecisionMock ,
142
143
track : jest . fn ( ) ,
143
- } as unknown ) as ReactSDKClient ;
144
+ setUser : jest . fn ( ) ,
145
+ } as unknown as ReactSDKClient ;
144
146
145
147
mockLog = jest . fn ( ) ;
146
148
UseExperimentLoggingComponent = ( { options = { } , overrides = { } } : any ) => {
@@ -164,8 +166,8 @@ describe('hooks', () => {
164
166
165
167
afterEach ( async ( ) => {
166
168
await optimizelyMock . onReady ( ) . then (
167
- res => res . dataReadyPromise ,
168
- err => null
169
+ ( res ) => res . dataReadyPromise ,
170
+ ( err ) => null
169
171
) ;
170
172
hooksLoggerErrorSpy . mockReset ( ) ;
171
173
} ) ;
@@ -204,16 +206,14 @@ describe('hooks', () => {
204
206
< MyExperimentComponent options = { { timeout : mockDelay } } />
205
207
</ OptimizelyProvider >
206
208
) ;
209
+
207
210
await waitFor ( ( ) => expect ( screen . getByTestId ( 'result' ) ) . toHaveTextContent ( 'null|false|false' ) ) ; // initial render
208
211
209
- await optimizelyMock . onReady ( ) ;
210
212
await waitFor ( ( ) => expect ( screen . getByTestId ( 'result' ) ) . toHaveTextContent ( 'null|false|true' ) ) ; // when didTimeout
211
213
212
214
// Simulate datafile fetch completing after timeout has already passed
213
215
// Activate now returns a variation
214
216
activateMock . mockReturnValue ( '12345' ) ;
215
- // Wait for completion of dataReadyPromise
216
- await optimizelyMock . onReady ( ) . then ( res => res . dataReadyPromise ) ;
217
217
await waitFor ( ( ) => expect ( screen . getByTestId ( 'result' ) ) . toHaveTextContent ( '12345|true|true' ) ) ; // when clientReady
218
218
} ) ;
219
219
@@ -255,7 +255,7 @@ describe('hooks', () => {
255
255
activateMock . mockReturnValue ( '12345' ) ;
256
256
// Simulate the user object changing
257
257
await act ( async ( ) => {
258
- userUpdateCallbacks . forEach ( fn => fn ( ) ) ;
258
+ userUpdateCallbacks . forEach ( ( fn ) => fn ( ) ) ;
259
259
} ) ;
260
260
// component.update();
261
261
// await waitFor(() => expect(screen.getByTestId('result')).toHaveTextContent('12345|true|false');
@@ -278,7 +278,7 @@ describe('hooks', () => {
278
278
activateMock . mockReturnValue ( '12345' ) ;
279
279
// Simulate the user object changing
280
280
await act ( async ( ) => {
281
- userUpdateCallbacks . forEach ( fn => fn ( ) ) ;
281
+ userUpdateCallbacks . forEach ( ( fn ) => fn ( ) ) ;
282
282
} ) ;
283
283
await waitFor ( ( ) => expect ( screen . getByTestId ( 'result' ) ) . toHaveTextContent ( 'null|true|false' ) ) ;
284
284
} ) ;
@@ -299,7 +299,7 @@ describe('hooks', () => {
299
299
it ( 'should re-render after the client becomes ready' , async ( ) => {
300
300
readySuccess = false ;
301
301
let resolveReadyPromise : ( result : { success : boolean ; dataReadyPromise : Promise < any > } ) => void ;
302
- const readyPromise : Promise < any > = new Promise ( res => {
302
+ const readyPromise : Promise < any > = new Promise ( ( res ) => {
303
303
resolveReadyPromise = ( result ) : void => {
304
304
readySuccess = true ;
305
305
res ( result ) ;
@@ -472,13 +472,13 @@ describe('hooks', () => {
472
472
isFeatureEnabledMock . mockReturnValue ( true ) ;
473
473
featureVariables = mockFeatureVariables ;
474
474
// Wait for completion of dataReadyPromise
475
- await optimizelyMock . onReady ( ) . then ( res => res . dataReadyPromise ) ;
475
+ await optimizelyMock . onReady ( ) . then ( ( res ) => res . dataReadyPromise ) ;
476
476
477
477
// Simulate datafile fetch completing after timeout has already passed
478
478
// Activate now returns a variation
479
479
activateMock . mockReturnValue ( '12345' ) ;
480
480
// Wait for completion of dataReadyPromise
481
- await optimizelyMock . onReady ( ) . then ( res => res . dataReadyPromise ) ;
481
+ await optimizelyMock . onReady ( ) . then ( ( res ) => res . dataReadyPromise ) ;
482
482
await waitFor ( ( ) => expect ( screen . getByTestId ( 'result' ) ) . toHaveTextContent ( 'true|{"foo":"bar"}|true|true' ) ) ; // when clientReady
483
483
} ) ;
484
484
@@ -522,7 +522,7 @@ describe('hooks', () => {
522
522
featureVariables = mockFeatureVariables ;
523
523
// Simulate the user object changing
524
524
await act ( async ( ) => {
525
- userUpdateCallbacks . forEach ( fn => fn ( ) ) ;
525
+ userUpdateCallbacks . forEach ( ( fn ) => fn ( ) ) ;
526
526
} ) ;
527
527
await waitFor ( ( ) => expect ( screen . getByTestId ( 'result' ) ) . toHaveTextContent ( 'true|{"foo":"bar"}|true|false' ) ) ;
528
528
} ) ;
@@ -546,7 +546,7 @@ describe('hooks', () => {
546
546
featureVariables = mockFeatureVariables ;
547
547
// Simulate the user object changing
548
548
act ( ( ) => {
549
- userUpdateCallbacks . forEach ( fn => fn ( ) ) ;
549
+ userUpdateCallbacks . forEach ( ( fn ) => fn ( ) ) ;
550
550
} ) ;
551
551
// component.update();
552
552
await waitFor ( ( ) => expect ( screen . getByTestId ( 'result' ) ) . toHaveTextContent ( 'false|{}|true|false' ) ) ;
@@ -567,7 +567,7 @@ describe('hooks', () => {
567
567
it ( 'should re-render after the client becomes ready' , async ( ) => {
568
568
readySuccess = false ;
569
569
let resolveReadyPromise : ( result : { success : boolean ; dataReadyPromise : Promise < any > } ) => void ;
570
- const readyPromise : Promise < any > = new Promise ( res => {
570
+ const readyPromise : Promise < any > = new Promise ( ( res ) => {
571
571
resolveReadyPromise = ( result ) : void => {
572
572
readySuccess = true ;
573
573
res ( result ) ;
@@ -731,11 +731,11 @@ describe('hooks', () => {
731
731
variables : { foo : 'bar' } ,
732
732
} ) ;
733
733
734
- await optimizelyMock . onReady ( ) . then ( res => res . dataReadyPromise ) ;
734
+ await optimizelyMock . onReady ( ) . then ( ( res ) => res . dataReadyPromise ) ;
735
735
736
736
// Simulate datafile fetch completing after timeout has already passed
737
737
// Wait for completion of dataReadyPromise
738
- await optimizelyMock . onReady ( ) . then ( res => res . dataReadyPromise ) ;
738
+ await optimizelyMock . onReady ( ) . then ( ( res ) => res . dataReadyPromise ) ;
739
739
740
740
await waitFor ( ( ) => expect ( screen . getByTestId ( 'result' ) ) . toHaveTextContent ( 'true|{"foo":"bar"}|true|true' ) ) ; // when clientReady
741
741
} ) ;
@@ -781,7 +781,7 @@ describe('hooks', () => {
781
781
} ) ;
782
782
// Simulate the user object changing
783
783
await act ( async ( ) => {
784
- userUpdateCallbacks . forEach ( fn => fn ( ) ) ;
784
+ userUpdateCallbacks . forEach ( ( fn ) => fn ( ) ) ;
785
785
} ) ;
786
786
await waitFor ( ( ) => expect ( screen . getByTestId ( 'result' ) ) . toHaveTextContent ( 'true|{"foo":"bar"}|true|false' ) ) ;
787
787
} ) ;
@@ -806,7 +806,7 @@ describe('hooks', () => {
806
806
} ) ;
807
807
// Simulate the user object changing
808
808
await act ( async ( ) => {
809
- userUpdateCallbacks . forEach ( fn => fn ( ) ) ;
809
+ userUpdateCallbacks . forEach ( ( fn ) => fn ( ) ) ;
810
810
} ) ;
811
811
await waitFor ( ( ) => expect ( screen . getByTestId ( 'result' ) ) . toHaveTextContent ( 'false|{}|true|false' ) ) ;
812
812
} ) ;
@@ -827,7 +827,7 @@ describe('hooks', () => {
827
827
it ( 'should re-render after the client becomes ready' , async ( ) => {
828
828
readySuccess = false ;
829
829
let resolveReadyPromise : ( result : { success : boolean ; dataReadyPromise : Promise < any > } ) => void ;
830
- const readyPromise : Promise < any > = new Promise ( res => {
830
+ const readyPromise : Promise < any > = new Promise ( ( res ) => {
831
831
resolveReadyPromise = ( result ) : void => {
832
832
readySuccess = true ;
833
833
res ( result ) ;
0 commit comments