@@ -57,6 +57,38 @@ describe('LDClient', () => {
57
57
} ) ;
58
58
} ) ;
59
59
60
+ describe ( 'identify' , ( ) => {
61
+ describe ( 'Disables synchronous XHR if page did not unload' , ( ) => {
62
+ async function setupClient ( ) {
63
+ const config = { bootstrap : { } , flushInterval : 100000 , fetchGoals : false , sendEvents : false } ;
64
+ const client = LDClient . initialize ( envName , user , config ) ;
65
+ await client . waitForInitialization ( ) ;
66
+ return client ;
67
+ }
68
+ function testWithUserAgent ( desc , ua ) {
69
+ it ( 'in ' + desc , async ( ) => {
70
+ window . navigator . __defineGetter__ ( 'userAgent' , ( ) => ua ) ;
71
+
72
+ const client = await setupClient ( ) ;
73
+ expect ( server . requests . length ) . toEqual ( 0 ) ;
74
+ window . dispatchEvent ( new window . Event ( 'beforeunload' ) ) ;
75
+ await client . identify ( { key : 'new-user' } ) ;
76
+ expect ( server . requests . length ) . toEqual ( 1 ) ;
77
+ expect ( server . requests [ 0 ] . async ) . toBe ( true ) ;
78
+ } ) ;
79
+ }
80
+
81
+ testWithUserAgent (
82
+ 'Chrome 72' ,
83
+ 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36'
84
+ ) ;
85
+
86
+ testWithUserAgent ( 'unknown browser' , 'Special Kitty Cat Browser' ) ;
87
+
88
+ testWithUserAgent ( 'empty user-agent' , null ) ;
89
+ } ) ;
90
+ } ) ;
91
+
60
92
describe ( 'goals' , ( ) => {
61
93
it ( 'fetches goals if fetchGoals is unspecified' , async ( ) => {
62
94
const client = LDClient . initialize ( envName , user , { sendEvents : false } ) ;
@@ -174,6 +206,32 @@ describe('LDClient', () => {
174
206
testWithUserAgent ( 'empty user-agent' , null ) ;
175
207
} ) ;
176
208
209
+ describe ( 'Disables synchronous XHR if page did not unload' , ( ) => {
210
+ function testWithUserAgent ( desc , ua ) {
211
+ it ( 'in ' + desc , async ( ) => {
212
+ window . navigator . __defineGetter__ ( 'userAgent' , ( ) => ua ) ;
213
+
214
+ const client = await setupClientAndTriggerUnload ( ) ;
215
+ expect ( server . requests . length ) . toEqual ( 2 ) ;
216
+ // ignore first request because it's just a side effect of calling browserPlatform.httpAllowsPost()
217
+ expect ( server . requests [ 1 ] . async ) . toBe ( false ) ; // events
218
+ client . track ( 'Test' ) ; // lets track a event that happen after a beforeunload event.
219
+ client . flush ( ) . catch ( ( ) => { } ) ; // flush that event
220
+ expect ( server . requests . length ) . toEqual ( 3 ) ; // assert the server got the request.
221
+ expect ( server . requests [ 2 ] . async ) . toBe ( true ) ;
222
+ } ) ;
223
+ }
224
+
225
+ testWithUserAgent (
226
+ 'Chrome 72' ,
227
+ 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36'
228
+ ) ;
229
+
230
+ testWithUserAgent ( 'unknown browser' , 'Special Kitty Cat Browser' ) ;
231
+
232
+ testWithUserAgent ( 'empty user-agent' , null ) ;
233
+ } ) ;
234
+
177
235
describe ( 'discards events during page unload' , ( ) => {
178
236
function testWithUserAgent ( desc , ua ) {
179
237
it ( 'in ' + desc , async ( ) => {
0 commit comments