-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #244 from Eldar2021/el/help-unit-test-to-mq-remote…
…-client Help Write unit test to `MqremoteClient`
- Loading branch information
Showing
5 changed files
with
163 additions
and
218 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import 'package:flutter/foundation.dart'; | ||
import 'package:mocktail/mocktail.dart'; | ||
import 'package:mq_remote_client/mq_remote_client.dart'; | ||
|
||
@immutable | ||
final class MockNetworkClient extends Mock implements NetworkClient { | ||
@override | ||
Future<bool> checkInternetConnection() => Future.value(true); | ||
} |
281 changes: 64 additions & 217 deletions
281
packages/mq_remote_client/test/mq_remote_client_test.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,232 +1,79 @@ | ||
import 'package:dio/dio.dart'; | ||
import 'package:flutter_test/flutter_test.dart'; | ||
import 'package:mocktail/mocktail.dart'; | ||
import 'package:mq_either/mq_either.dart'; | ||
import 'package:mq_remote_client/mq_remote_client.dart'; | ||
|
||
class MockClient extends Mock implements MqRemoteClient {} | ||
import 'mock_network_client.dart'; | ||
import 'test_model.dart'; | ||
|
||
void main() { | ||
late MockClient client; | ||
const mockUrl = 'https://jsonplaceholder.typicode.com/posts'; | ||
const mockValue = 'mock-value'; | ||
|
||
final requestBody = {'key': 'value'}; | ||
final mockResponse = {'responseKey': 'responseValue'}; | ||
|
||
setUp(() { | ||
client = MockClient(); | ||
}); | ||
|
||
group('GET requests', () { | ||
test('Success', () async { | ||
when(() => client.get<String>(mockUrl)).thenAnswer((_) async => const Right(mockValue)); | ||
|
||
final result = await client.get<String>(mockUrl); | ||
|
||
final value = result.fold( | ||
(error) => fail('Expected a successful result but got an error: $error'), | ||
(data) => data, | ||
); | ||
|
||
expect(value, mockValue); | ||
}); | ||
|
||
test('with type', () async { | ||
when( | ||
() => client.getType<String>(mockUrl, fromJson: any(named: 'fromJson')), | ||
).thenAnswer((_) async => const Right<String, MqRemoteException>(mockValue)); | ||
|
||
final result = await client.getType<String>(mockUrl, fromJson: (json) => json as String); | ||
|
||
final value = result.fold( | ||
(error) => fail('Expected a successful result but got an error: $error'), | ||
(data) => data, | ||
); | ||
|
||
expect(value, mockValue); | ||
}); | ||
|
||
test('with list of type', () async { | ||
when( | ||
() => client.getListOfType<Map<String, dynamic>>(mockUrl, fromJson: any(named: 'fromJson')), | ||
).thenAnswer( | ||
(_) async => const Right([ | ||
{'key': mockValue}, | ||
]), | ||
); | ||
|
||
final result = await client.getListOfType<Map<String, dynamic>>(mockUrl, fromJson: (json) => json); | ||
|
||
expect( | ||
result, | ||
const Right<List<Map<String, dynamic>>, dynamic>([ | ||
{'key': mockValue}, | ||
]), | ||
); | ||
}); | ||
}); | ||
|
||
group('POST requests', () { | ||
test('successful', () async { | ||
when( | ||
() => client.post<Map<String, dynamic>>( | ||
mockUrl, | ||
body: requestBody, | ||
), | ||
).thenAnswer( | ||
(_) async => const Right({'key': 'value'}), | ||
); | ||
|
||
final result = await client.post<Map<String, dynamic>>( | ||
mockUrl, | ||
body: requestBody, | ||
); | ||
|
||
expect(result, const Right<Map<String, dynamic>, dynamic>({'key': 'value'})); | ||
}); | ||
|
||
test('with type', () async { | ||
when( | ||
() => client.postType<Map<String, dynamic>>( | ||
mockUrl, | ||
body: requestBody, | ||
fromJson: any(named: 'fromJson'), | ||
), | ||
).thenAnswer( | ||
(_) async => const Right({'key': 'value'}), | ||
); | ||
|
||
final result = await client.postType<Map<String, dynamic>>( | ||
mockUrl, | ||
body: requestBody, | ||
fromJson: (json) => json, | ||
); | ||
|
||
expect(result, const Right<Map<String, dynamic>, dynamic>({'key': 'value'})); | ||
late MqRemoteClient client; | ||
|
||
const getListUrl = 'https://encointer.github.io/feed/community_messages/en/cm.json'; | ||
const getUrl = 'https://eldar2021.github.io/encointer/test_data.json'; | ||
const emptyList = 'https://eldar2021.github.io/encointer/test/empty_list.json'; | ||
|
||
setUp( | ||
() => client = MqRemoteClient( | ||
dio: Dio(), | ||
network: MockNetworkClient(), | ||
), | ||
); | ||
|
||
group('MqRemoteClient `get`, `getType`, `getListOfType`', () { | ||
test('Get', () async { | ||
Map<String, dynamic>? mapValue; | ||
List<dynamic>? listValue; | ||
|
||
final mapResponse = await client.get<Map<String, dynamic>>(getUrl); | ||
mapResponse.fold((l) => null, (r) => mapValue = r); | ||
|
||
expect(mapValue, isNotNull); | ||
expect(mapValue, isMap); | ||
expect(mapValue, isA<Map<String, dynamic>>()); | ||
|
||
final listResponse = await client.get<List<dynamic>>(getListUrl); | ||
listResponse.fold((l) => null, (r) => listValue = r); | ||
expect(listValue, isNotNull); | ||
expect(listValue, isList); | ||
expect(listValue?.isNotEmpty, true); | ||
expect(listValue, isA<List<dynamic>>()); | ||
}); | ||
|
||
test('List of type', () async { | ||
when( | ||
() => client.postListOfType<Map<String, dynamic>>( | ||
mockUrl, | ||
fromJson: any(named: 'fromJson'), | ||
), | ||
).thenAnswer( | ||
(_) async => const Right([ | ||
{'key': 'value'}, | ||
]), | ||
); | ||
|
||
final result = await client.postListOfType<Map<String, dynamic>>( | ||
mockUrl, | ||
fromJson: (json) => json, | ||
); | ||
|
||
expect( | ||
result, | ||
const Right<List<Map<String, dynamic>>, dynamic>([ | ||
{'key': 'value'}, | ||
]), | ||
test('Get Type', () async { | ||
TestModel? testModel; | ||
final response = await client.getType<TestModel>( | ||
getUrl, | ||
fromJson: TestModel.fromJson, | ||
); | ||
response.fold((l) => null, (r) => testModel = r); | ||
expect(testModel, isNotNull); | ||
expect(testModel, isA<TestModel>()); | ||
}); | ||
}); | ||
|
||
group('PUT requests', () { | ||
test('successful', () async { | ||
when( | ||
() => client.put<Map<String, dynamic>>( | ||
mockUrl, | ||
body: requestBody, | ||
), | ||
).thenAnswer( | ||
(_) async => const Right({'key': 'value'}), | ||
); | ||
|
||
final result = await client.put<Map<String, dynamic>>( | ||
mockUrl, | ||
body: requestBody, | ||
); | ||
|
||
expect(result, const Right<Map<String, dynamic>, dynamic>({'key': 'value'})); | ||
test('Get List Of Type', () async { | ||
List<TestModel>? testModelList; | ||
final response = await client.getListOfType<TestModel>( | ||
getListUrl, | ||
fromJson: TestModel.fromJson, | ||
); | ||
response.fold((l) => null, (r) => testModelList = r); | ||
expect(testModelList, isNotNull); | ||
expect(testModelList, isList); | ||
expect(testModelList, isA<List<TestModel>>()); | ||
expect(testModelList?[0], isA<TestModel>()); | ||
}); | ||
|
||
test('with type', () async { | ||
when( | ||
() => client.putType<Map<String, dynamic>>( | ||
mockUrl, | ||
body: requestBody, | ||
fromJson: any(named: 'fromJson'), | ||
), | ||
).thenAnswer( | ||
(_) async => const Right({'key': 'value'}), | ||
); | ||
|
||
final result = await client.putType<Map<String, dynamic>>( | ||
mockUrl, | ||
body: requestBody, | ||
fromJson: (json) => json, | ||
); | ||
|
||
expect(result, const Right<Map<String, dynamic>, dynamic>({'key': 'value'})); | ||
}); | ||
|
||
test('list of type', () async { | ||
when( | ||
() => client.putListOfType<Map<String, dynamic>>( | ||
mockUrl, | ||
fromJson: any(named: 'fromJson'), | ||
), | ||
).thenAnswer( | ||
(_) async => const Right([ | ||
{'key': 'value'}, | ||
]), | ||
); | ||
|
||
final result = await client.putListOfType<Map<String, dynamic>>(mockUrl, fromJson: (json) => json); | ||
|
||
expect( | ||
result, | ||
const Right<List<Map<String, dynamic>>, dynamic>([ | ||
{'key': 'value'}, | ||
]), | ||
); | ||
}); | ||
}); | ||
group('PATCH requests', () { | ||
test('successful', () async { | ||
when( | ||
() => client.patch<Map<String, dynamic>>(mockUrl, body: requestBody, fromJson: any(named: 'fromJson')), | ||
).thenAnswer((_) async => Right<Map<String, dynamic>, MqRemoteException>(mockResponse)); | ||
|
||
final result = await client.patch<Map<String, dynamic>>(mockUrl, body: requestBody, fromJson: (json) => json); | ||
|
||
expect(result, isA<Right<Map<String, dynamic>, MqRemoteException>>()); | ||
}); | ||
|
||
test('with type', () async { | ||
when( | ||
() => client.patchType<Map<String, dynamic>>(mockUrl, body: requestBody, fromJson: any(named: 'fromJson')), | ||
).thenAnswer( | ||
(_) async => Right<Map<String, dynamic>, MqRemoteException>(mockResponse), | ||
); | ||
|
||
final result = await client.patchType<Map<String, dynamic>>(mockUrl, body: requestBody, fromJson: (json) => json); | ||
|
||
expect(result, isA<Right<Map<String, dynamic>, MqRemoteException>>()); | ||
}); | ||
|
||
test('list of type', () async { | ||
when( | ||
() => client.patchListOfType<Map<String, dynamic>>(mockUrl, fromJson: any(named: 'fromJson')), | ||
).thenAnswer( | ||
(_) async => const Right<List<Map<String, dynamic>>, MqRemoteException>([ | ||
{'key': 'value'}, | ||
]), | ||
); | ||
|
||
final result = await client.patchListOfType<Map<String, dynamic>>(mockUrl, fromJson: (json) => json); | ||
|
||
expect(result, isA<Right<List<Map<String, dynamic>>, MqRemoteException>>()); | ||
test('Get empty List', () async { | ||
List<TestModel>? testModelList; | ||
final response = await client.getListOfType<TestModel>( | ||
emptyList, | ||
fromJson: TestModel.fromJson, | ||
); | ||
response.fold((l) => null, (r) => testModelList = r); | ||
expect(testModelList, isNotNull); | ||
expect(testModelList, isList); | ||
expect(testModelList, isA<List<TestModel>>()); | ||
expect(testModelList?.isEmpty, true); | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import 'package:connectivity_plus/connectivity_plus.dart'; | ||
import 'package:flutter_test/flutter_test.dart'; | ||
import 'package:mocktail/mocktail.dart'; | ||
import 'package:mq_remote_client/mq_remote_client.dart'; | ||
|
||
class MockConnectivity extends Mock implements Connectivity {} | ||
|
||
void main() { | ||
group('NetworkClient', () { | ||
late MockConnectivity mockConnectivity; | ||
late NetworkClient networkClient; | ||
|
||
setUp(() { | ||
mockConnectivity = MockConnectivity(); | ||
networkClient = NetworkClient(mockConnectivity); | ||
}); | ||
|
||
test('returns true when connected via Wi-Fi', () async { | ||
when(() => mockConnectivity.checkConnectivity()).thenAnswer( | ||
(_) async => [ConnectivityResult.wifi], | ||
); | ||
|
||
final result = await networkClient.checkInternetConnection(); | ||
|
||
expect(result, isTrue); | ||
}); | ||
|
||
test('returns true when connected via mobile', () async { | ||
when(() => mockConnectivity.checkConnectivity()).thenAnswer( | ||
(_) async => [ConnectivityResult.mobile], | ||
); | ||
|
||
final result = await networkClient.checkInternetConnection(); | ||
|
||
expect(result, isTrue); | ||
}); | ||
|
||
test('returns false when not connected', () async { | ||
when(() => mockConnectivity.checkConnectivity()).thenAnswer( | ||
(_) async => [ConnectivityResult.none], | ||
); | ||
|
||
final result = await networkClient.checkInternetConnection(); | ||
|
||
expect(result, isFalse); | ||
}); | ||
|
||
test('returns false when connected via bluetooth', () async { | ||
when(() => mockConnectivity.checkConnectivity()).thenAnswer( | ||
(_) async => [ConnectivityResult.bluetooth], | ||
); | ||
|
||
final result = await networkClient.checkInternetConnection(); | ||
|
||
expect(result, isFalse); | ||
}); | ||
}); | ||
} |
Oops, something went wrong.