@@ -112,6 +112,30 @@ describe('Analytics', () => {
112
112
expect ( ( ) => identifyAuthenticatedUser ( null ) )
113
113
. toThrowError ( new Error ( 'UserId is required for identifyAuthenticatedUser.' ) ) ;
114
114
} ) ;
115
+
116
+ it ( 'should call segment identify once' , ( ) => {
117
+ const testTraits = { anything : 'Yay!' } ;
118
+ identifyAuthenticatedUser ( testUserId , testTraits ) ;
119
+ identifyAuthenticatedUser ( testUserId , testTraits ) ;
120
+
121
+ expect ( window . analytics . identify . mock . calls . length ) . toBe ( 1 ) ;
122
+ } ) ;
123
+
124
+ it ( 'should call segment identify if hasIdentifyBeenCalled is false' , ( ) => {
125
+ const testTraits = { anything : 'Yay!' } ;
126
+ service . hasIdentifyBeenCalled = false ;
127
+ identifyAuthenticatedUser ( testUserId , testTraits ) ;
128
+
129
+ expect ( window . analytics . identify ) . toHaveBeenCalled ( ) ;
130
+ } ) ;
131
+
132
+ it ( 'should not call segment identify if hasIdentifyBeenCalled is true' , ( ) => {
133
+ const testTraits = { anything : 'Yay!' } ;
134
+ service . hasIdentifyBeenCalled = true ;
135
+ identifyAuthenticatedUser ( testUserId , testTraits ) ;
136
+
137
+ expect ( window . analytics . identify ) . not . toHaveBeenCalled ( ) ;
138
+ } ) ;
115
139
} ) ;
116
140
117
141
describe ( 'analytics identifyAnonymousUser' , ( ) => {
@@ -130,6 +154,33 @@ describe('Analytics', () => {
130
154
131
155
expect ( window . analytics . reset . mock . calls . length ) . toBe ( 1 ) ;
132
156
} ) ;
157
+
158
+ it ( 'should call segment reset once' , ( ) => {
159
+ window . analytics . user = ( ) => ( { id : ( ) => 1 } ) ;
160
+ const testTraits = { anything : 'Yay!' } ;
161
+ identifyAnonymousUser ( testTraits ) ;
162
+ identifyAnonymousUser ( testTraits ) ;
163
+
164
+ expect ( window . analytics . reset . mock . calls . length ) . toBe ( 1 ) ;
165
+ } ) ;
166
+
167
+ it ( 'should call segment reset if hasIdentifyBeenCalled is false' , ( ) => {
168
+ window . analytics . user = ( ) => ( { id : ( ) => 2 } ) ;
169
+ const testTraits = { anything : 'Yay!' } ;
170
+ service . hasIdentifyBeenCalled = false ;
171
+ identifyAnonymousUser ( testTraits ) ;
172
+
173
+ expect ( window . analytics . reset ) . toHaveBeenCalled ( ) ;
174
+ } ) ;
175
+
176
+ it ( 'should not call segment reset if hasIdentifyBeenCalled is true' , ( ) => {
177
+ window . analytics . user = ( ) => ( { id : ( ) => 3 } ) ;
178
+ const testTraits = { anything : 'Yay!' } ;
179
+ service . hasIdentifyBeenCalled = true ;
180
+ identifyAnonymousUser ( testTraits ) ;
181
+
182
+ expect ( window . analytics . reset ) . not . toHaveBeenCalled ( ) ;
183
+ } ) ;
133
184
} ) ;
134
185
135
186
function testSendPageAfterIdentify ( identifyFunction ) {
0 commit comments