Skip to content

Commit

Permalink
add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bibash28 committed May 22, 2024
1 parent cb8c793 commit a7083e8
Show file tree
Hide file tree
Showing 5 changed files with 227 additions and 2 deletions.
28 changes: 28 additions & 0 deletions test/app/shared/widget/m_webview/cubit/m_webview_cubit_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import 'package:altme/app/app.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
group('MWebViewCubit', () {
late MWebViewCubit cubit;

setUp(() {
cubit = MWebViewCubit();
});

tearDown(() {
cubit.close();
});

test('initial state is true', () {
expect(cubit.state, true);
});

test('emits new state when setLoading is called', () {
expect(cubit.state, true);
cubit.setLoading(isLoading: false);
expect(cubit.state, false);
cubit.setLoading(isLoading: true);
expect(cubit.state, true);
});
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import 'package:altme/app/app.dart';
import 'package:altme/dashboard/dashboard.dart';
import 'package:altme/onboarding/onboarding.dart';

import 'package:bloc_test/bloc_test.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mocktail/mocktail.dart';

class MockProfileCubit extends MockCubit<ProfileState>
implements ProfileCubit {}

void main() {
group('ActiveBiometricsCubit', () {
late ActiveBiometricsCubit activeBiometricsCubit;
late MockProfileCubit mockProfileCubit;

setUp(() {
mockProfileCubit = MockProfileCubit();

activeBiometricsCubit =
ActiveBiometricsCubit(profileCubit: mockProfileCubit);
});

test('initial state is false', () {
expect(activeBiometricsCubit.state, false);
});

blocTest<ActiveBiometricsCubit, bool>(
'load emits true if walletProtectionType is FA2 or biometrics',
build: () {
when(() => mockProfileCubit.state).thenReturn(
ProfileState(
model: ProfileModel(
polygonIdNetwork: PolygonIdNetwork.PolygonMainnet,
walletType: WalletType.personal,
walletProtectionType: WalletProtectionType.biometrics,
isDeveloperMode: false,
profileType: ProfileType.custom,
profileSetting: ProfileSetting.initial(),
),
),
);
return activeBiometricsCubit;
},
act: (cubit) => cubit.load(),
expect: () => [true],
);

blocTest<ActiveBiometricsCubit, bool>(
'load emits false if walletProtectionType is not FA2 or biometrics',
build: () {
when(() => mockProfileCubit.state).thenReturn(
ProfileState(
model: ProfileModel(
polygonIdNetwork: PolygonIdNetwork.PolygonMainnet,
walletType: WalletType.personal,
walletProtectionType: WalletProtectionType.pinCode,
isDeveloperMode: false,
profileType: ProfileType.custom,
profileSetting: ProfileSetting.initial(),
),
),
);
return activeBiometricsCubit;
},
act: (cubit) => cubit.load(),
expect: () => [false],
);

blocTest<ActiveBiometricsCubit, bool>(
'updateSwitch emits the provided value',
build: () => activeBiometricsCubit,
act: (cubit) => cubit.updateSwitch(value: true),
expect: () => [true],
);
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import 'package:altme/app/app.dart';
import 'package:altme/onboarding/onboarding.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_test/flutter_test.dart';

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

void main() {
group('BiometricsSwitch', () {
testWidgets('renders value correctly', (tester) async {
await tester.pumpApp(const BiometricsSwitch(value: true));

expect(
find.byWidgetPredicate(
(widget) =>
widget is Image &&
widget.image is AssetImage &&
(widget.image as AssetImage).assetName ==
IconStrings.fingerprint2,
),
findsOneWidget,
);
expect(find.byType(CupertinoSwitch), findsOneWidget);
});
});
}
26 changes: 26 additions & 0 deletions test/onboarding/cubit/onboarding_cubit_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import 'package:altme/app/app.dart';
import 'package:altme/onboarding/cubit/onboarding_cubit.dart';
import 'package:bloc_test/bloc_test.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
group('OnboardingCubit', () {
test('initial state is correct', () {
expect(OnboardingCubit().state, const OnboardingState());
});

blocTest<OnboardingCubit, OnboardingState>(
'emits [loading] when emitOnboardingProcessing is called',
build: OnboardingCubit.new,
act: (cubit) => cubit.emitOnboardingProcessing(),
expect: () => [const OnboardingState(status: AppStatus.loading)],
);

blocTest<OnboardingCubit, OnboardingState>(
'emits [success] when emitOnboardingDone is called',
build: OnboardingCubit.new,
act: (cubit) => cubit.emitOnboardingDone(),
expect: () => [const OnboardingState(status: AppStatus.success)],
);
});
}
72 changes: 70 additions & 2 deletions test/onboarding/tos/view/onboarding_tos_page_test.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
import 'package:altme/app/app.dart';
import 'package:altme/onboarding/onboarding.dart';
import 'package:bloc_test/bloc_test.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mocktail/mocktail.dart';

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

class MockOnBoardingTosCubit extends MockCubit<OnBoardingTosState>
implements OnBoardingTosCubit {}

void main() {
group('OnBoarding Terms Page', () {
late MockOnBoardingTosCubit onBoardingTosCubit;

setUpAll(() {
onBoardingTosCubit = MockOnBoardingTosCubit();
when(() => onBoardingTosCubit.state)
.thenReturn(const OnBoardingTosState());
});

testWidgets('is routable', (tester) async {
await tester.pumpApp(
Builder(
Expand All @@ -25,9 +39,14 @@ void main() {
});

testWidgets('renders OnBoardingTosPage', (tester) async {
await tester.pumpApp(const OnBoardingTosPage());
await tester.pumpApp(
BlocProvider.value(
value: onBoardingTosCubit,
child: const OnBoardingTosPage(),
),
);
await tester.pumpAndSettle();
expect(find.byType(OnBoardingTosPage), findsOneWidget);
expect(find.byType(OnBoardingTosView), findsOneWidget);
});

testWidgets('nothing happens when button is pressed', (tester) async {
Expand All @@ -37,6 +56,32 @@ void main() {
expect(find.byType(OnBoardingTosPage), findsOneWidget);
});

// testWidgets('checkboxes toggle state', (WidgetTester tester) async {
// when(() => onBoardingTosCubit.setAgreeTerms(
// agreeTerms: any(named: 'agreeTerms'))).thenAnswer((_) async {});
// when(() => onBoardingTosCubit.setReadTerms(
// readTerms: any(named: 'readTerms'))).thenAnswer((_) async {});

// await tester.pumpApp(
// BlocProvider<MockOnBoardingTosCubit>(
// create: (_) => onBoardingTosCubit,
// child: const OnBoardingTosPage(),
// ),
// );

// final agreeCheckbox = find.text('I agree to the terms and conditions.');
// final readCheckbox = find.text('I have read the terms of use.');

// expect(agreeCheckbox, findsOneWidget);
// expect(readCheckbox, findsOneWidget);

// await tester.tap(agreeCheckbox);
// await tester.pumpAndSettle();

// verify(() => onBoardingTosCubit.setAgreeTerms(agreeTerms: true))
// .called(1);
// });

testWidgets('blocks going back from OnBoardingTosPage start page',
(tester) async {
await tester.pumpApp(const OnBoardingTosPage());
Expand All @@ -46,6 +91,29 @@ void main() {
expect(find.byType(OnBoardingTosPage), findsOneWidget);
});

testWidgets(
'MyGradientButton press calls onAcceptancePressed',
(WidgetTester tester) async {
await tester.pumpApp(
BlocProvider.value(
value: onBoardingTosCubit,
child: const OnBoardingTosPage(),
),
);

when(() => onBoardingTosCubit.state).thenReturn(
const OnBoardingTosState(
agreeTerms: true,
readTerms: true,
),
);
await tester.pump();

final button = find.text('Start'.toUpperCase());
expect(button, findsOneWidget);
},
);

testWidgets('renders DisplayTerms', (tester) async {
await tester.pumpApp(const OnBoardingTosPage());
await tester.pumpAndSettle();
Expand Down

0 comments on commit a7083e8

Please sign in to comment.