From 3f22b049c0cd0f4ac8d92ddabb7c9b2f1406d3fd Mon Sep 17 00:00:00 2001 From: DenserMeerkat Date: Sat, 10 Aug 2024 22:31:39 +0530 Subject: [PATCH] test: add mockito and test cases --- pubspec.lock | 8 + pubspec.yaml | 1 + .../providers/environment_providers_test.dart | 50 +++ .../common_widgets/envvar_span_test.dart | 77 ++++ test/services/hive_services_mock.dart | 5 + test/services/hive_services_mock.mocks.dart | 343 ++++++++++++++++++ 6 files changed, 484 insertions(+) create mode 100644 test/providers/environment_providers_test.dart create mode 100644 test/screens/common_widgets/envvar_span_test.dart create mode 100644 test/services/hive_services_mock.dart create mode 100644 test/services/hive_services_mock.mocks.dart diff --git a/pubspec.lock b/pubspec.lock index 4e463339..f28b126a 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -905,6 +905,14 @@ packages: url: "https://pub.dev" source: hosted version: "3.0.0" + mockito: + dependency: "direct dev" + description: + name: mockito + sha256: "6841eed20a7befac0ce07df8116c8b8233ed1f4486a7647c7fc5a02ae6163917" + url: "https://pub.dev" + source: hosted + version: "5.4.4" mpv_dart: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index 515fc0c9..914147a1 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -89,6 +89,7 @@ dev_dependencies: freezed: ^2.5.2 json_serializable: ^6.7.1 spot: ^0.13.0 + mockito: ^5.4.4 flutter: uses-material-design: true diff --git a/test/providers/environment_providers_test.dart b/test/providers/environment_providers_test.dart new file mode 100644 index 00000000..66a13ec4 --- /dev/null +++ b/test/providers/environment_providers_test.dart @@ -0,0 +1,50 @@ +import 'package:flutter_test/flutter_test.dart'; +import 'package:mockito/mockito.dart'; +import 'package:flutter_riverpod/flutter_riverpod.dart'; +import 'package:apidash/providers/providers.dart'; +import 'package:apidash/consts.dart'; + +import '../models/environment_models.dart'; +import '../services/hive_services_mock.mocks.dart'; + +void main() { + late MockHiveHandler mockHiveHandler; + late ProviderContainer container; + + setUp(() { + mockHiveHandler = MockHiveHandler(); + + when(mockHiveHandler.getEnvironmentIds()) + .thenReturn([kGlobalEnvironmentId, environmentModel1.id]); + when(mockHiveHandler.getEnvironment(kGlobalEnvironmentId)) + .thenReturn(globalEnvironment.toJson()); + when(mockHiveHandler.getEnvironment(environmentModel1.id)) + .thenReturn(environmentModel1Json); + + container = ProviderContainer( + overrides: [ + environmentsStateNotifierProvider.overrideWith( + (ref) => EnvironmentsStateNotifier(ref, mockHiveHandler), + ), + activeEnvironmentIdStateProvider + .overrideWith((ref) => environmentModel1.id), + ], + ); + }); + + tearDown(() { + container.dispose(); + }); + + /// The availableEnvironmentVariablesStateProvider watches the + /// environmentsStateNotifierProvider and activeEnvironmentIdStateProvider + /// providers. We attempt to test the availableEnvironmentVariablesStateProvider + /// provider by providing the required values to the providers it watches. + test("Testing availableEnvironmentVariablesStateProvider", () { + final envMap = container.read(availableEnvironmentVariablesStateProvider); + expect(envMap, { + kGlobalEnvironmentId: [globalEnvironment.values], + environmentModel1.id: [environmentModel1.values], + }); + }); +} diff --git a/test/screens/common_widgets/envvar_span_test.dart b/test/screens/common_widgets/envvar_span_test.dart new file mode 100644 index 00000000..8d692931 --- /dev/null +++ b/test/screens/common_widgets/envvar_span_test.dart @@ -0,0 +1,77 @@ +import 'package:apidash/providers/providers.dart'; +import 'package:flutter/gestures.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter_portal/flutter_portal.dart'; +import 'package:flutter_riverpod/flutter_riverpod.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:mockito/mockito.dart'; +import 'package:apidash/consts.dart'; +import 'package:apidash/screens/common_widgets/envvar_span.dart'; + +import '../../models/environment_models.dart'; +import '../../services/hive_services_mock.mocks.dart'; + +void main() { + late MockHiveHandler mockHiveHandler; + late ProviderContainer container; + setUp(() { + mockHiveHandler = MockHiveHandler(); + + when(mockHiveHandler.getEnvironmentIds()) + .thenReturn([kGlobalEnvironmentId, environmentModel1.id]); + when(mockHiveHandler.getEnvironment(kGlobalEnvironmentId)) + .thenReturn(globalEnvironment.toJson()); + when(mockHiveHandler.getEnvironment(environmentModel1.id)) + .thenReturn(environmentModel1Json); + + container = ProviderContainer( + overrides: [ + environmentsStateNotifierProvider.overrideWith( + (ref) => EnvironmentsStateNotifier(ref, mockHiveHandler), + ), + ], + ); + }); + + tearDown(() { + container.dispose(); + }); + + /// To test the EnvVarSpan widget, we need to provide the required providers + /// with values and one of the providers is a StateNotifierProvider. Hence it + /// calls hive services which we attempt to mock with mockHiveHandler. + + group("Testing EnvVarSpan widget", () { + testWidgets("", (tester) async { + await tester.pumpWidget( + ProviderScope( + overrides: [ + environmentsStateNotifierProvider.overrideWith( + (ref) => EnvironmentsStateNotifier(ref, mockHiveHandler)), + activeEnvironmentIdStateProvider + .overrideWith((ref) => environmentModel1.id), + ], + child: const Portal( + child: MaterialApp( + home: Scaffold( + body: EnvVarSpan(variableKey: 'key1'), + ), + ), + ), + ), + ); + + /// On hovering over the EnvVarSpan widget, the EnvVarPopover widget appears + /// which we can test by checking if the text 'value1' is displayed. + + final gesture = await tester.createGesture(kind: PointerDeviceKind.mouse); + await gesture.addPointer(location: Offset.zero); + addTearDown(gesture.removePointer); + await tester.pump(); + await gesture.moveTo(tester.getCenter(find.byType(EnvVarSpan))); + await tester.pumpAndSettle(); + + expect(find.text('value1'), findsOneWidget); + }); + }); +} diff --git a/test/services/hive_services_mock.dart b/test/services/hive_services_mock.dart new file mode 100644 index 00000000..4f277768 --- /dev/null +++ b/test/services/hive_services_mock.dart @@ -0,0 +1,5 @@ +import 'package:mockito/annotations.dart'; +import 'package:apidash/services/hive_services.dart'; + +@GenerateMocks([HiveHandler]) +void main() {} diff --git a/test/services/hive_services_mock.mocks.dart b/test/services/hive_services_mock.mocks.dart new file mode 100644 index 00000000..d3805981 --- /dev/null +++ b/test/services/hive_services_mock.mocks.dart @@ -0,0 +1,343 @@ +// Mocks generated by Mockito 5.4.4 from annotations +// in apidash/test/services/hive_services_test.dart. +// Do not manually edit this file. + +// ignore_for_file: no_leading_underscores_for_library_prefixes +import 'dart:async' as _i4; + +import 'package:apidash/services/hive_services.dart' as _i3; +import 'package:hive_flutter/hive_flutter.dart' as _i2; +import 'package:mockito/mockito.dart' as _i1; + +// ignore_for_file: type=lint +// ignore_for_file: avoid_redundant_argument_values +// ignore_for_file: avoid_setters_without_getters +// ignore_for_file: comment_references +// ignore_for_file: deprecated_member_use +// ignore_for_file: deprecated_member_use_from_same_package +// ignore_for_file: implementation_imports +// ignore_for_file: invalid_use_of_visible_for_testing_member +// ignore_for_file: prefer_const_constructors +// ignore_for_file: unnecessary_parenthesis +// ignore_for_file: camel_case_types +// ignore_for_file: subtype_of_sealed_class + +class _FakeBox_0 extends _i1.SmartFake implements _i2.Box { + _FakeBox_0( + Object parent, + Invocation parentInvocation, + ) : super( + parent, + parentInvocation, + ); +} + +class _FakeLazyBox_1 extends _i1.SmartFake implements _i2.LazyBox { + _FakeLazyBox_1( + Object parent, + Invocation parentInvocation, + ) : super( + parent, + parentInvocation, + ); +} + +/// A class which mocks [HiveHandler]. +/// +/// See the documentation for Mockito's code generation for more information. +class MockHiveHandler extends _i1.Mock implements _i3.HiveHandler { + MockHiveHandler() { + _i1.throwOnMissingStub(this); + } + + @override + _i2.Box get dataBox => (super.noSuchMethod( + Invocation.getter(#dataBox), + returnValue: _FakeBox_0( + this, + Invocation.getter(#dataBox), + ), + ) as _i2.Box); + + @override + set dataBox(_i2.Box? _dataBox) => super.noSuchMethod( + Invocation.setter( + #dataBox, + _dataBox, + ), + returnValueForMissingStub: null, + ); + + @override + _i2.Box get settingsBox => (super.noSuchMethod( + Invocation.getter(#settingsBox), + returnValue: _FakeBox_0( + this, + Invocation.getter(#settingsBox), + ), + ) as _i2.Box); + + @override + set settingsBox(_i2.Box? _settingsBox) => super.noSuchMethod( + Invocation.setter( + #settingsBox, + _settingsBox, + ), + returnValueForMissingStub: null, + ); + + @override + _i2.Box get environmentBox => (super.noSuchMethod( + Invocation.getter(#environmentBox), + returnValue: _FakeBox_0( + this, + Invocation.getter(#environmentBox), + ), + ) as _i2.Box); + + @override + set environmentBox(_i2.Box? _environmentBox) => super.noSuchMethod( + Invocation.setter( + #environmentBox, + _environmentBox, + ), + returnValueForMissingStub: null, + ); + + @override + _i2.Box get historyMetaBox => (super.noSuchMethod( + Invocation.getter(#historyMetaBox), + returnValue: _FakeBox_0( + this, + Invocation.getter(#historyMetaBox), + ), + ) as _i2.Box); + + @override + set historyMetaBox(_i2.Box? _historyMetaBox) => super.noSuchMethod( + Invocation.setter( + #historyMetaBox, + _historyMetaBox, + ), + returnValueForMissingStub: null, + ); + + @override + _i2.LazyBox get historyLazyBox => (super.noSuchMethod( + Invocation.getter(#historyLazyBox), + returnValue: _FakeLazyBox_1( + this, + Invocation.getter(#historyLazyBox), + ), + ) as _i2.LazyBox); + + @override + set historyLazyBox(_i2.LazyBox? _historyLazyBox) => + super.noSuchMethod( + Invocation.setter( + #historyLazyBox, + _historyLazyBox, + ), + returnValueForMissingStub: null, + ); + + @override + Map get settings => (super.noSuchMethod( + Invocation.getter(#settings), + returnValue: {}, + ) as Map); + + @override + _i4.Future saveSettings(Map? data) => + (super.noSuchMethod( + Invocation.method( + #saveSettings, + [data], + ), + returnValue: _i4.Future.value(), + returnValueForMissingStub: _i4.Future.value(), + ) as _i4.Future); + + @override + _i4.Future setIds(List? ids) => (super.noSuchMethod( + Invocation.method( + #setIds, + [ids], + ), + returnValue: _i4.Future.value(), + returnValueForMissingStub: _i4.Future.value(), + ) as _i4.Future); + + @override + dynamic getRequestModel(String? id) => super.noSuchMethod(Invocation.method( + #getRequestModel, + [id], + )); + + @override + _i4.Future setRequestModel( + String? id, + Map? requestModelJson, + ) => + (super.noSuchMethod( + Invocation.method( + #setRequestModel, + [ + id, + requestModelJson, + ], + ), + returnValue: _i4.Future.value(), + returnValueForMissingStub: _i4.Future.value(), + ) as _i4.Future); + + @override + void delete(String? key) => super.noSuchMethod( + Invocation.method( + #delete, + [key], + ), + returnValueForMissingStub: null, + ); + + @override + _i4.Future setEnvironmentIds(List? ids) => (super.noSuchMethod( + Invocation.method( + #setEnvironmentIds, + [ids], + ), + returnValue: _i4.Future.value(), + returnValueForMissingStub: _i4.Future.value(), + ) as _i4.Future); + + @override + dynamic getEnvironment(String? id) => super.noSuchMethod(Invocation.method( + #getEnvironment, + [id], + )); + + @override + _i4.Future setEnvironment( + String? id, + Map? environmentJson, + ) => + (super.noSuchMethod( + Invocation.method( + #setEnvironment, + [ + id, + environmentJson, + ], + ), + returnValue: _i4.Future.value(), + returnValueForMissingStub: _i4.Future.value(), + ) as _i4.Future); + + @override + _i4.Future deleteEnvironment(String? id) => (super.noSuchMethod( + Invocation.method( + #deleteEnvironment, + [id], + ), + returnValue: _i4.Future.value(), + returnValueForMissingStub: _i4.Future.value(), + ) as _i4.Future); + + @override + _i4.Future setHistoryIds(List? ids) => (super.noSuchMethod( + Invocation.method( + #setHistoryIds, + [ids], + ), + returnValue: _i4.Future.value(), + returnValueForMissingStub: _i4.Future.value(), + ) as _i4.Future); + + @override + dynamic getHistoryMeta(String? id) => super.noSuchMethod(Invocation.method( + #getHistoryMeta, + [id], + )); + + @override + _i4.Future setHistoryMeta( + String? id, + Map? historyMetaJson, + ) => + (super.noSuchMethod( + Invocation.method( + #setHistoryMeta, + [ + id, + historyMetaJson, + ], + ), + returnValue: _i4.Future.value(), + returnValueForMissingStub: _i4.Future.value(), + ) as _i4.Future); + + @override + _i4.Future deleteHistoryMeta(String? id) => (super.noSuchMethod( + Invocation.method( + #deleteHistoryMeta, + [id], + ), + returnValue: _i4.Future.value(), + returnValueForMissingStub: _i4.Future.value(), + ) as _i4.Future); + + @override + _i4.Future getHistoryRequest(String? id) => (super.noSuchMethod( + Invocation.method( + #getHistoryRequest, + [id], + ), + returnValue: _i4.Future.value(), + ) as _i4.Future); + + @override + _i4.Future setHistoryRequest( + String? id, + Map? historyRequestJsoon, + ) => + (super.noSuchMethod( + Invocation.method( + #setHistoryRequest, + [ + id, + historyRequestJsoon, + ], + ), + returnValue: _i4.Future.value(), + returnValueForMissingStub: _i4.Future.value(), + ) as _i4.Future); + + @override + _i4.Future deleteHistoryRequest(String? id) => (super.noSuchMethod( + Invocation.method( + #deleteHistoryRequest, + [id], + ), + returnValue: _i4.Future.value(), + returnValueForMissingStub: _i4.Future.value(), + ) as _i4.Future); + + @override + _i4.Future clear() => (super.noSuchMethod( + Invocation.method( + #clear, + [], + ), + returnValue: _i4.Future.value(), + ) as _i4.Future); + + @override + _i4.Future removeUnused() => (super.noSuchMethod( + Invocation.method( + #removeUnused, + [], + ), + returnValue: _i4.Future.value(), + returnValueForMissingStub: _i4.Future.value(), + ) as _i4.Future); +}