From b5efc5ebd741679f880181a2d0133cb101cb79ca Mon Sep 17 00:00:00 2001 From: Dan Caseley Date: Wed, 25 Aug 2021 11:27:52 +0100 Subject: [PATCH] Make TrackEvent not require parameters --- lib/src/actions/track_event.dart | 4 +-- test/actions_test.dart | 39 ++++++++++++++++++++++++++++++ test/util/vendors_and_actions.dart | 21 ++++++++++++++++ 3 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 test/actions_test.dart diff --git a/lib/src/actions/track_event.dart b/lib/src/actions/track_event.dart index a329a5d..1e4a25c 100644 --- a/lib/src/actions/track_event.dart +++ b/lib/src/actions/track_event.dart @@ -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 parameters; + final Map? parameters; - TrackEvent(this.eventName, this.parameters); + TrackEvent(this.eventName, [this.parameters]); } diff --git a/test/actions_test.dart b/test/actions_test.dart new file mode 100644 index 0000000..b8f0a39 --- /dev/null +++ b/test/actions_test.dart @@ -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}); + }); + }); +} diff --git a/test/util/vendors_and_actions.dart b/test/util/vendors_and_actions.dart index bbad639..c4d3103 100644 --- a/test/util/vendors_and_actions.dart +++ b/test/util/vendors_and_actions.dart @@ -55,3 +55,24 @@ class BrokenFakeVendor extends FakeVendor { return super.handleAction(action); } } + +class RecordingVendor extends AnalyticsVendor { + RecordingVendor() : super('Recorder'); + Map trackEventRecordings = {}; + + @override + Future init() async { + //Nothing to do + } + + @override + Future handleAction(AnalyticsAction action) async { + if (action is TrackEvent) { + _recordTrackEvent(action.eventName, action.parameters); + } + } + + void _recordTrackEvent(String eventName, parameters) { + trackEventRecordings[eventName] = parameters; + } +}