1
1
import { AppState , AppStateStatus } from 'react-native' ;
2
- import { SegmentClient } from '../../analytics' ;
3
- import { EventType } from '../../types' ;
4
- import { getMockLogger } from '../__helpers__/mockLogger' ;
5
- import { MockSegmentStore } from '../__helpers__/mockSegmentStore' ;
2
+ import type { SegmentClient } from '../../analytics' ;
3
+ import type { UtilityPlugin } from '../../plugin' ;
4
+ import { EventType , SegmentEvent } from '../../types' ;
5
+ import type { MockSegmentStore } from '../__helpers__/mockSegmentStore' ;
6
+ import { createTestClient } from '../__helpers__/setupSegmentClient' ;
6
7
7
8
jest . mock ( '../../uuid' ) ;
8
9
jest . mock ( '../../context' ) ;
13
14
. mockReturnValue ( '2010-01-01T00:00:00.000Z' ) ;
14
15
15
16
describe ( 'SegmentClient #handleAppStateChange' , ( ) => {
16
- const store = new MockSegmentStore ( ) ;
17
-
18
- const clientArgs = {
19
- config : {
20
- writeKey : 'mock-write-key' ,
21
- trackAppLifecycleEvents : true ,
22
- } ,
23
- logger : getMockLogger ( ) ,
24
- store : store ,
25
- } ;
26
-
17
+ let store : MockSegmentStore ;
27
18
let client : SegmentClient ;
19
+ let appStateChangeListener : ( ( state : AppStateStatus ) => void ) | undefined ;
20
+ let expectEvent : ( event : Partial < SegmentEvent > ) => void ;
21
+ let mockPlugin : UtilityPlugin ;
28
22
29
23
afterEach ( ( ) => {
30
24
jest . clearAllMocks ( ) ;
31
- client . cleanup ( ) ;
32
- } ) ;
33
-
34
- beforeEach ( ( ) => {
35
25
store . reset ( ) ;
26
+ client . cleanup ( ) ;
36
27
} ) ;
37
28
38
29
const setupTest = async (
39
- segmentClient : SegmentClient ,
40
30
from : AppStateStatus ,
41
- to : AppStateStatus
31
+ to : AppStateStatus ,
32
+ initialTrackAppLifecycleEvents : boolean = false ,
33
+ trackAppLifecycleEvents : boolean = true
42
34
) => {
43
- // @ts -ignore
44
- segmentClient . appState = from ;
45
-
46
- let appStateChangeListener : ( ( state : AppStateStatus ) => void ) | undefined ;
47
35
AppState . addEventListener = jest
48
36
. fn ( )
49
37
. mockImplementation (
@@ -52,37 +40,48 @@ describe('SegmentClient #handleAppStateChange', () => {
52
40
}
53
41
) ;
54
42
55
- await segmentClient . init ( ) ;
56
- const clientProcess = jest . spyOn ( segmentClient , 'process' ) ;
43
+ const stuff = createTestClient ( undefined , {
44
+ trackAppLifecycleEvents : initialTrackAppLifecycleEvents ,
45
+ } ) ;
46
+ store = stuff . store ;
47
+ client = stuff . client ;
48
+ expectEvent = stuff . expectEvent ;
49
+ mockPlugin = stuff . plugin ;
50
+
51
+ // @ts -ignore
52
+ client . appState = from ;
53
+
54
+ await client . init ( ) ;
55
+
56
+ // @ts -ignore settings the track here to filter out initial events
57
+ client . config . trackAppLifecycleEvents = trackAppLifecycleEvents ;
57
58
58
59
expect ( appStateChangeListener ) . toBeDefined ( ) ;
59
60
60
61
appStateChangeListener ! ( to ) ;
61
- return clientProcess ;
62
+ // Since the calls to process lifecycle events are not awaitable we have to await for ticks here
63
+ await new Promise ( process . nextTick ) ;
64
+ await new Promise ( process . nextTick ) ;
65
+ await new Promise ( process . nextTick ) ;
62
66
} ;
63
67
64
68
it ( 'does not send events when trackAppLifecycleEvents is not enabled' , async ( ) => {
65
- client = new SegmentClient ( {
66
- ...clientArgs ,
67
- config : {
68
- writeKey : 'mock-write-key' ,
69
- trackAppLifecycleEvents : false ,
70
- } ,
71
- } ) ;
72
- const processSpy = await setupTest ( client , 'active' , 'background' ) ;
69
+ await setupTest ( 'active' , 'background' , false , false ) ;
73
70
74
- expect ( processSpy ) . not . toHaveBeenCalled ( ) ;
71
+ expect ( mockPlugin . execute ) . not . toHaveBeenCalled ( ) ;
75
72
76
73
// @ts -ignore
77
74
expect ( client . appState ) . toBe ( 'background' ) ;
78
75
} ) ;
79
76
80
77
it ( 'sends an event when inactive => active' , async ( ) => {
81
- client = new SegmentClient ( clientArgs ) ;
82
- const processSpy = await setupTest ( client , 'inactive' , 'active' ) ;
78
+ await setupTest ( 'inactive' , 'active' ) ;
83
79
84
- expect ( processSpy ) . toHaveBeenCalledTimes ( 1 ) ;
85
- expect ( processSpy ) . toHaveBeenCalledWith ( {
80
+ // @ts -ignore
81
+ expect ( client . appState ) . toBe ( 'active' ) ;
82
+
83
+ expect ( mockPlugin . execute ) . toHaveBeenCalledTimes ( 1 ) ;
84
+ expectEvent ( {
86
85
event : 'Application Opened' ,
87
86
properties : {
88
87
from_background : true ,
@@ -91,16 +90,16 @@ describe('SegmentClient #handleAppStateChange', () => {
91
90
} ,
92
91
type : EventType . TrackEvent ,
93
92
} ) ;
94
- // @ts -ignore
95
- expect ( client . appState ) . toBe ( 'active' ) ;
96
93
} ) ;
97
94
98
95
it ( 'sends an event when background => active' , async ( ) => {
99
- client = new SegmentClient ( clientArgs ) ;
100
- const processSpy = await setupTest ( client , 'background' , 'active' ) ;
96
+ await setupTest ( 'background' , 'active' ) ;
101
97
102
- expect ( processSpy ) . toHaveBeenCalledTimes ( 1 ) ;
103
- expect ( processSpy ) . toHaveBeenCalledWith ( {
98
+ // @ts -ignore
99
+ expect ( client . appState ) . toBe ( 'active' ) ;
100
+
101
+ expect ( mockPlugin . execute ) . toHaveBeenCalledTimes ( 1 ) ;
102
+ expectEvent ( {
104
103
event : 'Application Opened' ,
105
104
properties : {
106
105
from_background : true ,
@@ -109,62 +108,60 @@ describe('SegmentClient #handleAppStateChange', () => {
109
108
} ,
110
109
type : EventType . TrackEvent ,
111
110
} ) ;
112
- // @ts -ignore
113
- expect ( client . appState ) . toBe ( 'active' ) ;
114
111
} ) ;
115
112
116
113
it ( 'sends an event when active => inactive' , async ( ) => {
117
- client = new SegmentClient ( clientArgs ) ;
118
- const processSpy = await setupTest ( client , 'active' , 'inactive' ) ;
114
+ await setupTest ( 'active' , 'inactive' ) ;
119
115
120
- expect ( processSpy ) . toHaveBeenCalledTimes ( 1 ) ;
121
- expect ( processSpy ) . toHaveBeenCalledWith ( {
116
+ // @ts -ignore
117
+ expect ( client . appState ) . toBe ( 'inactive' ) ;
118
+
119
+ expect ( mockPlugin . execute ) . toHaveBeenCalledTimes ( 1 ) ;
120
+ expectEvent ( {
122
121
event : 'Application Backgrounded' ,
123
122
properties : { } ,
124
123
type : EventType . TrackEvent ,
125
124
} ) ;
126
- // @ts -ignore
127
- expect ( client . appState ) . toBe ( 'inactive' ) ;
128
125
} ) ;
129
126
130
127
it ( 'sends an event when active => background' , async ( ) => {
131
- client = new SegmentClient ( clientArgs ) ;
132
- const processSpy = await setupTest ( client , 'active' , 'background' ) ;
128
+ await setupTest ( 'active' , 'background' ) ;
133
129
134
- expect ( processSpy ) . toHaveBeenCalledTimes ( 1 ) ;
135
- expect ( processSpy ) . toHaveBeenCalledWith ( {
130
+ // @ts -ignore
131
+ expect ( client . appState ) . toBe ( 'background' ) ;
132
+
133
+ expect ( mockPlugin . execute ) . toHaveBeenCalledTimes ( 1 ) ;
134
+ expectEvent ( {
136
135
event : 'Application Backgrounded' ,
137
136
properties : { } ,
138
137
type : EventType . TrackEvent ,
139
138
} ) ;
140
- // @ts -ignore
141
- expect ( client . appState ) . toBe ( 'background' ) ;
142
139
} ) ;
143
140
144
- it ( 'does not send an event when unknown => active' , async ( ) => {
145
- client = new SegmentClient ( clientArgs ) ;
146
- const processSpy = await setupTest ( client , 'unknown' , 'active' ) ;
141
+ it ( 'sends an event when unknown => active' , async ( ) => {
142
+ await setupTest ( 'unknown' , 'active' ) ;
147
143
148
- expect ( processSpy ) . not . toHaveBeenCalled ( ) ;
149
144
// @ts -ignore
150
145
expect ( client . appState ) . toBe ( 'active' ) ;
146
+
147
+ expect ( mockPlugin . execute ) . not . toHaveBeenCalled ( ) ;
151
148
} ) ;
152
149
153
- it ( 'does not send an event when unknown => background' , async ( ) => {
154
- client = new SegmentClient ( clientArgs ) ;
155
- const processSpy = await setupTest ( client , 'unknown' , 'background' ) ;
150
+ it ( 'sends an event when unknown => background' , async ( ) => {
151
+ await setupTest ( 'unknown' , 'background' ) ;
156
152
157
- expect ( processSpy ) . not . toHaveBeenCalled ( ) ;
158
153
// @ts -ignore
159
154
expect ( client . appState ) . toBe ( 'background' ) ;
155
+
156
+ expect ( mockPlugin . execute ) . not . toHaveBeenCalled ( ) ;
160
157
} ) ;
161
158
162
- it ( 'does not send an event when unknown => inactive' , async ( ) => {
163
- client = new SegmentClient ( clientArgs ) ;
164
- const processSpy = await setupTest ( client , 'unknown' , 'inactive' ) ;
159
+ it ( 'sends an event when unknown => inactive' , async ( ) => {
160
+ await setupTest ( 'unknown' , 'inactive' ) ;
165
161
166
- expect ( processSpy ) . not . toHaveBeenCalled ( ) ;
167
162
// @ts -ignore
168
163
expect ( client . appState ) . toBe ( 'inactive' ) ;
164
+
165
+ expect ( mockPlugin . execute ) . not . toHaveBeenCalled ( ) ;
169
166
} ) ;
170
167
} ) ;
0 commit comments