Skip to content

Commit

Permalink
some onboarding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bibash28 committed May 22, 2024
1 parent a7083e8 commit d6a8815
Show file tree
Hide file tree
Showing 4 changed files with 213 additions and 64 deletions.
2 changes: 1 addition & 1 deletion lib/onboarding/gen_phrase/view/onboarding_gen_phrase.dart
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ class _OnBoardingGenPhraseViewState extends State<OnBoardingGenPhraseView> {
text: l10n.verifyNow,
verticalSpacing: 18,
onPressed: () {
Navigator.of(context).push(
Navigator.of(context).push<void>(
OnBoardingVerifyPhrasePage.route(
mnemonic: mnemonic!,
isFromOnboarding: true,
Expand Down
22 changes: 22 additions & 0 deletions test/onboarding/cubit/onboarding_state_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import 'package:altme/app/app.dart';
import 'package:altme/onboarding/cubit/onboarding_cubit.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
group('OnboardingState', () {
test('can be instantiated from JSON', () {
final json = {'status': 'init'};

final onboardingState = OnboardingState.fromJson(json);

expect(onboardingState.status, AppStatus.init);
});

test('can be converted to JSON', () {
const onboardingState = OnboardingState(status: AppStatus.init);
final json = onboardingState.toJson();

expect(json, {'status': 'init'});
});
});
}

This file was deleted.

190 changes: 190 additions & 0 deletions test/onboarding/gen_phrase/view/onboarding_gen_phrase_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
import 'package:altme/app/app.dart';
import 'package:altme/chat_room/chat_room.dart';
import 'package:altme/dashboard/dashboard.dart';
import 'package:altme/onboarding/onboarding.dart';
import 'package:altme/splash/splash.dart';
import 'package:altme/wallet/wallet.dart';
import 'package:bloc_test/bloc_test.dart';
import 'package:did_kit/did_kit.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:key_generator/key_generator.dart';
import 'package:mockingjay/mockingjay.dart';
import 'package:secure_storage/secure_storage.dart';

import '../../../helpers/helpers.dart';

class MockSecureStorage extends Mock implements SecureStorageProvider {}

class MockDIDKitProvider extends Mock implements DIDKitProvider {}

class MockKeyGenerator extends Mock implements KeyGenerator {}

class MockHomeCubit extends MockCubit<HomeState> implements HomeCubit {
@override
Future<void> emitHasWallet() async {}
}

class MockWalletCubit extends MockCubit<WalletState> implements WalletCubit {
@override
final state = WalletState(
cryptoAccount: CryptoAccount(
data: [
CryptoAccountData(
name: '',
secretKey: '',
blockchainType: BlockchainType.tezos,
walletAddress: '',
),
],
),
);

@override
Future<void> createCryptoWallet({
String? accountName,
required String mnemonicOrKey,
required bool isImported,
required bool isFromOnboarding,
BlockchainType? blockchainType,
bool showStatus = true,
void Function({
required CryptoAccount cryptoAccount,
required MessageHandler messageHandler,
})? onComplete,
}) async {}
}

class MockSplashCubit extends MockCubit<SplashState> implements SplashCubit {}

class MockAltmeChatSupportCubit extends MockCubit<ChatRoomState>
implements AltmeChatSupportCubit {}

class MockProfileCubit extends MockCubit<ProfileState> implements ProfileCubit {
@override
final state = ProfileState(model: ProfileModel.empty());
}

void main() {
late SecureStorageProvider mockSecureStorage;
late DIDKitProvider didKitProvider;
late KeyGenerator keyGenerator;
late HomeCubit homeCubit;
late WalletCubit walletCubit;
late SplashCubit splashCubit;
late AltmeChatSupportCubit altmeChatSupportCubit;
late ProfileCubit profileCubit;

setUpAll(() {
WidgetsFlutterBinding.ensureInitialized();
mockSecureStorage = MockSecureStorage();
didKitProvider = MockDIDKitProvider();
keyGenerator = MockKeyGenerator();
homeCubit = MockHomeCubit();
splashCubit = MockSplashCubit();
walletCubit = MockWalletCubit();
altmeChatSupportCubit = MockAltmeChatSupportCubit();
profileCubit = MockProfileCubit();

when(() => mockSecureStorage.set(any(), any())).thenAnswer((_) async {});
when(() => mockSecureStorage.get(any())).thenAnswer((_) async {});
});

group('OnBoarding GenPhrase Page', () {
late OnBoardingGenPhraseCubit onBoardingGenPhraseCubit;

late MockNavigator navigator;

setUpAll(() {
onBoardingGenPhraseCubit = OnBoardingGenPhraseCubit(
secureStorageProvider: mockSecureStorage,
didKitProvider: didKitProvider,
keyGenerator: keyGenerator,
homeCubit: homeCubit,
walletCubit: walletCubit,
splashCubit: splashCubit,
altmeChatSupportCubit: altmeChatSupportCubit,
profileCubit: profileCubit,
);
navigator = MockNavigator();
when(navigator.canPop).thenReturn(true);

when(() => navigator.push<void>(any())).thenAnswer((_) async {});
});

testWidgets('is routable', (tester) async {
await tester.pumpApp(
MockNavigatorProvider(
navigator: navigator,
child: Builder(
builder: (context) => Scaffold(
floatingActionButton: FloatingActionButton(
onPressed: () {
Navigator.of(context)
.push<void>(OnBoardingVerifyPhrasePage.route(
isFromOnboarding: true,
mnemonic: [],
));
},
),
),
),
),
);
await tester.tap(find.byType(FloatingActionButton));
await tester.pumpAndSettle();

verify(
() => navigator.push<void>(
any(
that: isRoute<void>(
whereName: equals('/OnBoardingVerifyPhrasePage'),
),
),
),
).called(1);
});

testWidgets('renders UI correctly', (tester) async {
await tester.pumpApp(
BlocProvider.value(
value: onBoardingGenPhraseCubit,
child: const OnBoardingGenPhraseView(),
),
);

expect(find.byType(BasePage), findsOneWidget);
expect(find.byType(MStepper), findsOneWidget);
expect(find.byType(MnemonicDisplay), findsOneWidget);
expect(find.byType(MyElevatedButton), findsOneWidget);
expect(find.byType(MyGradientButton), findsOneWidget);
});

testWidgets(
'navigates to OnBoardingVerifyPhrasePage when Verify Now button'
' is tapped', (tester) async {
await tester.pumpApp(
MockNavigatorProvider(
navigator: navigator,
child: BlocProvider.value(
value: onBoardingGenPhraseCubit,
child: const OnBoardingGenPhraseView(),
),
),
);

await tester.tap(find.text('Verify Now'.toUpperCase()));

verify(
() => navigator.push<void>(
any(
that: isRoute<void>(
whereName: equals('/OnBoardingVerifyPhrasePage'),
),
),
),
).called(1);
});
});
}

0 comments on commit d6a8815

Please sign in to comment.