fix: broken tests

This commit is contained in:
DenserMeerkat
2024-06-30 02:35:29 +05:30
parent 6e891c6350
commit 0083706d9b
5 changed files with 204 additions and 91 deletions

View File

@@ -0,0 +1,33 @@
import 'dart:ui';
import 'package:flutter_test/flutter_test.dart';
class ScreenSize {
const ScreenSize(this.name, this.width, this.height, this.pixelDensity);
final String name;
final double width, height, pixelDensity;
}
const compactWidthDevice = ScreenSize('compact__width_device', 500, 600, 1);
const mediumWidthDevice = ScreenSize('medium__width_device', 800, 800, 1);
const largeWidthDevice = ScreenSize('large_width_device', 1300, 800, 1);
extension ScreenSizeManager on WidgetTester {
Future<void> setScreenSize(ScreenSize screenSize) async {
return _setScreenSize(
width: screenSize.width,
height: screenSize.height,
pixelDensity: screenSize.pixelDensity,
);
}
Future<void> _setScreenSize({
required double width,
required double height,
required double pixelDensity,
}) async {
final size = Size(width, height);
await binding.setSurfaceSize(size);
view.physicalSize = size;
view.devicePixelRatio = pixelDensity;
}
}

View File

@@ -13,6 +13,7 @@ void main() {
defaultCodeGenLang: CodegenLanguage.curl, defaultCodeGenLang: CodegenLanguage.curl,
saveResponses: true, saveResponses: true,
promptBeforeClosing: true, promptBeforeClosing: true,
activeEnvironmentId: null,
); );
test('Testing toJson()', () { test('Testing toJson()', () {
@@ -26,7 +27,8 @@ void main() {
"defaultUriScheme": "http", "defaultUriScheme": "http",
"defaultCodeGenLang": "curl", "defaultCodeGenLang": "curl",
"saveResponses": true, "saveResponses": true,
"promptBeforeClosing": true "promptBeforeClosing": true,
'activeEnvironmentId': null
}; };
expect(sm.toJson(), expectedResult); expect(sm.toJson(), expectedResult);
}); });
@@ -42,7 +44,8 @@ void main() {
"defaultUriScheme": "http", "defaultUriScheme": "http",
"defaultCodeGenLang": "curl", "defaultCodeGenLang": "curl",
"saveResponses": true, "saveResponses": true,
"promptBeforeClosing": true "promptBeforeClosing": true,
'activeEnvironmentId': null
}; };
expect(SettingsModel.fromJson(input), sm); expect(SettingsModel.fromJson(input), sm);
}); });
@@ -57,6 +60,7 @@ void main() {
defaultCodeGenLang: CodegenLanguage.curl, defaultCodeGenLang: CodegenLanguage.curl,
saveResponses: false, saveResponses: false,
promptBeforeClosing: true, promptBeforeClosing: true,
activeEnvironmentId: null,
); );
expect( expect(
sm.copyWith( sm.copyWith(
@@ -68,7 +72,7 @@ void main() {
test('Testing toString()', () { test('Testing toString()', () {
const expectedResult = const expectedResult =
"{isDark: false, alwaysShowCollectionPaneScrollbar: true, width: 300.0, height: 200.0, dx: 100.0, dy: 150.0, defaultUriScheme: http, defaultCodeGenLang: curl, saveResponses: true, promptBeforeClosing: true}"; "{isDark: false, alwaysShowCollectionPaneScrollbar: true, width: 300.0, height: 200.0, dx: 100.0, dy: 150.0, defaultUriScheme: http, defaultCodeGenLang: curl, saveResponses: true, promptBeforeClosing: true, activeEnvironmentId: null}";
expect(sm.toString(), expectedResult); expect(sm.toString(), expectedResult);
}); });

View File

@@ -1,7 +1,9 @@
import 'dart:io'; import 'dart:io';
import 'package:apidash/providers/providers.dart'; import 'package:apidash/providers/providers.dart';
import 'package:apidash/screens/common_widgets/common_widgets.dart';
import 'package:apidash/screens/dashboard.dart'; import 'package:apidash/screens/dashboard.dart';
import 'package:apidash/screens/envvar/environment_page.dart';
import 'package:apidash/screens/home_page/collection_pane.dart'; import 'package:apidash/screens/home_page/collection_pane.dart';
import 'package:apidash/screens/home_page/editor_pane/details_card/code_pane.dart'; import 'package:apidash/screens/home_page/editor_pane/details_card/code_pane.dart';
import 'package:apidash/screens/home_page/editor_pane/details_card/response_pane.dart'; import 'package:apidash/screens/home_page/editor_pane/details_card/response_pane.dart';
@@ -15,9 +17,12 @@ import 'package:apidash/services/hive_services.dart';
import 'package:apidash/widgets/widgets.dart'; import 'package:apidash/widgets/widgets.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
import 'package:flutter_portal/flutter_portal.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_test/flutter_test.dart'; import 'package:flutter_test/flutter_test.dart';
import 'package:mention_tag_text_field/mention_tag_text_field.dart';
import '../extensions/widget_tester_extensions.dart';
import '../test_consts.dart'; import '../test_consts.dart';
void main() { void main() {
@@ -52,12 +57,13 @@ void main() {
// Verify that the HomePage is displayed initially // Verify that the HomePage is displayed initially
expect(find.byType(HomePage), findsOneWidget); expect(find.byType(HomePage), findsOneWidget);
expect(find.byType(EnvironmentPage), findsNothing);
expect(find.byType(IntroPage), findsNothing); expect(find.byType(IntroPage), findsNothing);
expect(find.byType(SettingsPage), findsNothing); expect(find.byType(SettingsPage), findsNothing);
}); });
testWidgets( testWidgets(
"Dashboard should display IntroPage when navRailIndexStateProvider is 1", "Dashboard should display EnvironmentPage when navRailIndexStateProvider is 1",
(WidgetTester tester) async { (WidgetTester tester) async {
await tester.pumpWidget( await tester.pumpWidget(
ProviderScope( ProviderScope(
@@ -70,30 +76,57 @@ void main() {
), ),
); );
// Verify that the IntroPage is displayed // Verify that the EnvironmentPage is displayed
expect(find.byType(IntroPage), findsOneWidget);
expect(find.byType(HomePage), findsNothing); expect(find.byType(HomePage), findsNothing);
expect(find.byType(EnvironmentPage), findsOneWidget);
expect(find.byType(IntroPage), findsNothing);
expect(find.byType(SettingsPage), findsNothing); expect(find.byType(SettingsPage), findsNothing);
}); });
testWidgets( testWidgets(
"Dashboard should display SettingsPage when navRailIndexStateProvider is 2", "Dashboard should display IntroPage when navRailIndexStateProvider is 2",
(WidgetTester tester) async { (WidgetTester tester) async {
await tester.pumpWidget( await tester.pumpWidget(
ProviderScope( ProviderScope(
overrides: [ overrides: [
navRailIndexStateProvider.overrideWith((ref) => 2), navRailIndexStateProvider.overrideWith((ref) => 2),
], ],
child: const MaterialApp( child: const Portal(
home: Dashboard(), child: MaterialApp(
home: Dashboard(),
),
),
),
);
// Verify that the IntroPage is displayed
expect(find.byType(HomePage), findsNothing);
expect(find.byType(EnvironmentPage), findsNothing);
expect(find.byType(IntroPage), findsOneWidget);
expect(find.byType(SettingsPage), findsNothing);
});
testWidgets(
"Dashboard should display SettingsPage when navRailIndexStateProvider is 3",
(WidgetTester tester) async {
await tester.pumpWidget(
ProviderScope(
overrides: [
navRailIndexStateProvider.overrideWith((ref) => 3),
],
child: const Portal(
child: MaterialApp(
home: Dashboard(),
),
), ),
), ),
); );
// Verify that the SettingsPage is displayed // Verify that the SettingsPage is displayed
expect(find.byType(SettingsPage), findsOneWidget);
expect(find.byType(IntroPage), findsNothing);
expect(find.byType(HomePage), findsNothing); expect(find.byType(HomePage), findsNothing);
expect(find.byType(EnvironmentPage), findsNothing);
expect(find.byType(IntroPage), findsNothing);
expect(find.byType(SettingsPage), findsOneWidget);
}); });
testWidgets( testWidgets(
@@ -101,8 +134,10 @@ void main() {
(tester) async { (tester) async {
await tester.pumpWidget( await tester.pumpWidget(
const ProviderScope( const ProviderScope(
child: MaterialApp( child: Portal(
home: Dashboard(), child: MaterialApp(
home: Dashboard(),
),
), ),
), ),
); );
@@ -114,7 +149,7 @@ void main() {
// Verify that the navRailIndexStateProvider is updated // Verify that the navRailIndexStateProvider is updated
final dashboard = tester.element(find.byType(Dashboard)); final dashboard = tester.element(find.byType(Dashboard));
final container = ProviderScope.containerOf(dashboard); final container = ProviderScope.containerOf(dashboard);
expect(container.read(navRailIndexStateProvider), 1); expect(container.read(navRailIndexStateProvider), 2);
}); });
testWidgets( testWidgets(
@@ -123,21 +158,25 @@ void main() {
// Pump the initial widget tree // Pump the initial widget tree
await tester.pumpWidget( await tester.pumpWidget(
const ProviderScope( const ProviderScope(
child: MaterialApp( child: Portal(
home: Dashboard(), child: MaterialApp(
home: Dashboard(),
),
), ),
), ),
); );
// Tap on the Settings icon to change the index to 2 // Tap on the Settings icon to change the index to 3
await tester.tap(find.byIcon(Icons.settings_outlined)); await tester.tap(find.byIcon(Icons.settings_outlined));
await tester.pump(); await tester.pump();
// Rebuild the widget tree with the same ProviderScope // Rebuild the widget tree with the same ProviderScope
await tester.pumpWidget( await tester.pumpWidget(
const ProviderScope( const ProviderScope(
child: MaterialApp( child: Portal(
home: Dashboard(), child: MaterialApp(
home: Dashboard(),
),
), ),
), ),
); );
@@ -145,12 +184,13 @@ void main() {
// Verify that the navRailIndexStateProvider still has the updated value // Verify that the navRailIndexStateProvider still has the updated value
final dashboard = tester.element(find.byType(Dashboard)); final dashboard = tester.element(find.byType(Dashboard));
final container = ProviderScope.containerOf(dashboard); final container = ProviderScope.containerOf(dashboard);
expect(container.read(navRailIndexStateProvider), 2); expect(container.read(navRailIndexStateProvider), 3);
// Verify that the SettingsPage is still displayed after the rebuild // Verify that the SettingsPage is still displayed after the rebuild
expect(find.byType(SettingsPage), findsOneWidget); expect(find.byType(SettingsPage), findsOneWidget);
expect(find.byType(IntroPage), findsNothing); expect(find.byType(IntroPage), findsNothing);
expect(find.byType(HomePage), findsNothing); expect(find.byType(HomePage), findsNothing);
expect(find.byType(EnvironmentPage), findsNothing);
}); });
testWidgets( testWidgets(
@@ -158,8 +198,10 @@ void main() {
(tester) async { (tester) async {
await tester.pumpWidget( await tester.pumpWidget(
const ProviderScope( const ProviderScope(
child: MaterialApp( child: Portal(
home: Dashboard(), child: MaterialApp(
home: Dashboard(),
),
), ),
), ),
); );
@@ -168,17 +210,26 @@ void main() {
final dashboard = tester.element(find.byType(Dashboard)); final dashboard = tester.element(find.byType(Dashboard));
final container = ProviderScope.containerOf(dashboard); final container = ProviderScope.containerOf(dashboard);
// Go to IntroPage // Go to EnvironmentPage
container.read(navRailIndexStateProvider.notifier).state = 1; container.read(navRailIndexStateProvider.notifier).state = 1;
await tester.pump(); await tester.pump();
// Verify that the EnvironmentPage is displayed
expect(find.byType(EnvironmentPage), findsOneWidget);
// Verify that the selected icon is the filled version (selectedIcon)
expect(find.byIcon(Icons.computer_rounded), findsOneWidget);
// Go to IntroPage
container.read(navRailIndexStateProvider.notifier).state = 2;
await tester.pump();
// Verify that the IntroPage is displayed // Verify that the IntroPage is displayed
expect(find.byType(IntroPage), findsOneWidget); expect(find.byType(IntroPage), findsOneWidget);
// Verify that the selected icon is the filled version (selectedIcon) // Verify that the selected icon is the filled version (selectedIcon)
expect(find.byIcon(Icons.help), findsOneWidget); expect(find.byIcon(Icons.help), findsOneWidget);
// Go to SettingsPage // Go to SettingsPage
container.read(navRailIndexStateProvider.notifier).state = 2; container.read(navRailIndexStateProvider.notifier).state = 3;
await tester.pump(); await tester.pump();
// Verify that the SettingsPage is displayed // Verify that the SettingsPage is displayed
@@ -192,8 +243,10 @@ void main() {
(tester) async { (tester) async {
await tester.pumpWidget( await tester.pumpWidget(
const ProviderScope( const ProviderScope(
child: MaterialApp( child: Portal(
home: Dashboard(), child: MaterialApp(
home: Dashboard(),
),
), ),
), ),
); );
@@ -364,53 +417,14 @@ void main() {
testWidgets("When state is false ResponsePane should be visible", testWidgets("When state is false ResponsePane should be visible",
(tester) async { (tester) async {
await tester.setScreenSize(largeWidthDevice);
await tester.pumpWidget( await tester.pumpWidget(
const ProviderScope( const ProviderScope(
child: MaterialApp( child: Portal(
home: RequestEditorPane(), child: MaterialApp(
), home: Material(
), child: RequestEditorPane(),
); ),
expect(find.byType(RequestEditorDefault), findsOneWidget);
// Tap on the "Plus New" button
Finder plusNewButton = find.descendant(
of: find.byType(RequestEditorDefault),
matching: find.byType(ElevatedButton),
);
await tester.tap(plusNewButton);
await tester.pump();
// Verify that NotSentWidget is visible
expect(find.byType(NotSentWidget), findsOneWidget);
// Add some data in URLTextField
Finder field = find.descendant(
of: find.byType(URLField),
matching: find.byType(TextFormField),
);
await tester.enterText(field, kTestUrl);
await tester.pump();
// Tap on the "Send" button
Finder sendButton = find.byType(SendRequestButton);
await tester.tap(sendButton);
await tester.pump();
final editorPane = tester.element(find.byType(RequestEditorPane));
final container = ProviderScope.containerOf(editorPane);
expect(container.read(codePaneVisibleStateProvider), false);
expect(find.byType(ResponsePane), findsOneWidget);
});
testWidgets("When state is true CodePane should be visible",
(tester) async {
await tester.pumpWidget(
const ProviderScope(
child: MaterialApp(
home: Scaffold(
body: RequestEditorPane(),
), ),
), ),
), ),
@@ -431,8 +445,55 @@ void main() {
// Add some data in URLTextField // Add some data in URLTextField
Finder field = find.descendant( Finder field = find.descendant(
of: find.byType(URLField), of: find.byType(EnvURLField),
matching: find.byType(TextFormField), matching: find.byType(MentionTagTextField),
);
await tester.enterText(field, kTestUrl);
await tester.pump();
// Tap on the "Send" button
Finder sendButton = find.byType(SendRequestButton);
await tester.tap(sendButton);
await tester.pump();
final editorPane = tester.element(find.byType(RequestEditorPane));
final container = ProviderScope.containerOf(editorPane);
expect(container.read(codePaneVisibleStateProvider), false);
expect(find.byType(ResponsePane), findsOneWidget);
});
testWidgets("When state is true CodePane should be visible",
(tester) async {
await tester.setScreenSize(largeWidthDevice);
await tester.pumpWidget(
const ProviderScope(
child: Portal(
child: MaterialApp(
home: Scaffold(
body: RequestEditorPane(),
),
),
),
),
);
expect(find.byType(RequestEditorDefault), findsOneWidget);
// Tap on the "Plus New" button
Finder plusNewButton = find.descendant(
of: find.byType(RequestEditorDefault),
matching: find.byType(ElevatedButton),
);
await tester.tap(plusNewButton);
await tester.pump();
// Verify that NotSentWidget is visible
expect(find.byType(NotSentWidget), findsOneWidget);
// Add some data in URLTextField
Finder field = find.descendant(
of: find.byType(EnvURLField),
matching: find.byType(MentionTagTextField),
); );
await tester.enterText(field, kTestUrl); await tester.enterText(field, kTestUrl);
await tester.pump(); await tester.pump();
@@ -454,11 +515,14 @@ void main() {
}); });
testWidgets("Hide/View Code button toggles the state", (tester) async { testWidgets("Hide/View Code button toggles the state", (tester) async {
await tester.setScreenSize(largeWidthDevice);
await tester.pumpWidget( await tester.pumpWidget(
const ProviderScope( const ProviderScope(
child: MaterialApp( child: Portal(
home: Scaffold( child: MaterialApp(
body: RequestEditorPane(), home: Scaffold(
body: RequestEditorPane(),
),
), ),
), ),
), ),
@@ -479,8 +543,8 @@ void main() {
// Add some data in URLTextField // Add some data in URLTextField
Finder field = find.descendant( Finder field = find.descendant(
of: find.byType(URLField), of: find.byType(EnvURLField),
matching: find.byType(TextFormField), matching: find.byType(MentionTagTextField),
); );
await tester.enterText(field, kTestUrl); await tester.enterText(field, kTestUrl);
await tester.pump(); await tester.pump();
@@ -511,11 +575,14 @@ void main() {
}); });
testWidgets("That state persists across widget rebuilds", (tester) async { testWidgets("That state persists across widget rebuilds", (tester) async {
await tester.setScreenSize(largeWidthDevice);
await tester.pumpWidget( await tester.pumpWidget(
const ProviderScope( const ProviderScope(
child: MaterialApp( child: Portal(
home: Scaffold( child: MaterialApp(
body: RequestEditorPane(), home: Scaffold(
body: RequestEditorPane(),
),
), ),
), ),
), ),
@@ -536,8 +603,8 @@ void main() {
// Add some data in URLTextField // Add some data in URLTextField
Finder field = find.descendant( Finder field = find.descendant(
of: find.byType(URLField), of: find.byType(EnvURLField),
matching: find.byType(TextFormField), matching: find.byType(MentionTagTextField),
); );
await tester.enterText(field, kTestUrl); await tester.enterText(field, kTestUrl);
await tester.pump(); await tester.pump();
@@ -562,9 +629,11 @@ void main() {
// Rebuild the widget tree // Rebuild the widget tree
await tester.pumpWidget( await tester.pumpWidget(
const ProviderScope( const ProviderScope(
child: MaterialApp( child: Portal(
home: Scaffold( child: MaterialApp(
body: RequestEditorPane(), home: Scaffold(
body: RequestEditorPane(),
),
), ),
), ),
), ),

View File

@@ -2,6 +2,7 @@ import 'dart:typed_data';
import 'package:apidash/screens/common_widgets/sidebar_save_button.dart'; import 'package:apidash/screens/common_widgets/sidebar_save_button.dart';
import 'package:apidash/widgets/widgets.dart'; import 'package:apidash/widgets/widgets.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_test/flutter_test.dart'; import 'package:flutter_test/flutter_test.dart';
import 'package:apidash/consts.dart'; import 'package:apidash/consts.dart';
import '../test_consts.dart'; import '../test_consts.dart';
@@ -175,11 +176,13 @@ void main() {
testWidgets('Testing for Save button', (tester) async { testWidgets('Testing for Save button', (tester) async {
await tester.pumpWidget( await tester.pumpWidget(
MaterialApp( ProviderScope(
title: 'Save button', child: MaterialApp(
theme: kThemeDataLight, title: 'Save button',
home: const Scaffold( theme: kThemeDataLight,
body: SaveButton(), home: const Scaffold(
body: SaveButton(),
),
), ),
), ),
); );

View File

@@ -1,10 +1,12 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart'; import 'package:flutter_test/flutter_test.dart';
import 'package:apidash/widgets/request_widgets.dart'; import 'package:apidash/widgets/request_widgets.dart';
import '../extensions/widget_tester_extensions.dart';
import '../test_consts.dart'; import '../test_consts.dart';
void main() { void main() {
testWidgets('Testing Request Pane for 1st tab', (tester) async { testWidgets('Testing Request Pane for 1st tab', (tester) async {
await tester.setScreenSize(largeWidthDevice);
await tester.pumpWidget( await tester.pumpWidget(
MaterialApp( MaterialApp(
title: 'Request Pane', title: 'Request Pane',
@@ -34,6 +36,7 @@ void main() {
expect(find.byIcon(Icons.code_rounded), findsNothing); expect(find.byIcon(Icons.code_rounded), findsNothing);
}); });
testWidgets('Testing Request Pane for 2nd tab', (tester) async { testWidgets('Testing Request Pane for 2nd tab', (tester) async {
await tester.setScreenSize(largeWidthDevice);
await tester.pumpWidget( await tester.pumpWidget(
MaterialApp( MaterialApp(
title: 'Request Pane', title: 'Request Pane',
@@ -64,6 +67,7 @@ void main() {
expect(find.byIcon(Icons.code_rounded), findsNothing); expect(find.byIcon(Icons.code_rounded), findsNothing);
}); });
testWidgets('Testing Request Pane for 3rd tab', (tester) async { testWidgets('Testing Request Pane for 3rd tab', (tester) async {
await tester.setScreenSize(largeWidthDevice);
await tester.pumpWidget( await tester.pumpWidget(
MaterialApp( MaterialApp(
title: 'Request Pane', title: 'Request Pane',