Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit Test for mq_remote_client package #243

Merged
merged 6 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions app/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -213,18 +213,18 @@ packages:
dependency: "direct main"
description:
name: connectivity_plus
sha256: db7a4e143dc72cc3cb2044ef9b052a7ebfe729513e6a82943bc3526f784365b8
sha256: "2056db5241f96cdc0126bd94459fc4cdc13876753768fc7a31c425e50a7177d0"
url: "https://pub.dev"
source: hosted
version: "6.0.3"
version: "6.0.5"
connectivity_plus_platform_interface:
dependency: transitive
description:
name: connectivity_plus_platform_interface
sha256: b6a56efe1e6675be240de39107281d4034b64ac23438026355b4234042a35adb
sha256: "42657c1715d48b167930d5f34d00222ac100475f73d10162ddf43e714932f204"
url: "https://pub.dev"
source: hosted
version: "2.0.0"
version: "2.0.1"
convert:
dependency: transitive
description:
Expand Down
4 changes: 2 additions & 2 deletions packages/mq_remote_client/lib/mq_remote_client.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/// MQ remote client package
library mq_remote_client;

export 'src/http_exception.dart';
export 'src/exception/mq_remote_exception.dart';
export 'src/mq_remote_client.dart';
export 'src/network_client.dart';
export 'src/network_client/network_client.dart';
4 changes: 2 additions & 2 deletions packages/mq_remote_client/lib/src/mq_remote_client.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:dio/dio.dart';
import 'package:mq_either/mq_either.dart';
import 'package:mq_remote_client/src/http_exception.dart';
import 'package:mq_remote_client/src/network_client.dart';

import 'package:mq_remote_client/mq_remote_client.dart';

part 'mq_remote_client_base_extension.dart';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:flutter/foundation.dart';
import 'package:meta/meta.dart';

/// A client for checking the device's internet connection status.
@immutable
final class NetworkClient {
class NetworkClient {
/// Creates a [NetworkClient] with the given [connectivity] instance.
const NetworkClient(this.connectivity);

Expand Down
4 changes: 2 additions & 2 deletions packages/mq_remote_client/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: mq_remote_client
description: MQ remote client package
description: My Quran Remote Client
version: 0.1.0+1
publish_to: none

Expand All @@ -10,7 +10,7 @@ environment:
dependencies:
flutter:
sdk: flutter
connectivity_plus: ^6.0.3
connectivity_plus: ^6.0.5
dio: ^5.5.0+1
meta: ^1.12.0
mq_either:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import 'package:meta/meta.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);
}
30 changes: 30 additions & 0 deletions packages/mq_remote_client/test/helpers/test_model.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
final class TestModel {
const TestModel({
required this.userId,
required this.id,
required this.title,
required this.body,
});

factory TestModel.fromJson(Map<String, dynamic> json) {
return TestModel(
userId: json['userId'] as int,
id: json['id'] as int,
title: json['title'] as String,
body: json['body'] as String,
);
}
final int userId;
final int id;
final String title;
final String body;

Map<String, dynamic> toJson() {
return <String, dynamic>{
'userId': userId,
'id': id,
'title': title,
'body': body,
};
}
}
163 changes: 155 additions & 8 deletions packages/mq_remote_client/test/mq_remote_client_test.dart
Original file line number Diff line number Diff line change
@@ -1,18 +1,165 @@
import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:dio/dio.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mocktail/mocktail.dart';
import 'package:mq_remote_client/mq_remote_client.dart';

class MockDio extends Mock implements Dio {}
import 'helpers/mock_network_client.dart';
import 'helpers/test_model.dart';

void main() {
group('MqRemoteClient', () {
test('can be instantiated', () {
expect(
MqRemoteClient(dio: MockDio(), network: NetworkClient(Connectivity())),
isNotNull,
late MqRemoteClient client;

const emptyList = 'https://eldar2021.github.io/encointer/test/empty_list.json';
const postList = 'https://jsonplaceholder.typicode.com/posts';
const postDatas = 'https://jsonplaceholder.typicode.com/posts/1';

final requestBody = {
'title': 'foo',
'body': 'bar',
'userId': 1,
};

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>>(postDatas);
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>>(postList);
listResponse.fold((l) => null, (r) => listValue = r);
expect(listValue, isNotNull);
expect(listValue, isList);
expect(listValue?.isNotEmpty, true);
expect(listValue, isA<List<dynamic>>());
});

test('Get Type', () async {
TestModel? testModel;
final response = await client.getType<TestModel>(
postDatas,
fromJson: TestModel.fromJson,
);
response.fold((l) => null, (r) => testModel = r);
expect(testModel, isNotNull);
expect(testModel, isA<TestModel>());
});

test('Get List Of Type', () async {
List<TestModel>? testModelList;
final response = await client.getListOfType<TestModel>(
postList,
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('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);
});
});

group('MqRemoteClient `post`, `postType`, `postListOfType`', () {
test('Post', () async {
Map<String, dynamic>? mapValue;
final mapResponse = await client.post<Map<String, dynamic>>(
postList,
body: requestBody,
);

mapResponse.fold((l) => null, (r) => mapValue = r);

expect(mapValue, isNotNull);
expect(mapValue, isA<Map<String, dynamic>>());
});

test('Post Type', () async {
TestModel? testModel;
final response = await client.postType<TestModel>(
postList,
body: requestBody,
fromJson: TestModel.fromJson,
);
response.fold((l) => null, (r) => testModel = r);

expect(testModel, isA<TestModel>());
});
});

group('MqRemoteClient `put`, `putType`, `putListOfType`', () {
test('Put', () async {
Map<String, dynamic>? mapValue;

final mapResponse = await client.put<Map<String, dynamic>>(postDatas, body: requestBody);
mapResponse.fold((l) => null, (r) => mapValue = r);
expect(mapValue, isNotNull);

expect(mapValue, isA<Map<String, dynamic>>());
});

test('Put Type', () async {
TestModel? testModel;
final response = await client.putType<TestModel>(
postDatas,
body: requestBody,
fromJson: TestModel.fromJson,
);
response.fold((l) => null, (r) => testModel = r);

expect(testModel, isA<TestModel>());
});
});

group('MqRemoteClient `patch`, `patchType`, `patchListOfType`', () {
test('Patch', () async {
Map<String, dynamic>? mapValue;

final mapResponse = await client.patch<Map<String, dynamic>>(
postDatas,
fromJson: (json) => json,
body: requestBody,
);

mapResponse.fold((l) => null, (r) => mapValue = r);
expect(mapValue, isNotNull);

expect(mapValue, isA<Map<String, dynamic>>());
});

test('Patch Type', () async {
TestModel? testModel;
final response = await client.patchType<TestModel>(
postDatas,
body: requestBody,
fromJson: TestModel.fromJson,
);
response.fold((l) => null, (r) => testModel = r);

expect(testModel, isA<TestModel>());
});
});
}
58 changes: 58 additions & 0 deletions packages/mq_remote_client/test/network_cleint_test.dart
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);
});
});
}
Loading