WIP: Widget testing

This commit is contained in:
Likho 2022-10-17 17:50:38 +02:00
parent 145da3cc3c
commit ec927b6b8f
5 changed files with 418 additions and 149 deletions

View file

@ -0,0 +1,26 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:stackwallet/utilities/theme/light_colors.dart';
import 'package:stackwallet/utilities/theme/stack_colors.dart';
import 'package:stackwallet/widgets/table_view/table_view_cell.dart';
void main() {
testWidgets("Widget build correctly", (widgetTester) async {
const tableViewCell = TableViewCell(flex: 16, child: Text("data"));
await widgetTester.pumpWidget(
MaterialApp(
theme: ThemeData(
extensions: [
StackColors.fromStackColorTheme(LightColors()),
],
),
home: const Material(
child: tableViewCell,
),
),
);
expect(find.text("data"), findsOneWidget);
expect(find.byWidget(tableViewCell), findsOneWidget);
});
}

View file

@ -0,0 +1,39 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/annotations.dart';
import 'package:stackwallet/pages_desktop_specific/home/my_stack_view/coin_wallets_table.dart';
import 'package:stackwallet/services/wallets.dart';
import 'package:stackwallet/utilities/theme/light_colors.dart';
import 'package:stackwallet/utilities/theme/stack_colors.dart';
import 'package:stackwallet/widgets/table_view/table_view_cell.dart';
import 'package:stackwallet/widgets/table_view/table_view_row.dart';
import 'table_view_row_test.mocks.dart';
@GenerateMocks([Wallets])
void main() {
testWidgets('Blah blah', (widgetTester) async {
final mockWallets = MockWallets();
// final walletIds = mock
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: nu)),
),
),
);
expect(find.text("Some Text 1"), findsOneWidget);
expect(find.byType(TableViewRow), findsWidgets);
expect(find.byType(TableViewCell), findsWidgets);
});
}

View file

