diff --git a/packages/core/src/__tests__/internal/handleAppStateChange.test.ts b/packages/core/src/__tests__/internal/handleAppStateChange.test.ts index d1ec68b44..65a3ae110 100644 --- a/packages/core/src/__tests__/internal/handleAppStateChange.test.ts +++ b/packages/core/src/__tests__/internal/handleAppStateChange.test.ts @@ -163,7 +163,7 @@ describe('SegmentClient #handleAppStateChange', () => { // @ts-ignore expect(client.appState).toBe('background'); - expect(mockPlugin.execute).not.toHaveBeenCalled(); + expect(mockPlugin.execute).toHaveBeenCalledTimes(1); }); it('sends an event when unknown => inactive', async () => { @@ -173,6 +173,6 @@ describe('SegmentClient #handleAppStateChange', () => { // @ts-ignore expect(client.appState).toBe('inactive'); - expect(mockPlugin.execute).not.toHaveBeenCalled(); + expect(mockPlugin.execute).toHaveBeenCalledTimes(1); }); }); diff --git a/packages/core/src/analytics.ts b/packages/core/src/analytics.ts index 374b4b9e6..1eca79be2 100644 --- a/packages/core/src/analytics.ts +++ b/packages/core/src/analytics.ts @@ -718,8 +718,8 @@ export class SegmentClient { void this.process(event); this.logger.info('TRACK (Application Opened) event saved', event); } else if ( - this.appState === 'active' && - ['inactive', 'background'].includes(nextAppState) + (this.appState === 'active' || this.appState === 'unknown') && // Check if appState is 'active' or 'unknown' //redundant condition need to check without this + ['inactive', 'background'].includes(nextAppState) // Check if next app state is 'inactive' or 'background' ) { const event = createTrackEvent({ event: 'Application Backgrounded', diff --git a/packages/core/src/flushPolicies/__tests__/background-flush-policy.test.ts b/packages/core/src/flushPolicies/__tests__/background-flush-policy.test.ts index 45ab22c7c..0e822088b 100644 --- a/packages/core/src/flushPolicies/__tests__/background-flush-policy.test.ts +++ b/packages/core/src/flushPolicies/__tests__/background-flush-policy.test.ts @@ -2,6 +2,14 @@ import { AppState, AppStateStatus } from 'react-native'; import { BackgroundFlushPolicy } from '../background-flush-policy'; describe('BackgroundFlushPolicy', () => { + beforeEach(() => { + jest.useFakeTimers(); // Mock timers + }); + + afterEach(() => { + jest.useRealTimers(); // Restore real timers + }); + it('triggers a flush when reaching limit', () => { let updateCallback = (_val: AppStateStatus) => { return; @@ -14,6 +22,7 @@ describe('BackgroundFlushPolicy', () => { return { remove: jest.fn() }; }); + AppState.currentState = 'active'; const policy = new BackgroundFlushPolicy(); policy.start(); const observer = jest.fn(); @@ -21,15 +30,21 @@ describe('BackgroundFlushPolicy', () => { policy.shouldFlush.onChange(observer); expect(addSpy).toHaveBeenCalledTimes(1); + updateCallback('inactive'); + jest.advanceTimersByTime(2000); + + console.log('Observer calls:', observer.mock.calls); + expect(observer).toHaveBeenCalledWith(true); + observer.mockClear(); updateCallback('background'); + jest.advanceTimersByTime(2000); // Simulate timer triggering expect(observer).toHaveBeenCalledWith(true); observer.mockClear(); updateCallback('active'); + jest.advanceTimersByTime(2000); expect(observer).not.toHaveBeenCalled(); - - updateCallback('inactive'); - expect(observer).toHaveBeenCalledWith(true); + observer.mockClear(); }); }); diff --git a/packages/core/src/flushPolicies/background-flush-policy.ts b/packages/core/src/flushPolicies/background-flush-policy.ts index 397a5142d..0556c69d7 100644 --- a/packages/core/src/flushPolicies/background-flush-policy.ts +++ b/packages/core/src/flushPolicies/background-flush-policy.ts @@ -18,18 +18,24 @@ export class BackgroundFlushPolicy extends FlushPolicyBase { 'change', (nextAppState) => { if ( - this.appState === 'active' && + ['active', 'inactive'].includes(this.appState) && ['inactive', 'background'].includes(nextAppState) ) { - // When the app goes into the background we will trigger a flush - this.shouldFlush.value = true; + setTimeout(() => { + this.shouldFlush.value = true; + }, 2000); } + this.appState = nextAppState; } ); } onEvent(_event: SegmentEvent): void { - // Nothing to do + //if ('event' in _event && _event.event === 'Application Backgrounded') { + // setTimeout(() => { + // this.shouldFlush.value = true; + // }, 2000); + // } } end(): void {