11import 'package:segment_analytics/analytics.dart' ;
22import 'package:segment_analytics/analytics_platform_interface.dart' ;
3+ import 'package:segment_analytics/client.dart' ;
34import 'package:segment_analytics/event.dart' ;
5+ import 'package:segment_analytics/flush_policies/count_flush_policy.dart' ;
6+ import 'package:segment_analytics/flush_policies/flush_policy.dart' ;
47import 'package:segment_analytics/logger.dart' ;
8+ import 'package:segment_analytics/plugins/event_logger.dart' ;
59import 'package:segment_analytics/state.dart' ;
610import 'package:flutter/widgets.dart' ;
711import 'package:flutter_test/flutter_test.dart' ;
812import 'package:mockito/mockito.dart' ;
913import 'package:shared_preferences/shared_preferences.dart' ;
1014
1115import 'mocks/mocks.dart' ;
16+ import 'mocks/mocks.mocks.dart' ;
1217
1318void main () {
1419 WidgetsFlutterBinding .ensureInitialized ();
@@ -21,55 +26,118 @@ void main() {
2126 ];
2227
2328 group ("analytics" , () {
24-
25- setUp (() {
29+ late Analytics analytics;
30+ late MockHTTPClient httpClient;
31+ setUp (() async {
2632 AnalyticsPlatform .instance = MockPlatform ();
27-
2833 // Prevents spamming the test console. Eventually logging info will be behind a debug flag so this won't be needed
2934 LogFactory .logger = Mocks .logTarget ();
30-
3135 SharedPreferences .setMockInitialValues ({});
32- });
3336
34- test (
35- "it fetches settings but does not fire track event when not tracking lifecycle events" ,
36- () async {
37- final httpClient = Mocks .httpClient ();
37+ httpClient = Mocks .httpClient ();
3838 when (httpClient.settingsFor (writeKey))
3939 .thenAnswer ((_) => Future .value (SegmentAPISettings ({})));
4040 when (httpClient.startBatchUpload (writeKey, batch))
4141 .thenAnswer ((_) => Future .value (true ));
4242
43- Analytics analytics = Analytics (
43+ analytics = Analytics (
4444 Configuration ("123" ,
4545 trackApplicationLifecycleEvents: false ,
46- appStateStream : () => Mocks . streamSubscription () ),
46+ token : "abcdef12345" ),
4747 Mocks .store (),
4848 httpClient: (_) => httpClient);
4949 await analytics.init ();
50+ });
5051
52+ test (
53+ "it fetches settings but does not fire track event when not tracking lifecycle events" ,
54+ () async {
55+
5156 verify (httpClient.settingsFor (writeKey));
5257 verifyNever (httpClient.startBatchUpload (writeKey, batch));
5358 });
5459 test (
5560 "it fetches settings and fires track event when tracking lifecycle events" ,
5661 () async {
57- final httpClient = Mocks .httpClient ();
58- when (httpClient.settingsFor (writeKey))
59- .thenAnswer ((_) => Future .value (SegmentAPISettings ({})));
60- when (httpClient.startBatchUpload (writeKey, batch))
61- .thenAnswer ((_) => Future .value (true ));
62-
63- Analytics analytics = Analytics (
64- Configuration ("123" ,
65- trackApplicationLifecycleEvents: true ,
66- appStateStream: () => Mocks .streamSubscription ()),
67- Mocks .store (),
68- httpClient: (_) => httpClient);
69- await analytics.init ();
7062
7163 verify (httpClient.settingsFor (writeKey));
7264 verifyNever (httpClient.startBatchUpload (writeKey, batch));
7365 });
66+
67+ test ('it analytics track should be callable' , () {
68+ analytics.track ("test track" );
69+ });
70+ test ('it analytics screen should be callable' , () {
71+ analytics.screen ("test screem" );
72+ });
73+ test ('it analytics identify should be callable' , () {
74+ analytics.identify ();
75+ });
76+ test ('it analytics group should be callable' , () {
77+ analytics.group ("test group" );
78+ });
79+ test ('it analytics alias should be callable' , () {
80+ analytics.alias ("test alias" );
81+ });
82+ test ('it analytics cleanup should be callable' , () {
83+ analytics.cleanup ();
84+ });
85+ test ('it analytics reset should be callable' , () {
86+ analytics.reset ();
87+ });
88+ test ('it analytics addFlushPolicy should be callable' , () {
89+ List <FlushPolicy > policies = [];
90+ policies.add (CountFlushPolicy (5 ));
91+ analytics.addFlushPolicy (policies);
92+ });
93+ test ('it analytics getFlushPolicies should be callable' , () {
94+ analytics.getFlushPolicies ();
95+ });
96+ test ('it analytics removeFlushPolicy should be callable' , () {
97+ List <FlushPolicy > policies = [];
98+ policies.add (CountFlushPolicy (5 ));
99+ analytics.removeFlushPolicy (policies);
100+ });
101+ test ('it analytics removePlugin should be callable' , () {
102+ analytics.addPlugin (EventLogger (), settings: {"event" : "Track Event" });
103+ });
104+ test ('it analytics removePlugin should be callable' , () {
105+ analytics.removePlugin (EventLogger ());
106+ });
107+ test ('it analytics onContextLoaded should be callable' , () {
108+ analytics.onContextLoaded ((p0) { });
109+ });
110+ test ('it analytics onPluginLoaded should be callable' , () {
111+ analytics.onPluginLoaded ((p0) { });
112+ });
113+
114+ test ("Test analytics platform getContext" , () {
115+ AnalyticsPlatform analyticsPlatform = MockAnalyticsPlatform ();
116+
117+ expect (
118+ () async => await analyticsPlatform.getContext (),
119+ throwsA (isA <UnimplementedError >()),
120+ );
121+ });
122+ test ("Test analytics platform linkStream" , () {
123+ AnalyticsPlatform analyticsPlatform = MockAnalyticsPlatform ();
124+
125+ expect (
126+ () async => analyticsPlatform.linkStream,
127+ throwsA (isA <UnimplementedError >()),
128+ );
129+ });
130+
131+ test ("it createClient" , () async {
132+ Analytics analytics = createClient (Configuration ("123" ,
133+ debug: false ,
134+ trackApplicationLifecycleEvents: true ,
135+ trackDeeplinks: true ,
136+ token: "abcdef12345" )
137+ );
138+ expect (analytics, isA <Analytics >());
139+ });
74140 });
75141}
142+
143+ class MockAnalyticsPlatform extends AnalyticsPlatform { }
0 commit comments