forked from customerio/customerio-flutter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
customer_io_method_channel_test.dart
132 lines (101 loc) · 4.87 KB
/
customer_io_method_channel_test.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import 'package:customer_io/customer_io_config.dart';
import 'package:customer_io/customer_io_enums.dart';
import 'package:customer_io/customer_io_method_channel.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
/// This is more of test of what our Native platform is expecting.
void main() {
const MethodChannel channel = MethodChannel('customer_io');
final Map<String, dynamic> methodInvocations = {};
TestWidgetsFlutterBinding.ensureInitialized();
setUp(() {
channel.setMockMethodCallHandler((MethodCall methodCall) async {
methodInvocations[methodCall.method] = methodCall.arguments;
switch (methodCall.method) {
case 'initialize':
return Future
.value(); // Simulate a successful response from the platform.
case 'identify':
case 'track':
case 'trackMetric':
case 'screen':
case 'registerDeviceToken':
case 'clearIdentify':
case 'setProfileAttributes':
case 'setDeviceAttributes':
return;
default:
throw MissingPluginException();
}
});
});
tearDown(() {
channel.setMockMethodCallHandler(null);
});
void expectMethodInvocationArguments(String methodKey, Map<String, dynamic> arguments) {
expect(methodInvocations.containsKey(methodKey), true, reason: 'method `$methodKey` was called');
arguments.forEach((key, value) {
expect(methodInvocations[methodKey][key], value, reason: 'method arg $key matches');
});
}
test('initialize() should call platform method with correct arguments', () async {
final customerIO = CustomerIOMethodChannel();
final config = CustomerIOConfig(siteId: 'site_id', apiKey: 'api_key');
await customerIO.initialize(config: config);
expectMethodInvocationArguments('initialize', { 'siteId': config.siteId, 'apiKey': config.apiKey });
});
test('identify() should call platform method with correct arguments', () async {
final Map<String, dynamic> args = { 'identifier': 'Customer 1', 'attributes': { 'email': 'customer@email.com' } };
final customerIO = CustomerIOMethodChannel();
customerIO.identify(identifier: args['identifier'] as String, attributes: args['attributes']);
expectMethodInvocationArguments('identify', args);
});
test('track() should call platform method with correct arguments', () async {
final Map<String, dynamic> args = { 'eventName': 'test_event', 'attributes': { 'eventData': 2 }};
final customerIO = CustomerIOMethodChannel();
customerIO.track(name: args['eventName'], attributes: args['attributes']);
expectMethodInvocationArguments('track', args);
});
test('trackMetric() should call platform method with correct arguments', () async {
final Map<String, dynamic> args = {
'deliveryId': '123',
'deliveryToken': 'asdf',
'metricEvent': 'clicked'};
final customerIO = CustomerIOMethodChannel();
customerIO.trackMetric(
deliveryID: args['deliveryId'],
deviceToken: args['deliveryToken'],
event: MetricEvent.values.byName(args['metricEvent']));
expectMethodInvocationArguments('trackMetric', args);
});
test('screen() should call platform method with correct arguments', () async {
final Map<String, dynamic> args = { 'eventName': 'screen_event', 'attributes': { 'screenName': '你好' }};
final customerIO = CustomerIOMethodChannel();
customerIO.screen(name: args['eventName'], attributes: args['attributes']);
expectMethodInvocationArguments('screen', args);
});
test('registerDeviceToken() should call platform method with correct arguments', () async {
final Map<String, String> args = { 'token': 'asdf' };
final customerIO = CustomerIOMethodChannel();
customerIO.registerDeviceToken(deviceToken: args['token'] as String);
expectMethodInvocationArguments('registerDeviceToken', args);
});
test('clearIdentify() should call platform method with correct arguments', () async {
final Map<String, String> args = {};
final customerIO = CustomerIOMethodChannel();
customerIO.clearIdentify();
expectMethodInvocationArguments('clearIdentify', args);
});
test('setProfileAttributes() should call platform method with correct arguments', () async {
final Map<String, dynamic> args = { 'attributes': { 'age': 1 }};
final customerIO = CustomerIOMethodChannel();
customerIO.setProfileAttributes(attributes: args['attributes']);
expectMethodInvocationArguments('setProfileAttributes', args);
});
test('setDeviceAttributes() should call platform method with correct arguments', () async {
final Map<String, dynamic> args = { 'attributes': { 'os': 'Android' }};
final customerIO = CustomerIOMethodChannel();
customerIO.setDeviceAttributes(attributes: args['attributes']);
expectMethodInvocationArguments('setDeviceAttributes', args);
});
}