@@ -23,6 +23,7 @@ import {
2323 SDK_VERSION_HEADER_NAME ,
2424} from "../src/config" ;
2525import fetchClient from "../src/fetch-http-client" ;
26+ import { subscribe as triggerOnExit } from "../src/flusher" ;
2627import { newRateLimiter } from "../src/rate-limiter" ;
2728import { ClientOptions , Context , FeaturesAPIResponse } from "../src/types" ;
2829
@@ -45,6 +46,10 @@ vi.mock("../src/rate-limiter", async (importOriginal) => {
4546 } ;
4647} ) ;
4748
49+ vi . mock ( "../src/flusher" , ( ) => ( {
50+ subscribe : vi . fn ( ) ,
51+ } ) ) ;
52+
4853const user = {
4954 id : "user123" ,
5055 age : 1 ,
@@ -82,6 +87,7 @@ const validOptions: ClientOptions = {
8287 batchOptions : {
8388 maxSize : 99 ,
8489 intervalMs : 100 ,
90+ flushOnExit : false ,
8591 } ,
8692 offline : false ,
8793} ;
@@ -372,6 +378,36 @@ describe("BucketClient", () => {
372378 ) ;
373379 } ) ;
374380
381+ it ( "should not register an exit flush handler if `batchOptions.flushOnExit` is false" , ( ) => {
382+ new BucketClient ( {
383+ ...validOptions ,
384+ batchOptions : { ...validOptions . batchOptions , flushOnExit : false } ,
385+ } ) ;
386+
387+ expect ( triggerOnExit ) . not . toHaveBeenCalled ( ) ;
388+ } ) ;
389+
390+ it ( "should not register an exit flush handler if `offline` is true" , ( ) => {
391+ new BucketClient ( {
392+ ...validOptions ,
393+ offline : true ,
394+ } ) ;
395+
396+ expect ( triggerOnExit ) . not . toHaveBeenCalled ( ) ;
397+ } ) ;
398+
399+ it . each ( [ undefined , true ] ) (
400+ "should register an exit flush handler if `batchOptions.flushOnExit` is `%s`" ,
401+ ( flushOnExit ) => {
402+ new BucketClient ( {
403+ ...validOptions ,
404+ batchOptions : { ...validOptions . batchOptions , flushOnExit } ,
405+ } ) ;
406+
407+ expect ( triggerOnExit ) . toHaveBeenCalledWith ( expect . any ( Function ) ) ;
408+ } ,
409+ ) ;
410+
375411 it . each ( [
376412 [ "https://api.example.com" , "https://api.example.com/bulk" ] ,
377413 [ "https://api.example.com/" , "https://api.example.com/bulk" ] ,
@@ -971,6 +1007,18 @@ describe("BucketClient", () => {
9711007 ] ,
9721008 ) ;
9731009 } ) ;
1010+
1011+ it ( "should not flush all bulk data if `offline` is true" , async ( ) => {
1012+ const client = new BucketClient ( {
1013+ ...validOptions ,
1014+ offline : true ,
1015+ } ) ;
1016+
1017+ await client . updateUser ( user . id , { attributes : { age : 2 } } ) ;
1018+ await client . flush ( ) ;
1019+
1020+ expect ( httpClient . post ) . not . toHaveBeenCalled ( ) ;
1021+ } ) ;
9741022 } ) ;
9751023
9761024 describe ( "getFeature" , ( ) => {
0 commit comments