Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@

expect(appStateChangeListener).toBeDefined();

appStateChangeListener!(to);

Check warning on line 64 in packages/core/src/__tests__/internal/handleAppStateChange.test.ts

View workflow job for this annotation

GitHub Actions / build-and-test

Forbidden non-null assertion
// Since the calls to process lifecycle events are not awaitable we have to await for ticks here
await new Promise(process.nextTick);
await new Promise(process.nextTick);
Expand Down Expand Up @@ -163,7 +163,7 @@
// @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 () => {
Expand All @@ -173,6 +173,6 @@
// @ts-ignore
expect(client.appState).toBe('inactive');

expect(mockPlugin.execute).not.toHaveBeenCalled();
expect(mockPlugin.execute).toHaveBeenCalledTimes(1);
});
});
4 changes: 2 additions & 2 deletions packages/core/src/analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -14,22 +22,29 @@ describe('BackgroundFlushPolicy', () => {
return { remove: jest.fn() };
});

AppState.currentState = 'active';
const policy = new BackgroundFlushPolicy();
policy.start();
const observer = jest.fn();

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();
});
});
14 changes: 10 additions & 4 deletions packages/core/src/flushPolicies/background-flush-policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading