stack_wallet/test/widget_tests/wallet_card_test.dart

126 lines
4.3 KiB
Dart
Raw Normal View History

2022-10-13 18:40:19 +00:00
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_test/flutter_test.dart';
2022-10-17 16:17:58 +00:00
import 'package:mockingjay/mockingjay.dart' as mockingjay;
2022-10-13 18:40:19 +00:00
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart' as mockito;
2022-10-13 18:40:19 +00:00
import 'package:stackwallet/providers/providers.dart';
import 'package:stackwallet/services/coins/bitcoin/bitcoin_wallet.dart';
import 'package:stackwallet/services/coins/coin_service.dart';
import 'package:stackwallet/services/coins/manager.dart';
import 'package:stackwallet/services/locale_service.dart';
2022-10-13 18:40:19 +00:00
import 'package:stackwallet/services/wallets.dart';
import 'package:stackwallet/utilities/enums/coin_enum.dart';
import 'package:stackwallet/utilities/theme/light_colors.dart';
import 'package:stackwallet/utilities/theme/stack_colors.dart';
import 'package:stackwallet/widgets/wallet_card.dart';
2022-10-17 15:50:38 +00:00
import 'package:tuple/tuple.dart';
2022-10-13 18:40:19 +00:00
import 'wallet_card_test.mocks.dart';
// class MockNavigatorObserver extends Mock implements NavigatorObserver {}
2022-10-13 18:40:19 +00:00
@GenerateMocks([Wallets, BitcoinWallet, LocaleService])
2022-10-13 18:40:19 +00:00
void main() {
2022-10-17 18:33:09 +00:00
testWidgets("Test button pressed", (widgetTester) async {
2022-10-17 15:50:38 +00:00
final CoinServiceAPI wallet = MockBitcoinWallet();
mockito.when(wallet.walletId).thenAnswer((realInvocation) => "wallet id");
mockito.when(wallet.coin).thenAnswer((realInvocation) => Coin.bitcoin);
mockito
.when(wallet.walletName)
.thenAnswer((realInvocation) => "wallet name");
2022-10-17 15:50:38 +00:00
final wallets = MockWallets();
final locale = MockLocaleService();
final manager = Manager(wallet);
2022-10-17 16:17:58 +00:00
final managerProvider = ChangeNotifierProvider((ref) => manager);
2022-10-13 18:40:19 +00:00
2022-10-17 16:17:58 +00:00
mockito
.when(wallets.getManagerProvider("wallet id"))
.thenAnswer((realInvocation) => managerProvider);
2022-10-17 15:50:38 +00:00
mockito.when(locale.locale).thenAnswer((_) => "en_US");
2022-10-13 18:40:19 +00:00
2022-10-17 16:17:58 +00:00
mockito
.when(wallets.getManagerProvider("wallet id"))
.thenAnswer((realInvocation) => managerProvider);
2023-01-02 18:43:26 +00:00
mockito
.when(wallets.getManager("wallet id"))
.thenAnswer((realInvocation) => manager);
2022-10-17 15:50:38 +00:00
final navigator = mockingjay.MockNavigator();
mockingjay
2022-10-17 16:17:58 +00:00
.when(() => navigator.pushNamed("/wallet",
arguments: Tuple2("wallet id", managerProvider)))
.thenAnswer((_) async => {});
2022-10-17 15:50:38 +00:00
await widgetTester.pumpWidget(
ProviderScope(
overrides: [
walletsChangeNotifierProvider.overrideWithValue(wallets),
localeServiceChangeNotifierProvider.overrideWithValue(locale),
],
child: MaterialApp(
theme: ThemeData(
extensions: [
StackColors.fromStackColorTheme(LightColors()),
],
2022-10-13 18:40:19 +00:00
),
2022-10-17 15:50:38 +00:00
home: mockingjay.MockNavigatorProvider(
navigator: navigator,
child: const WalletSheetCard(
walletId: "wallet id",
)),
2022-10-13 18:40:19 +00:00
),
2022-10-17 15:50:38 +00:00
),
);
2022-10-17 18:33:09 +00:00
2023-01-20 16:22:18 +00:00
await widgetTester.pumpAndSettle();
2022-10-17 15:50:38 +00:00
expect(find.byType(MaterialButton), findsOneWidget);
await widgetTester.tap(find.byType(MaterialButton));
2023-01-20 16:22:18 +00:00
await widgetTester.pumpAndSettle();
2022-10-13 18:40:19 +00:00
});
testWidgets('test widget loads correctly', (widgetTester) async {
final CoinServiceAPI wallet = MockBitcoinWallet();
mockito.when(wallet.walletId).thenAnswer((realInvocation) => "wallet id");
mockito.when(wallet.coin).thenAnswer((realInvocation) => Coin.bitcoin);
mockito
.when(wallet.walletName)
.thenAnswer((realInvocation) => "wallet name");
2022-10-13 18:40:19 +00:00
final wallets = MockWallets();
final manager = Manager(wallet);
mockito.when(wallets.getManagerProvider("wallet id")).thenAnswer(
2022-10-13 18:40:19 +00:00
(realInvocation) => ChangeNotifierProvider((ref) => manager));
const walletSheetCard = WalletSheetCard(
walletId: "wallet id",
);
await widgetTester.pumpWidget(
ProviderScope(
overrides: [
walletsChangeNotifierProvider.overrideWithValue(wallets),
],
child: MaterialApp(
theme: ThemeData(
extensions: [
StackColors.fromStackColorTheme(LightColors()),
],
),
home: const Material(
child: walletSheetCard,
),
),
),
);
2023-01-20 16:22:18 +00:00
await widgetTester.pumpAndSettle();
2022-10-13 18:40:19 +00:00
expect(find.byWidget(walletSheetCard), findsOneWidget);
});
}