Skip to content

Commit

Permalink
Make TrackEvent not require parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan Caseley committed Aug 25, 2021
1 parent 90db638 commit b5efc5e
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/src/actions/track_event.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class TrackEvent extends AnalyticsAction {
final String eventName;

/// Any parameters required or desired to accompany the event, e.g. user's analytics ID, device info, session ID
final Map<String, dynamic> parameters;
final Map<String, dynamic>? parameters;

TrackEvent(this.eventName, this.parameters);
TrackEvent(this.eventName, [this.parameters]);
}
39 changes: 39 additions & 0 deletions test/actions_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import 'package:analyticsx/analytics_x.dart';
import 'package:test/test.dart';

import 'util/vendors_and_actions.dart';

void main() {
late AnalyticsX ax;

setUp(() {
ax = AnalyticsX();
});

tearDown(() {
ax.reset();
});

group('TrackEvent', () {
test('TrackEvent is recorded', () async {
const testEventName = 'test_event';
const testParams = {'p1': 'one', 'p2': 'two'};
final recordingVendor = RecordingVendor();

await ax.init([recordingVendor]);
await ax.invokeAction(TrackEvent(testEventName, testParams));

expect(recordingVendor.trackEventRecordings, {testEventName: testParams});
});

test('TrackEvent is recorded without params', () async {
const testEventName = 'test_event';
final recordingVendor = RecordingVendor();

await ax.init([recordingVendor]);
await ax.invokeAction(TrackEvent(testEventName));

expect(recordingVendor.trackEventRecordings, {testEventName: null});
});
});
}
21 changes: 21 additions & 0 deletions test/util/vendors_and_actions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,24 @@ class BrokenFakeVendor extends FakeVendor {
return super.handleAction(action);
}
}

class RecordingVendor extends AnalyticsVendor {
RecordingVendor() : super('Recorder');
Map<String, dynamic> trackEventRecordings = {};

@override
Future<void> init() async {
//Nothing to do
}

@override
Future<void> handleAction(AnalyticsAction action) async {
if (action is TrackEvent) {
_recordTrackEvent(action.eventName, action.parameters);
}
}

void _recordTrackEvent(String eventName, parameters) {
trackEventRecordings[eventName] = parameters;
}
}

0 comments on commit b5efc5e

Please sign in to comment.