stack_wallet/test/widget_tests/node_options_sheet_test.dart

243 lines
7.4 KiB
Dart
Raw Normal View History

import 'package:flutter/material.dart';
2023-05-08 17:59:00 +00:00
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_svg/svg.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
2023-05-12 20:02:04 +00:00
import 'package:stackwallet/models/isar/stack_theme.dart';
import 'package:stackwallet/models/node_model.dart';
import 'package:stackwallet/providers/providers.dart';
2023-05-08 17:59:00 +00:00
import 'package:stackwallet/services/node_service.dart';
2023-09-08 16:04:56 +00:00
import 'package:stackwallet/services/tor_service.dart';
import 'package:stackwallet/services/wallets.dart';
import 'package:stackwallet/themes/stack_colors.dart';
import 'package:stackwallet/utilities/prefs.dart';
import 'package:stackwallet/wallets/crypto_currency/crypto_currency.dart';
import 'package:stackwallet/widgets/node_options_sheet.dart';
2023-05-12 20:02:04 +00:00
import '../sample_data/theme_json.dart';
import 'node_options_sheet_test.mocks.dart';
2023-09-08 16:04:56 +00:00
@GenerateMocks([Wallets, Prefs, NodeService, TorService])
void main() {
testWidgets("Load Node Options widget", (tester) async {
final mockWallets = MockWallets();
final mockPrefs = MockPrefs();
final mockNodeService = MockNodeService();
when(mockNodeService.getNodeById(id: "node id")).thenAnswer(
(realInvocation) => NodeModel(
host: "127.0.0.1",
port: 2000,
name: "Some other name",
id: "node id",
useSSL: true,
enabled: true,
coinName: "Bitcoin",
isFailover: false,
isDown: false));
when(mockNodeService.getPrimaryNodeFor(
currency: Bitcoin(CryptoCurrencyNetwork.main)))
.thenAnswer((realInvocation) => NodeModel(
host: "127.0.0.1",
port: 2000,
name: "Some other name",
id: "node id",
useSSL: true,
enabled: true,
coinName: "Bitcoin",
isFailover: false,
isDown: false));
await tester.pumpWidget(
ProviderScope(
overrides: [
pWallets.overrideWithValue(mockWallets),
prefsChangeNotifierProvider.overrideWithValue(mockPrefs),
nodeServiceChangeNotifierProvider.overrideWithValue(mockNodeService)
],
child: MaterialApp(
theme: ThemeData(
extensions: [
StackColors.fromStackColorTheme(
2023-05-08 17:59:00 +00:00
StackTheme.fromJson(
2023-05-12 20:02:04 +00:00
json: lightThemeJsonMap,
2023-05-08 17:59:00 +00:00
),
),
],
),
home: NodeOptionsSheet(
nodeId: "node id",
coin: Bitcoin(CryptoCurrencyNetwork.main),
popBackToRoute: ""),
),
),
);
await tester.pumpAndSettle();
expect(find.text("Node options"), findsOneWidget);
expect(find.text("Some other name"), findsOneWidget);
expect(find.text("Connected"), findsOneWidget);
expect(find.byType(SvgPicture), findsNWidgets(2));
expect(find.text("Details"), findsOneWidget);
expect(find.text("Connect"), findsOneWidget);
verify(mockNodeService.getPrimaryNodeFor(
currency: Bitcoin(CryptoCurrencyNetwork.main)))
.called(1);
verify(mockNodeService.getNodeById(id: "node id")).called(1);
verify(mockNodeService.addListener(any)).called(1);
verifyNoMoreInteractions(mockNodeService);
});
testWidgets("Details tap", (tester) async {
2024-01-26 22:55:26 +00:00
final navigatorKey = GlobalKey<NavigatorState>();
final mockWallets = MockWallets();
final mockPrefs = MockPrefs();
final mockNodeService = MockNodeService();
2024-01-26 22:55:26 +00:00
final mockTorService = MockTorService();
when(mockNodeService.getNodeById(id: "node id")).thenAnswer(
2024-01-26 22:55:26 +00:00
(_) => NodeModel(
host: "127.0.0.1",
port: 2000,
name: "Stack Default",
id: "node id",
useSSL: true,
enabled: true,
coinName: "Bitcoin",
isFailover: false,
isDown: false,
),
);
when(mockNodeService.getPrimaryNodeFor(
currency: Bitcoin(CryptoCurrencyNetwork.main)))
.thenAnswer(
2024-01-26 22:55:26 +00:00
(_) => NodeModel(
host: "127.0.0.1",
port: 2000,
name: "Stack Default",
id: "some node id",
useSSL: true,
enabled: true,
coinName: "Bitcoin",
isFailover: false,
isDown: false,
),
);
await tester.pumpWidget(
ProviderScope(
overrides: [
pWallets.overrideWithValue(mockWallets),
prefsChangeNotifierProvider.overrideWithValue(mockPrefs),
2024-01-26 22:55:26 +00:00
nodeServiceChangeNotifierProvider.overrideWithValue(mockNodeService),
pTorService.overrideWithValue(mockTorService),
],
child: MaterialApp(
2024-01-26 22:55:26 +00:00
navigatorKey: navigatorKey,
theme: ThemeData(
extensions: [
2023-05-08 17:59:00 +00:00
StackColors.fromStackColorTheme(
StackTheme.fromJson(
2023-05-12 20:02:04 +00:00
json: lightThemeJsonMap,
2023-05-08 17:59:00 +00:00
),
),
],
),
2024-01-26 22:55:26 +00:00
onGenerateRoute: (settings) {
if (settings.name == '/nodeDetails') {
return MaterialPageRoute(builder: (_) => Scaffold());
}
return null;
},
home: NodeOptionsSheet(
2024-01-26 22:55:26 +00:00
nodeId: "node id",
coin: Bitcoin(CryptoCurrencyNetwork.main),
2024-01-26 22:55:26 +00:00
popBackToRoute: "coinNodes",
),
),
),
);
await tester.tap(find.text("Details"));
await tester.pumpAndSettle();
2024-05-27 23:56:22 +00:00
final currentRoute = navigatorKey.currentState?.overlay?.context;
2024-01-26 22:55:26 +00:00
expect(currentRoute, isNotNull);
});
testWidgets("Connect tap", (tester) async {
final mockWallets = MockWallets();
final mockPrefs = MockPrefs();
final mockNodeService = MockNodeService();
2023-09-08 16:04:56 +00:00
final mockTorService = MockTorService();
when(mockNodeService.getNodeById(id: "node id")).thenAnswer(
2024-01-26 22:57:16 +00:00
(_) => NodeModel(
host: "127.0.0.1",
port: 2000,
name: "Stack Default",
id: "node id",
useSSL: true,
enabled: true,
coinName: "Bitcoin",
isFailover: false,
isDown: false,
),
);
when(mockNodeService.getPrimaryNodeFor(
currency: Bitcoin(CryptoCurrencyNetwork.main)))
.thenAnswer(
2024-01-26 22:57:16 +00:00
(_) => NodeModel(
host: "127.0.0.1",
port: 2000,
name: "Some other node name",
id: "some node id",
useSSL: true,
enabled: true,
coinName: "Bitcoin",
isFailover: false,
isDown: false,
),
);
await tester.pumpWidget(
ProviderScope(
overrides: [
pWallets.overrideWithValue(mockWallets),
prefsChangeNotifierProvider.overrideWithValue(mockPrefs),
2023-09-08 16:04:56 +00:00
nodeServiceChangeNotifierProvider.overrideWithValue(mockNodeService),
pTorService.overrideWithValue(mockTorService),
],
child: MaterialApp(
theme: ThemeData(
extensions: [
StackColors.fromStackColorTheme(
2023-05-08 17:59:00 +00:00
StackTheme.fromJson(
2023-05-12 20:02:04 +00:00
json: lightThemeJsonMap,
2023-05-08 17:59:00 +00:00
),
),
],
),
home: NodeOptionsSheet(
2024-01-26 22:57:16 +00:00
nodeId: "node id",
coin: Bitcoin(CryptoCurrencyNetwork.main),
2024-01-26 22:57:16 +00:00
popBackToRoute: "",
),
),
),
);
await tester.pumpAndSettle();
expect(find.text("Node options"), findsOneWidget);
expect(find.text("Disconnected"), findsOneWidget);
await tester.tap(find.text("Connect"));
await tester.pumpAndSettle();
});
}