Widget testing, add more tests

This commit is contained in:
Likho 2022-10-17 20:33:09 +02:00
parent 777ca516bf
commit c8ec409efa
8 changed files with 4610 additions and 40 deletions

View file

@ -0,0 +1,66 @@
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
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/node_service.dart';
import 'package:stackwallet/services/wallets.dart';
import 'package:stackwallet/services/wallets_service.dart';
import 'package:stackwallet/widgets/managed_favorite.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 'managed_favorite_test.mocks.dart';
@GenerateMocks([
Wallets,
WalletsService,
BitcoinWallet
], customMocks: [
MockSpec<NodeService>(returnNullOnMissingStub: true),
MockSpec<Manager>(returnNullOnMissingStub: true),
MockSpec<CoinServiceAPI>(returnNullOnMissingStub: true),
// MockSpec<WalletsService>(returnNullOnMissingStub: true),
])
void main() {
testWidgets("Test wallet info row displays correctly", (widgetTester) async {
final wallets = MockWallets();
final CoinServiceAPI wallet = MockBitcoinWallet();
when(wallet.coin).thenAnswer((_) => Coin.bitcoin);
when(wallet.walletName).thenAnswer((_) => "some wallet");
when(wallet.walletId).thenAnswer((_) => "some wallet id");
final manager = Manager(wallet);
when(wallets.getManager("some wallet id"))
.thenAnswer((realInvocation) => manager);
when(manager.isFavorite).thenAnswer((realInvocation) => false);
const managedFavorite = ManagedFavorite(walletId: "some wallet id");
await widgetTester.pumpWidget(
ProviderScope(
overrides: [
walletsChangeNotifierProvider.overrideWithValue(wallets),
],
child: MaterialApp(
theme: ThemeData(
extensions: [
StackColors.fromStackColorTheme(
LightColors(),
),
],
),
home: const Material(
child: managedFavorite,
),
),
),
);
expect(find.byType(ManagedFavorite), findsOneWidget);
});
}

File diff suppressed because it is too large Load diff

View file

@ -27,7 +27,7 @@ import 'table_view_row_test.mocks.dart';
MockSpec<CoinServiceAPI>(returnNullOnMissingStub: true) MockSpec<CoinServiceAPI>(returnNullOnMissingStub: true)
]) ])
void main() { void main() {
testWidgets('Blah blah', (widgetTester) async { testWidgets('Test table view row', (widgetTester) async {
final mockWallet = MockWallets(); final mockWallet = MockWallets();
final CoinServiceAPI wallet = MockBitcoinWallet(); final CoinServiceAPI wallet = MockBitcoinWallet();
when(wallet.coin).thenAnswer((_) => Coin.bitcoin); when(wallet.coin).thenAnswer((_) => Coin.bitcoin);
@ -71,22 +71,6 @@ void main() {
), ),
); );
// await widgetTester.pumpWidget(
// MaterialApp(
// theme: ThemeData(
// extensions: [
// StackColors.fromStackColorTheme(LightColors()),
// ],
// ),
// home: Material(
// child: TableViewRow(cells: [
// for (int j = 1; j <= 5; j++)
// TableViewCell(flex: 16, child: Text("Some Text ${j}"))
// ], expandingChild: CoinWalletsTable(walletIds: walletIds)),
// ),
// ),
// );
expect(find.text("Some Text 1"), findsOneWidget); expect(find.text("Some Text 1"), findsOneWidget);
expect(find.byType(TableViewRow), findsWidgets); expect(find.byType(TableViewRow), findsWidgets);
expect(find.byType(TableViewCell), findsWidgets); expect(find.byType(TableViewCell), findsWidgets);

View file

@ -351,13 +351,12 @@ void main() {
when(wallet.coin).thenAnswer((_) => Coin.firo); when(wallet.coin).thenAnswer((_) => Coin.firo);
when(wallets.getManager("wallet-id")) when(wallets.getManager("wallet id"))
.thenAnswer((realInvocation) => Manager(wallet)); .thenAnswer((realInvocation) => Manager(wallet));
mockingjay mockingjay
.when(() => navigator.push(mockingjay.any( .when(() => navigator.pushNamed("/transactionDetails",
that: mockingjay.isRoute( arguments: Tuple3(tx, Coin.firo, "wallet id")))
whereName: equals("/transactiondetailsview")))))
.thenAnswer((_) async => {}); .thenAnswer((_) async => {});
await tester.pumpWidget( await tester.pumpWidget(
@ -377,7 +376,7 @@ void main() {
), ),
home: mockingjay.MockNavigatorProvider( home: mockingjay.MockNavigatorProvider(
navigator: navigator, navigator: navigator,
child: TransactionCard(transaction: tx, walletId: "wallet-id")), child: TransactionCard(transaction: tx, walletId: "wallet id")),
), ),
), ),
); );
@ -386,11 +385,11 @@ void main() {
await tester.tap(find.byType(GestureDetector)); await tester.tap(find.byType(GestureDetector));
await tester.pump(); await tester.pump();
//
// verify(mockManager.addListener(any)).called(1); // verify(mockManager.addListener(any)).called(1);
// verify(mockLocaleService.addListener(any)).called(1); verify(mockLocaleService.addListener(any)).called(1);
// verify(mockNotesService.addListener(any)).called(1); // verify(mockNotesService.addListener(any)).called(1);
//
// verify(mockNotesService.getNoteFor(txid: "some txid")).called(1); // verify(mockNotesService.getNoteFor(txid: "some txid")).called(1);
// //
// verify(mockManager.fiatCurrency).called(1); // verify(mockManager.fiatCurrency).called(1);
@ -405,10 +404,10 @@ void main() {
// //
// mockingjay // mockingjay
// .verify(() => navigator.push(mockingjay.any( // .verify(() => navigator.push(mockingjay.any(
// that: mockingjay.isRoute( // that:
// whereName: equals("/transactiondetailsview"))))) // mockingjay.isRoute(whereName: equals("/transactionDetails"), whereArguments: equals(Tuple3(item1, item2, item3))))))
// .called(1); // .called(1);
//
// mockingjay.verifyNoMoreInteractions(navigator); // mockingjay.verifyNoMoreInteractions(navigator);
}); });
} }

