Add some tests for codePaneVisibleStateProvider

I added some tests for the value of this provider by state injection.
Still working on tests for interaction, value persistence across
rebuilds and proper disposal of this provider.
This commit is contained in:
Sixtus Agbo
2024-03-26 04:08:13 +01:00
parent 15c573d181
commit 3fa3076790

View File

@ -3,14 +3,23 @@ import 'dart:io';
import 'package:apidash/providers/providers.dart'; import 'package:apidash/providers/providers.dart';
import 'package:apidash/screens/dashboard.dart'; import 'package:apidash/screens/dashboard.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/response_pane.dart';
import 'package:apidash/screens/home_page/editor_pane/editor_default.dart';
import 'package:apidash/screens/home_page/editor_pane/editor_pane.dart';
import 'package:apidash/screens/home_page/editor_pane/url_card.dart';
import 'package:apidash/screens/home_page/home_page.dart'; import 'package:apidash/screens/home_page/home_page.dart';
import 'package:apidash/screens/intro_page.dart'; import 'package:apidash/screens/intro_page.dart';
import 'package:apidash/screens/settings_page.dart'; import 'package:apidash/screens/settings_page.dart';
import 'package:apidash/services/hive_services.dart'; import 'package:apidash/services/hive_services.dart';
import 'package:apidash/widgets/response_widgets.dart';
import 'package:apidash/widgets/textfields.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/services.dart'; import 'package:flutter/services.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:http/http.dart' as http;
import 'package:http/testing.dart';
void main() { void main() {
TestWidgetsFlutterBinding.ensureInitialized(); TestWidgetsFlutterBinding.ensureInitialized();
@ -336,4 +345,145 @@ void main() {
expect(isDisposed, true); expect(isDisposed, true);
}); });
}); });
group('Testing codePaneVisibleStateProvider', () {
testWidgets("It should have have an initial value of false",
(tester) async {
// Mock the http client to return a 200 response
final mockClient = MockClient((request) async {
return http.Response('{"countries": "codes"}', 200);
});
await tester.pumpWidget(
ProviderScope(
overrides: [
collectionStateNotifierProvider.overrideWith(
(ref) => CollectionStateNotifier(ref, hiveHandler, mockClient),
)
],
child: const MaterialApp(
home: RequestEditorPane(),
),
),
);
// Verify that the initial value is false
final editorPane = tester.element(find.byType(RequestEditorPane));
final container = ProviderScope.containerOf(editorPane);
expect(container.read(codePaneVisibleStateProvider), false);
});
testWidgets("When state is false ResponsePane should be visible",
(tester) async {
// Mock the http client to return a 200 response
final mockClient = MockClient((request) async {
return http.Response('{"countries": "codes"}', 200);
});
await tester.pumpWidget(
ProviderScope(
overrides: [
collectionStateNotifierProvider.overrideWith(
(ref) => CollectionStateNotifier(ref, hiveHandler, mockClient),
)
],
child: const MaterialApp(
home: 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),
);
const url = 'https://api.foss42.com/country/codes';
await tester.enterText(field, url);
await tester.pump();
// Tap on the "Send" button
Finder sendButton = find.byType(SendButton);
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 {
// Mock the http client to return a 200 response
final mockClient = MockClient((request) async {
return http.Response('{"countries": "codes"}', 200);
});
await tester.pumpWidget(
ProviderScope(
overrides: [
collectionStateNotifierProvider.overrideWith(
(ref) => CollectionStateNotifier(ref, hiveHandler, mockClient),
)
],
child: const 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(URLField),
matching: find.byType(TextFormField),
);
const url = 'https://api.foss42.com/country/codes';
await tester.enterText(field, url);
await tester.pump();
// Tap on the "Send" button
Finder sendButton = find.byType(SendButton);
await tester.tap(sendButton);
await tester.pump();
final editorPane = tester.element(find.byType(RequestEditorPane));
final container = ProviderScope.containerOf(editorPane);
// Change codePaneVisibleStateProvider state to true
container.read(codePaneVisibleStateProvider.notifier).state = true;
await tester.pump();
// Verify that the CodePane is visible
expect(container.read(codePaneVisibleStateProvider), true);
expect(find.byType(CodePane), findsOneWidget);
});
});
} }