|
1 | | -import { AppState, AppStateStatus } from 'react-native'; |
2 | 1 | import { BackgroundFlushPolicy } from '../background-flush-policy'; |
| 2 | +import { FlushPolicyBase } from '../types'; |
| 3 | +import { createTrackEvent } from '../../events'; |
3 | 4 |
|
4 | 5 | describe('BackgroundFlushPolicy', () => { |
5 | | - it('triggers a flush when reaching limit', () => { |
6 | | - let updateCallback = (_val: AppStateStatus) => { |
7 | | - return; |
8 | | - }; |
| 6 | + let policy: BackgroundFlushPolicy; |
9 | 7 |
|
10 | | - const addSpy = jest |
11 | | - .spyOn(AppState, 'addEventListener') |
12 | | - .mockImplementation((_action, callback) => { |
13 | | - updateCallback = callback; |
14 | | - return { remove: jest.fn() }; |
15 | | - }); |
16 | | - |
17 | | - const policy = new BackgroundFlushPolicy(); |
18 | | - policy.start(); |
19 | | - const observer = jest.fn(); |
| 8 | + beforeEach(() => { |
| 9 | + policy = new BackgroundFlushPolicy(); |
| 10 | + }); |
20 | 11 |
|
21 | | - policy.shouldFlush.onChange(observer); |
| 12 | + test('should be an instance of FlushPolicyBase', () => { |
| 13 | + expect(policy).toBeInstanceOf(FlushPolicyBase); |
| 14 | + }); |
22 | 15 |
|
23 | | - expect(addSpy).toHaveBeenCalledTimes(1); |
| 16 | + test('start() should do nothing', () => { |
| 17 | + expect(policy.start()).toBeUndefined(); |
| 18 | + }); |
24 | 19 |
|
25 | | - updateCallback('background'); |
26 | | - expect(observer).toHaveBeenCalledWith(true); |
27 | | - observer.mockClear(); |
| 20 | + test('end() should do nothing', () => { |
| 21 | + expect(policy.end()).toBeUndefined(); |
| 22 | + }); |
28 | 23 |
|
29 | | - updateCallback('active'); |
30 | | - expect(observer).not.toHaveBeenCalled(); |
| 24 | + test('onEvent() should set shouldFlush to true for "Application Backgrounded" event', () => { |
| 25 | + const event = createTrackEvent({ |
| 26 | + event: 'Application Backgrounded', |
| 27 | + }); |
| 28 | + policy.onEvent(event); |
| 29 | + expect(policy.shouldFlush.value).toBe(true); |
| 30 | + }); |
31 | 31 |
|
32 | | - updateCallback('inactive'); |
33 | | - expect(observer).toHaveBeenCalledWith(true); |
| 32 | + test('onEvent() should not set shouldFlush for other events', () => { |
| 33 | + const event = createTrackEvent({ |
| 34 | + event: 'Application Opened', |
| 35 | + }); |
| 36 | + policy.onEvent(event); |
| 37 | + expect(policy.shouldFlush.value).toBeFalsy(); |
34 | 38 | }); |
35 | 39 | }); |
0 commit comments