View file

@ -22,7 +22,7 @@ import 'wallet_card_test.mocks.dart';
@GenerateMocks([Wallets, BitcoinWallet, LocaleService]) @GenerateMocks([Wallets, BitcoinWallet, LocaleService])
void main() { void main() {
testWidgets("When pop popOver redirect", (widgetTester) async { testWidgets("Test button pressed", (widgetTester) async {
final CoinServiceAPI wallet = MockBitcoinWallet(); final CoinServiceAPI wallet = MockBitcoinWallet();
mockito.when(wallet.walletId).thenAnswer((realInvocation) => "wallet id"); mockito.when(wallet.walletId).thenAnswer((realInvocation) => "wallet id");
mockito.when(wallet.coin).thenAnswer((realInvocation) => Coin.bitcoin); mockito.when(wallet.coin).thenAnswer((realInvocation) => Coin.bitcoin);
@ -50,15 +50,6 @@ void main() {
arguments: Tuple2("wallet id", managerProvider))) arguments: Tuple2("wallet id", managerProvider)))
.thenAnswer((_) async => {}); .thenAnswer((_) async => {});
// mockingjay
// .when(() => navigator.push(mockingjay.any(
// that: mockingjay.isRoute(
// whereName: equals("/wallets"),
// whereArguments: equals(Tuple2(
// "wallet id", wallets.getManagerProvider("wallet id")))))))
// .thenAnswer((_) async => {});
// mockingjay.when(() => navigator.pop()).thenAnswer((invocation) {});
await widgetTester.pumpWidget( await widgetTester.pumpWidget(
ProviderScope( ProviderScope(
overrides: [ overrides: [
@ -79,10 +70,9 @@ void main() {
), ),
), ),
); );
//
expect(find.byType(MaterialButton), findsOneWidget); expect(find.byType(MaterialButton), findsOneWidget);
await widgetTester.tap(find.byType(MaterialButton)); await widgetTester.tap(find.byType(MaterialButton));
// });
}); });
testWidgets('test widget loads correctly', (widgetTester) async { testWidgets('test widget loads correctly', (widgetTester) async {

View file

@ -0,0 +1,68 @@
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
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/node_service.dart';
import 'package:stackwallet/services/wallets.dart';
import 'package:stackwallet/services/wallets_service.dart';
import 'package:stackwallet/widgets/wallet_info_row/sub_widgets/wallet_info_row_balance_future.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 'wallet_info_row_balance_future_test.mocks.dart';
@GenerateMocks([
Wallets,
WalletsService,
BitcoinWallet
], customMocks: [
MockSpec<NodeService>(returnNullOnMissingStub: true),
MockSpec<Manager>(returnNullOnMissingStub: true),
MockSpec<CoinServiceAPI>(returnNullOnMissingStub: true),
// MockSpec<WalletsService>(returnNullOnMissingStub: true),
])
void main() {
testWidgets("Test wallet info row balance loads correctly",
(widgetTester) async {
final wallets = MockWallets();
final CoinServiceAPI wallet = MockBitcoinWallet();
when(wallet.coin).thenAnswer((_) => Coin.bitcoin);
when(wallet.walletName).thenAnswer((_) => "some wallet");
when(wallet.walletId).thenAnswer((_) => "some-wallet-id");
final manager = Manager(wallet);
when(wallets.getManagerProvider("some-wallet-id")).thenAnswer(
(realInvocation) => ChangeNotifierProvider((ref) => manager));
const walletInfoRowBalance =
WalletInfoRowBalanceFuture(walletId: "some-wallet-id");
await widgetTester.pumpWidget(
ProviderScope(
overrides: [
walletsChangeNotifierProvider.overrideWithValue(wallets),
],
child: MaterialApp(
theme: ThemeData(
extensions: [
StackColors.fromStackColorTheme(
LightColors(),
),
],
),
home: const Material(
child: walletInfoRowBalance,
),
),
),
);
//
// expect(find.text("some wallet"), findsOneWidget);
expect(find.byType(WalletInfoRowBalanceFuture), findsOneWidget);
});
}

View file

@ -64,5 +64,6 @@ void main() {
); );
expect(find.text("some wallet"), findsOneWidget); expect(find.text("some wallet"), findsOneWidget);
expect(find.byType(WalletInfoRowBalanceFuture), findsOneWidget);
}); });
} }