Skip to content

Commit

Permalink
chore: replace mockito with mocktail (#4813)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jordan-Nelson authored May 4, 2024
1 parent 46534e6 commit dc7de26
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 883 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ dev_dependencies:
built_value_generator: 8.8.1
flutter_test:
sdk: flutter
mockito: ^5.0.0
mocktail: ^1.0.0
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,24 @@ import 'package:amplify_analytics_pinpoint_dart/src/impl/analytics_client/event_
import 'package:amplify_flutter/amplify_flutter.dart';
import 'package:amplify_push_notifications_pinpoint/src/pinpoint_provider.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
import 'package:mocktail/mocktail.dart';

import 'pinpoint_provider_test.mocks.dart';
import 'test_data/fake_notification_messges.dart';

@GenerateMocks(
[
AmplifyAuthProviderRepository,
AWSIamAmplifyAuthProvider,
UserProfile,
AnalyticsClient,
EndpointClient,
EventClient,
],
)
class MockAmplifyAuthProviderRepository extends Mock
implements AmplifyAuthProviderRepository {}

class MockAWSIamAmplifyAuthProvider extends Mock
implements AWSIamAmplifyAuthProvider {}

class MockUserProfile extends Mock implements UserProfile {}

class MockAnalyticsClient extends Mock implements AnalyticsClient {}

class MockEndpointClient extends Mock implements EndpointClient {}

class MockEventClient extends Mock implements EventClient {}

void main() {
TestWidgetsFlutterBinding.ensureInitialized();
final pinpointProvider = PinpointProvider();
Expand All @@ -35,9 +37,24 @@ void main() {
final awsIamAmplifyAuthProvider = MockAWSIamAmplifyAuthProvider();
final mockAnalyticsClient = MockAnalyticsClient();
group('PinpointProvider', () {
setUpAll(() {
registerFallbackValue(
const AmplifyAuthProviderToken<AmplifyAuthProvider>('mock-token'),
);
registerFallbackValue(
const AWSCredentialsProvider(
AWSCredentials(
'accessKeyId',
'secretAccessKey',
),
),
);
});
test('init fails when retrieving an Auth provider was not successful', () {
when(mockAmplifyAuthProviderRepository.getAuthProvider(any))
.thenReturn(null);
when(
() => mockAmplifyAuthProviderRepository
.getAuthProvider<AmplifyAuthProvider>(any()),
).thenReturn(null);
expect(
() async => pinpointProvider.init(
config: notificationsPinpointConfig,
Expand Down Expand Up @@ -72,28 +89,32 @@ void main() {

test('identifyUser should throw exception if the underlying call throws',
() async {
when(mockAmplifyAuthProviderRepository.getAuthProvider(any))
.thenReturn(awsIamAmplifyAuthProvider);
when(
mockAnalyticsClient.init(
pinpointAppId: anyNamed('pinpointAppId'),
region: anyNamed('region'),
authProvider: anyNamed('authProvider'),
() => mockAmplifyAuthProviderRepository.getAuthProvider(
APIAuthorizationType.iam.authProviderToken,
),
).thenReturn(awsIamAmplifyAuthProvider);
when(
() => mockAnalyticsClient.init(
pinpointAppId: any(named: 'pinpointAppId'),
region: any(named: 'region'),
authProvider: any(named: 'authProvider'),
),
).thenAnswer((realInvocation) async {});

final mockEndpointClient = MockEndpointClient();

when(
mockAnalyticsClient.endpointClient,
() => mockAnalyticsClient.endpointClient,
).thenReturn(mockEndpointClient);

await pinpointProvider.init(
config: notificationsPinpointConfig,
authProviderRepo: mockAmplifyAuthProviderRepository,
analyticsClient: mockAnalyticsClient,
);
when(mockEndpointClient.setUser(any, any)).thenThrow(Exception());
when(() => mockEndpointClient.setUser(any(), any()))
.thenThrow(Exception());
expect(
pinpointProvider.identifyUser(
userId: 'userId',
Expand Down Expand Up @@ -135,16 +156,32 @@ void main() {
});

group('Happy path test', () {
setUpAll(() {
registerFallbackValue(
const AmplifyAuthProviderToken<AmplifyAuthProvider>('mock-token'),
);
registerFallbackValue(
const AWSCredentialsProvider(
AWSCredentials(
'accessKeyId',
'secretAccessKey',
),
),
);
});
TestWidgetsFlutterBinding.ensureInitialized();

test('init should run successfully', () async {
when(mockAmplifyAuthProviderRepository.getAuthProvider(any))
.thenReturn(awsIamAmplifyAuthProvider);
when(
mockAnalyticsClient.init(
pinpointAppId: anyNamed('pinpointAppId'),
region: anyNamed('region'),
authProvider: anyNamed('authProvider'),
() => mockAmplifyAuthProviderRepository.getAuthProvider(
APIAuthorizationType.iam.authProviderToken,
),
).thenReturn(awsIamAmplifyAuthProvider);
when(
() => mockAnalyticsClient.init(
pinpointAppId: any(named: 'pinpointAppId'),
region: any(named: 'region'),
authProvider: any(named: 'authProvider'),
),
).thenAnswer((realInvocation) async {});
expect(
Expand All @@ -158,20 +195,26 @@ void main() {
});

test('identifyUser should run successfully', () async {
when(mockAmplifyAuthProviderRepository.getAuthProvider(any))
.thenReturn(awsIamAmplifyAuthProvider);
when(
mockAnalyticsClient.init(
pinpointAppId: anyNamed('pinpointAppId'),
region: anyNamed('region'),
authProvider: anyNamed('authProvider'),
() => mockAmplifyAuthProviderRepository.getAuthProvider(
APIAuthorizationType.iam.authProviderToken,
),
).thenReturn(awsIamAmplifyAuthProvider);
when(
() => mockAnalyticsClient.init(
pinpointAppId: any(named: 'pinpointAppId'),
region: any(named: 'region'),
authProvider: any(named: 'authProvider'),
),
).thenAnswer((realInvocation) async {});

final mockEndpointClient = MockEndpointClient();
when(() => mockEndpointClient.setUser(any(), any()))
.thenAnswer((_) async => {});
when(mockEndpointClient.updateEndpoint).thenAnswer((_) async => {});

when(
mockAnalyticsClient.endpointClient,
() => mockAnalyticsClient.endpointClient,
).thenReturn(mockEndpointClient);

await pinpointProvider.init(
Expand All @@ -187,27 +230,33 @@ void main() {
),
completes,
);
verify(mockEndpointClient.setUser(any, any));
verify(() => mockEndpointClient.setUser(any(), any()));
});

test('registerDevice should run successfully', () async {
when(mockAmplifyAuthProviderRepository.getAuthProvider(any))
.thenReturn(awsIamAmplifyAuthProvider);
when(
mockAnalyticsClient.init(
pinpointAppId: anyNamed('pinpointAppId'),
region: anyNamed('region'),
authProvider: anyNamed('authProvider'),
() => mockAmplifyAuthProviderRepository.getAuthProvider(
APIAuthorizationType.iam.authProviderToken,
),
).thenReturn(awsIamAmplifyAuthProvider);
when(
() => mockAnalyticsClient.init(
pinpointAppId: any(named: 'pinpointAppId'),
region: any(named: 'region'),
authProvider: any(named: 'authProvider'),
),
).thenAnswer((realInvocation) async {});

final mockEndpointClient = MockEndpointClient();
when(() => mockEndpointClient.setUser(any(), any()))
.thenAnswer((_) async => {});
when(mockEndpointClient.updateEndpoint).thenAnswer((_) async => {});

when(
mockAnalyticsClient.endpointClient,
() => mockAnalyticsClient.endpointClient,
).thenReturn(mockEndpointClient);

expect(
await expectLater(
pinpointProvider.init(
config: notificationsPinpointConfig,
authProviderRepo: mockAmplifyAuthProviderRepository,
Expand All @@ -222,27 +271,37 @@ void main() {
),
completes,
);
verify(mockEndpointClient.updateEndpoint());
verify(mockEndpointClient.updateEndpoint);
});

test('recordEvent should run successfully', () async {
when(mockAmplifyAuthProviderRepository.getAuthProvider(any))
.thenReturn(awsIamAmplifyAuthProvider);
when(
mockAnalyticsClient.init(
pinpointAppId: anyNamed('pinpointAppId'),
region: anyNamed('region'),
authProvider: anyNamed('authProvider'),
() => mockAmplifyAuthProviderRepository.getAuthProvider(
APIAuthorizationType.iam.authProviderToken,
),
).thenReturn(awsIamAmplifyAuthProvider);
when(
() => mockAnalyticsClient.init(
pinpointAppId: any(named: 'pinpointAppId'),
region: any(named: 'region'),
authProvider: any(named: 'authProvider'),
),
).thenAnswer((realInvocation) async {});

final mockEventClient = MockEventClient();
when(
() => mockEventClient.recordEvent(
eventType: any(named: 'eventType'),
session: any(named: 'session'),
properties: any(named: 'properties'),
),
).thenAnswer((_) async => {});

when(
mockAnalyticsClient.eventClient,
() => mockAnalyticsClient.eventClient,
).thenReturn(mockEventClient);

expect(
await expectLater(
pinpointProvider.init(
config: notificationsPinpointConfig,
authProviderRepo: mockAmplifyAuthProviderRepository,
Expand All @@ -251,7 +310,7 @@ void main() {
completes,
);

expect(
await expectLater(
pinpointProvider.recordNotificationEvent(
eventType: PinpointEventType.foregroundMessageReceived,
notification:
Expand All @@ -260,10 +319,10 @@ void main() {
completes,
);
verify(
mockEventClient.recordEvent(
() => mockEventClient.recordEvent(
eventType:
'${PinpointEventSource.campaign.name}.${PinpointEventType.foregroundMessageReceived.name}',
properties: anyNamed('properties'),
properties: any(named: 'properties'),
),
);
});
Expand Down
Loading

0 comments on commit dc7de26

Please sign in to comment.