stack_wallet/test/widget_tests/node_card_test.dart

225 lines
7.2 KiB
Dart
Raw Normal View History

import 'package:flutter/material.dart';
2022-11-02 23:47:24 +00:00
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_svg/svg.dart';
import 'package:flutter_test/flutter_test.dart';
2022-08-26 08:11:35 +00:00
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';
2022-11-02 23:47:24 +00:00
import 'package:stackwallet/services/node_service.dart';
import 'package:stackwallet/themes/stack_colors.dart';
import 'package:stackwallet/utilities/enums/coin_enum.dart';
2022-11-02 23:47:24 +00:00
import 'package:stackwallet/utilities/util.dart';
import 'package:stackwallet/widgets/node_card.dart';
import 'package:stackwallet/widgets/node_options_sheet.dart';
2022-08-26 08:11:35 +00:00
2023-05-12 20:02:04 +00:00
import '../sample_data/theme_json.dart';
import 'node_card_test.mocks.dart';
2022-08-26 08:11:35 +00:00
@GenerateMocks([NodeService])
2022-08-26 08:11:35 +00:00
void main() {
testWidgets("NodeCard builds inactive node correctly", (tester) async {
final nodeService = MockNodeService();
when(nodeService.getPrimaryNodeFor(coin: Coin.bitcoin)).thenAnswer(
(realInvocation) => 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(nodeService.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));
await tester.pumpWidget(
ProviderScope(
overrides: [
nodeServiceChangeNotifierProvider.overrideWithValue(nodeService),
],
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,
applicationThemesDirectoryPath: "test",
2023-05-08 17:59:00 +00:00
),
),
],
),
home: const NodeCard(
nodeId: "node id", coin: Coin.bitcoin, popBackToRoute: ""),
),
),
);
await tester.pumpAndSettle();
expect(find.text("some other name"), findsOneWidget);
expect(find.text("Disconnected"), findsOneWidget);
expect(find.byType(SvgPicture), findsWidgets);
verify(nodeService.getPrimaryNodeFor(coin: Coin.bitcoin)).called(1);
verify(nodeService.getNodeById(id: "node id")).called(1);
verify(nodeService.addListener(any)).called(1);
verifyNoMoreInteractions(nodeService);
});
testWidgets("NodeCard builds active node correctly", (tester) async {
final nodeService = MockNodeService();
when(nodeService.getPrimaryNodeFor(coin: Coin.bitcoin)).thenAnswer(
(realInvocation) => NodeModel(
host: "127.0.0.1",
port: 2000,
name: "Some other node name",
id: "node id",
useSSL: true,
enabled: true,
coinName: "Bitcoin",
isFailover: false,
isDown: false));
when(nodeService.getNodeById(id: "node id")).thenAnswer((realInvocation) =>
NodeModel(
host: "127.0.0.1",
port: 2000,
name: "Some other node name",
id: "node id",
useSSL: true,
enabled: true,
coinName: "Bitcoin",
isFailover: false,
isDown: false));
await tester.pumpWidget(
ProviderScope(
overrides: [
nodeServiceChangeNotifierProvider.overrideWithValue(nodeService),
],
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,
applicationThemesDirectoryPath: "test",
2023-05-08 17:59:00 +00:00
),
),
],
),
home: const NodeCard(
nodeId: "node id", coin: Coin.bitcoin, popBackToRoute: ""),
),
),
);
await tester.pumpAndSettle();
expect(find.text("Some other node name"), findsOneWidget);
expect(find.text("Connected"), findsOneWidget);
expect(find.byType(Text), findsNWidgets(2));
expect(find.byType(SvgPicture), findsWidgets);
verify(nodeService.getPrimaryNodeFor(coin: Coin.bitcoin)).called(1);
verify(nodeService.getNodeById(id: "node id")).called(1);
verify(nodeService.addListener(any)).called(1);
verifyNoMoreInteractions(nodeService);
});
testWidgets("tap to open context menu on default node", (tester) async {
final nodeService = MockNodeService();
when(nodeService.getPrimaryNodeFor(coin: Coin.bitcoin)).thenAnswer(
(realInvocation) => 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(nodeService.getNodeById(id: "node id")).thenAnswer((realInvocation) =>
NodeModel(
host: "127.0.0.1",
port: 2000,
name: "Stack Default",
id: "node id",
useSSL: true,
enabled: true,
coinName: "Bitcoin",
isFailover: false,
isDown: false));
await tester.pumpWidget(
ProviderScope(
overrides: [
nodeServiceChangeNotifierProvider.overrideWithValue(nodeService),
],
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,
applicationThemesDirectoryPath: "test",
2023-05-08 17:59:00 +00:00
),
),
],
),
home: const NodeCard(
nodeId: "node id", coin: Coin.bitcoin, popBackToRoute: ""),
),
),
);
await tester.pumpAndSettle();
expect(find.text("Stack Default"), findsOneWidget);
expect(find.text("Connected"), findsOneWidget);
expect(find.byType(Text), findsNWidgets(2));
expect(find.byType(SvgPicture), findsNWidgets(2));
await tester.tap(find.byType(NodeCard));
await tester.pumpAndSettle();
2022-11-02 23:47:24 +00:00
if (Util.isDesktop) {
expect(find.text("Connect"), findsNothing);
expect(find.text("Details"), findsNothing);
verify(nodeService.getPrimaryNodeFor(coin: Coin.bitcoin)).called(1);
verify(nodeService.getNodeById(id: "node id")).called(1);
} else {
expect(find.text("Connect"), findsOneWidget);
expect(find.text("Details"), findsOneWidget);
expect(find.byType(NodeOptionsSheet), findsOneWidget);
expect(find.byType(Text), findsNWidgets(7));
verify(nodeService.getPrimaryNodeFor(coin: Coin.bitcoin)).called(2);
verify(nodeService.getNodeById(id: "node id")).called(2);
}
verify(nodeService.addListener(any)).called(1);
verifyNoMoreInteractions(nodeService);
});
2022-08-26 08:11:35 +00:00
}