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

PR with improvable code and test case mentioned with outcome desired #442

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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: 8 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
50 changes: 50 additions & 0 deletions test/providers/environment_providers_test.dart
Original file line number Diff line number Diff line change
@@ -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],
});
});
}
77 changes: 77 additions & 0 deletions test/screens/common_widgets/envvar_span_test.dart
Original file line number Diff line number Diff line change
@@ -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);
});
});
}
5 changes: 5 additions & 0 deletions test/services/hive_services_mock.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import 'package:mockito/annotations.dart';
import 'package:apidash/services/hive_services.dart';

@GenerateMocks([HiveHandler])
void main() {}
Loading