stack_wallet/test/widget_tests/transaction_card_test.dart

502 lines
16 KiB
Dart
Raw Normal View History

2022-11-02 14:49:19 +00:00
import 'package:decimal/decimal.dart';
import 'package:flutter/material.dart';
import 'package:flutter_feather_icons/flutter_feather_icons.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockingjay/mockingjay.dart' as mockingjay;
2022-08-26 08:11:35 +00:00
import 'package:mockito/annotations.dart';
2022-11-02 14:49:19 +00:00
import 'package:mockito/mockito.dart';
2023-02-03 19:22:21 +00:00
import 'package:stackwallet/models/isar/models/blockchain_data/address.dart';
2023-01-19 18:20:27 +00:00
import 'package:stackwallet/models/isar/models/blockchain_data/transaction.dart';
2023-05-12 20:02:04 +00:00
import 'package:stackwallet/models/isar/stack_theme.dart';
2022-11-02 14:49:19 +00:00
import 'package:stackwallet/pages/wallet_view/transaction_views/transaction_details_view.dart';
import 'package:stackwallet/providers/providers.dart';
import 'package:stackwallet/services/coins/coin_service.dart';
import 'package:stackwallet/services/coins/firo/firo_wallet.dart';
2022-08-26 08:11:35 +00:00
import 'package:stackwallet/services/coins/manager.dart';
import 'package:stackwallet/services/locale_service.dart';
import 'package:stackwallet/services/notes_service.dart';
import 'package:stackwallet/services/price_service.dart';
import 'package:stackwallet/services/wallets.dart';
import 'package:stackwallet/themes/stack_colors.dart';
2023-05-12 20:02:04 +00:00
import 'package:stackwallet/themes/theme_service.dart';
2023-04-06 21:24:56 +00:00
import 'package:stackwallet/utilities/amount/amount.dart';
2022-11-02 14:49:19 +00:00
import 'package:stackwallet/utilities/enums/coin_enum.dart';
import 'package:stackwallet/utilities/prefs.dart';
2022-11-02 14:49:19 +00:00
import 'package:stackwallet/utilities/util.dart';
import 'package:stackwallet/widgets/transaction_card.dart';
import 'package:tuple/tuple.dart';
2023-05-12 20:02:04 +00:00
import '../sample_data/theme_json.dart';
2022-11-02 14:49:19 +00:00
import 'transaction_card_test.mocks.dart';
2022-08-26 08:11:35 +00:00
@GenerateMocks([
Wallets,
Manager,
CoinServiceAPI,
FiroWallet,
LocaleService,
Prefs,
PriceService,
2023-05-12 20:02:04 +00:00
NotesService,
ThemeService,
], customMocks: [])
2022-08-26 08:11:35 +00:00
void main() {
2022-11-02 14:49:19 +00:00
TestWidgetsFlutterBinding.ensureInitialized();
testWidgets("Sent confirmed tx displays correctly", (tester) async {
final mockManager = MockManager();
final mockLocaleService = MockLocaleService();
final wallets = MockWallets();
final mockPrefs = MockPrefs();
final mockPriceService = MockPriceService();
2023-05-12 20:02:04 +00:00
final mockThemeService = MockThemeService();
2022-11-02 14:49:19 +00:00
final tx = Transaction(
2023-02-03 19:22:21 +00:00
txid: "some txid",
timestamp: 1648595998,
type: TransactionType.outgoing,
amount: 100000000,
2023-03-24 21:55:17 +00:00
amountString: Amount(
rawValue: BigInt.from(100000000),
fractionDigits: Coin.firo.decimals,
).toJsonString(),
2023-02-03 19:22:21 +00:00
fee: 3794,
height: 450123,
subType: TransactionSubType.none,
isCancelled: false,
walletId: '',
isLelantus: null,
slateId: '',
otherData: '',
2023-03-31 16:15:42 +00:00
nonce: null,
2023-02-03 19:22:21 +00:00
inputs: [],
outputs: [],
)..address.value = Address(
walletId: "walletId",
value: "",
publicKey: [],
derivationIndex: 0,
derivationPath: null,
2023-02-03 19:22:21 +00:00
type: AddressType.p2pkh,
subType: AddressSubType.receiving);
2022-11-02 14:49:19 +00:00
final CoinServiceAPI wallet = MockFiroWallet();
2023-05-12 20:02:04 +00:00
when(mockThemeService.getTheme(themeId: "light")).thenAnswer(
(_) => StackTheme.fromJson(
json: lightThemeJsonMap,
applicationThemesDirectoryPath: "test",
),
);
2022-11-02 14:49:19 +00:00
when(wallet.coin.ticker).thenAnswer((_) => "FIRO");
when(mockLocaleService.locale).thenAnswer((_) => "en_US");
when(mockPrefs.currency).thenAnswer((_) => "USD");
when(mockPrefs.externalCalls).thenAnswer((_) => true);
when(mockPriceService.getPrice(Coin.firo))
.thenAnswer((realInvocation) => Tuple2(Decimal.ten, 0.00));
when(wallet.coin).thenAnswer((_) => Coin.firo);
when(wallets.getManager("wallet-id"))
.thenAnswer((realInvocation) => Manager(wallet));
2023-01-30 20:15:44 +00:00
when(wallet.storedChainHeight).thenAnswer((_) => 6000000);
2022-11-02 14:49:19 +00:00
//
await tester.pumpWidget(
ProviderScope(
overrides: [
walletsChangeNotifierProvider.overrideWithValue(wallets),
localeServiceChangeNotifierProvider
.overrideWithValue(mockLocaleService),
2023-05-12 20:02:04 +00:00
pThemeService.overrideWithValue(mockThemeService),
2022-11-02 14:49:19 +00:00
prefsChangeNotifierProvider.overrideWithValue(mockPrefs),
priceAnd24hChangeNotifierProvider.overrideWithValue(mockPriceService)
],
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
),
2022-11-02 14:49:19 +00:00
),
],
),
home: TransactionCard(transaction: tx, walletId: "wallet-id"),
),
),
);
//
final title = find.text("Sent");
// final price1 = find.text("0.00 USD");
final amount = Util.isDesktop
? find.text("-1.00000000 FIRO")
: find.text("1.00000000 FIRO");
final icon = find.byIcon(FeatherIcons.arrowUp);
expect(title, findsOneWidget);
// expect(price1, findsOneWidget);
expect(amount, findsOneWidget);
// expect(icon, findsOneWidget);
//
await tester.pumpAndSettle(const Duration(seconds: 2));
//
// final price2 = find.text("\$10.00");
// expect(price2, findsOneWidget);
//
// verify(mockManager.addListener(any)).called(1);
verify(mockLocaleService.addListener(any)).called(1);
verify(mockPrefs.currency).called(1);
verify(mockPriceService.getPrice(Coin.firo)).called(1);
2023-04-01 00:53:52 +00:00
verify(wallet.coin.ticker).called(1);
2022-11-02 14:49:19 +00:00
verify(mockLocaleService.locale).called(1);
verifyNoMoreInteractions(mockManager);
verifyNoMoreInteractions(mockLocaleService);
});
testWidgets("Anonymized confirmed tx displays correctly", (tester) async {
final mockManager = MockManager();
final mockLocaleService = MockLocaleService();
final wallets = MockWallets();
final mockPrefs = MockPrefs();
final mockPriceService = MockPriceService();
2023-05-12 20:02:04 +00:00
final mockThemeService = MockThemeService();
2022-11-02 14:49:19 +00:00
final tx = Transaction(
2023-02-03 19:22:21 +00:00
txid: "some txid",
timestamp: 1648595998,
type: TransactionType.outgoing,
amount: 9659,
2023-03-24 21:55:17 +00:00
amountString: Amount(
rawValue: BigInt.from(9659),
fractionDigits: Coin.firo.decimals,
).toJsonString(),
2023-02-03 19:22:21 +00:00
fee: 3794,
height: 450123,
subType: TransactionSubType.mint,
isCancelled: false,
walletId: '',
isLelantus: null,
slateId: '',
otherData: '',
2023-03-31 16:15:42 +00:00
nonce: null,
2023-02-03 19:22:21 +00:00
inputs: [],
outputs: [],
)..address.value = Address(
walletId: "walletId",
value: "",
publicKey: [],
derivationIndex: 0,
derivationPath: null,
2023-02-03 19:22:21 +00:00
type: AddressType.p2pkh,
subType: AddressSubType.receiving);
2022-11-02 14:49:19 +00:00
final CoinServiceAPI wallet = MockFiroWallet();
2023-05-12 20:02:04 +00:00
when(mockThemeService.getTheme(themeId: "light")).thenAnswer(
(_) => StackTheme.fromJson(
json: lightThemeJsonMap,
applicationThemesDirectoryPath: "test",
),
);
2022-11-02 14:49:19 +00:00
when(wallet.coin.ticker).thenAnswer((_) => "FIRO");
when(mockLocaleService.locale).thenAnswer((_) => "en_US");
when(mockPrefs.currency).thenAnswer((_) => "USD");
when(mockPrefs.externalCalls).thenAnswer((_) => true);
when(mockPriceService.getPrice(Coin.firo))
.thenAnswer((realInvocation) => Tuple2(Decimal.ten, 0.00));
when(wallet.coin).thenAnswer((_) => Coin.firo);
2023-01-30 20:15:44 +00:00
when(wallet.storedChainHeight).thenAnswer((_) => 6000000);
2022-11-02 14:49:19 +00:00
when(wallets.getManager("wallet-id"))
.thenAnswer((realInvocation) => Manager(wallet));
//
await tester.pumpWidget(
ProviderScope(
overrides: [
walletsChangeNotifierProvider.overrideWithValue(wallets),
localeServiceChangeNotifierProvider
.overrideWithValue(mockLocaleService),
prefsChangeNotifierProvider.overrideWithValue(mockPrefs),
2023-05-12 20:02:04 +00:00
pThemeService.overrideWithValue(mockThemeService),
2022-11-02 14:49:19 +00:00
priceAnd24hChangeNotifierProvider.overrideWithValue(mockPriceService)
],
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
),
2022-11-02 14:49:19 +00:00
),
],
),
home: TransactionCard(transaction: tx, walletId: "wallet-id"),
),
),
);
//
final title = find.text("Anonymized");
// final price1 = find.text("0.00 USD");
2023-01-19 18:20:27 +00:00
final amount = find.text("-0.00009659 FIRO");
2022-11-02 14:49:19 +00:00
final icon = find.byIcon(FeatherIcons.arrowUp);
expect(title, findsOneWidget);
// expect(price1, findsOneWidget);
expect(amount, findsOneWidget);
// expect(icon, findsOneWidget);
//
await tester.pumpAndSettle(const Duration(seconds: 2));
//
// final price2 = find.text("\$10.00");
// expect(price2, findsOneWidget);
//
// verify(mockManager.addListener(any)).called(1);
verify(mockLocaleService.addListener(any)).called(1);
verify(mockPrefs.currency).called(1);
verify(mockPriceService.getPrice(Coin.firo)).called(1);
2023-04-01 00:53:52 +00:00
verify(wallet.coin.ticker).called(1);
2022-11-02 14:49:19 +00:00
verify(mockLocaleService.locale).called(1);
verifyNoMoreInteractions(mockManager);
verifyNoMoreInteractions(mockLocaleService);
});
testWidgets("Received unconfirmed tx displays correctly", (tester) async {
final mockManager = MockManager();
final mockLocaleService = MockLocaleService();
final wallets = MockWallets();
final mockPrefs = MockPrefs();
final mockPriceService = MockPriceService();
2023-05-12 20:02:04 +00:00
final mockThemeService = MockThemeService();
2022-11-02 14:49:19 +00:00
final tx = Transaction(
2023-02-03 19:22:21 +00:00
txid: "some txid",
timestamp: 1648595998,
type: TransactionType.incoming,
amount: 100000000,
2023-03-24 21:55:17 +00:00
amountString: Amount(
rawValue: BigInt.from(100000000),
fractionDigits: Coin.firo.decimals,
).toJsonString(),
2023-02-03 19:22:21 +00:00
fee: 3794,
height: 450123,
subType: TransactionSubType.none,
isCancelled: false,
walletId: '',
isLelantus: null,
slateId: '',
otherData: '',
2023-03-31 16:15:42 +00:00
nonce: null,
2023-02-03 19:22:21 +00:00
inputs: [],
outputs: [],
)..address.value = Address(
walletId: "walletId",
value: "",
publicKey: [],
derivationIndex: 0,
derivationPath: null,
2023-02-03 19:22:21 +00:00
type: AddressType.p2pkh,
subType: AddressSubType.receiving);
2022-11-02 14:49:19 +00:00
final CoinServiceAPI wallet = MockFiroWallet();
2023-05-12 20:02:04 +00:00
when(mockThemeService.getTheme(themeId: "light")).thenAnswer(
(_) => StackTheme.fromJson(
json: lightThemeJsonMap,
applicationThemesDirectoryPath: "test",
),
);
2022-11-02 14:49:19 +00:00
when(wallet.coin.ticker).thenAnswer((_) => "FIRO");
when(mockLocaleService.locale).thenAnswer((_) => "en_US");
when(mockPrefs.currency).thenAnswer((_) => "USD");
when(mockPrefs.externalCalls).thenAnswer((_) => true);
when(mockPriceService.getPrice(Coin.firo))
.thenAnswer((realInvocation) => Tuple2(Decimal.ten, 0.00));
when(wallet.coin).thenAnswer((_) => Coin.firo);
when(wallets.getManager("wallet-id"))
.thenAnswer((realInvocation) => Manager(wallet));
2023-01-30 20:15:44 +00:00
when(wallet.storedChainHeight).thenAnswer((_) => 6000000);
2022-11-02 14:49:19 +00:00
await tester.pumpWidget(
ProviderScope(
overrides: [
walletsChangeNotifierProvider.overrideWithValue(wallets),
localeServiceChangeNotifierProvider
.overrideWithValue(mockLocaleService),
prefsChangeNotifierProvider.overrideWithValue(mockPrefs),
2023-05-12 20:02:04 +00:00
pThemeService.overrideWithValue(mockThemeService),
2022-11-02 14:49:19 +00:00
priceAnd24hChangeNotifierProvider.overrideWithValue(mockPriceService)
],
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
),
2022-11-02 14:49:19 +00:00
),
],
),
home: TransactionCard(transaction: tx, walletId: "wallet-id"),
),
),
);
2023-01-19 18:20:27 +00:00
final title = find.text("Received");
2022-11-02 14:49:19 +00:00
final amount = Util.isDesktop
? find.text("+1.00000000 FIRO")
: find.text("1.00000000 FIRO");
expect(title, findsOneWidget);
expect(amount, findsOneWidget);
await tester.pumpAndSettle(const Duration(seconds: 2));
verify(mockLocaleService.addListener(any)).called(1);
verify(mockPrefs.currency).called(1);
verify(mockPriceService.getPrice(Coin.firo)).called(1);
2023-04-01 00:53:52 +00:00
verify(wallet.coin.ticker).called(1);
2022-11-02 14:49:19 +00:00
verify(mockLocaleService.locale).called(1);
verifyNoMoreInteractions(mockManager);
verifyNoMoreInteractions(mockLocaleService);
});
testWidgets("Tap gesture", (tester) async {
final mockManager = MockManager();
final mockLocaleService = MockLocaleService();
final wallets = MockWallets();
final mockPrefs = MockPrefs();
final mockPriceService = MockPriceService();
2023-05-12 20:02:04 +00:00
final mockThemeService = MockThemeService();
2022-11-02 14:49:19 +00:00
final navigator = mockingjay.MockNavigator();
final tx = Transaction(
2023-02-03 19:22:21 +00:00
txid: "some txid",
timestamp: 1648595998,
type: TransactionType.outgoing,
amount: 100000000,
2023-03-24 21:55:17 +00:00
amountString: Amount(
rawValue: BigInt.from(100000000),
fractionDigits: Coin.firo.decimals,
).toJsonString(),
2023-02-03 19:22:21 +00:00
fee: 3794,
height: 450123,
subType: TransactionSubType.none,
isCancelled: false,
walletId: '',
isLelantus: null,
slateId: '',
otherData: '',
2023-03-31 16:15:42 +00:00
nonce: null,
2023-02-03 19:22:21 +00:00
inputs: [],
outputs: [],
)..address.value = Address(
walletId: "walletId",
value: "",
publicKey: [],
derivationIndex: 0,
derivationPath: null,
2023-02-03 19:22:21 +00:00
type: AddressType.p2pkh,
subType: AddressSubType.receiving);
2022-11-02 14:49:19 +00:00
final CoinServiceAPI wallet = MockFiroWallet();
2023-05-12 20:02:04 +00:00
when(mockThemeService.getTheme(themeId: "light")).thenAnswer(
(_) => StackTheme.fromJson(
json: lightThemeJsonMap,
applicationThemesDirectoryPath: "test",
),
);
2022-11-02 14:49:19 +00:00
when(wallet.coin.ticker).thenAnswer((_) => "FIRO");
when(mockLocaleService.locale).thenAnswer((_) => "en_US");
when(mockPrefs.currency).thenAnswer((_) => "USD");
when(mockPrefs.externalCalls).thenAnswer((_) => true);
when(mockPriceService.getPrice(Coin.firo))
.thenAnswer((realInvocation) => Tuple2(Decimal.ten, 0.00));
when(wallet.coin).thenAnswer((_) => Coin.firo);
when(wallets.getManager("wallet id"))
.thenAnswer((realInvocation) => Manager(wallet));
2023-01-30 20:15:44 +00:00
when(wallet.storedChainHeight).thenAnswer((_) => 6000000);
2022-11-02 14:49:19 +00:00
mockingjay
.when(() => navigator.pushNamed("/transactionDetails",
arguments: Tuple3(tx, Coin.firo, "wallet id")))
.thenAnswer((_) async => {});
await tester.pumpWidget(
ProviderScope(
overrides: [
walletsChangeNotifierProvider.overrideWithValue(wallets),
localeServiceChangeNotifierProvider
.overrideWithValue(mockLocaleService),
prefsChangeNotifierProvider.overrideWithValue(mockPrefs),
2023-05-12 20:02:04 +00:00
pThemeService.overrideWithValue(mockThemeService),
2022-11-02 14:49:19 +00:00
priceAnd24hChangeNotifierProvider.overrideWithValue(mockPriceService)
],
child: MaterialApp(
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,
applicationThemesDirectoryPath: "test",
2023-05-08 17:59:00 +00:00
),
),
2022-11-02 14:49:19 +00:00
],
),
home: mockingjay.MockNavigatorProvider(
navigator: navigator,
child: TransactionCard(transaction: tx, walletId: "wallet id")),
),
),
);
expect(find.byType(GestureDetector), findsOneWidget);
await tester.tap(find.byType(GestureDetector));
await tester.pump();
verify(mockLocaleService.addListener(any)).called(1);
verify(mockPrefs.currency).called(2);
verify(mockLocaleService.locale).called(4);
2023-04-01 00:53:52 +00:00
verify(wallet.coin.ticker).called(1);
2023-01-30 20:15:44 +00:00
verify(wallet.storedChainHeight).called(2);
2022-11-02 14:49:19 +00:00
verifyNoMoreInteractions(wallet);
verifyNoMoreInteractions(mockLocaleService);
if (Util.isDesktop) {
expect(find.byType(TransactionDetailsView), findsOneWidget);
} else {
mockingjay
.verify(() => navigator.pushNamed("/transactionDetails",
arguments: Tuple3(tx, Coin.firo, "wallet id")))
.called(1);
}
});
2022-08-26 08:11:35 +00:00
}