@ -0,0 +1,265 @@
// Mocks generated by Mockito 5.3.2 from annotations
// in stackwallet/test/widget_tests/table_view/table_view_row_test.dart.
// Do not manually edit this file.
// ignore_for_file: no_leading_underscores_for_library_prefixes
import 'dart:async' as _i9;
import 'dart:ui' as _i11;
import 'package:flutter/foundation.dart' as _i4;
import 'package:flutter_riverpod/flutter_riverpod.dart' as _i5;
import 'package:mockito/mockito.dart' as _i1;
import 'package:stackwallet/services/coins/manager.dart' as _i6;
import 'package:stackwallet/services/node_service.dart' as _i3;
import 'package:stackwallet/services/wallets.dart' as _i7;
import 'package:stackwallet/services/wallets_service.dart' as _i2;
import 'package:stackwallet/utilities/enums/coin_enum.dart' as _i8;
import 'package:stackwallet/utilities/prefs.dart' as _i10;
// ignore_for_file: type=lint
// ignore_for_file: avoid_redundant_argument_values
// ignore_for_file: avoid_setters_without_getters
// ignore_for_file: comment_references
// ignore_for_file: implementation_imports
// ignore_for_file: invalid_use_of_visible_for_testing_member
// ignore_for_file: prefer_const_constructors
// ignore_for_file: unnecessary_parenthesis
// ignore_for_file: camel_case_types
// ignore_for_file: subtype_of_sealed_class
class _FakeWalletsService_0 extends _i1.SmartFake
implements _i2.WalletsService {
_FakeWalletsService_0(
Object parent,
Invocation parentInvocation,
) : super(
parent,
parentInvocation,
);
}
class _FakeNodeService_1 extends _i1.SmartFake implements _i3.NodeService {
_FakeNodeService_1(
Object parent,
Invocation parentInvocation,
) : super(
parent,
parentInvocation,
);
}
class _FakeChangeNotifierProvider_2<Notifier extends _i4.ChangeNotifier?>
extends _i1.SmartFake implements _i5.ChangeNotifierProvider<Notifier> {
_FakeChangeNotifierProvider_2(
Object parent,
Invocation parentInvocation,
) : super(
parent,
parentInvocation,
);
}
class _FakeManager_3 extends _i1.SmartFake implements _i6.Manager {
_FakeManager_3(
Object parent,
Invocation parentInvocation,
) : super(
parent,
parentInvocation,
);
}
/// A class which mocks [Wallets].
///
/// See the documentation for Mockito's code generation for more information.
class MockWallets extends _i1.Mock implements _i7.Wallets {
MockWallets() {
_i1.throwOnMissingStub(this);
}
@override
_i2.WalletsService get walletsService => (super.noSuchMethod(
Invocation.getter(#walletsService),
returnValue: _FakeWalletsService_0(
this,
Invocation.getter(#walletsService),
),
) as _i2.WalletsService);
@override
set walletsService(_i2.WalletsService? _walletsService) => super.noSuchMethod(
Invocation.setter(
#walletsService,
_walletsService,
),
returnValueForMissingStub: null,
);
@override
_i3.NodeService get nodeService => (super.noSuchMethod(
Invocation.getter(#nodeService),
returnValue: _FakeNodeService_1(
this,
Invocation.getter(#nodeService),
),
) as _i3.NodeService);
@override
set nodeService(_i3.NodeService? _nodeService) => super.noSuchMethod(
Invocation.setter(
#nodeService,
_nodeService,
),
returnValueForMissingStub: null,
);
@override
bool get hasWallets => (super.noSuchMethod(
Invocation.getter(#hasWallets),
returnValue: false,
) as bool);
@override
List<_i5.ChangeNotifierProvider<_i6.Manager>> get managerProviders =>
(super.noSuchMethod(
Invocation.getter(#managerProviders),
returnValue: <_i5.ChangeNotifierProvider<_i6.Manager>>[],
) as List<_i5.ChangeNotifierProvider<_i6.Manager>>);
@override
List<_i6.Manager> get managers => (super.noSuchMethod(
Invocation.getter(#managers),
returnValue: <_i6.Manager>[],
) as List<_i6.Manager>);
@override
bool get hasListeners => (super.noSuchMethod(
Invocation.getter(#hasListeners),
returnValue: false,
) as bool);
@override
void dispose() => super.noSuchMethod(
Invocation.method(
#dispose,
[],
),
returnValueForMissingStub: null,
);
@override
List<String> getWalletIdsFor({required _i8.Coin? coin}) =>
(super.noSuchMethod(
Invocation.method(
#getWalletIdsFor,
[],
{#coin: coin},
),
returnValue: <String>[],
) as List<String>);
@override
Map<_i8.Coin, List<_i5.ChangeNotifierProvider<_i6.Manager>>>
getManagerProvidersByCoin() => (super.noSuchMethod(
Invocation.method(
#getManagerProvidersByCoin,
[],
),
returnValue: <_i8.Coin,
List<_i5.ChangeNotifierProvider<_i6.Manager>>>{},
) as Map<_i8.Coin, List<_i5.ChangeNotifierProvider<_i6.Manager>>>);
@override
_i5.ChangeNotifierProvider<_i6.Manager> getManagerProvider(
String? walletId) =>
(super.noSuchMethod(
Invocation.method(
#getManagerProvider,
[walletId],
),
returnValue: _FakeChangeNotifierProvider_2<_i6.Manager>(
this,
Invocation.method(
#getManagerProvider,
[walletId],
),
),
) as _i5.ChangeNotifierProvider<_i6.Manager>);
@override
_i6.Manager getManager(String? walletId) => (super.noSuchMethod(
Invocation.method(
#getManager,
[walletId],
),
returnValue: _FakeManager_3(
this,
Invocation.method(
#getManager,
[walletId],
),
),
) as _i6.Manager);
@override
void addWallet({
required String? walletId,
required _i6.Manager? manager,
}) =>
super.noSuchMethod(
Invocation.method(
#addWallet,
[],
{
#walletId: walletId,
#manager: manager,
},
),
returnValueForMissingStub: null,
);
@override
void removeWallet({required String? walletId}) => super.noSuchMethod(
Invocation.method(
#removeWallet,
[],
{#walletId: walletId},
),
returnValueForMissingStub: null,
);
@override
_i9.Future<void> load(_i10.Prefs? prefs) => (super.noSuchMethod(
Invocation.method(
#load,
[prefs],
),
returnValue: _i9.Future<void>.value(),
returnValueForMissingStub: _i9.Future<void>.value(),
) as _i9.Future<void>);
@override
_i9.Future<void> loadAfterStackRestore(
_i10.Prefs? prefs,
List<_i6.Manager>? managers,
) =>
(super.noSuchMethod(
Invocation.method(
#loadAfterStackRestore,
[
prefs,
managers,
],
),
returnValue: _i9.Future<void>.value(),
returnValueForMissingStub: _i9.Future<void>.value(),
) as _i9.Future<void>);
@override
void addListener(_i11.VoidCallback? listener) => super.noSuchMethod(
Invocation.method(
#addListener,
[listener],
),
returnValueForMissingStub: null,
);
@override
void removeListener(_i11.VoidCallback? listener) => super.noSuchMethod(
Invocation.method(
#removeListener,
[listener],
),
returnValueForMissingStub: null,
);
@override
void notifyListeners() => super.noSuchMethod(
Invocation.method(
#notifyListeners,
[],
),
returnValueForMissingStub: null,
);
}

View file

@ -0,0 +1,36 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:stackwallet/utilities/theme/light_colors.dart';
import 'package:stackwallet/utilities/theme/stack_colors.dart';
import 'package:stackwallet/widgets/table_view/table_view.dart';
import 'package:stackwallet/widgets/table_view/table_view_cell.dart';
import 'package:stackwallet/widgets/table_view/table_view_row.dart';
void main() {
testWidgets("Test create table row widget ", (widgetTester) async {
await widgetTester.pumpWidget(
MaterialApp(
theme: ThemeData(
extensions: [
StackColors.fromStackColorTheme(LightColors()),
],
),
home: Material(
child: TableView(
rows: [
for (int i = 0; i < 10; i++)
TableViewRow(cells: [
for (int j = 1; j <= 5; j++)
const TableViewCell(flex: 16, child: Text("Some Text"))
], expandingChild: null)
],
),
),
),
);
expect(find.byType(TableView), findsOneWidget);
expect(find.byType(TableViewRow), findsWidgets);
expect(find.byType(TableViewCell), findsWidgets);
});
}

View file

@ -14,6 +14,7 @@ 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';
import 'package:tuple/tuple.dart';
import 'wallet_card_test.mocks.dart';
@ -21,119 +22,65 @@ import 'wallet_card_test.mocks.dart';
@GenerateMocks([Wallets, BitcoinWallet, LocaleService])
void main() {
group('Navigation tests', () {
late mockingjay.MockNavigator navigator;
testWidgets("When pop popOver redirect", (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");
setUp(() {
navigator = mockingjay.MockNavigator();
// mockingjay
// .when(navigator.push(mockingjay.any()))
// .thenAnswer((invocation) async {});
});
final wallets = MockWallets();
final locale = MockLocaleService();
final manager = Manager(wallet);
Future<void> _builAddressSheetCard(
WidgetTester widgetTester, bool popPrevious) async {
// final CoinServiceAPI wallet = MockBitcoinWallet();
// when(wallet.walletId).thenAnswer((realInvocation) => "wallet id");
// when(wallet.coin).thenAnswer((realInvocation) => Coin.bitcoin);
// when(wallet.walletName).thenAnswer((realInvocation) => "wallet name");
//
// final wallets = MockWallets();
// final manager = Manager(wallet);
//
// when(wallets.getManagerProvider("wallet id")).thenAnswer(
// (realInvocation) => ChangeNotifierProvider((ref) => manager));
//
// await widgetTester.pumpWidget(
// ProviderScope(
// overrides: [
// walletsChangeNotifierProvider.overrideWithValue(wallets),
// ],
// child: MaterialApp(
// theme: ThemeData(
// extensions: [
// StackColors.fromStackColorTheme(LightColors()),
// ],
// ),
// home: Material(
// child: WalletSheetCard(
// walletId: "wallet id",
// popPrevious: popPrevious,
// ),
// ),
// navigatorObservers: [mockObserver],
// ),
// ),
// );
}
mockito.when(wallets.getManagerProvider("wallet id")).thenAnswer(
(realInvocation) => ChangeNotifierProvider((ref) => manager));
mockito.when(locale.locale).thenAnswer((_) => "en_US");
testWidgets("When pop popOver redirect", (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");
mockito.when(wallets.getManagerProvider("wallet id")).thenAnswer(
(realInvocation) => ChangeNotifierProvider((ref) => manager));
final wallets = MockWallets();
final locale = MockLocaleService();
final manager = Manager(wallet);
final navigator = mockingjay.MockNavigator();
mockingjay
.when(() => navigator.pushNamed("/wallet", arguments: [
Tuple2("wallet id", wallets.getManagerProvider("wallet id"))
]))
.thenAnswer((_) async => Object());
mockito.when(wallets.getManagerProvider("wallet id")).thenAnswer(
(realInvocation) => ChangeNotifierProvider((ref) => manager));
mockito.when(locale.locale).thenAnswer((_) => "en_US");
// 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) {});
final navigator = mockingjay.MockNavigator();
mockingjay
.when(() => navigator.pushNamed("/wallets", arguments: []))
.thenAnswer((_) async => {});
// mockingjay.when(() => navigator.pop()).thenAnswer((invocation) {});
await widgetTester.pumpWidget(
ProviderScope(
overrides: [
walletsChangeNotifierProvider.overrideWithValue(wallets),
localeServiceChangeNotifierProvider.overrideWithValue(locale),
],
child: MaterialApp(
theme: ThemeData(
extensions: [
StackColors.fromStackColorTheme(LightColors()),
],
),
home: mockingjay.MockNavigatorProvider(
navigator: navigator,
child: const WalletSheetCard(
walletId: "wallet id",
popPrevious: true,
)),
await widgetTester.pumpWidget(
ProviderScope(
overrides: [
walletsChangeNotifierProvider.overrideWithValue(wallets),
localeServiceChangeNotifierProvider.overrideWithValue(locale),
],
child: MaterialApp(
theme: ThemeData(
extensions: [
StackColors.fromStackColorTheme(LightColors()),
],
),
home: mockingjay.MockNavigatorProvider(
navigator: navigator,
child: const WalletSheetCard(
walletId: "wallet id",
)),
),
);
// await widgetTester.pumpAndSettle();
// final navigator = mockingjay.MockNavigator();
// // mockingjay.when(() => navi)
// await _builAddressSheetCard(widgetTester, false);
//
// // final Route pushedRoute = verify(mocki)
// expect(find.byType(MaterialButton), findsOneWidget);
// mockingjay.verify(() => navigator.pushNamed("wallets")).called(1);
await widgetTester.tap(find.byType(MaterialButton));
// mockingjay
// .verifyNever(
// () => navigator.push<void>(
// mockingjay.any(
// that: mockingjay.isRoute<void>(
// whereName: equals("/wallet"),
// ),
// ),
// ),
// )
// .called(0);
// verify(mockObserver.didPop(mockingjay.any(), mockingjay.any()));
});
),
);
//
expect(find.byType(MaterialButton), findsOneWidget);
await widgetTester.tap(find.byType(MaterialButton));
// });
});
testWidgets('test widget loads correctly', (widgetTester) async {
@ -173,48 +120,4 @@ void main() {
);
expect(find.byWidget(walletSheetCard), findsOneWidget);
});
// testWidgets("test pop previous is false does nothing", (widgetTester) async {
// final CoinServiceAPI wallet = MockBitcoinWallet();
// when(wallet.walletId).thenAnswer((realInvocation) => "wallet id");
// when(wallet.coin).thenAnswer((realInvocation) => Coin.bitcoin);
// when(wallet.walletName).thenAnswer((realInvocation) => "wallet name");
//
// final wallets = MockWallets();
// final manager = Manager(wallet);
//
// when(wallets.getManagerProvider("wallet id")).thenAnswer(
// (realInvocation) => ChangeNotifierProvider((ref) => manager));
//
// const walletSheetCard = WalletSheetCard(
// walletId: "wallet id",
// popPrevious: false,
// );
//
// // late NavigatorObserver mockObserver;
// //
// // setUp(() {
// // mockObserver = MockNavigatorObserver();
// // });
//
// await widgetTester.pumpWidget(
// ProviderScope(
// overrides: [
// walletsChangeNotifierProvider.overrideWithValue(wallets),
// ],
// child: MaterialApp(
// theme: ThemeData(
// extensions: [
// StackColors.fromStackColorTheme(LightColors()),
// ],
// ),
// home: const Material(
// child: walletSheetCard,
// ),
// ),
// ),
// );
//
// await widgetTester.tap(find.byType(MaterialButton));
// });
}