Skip to content

Commit

Permalink
Merge pull request #352 from CollActionteam/tests/fix-and-add-tests
Browse files Browse the repository at this point in the history
tests: add and fix tests
  • Loading branch information
Xazin authored Feb 12, 2023
2 parents 3519ad0 + f9ce74a commit 7fa9ecc
Show file tree
Hide file tree
Showing 20 changed files with 1,022 additions and 195 deletions.
2 changes: 1 addition & 1 deletion lib/application/user/profile_tab/profile_tab_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ part 'profile_tab_state.dart';
class ProfileTabBloc extends Bloc<ProfileTabEvent, ProfileTabState> {
final ICrowdActionRepository crowdActionRepository;

ProfileTabBloc({required this.crowdActionRepository})
ProfileTabBloc(this.crowdActionRepository)
: super(ProfileTabState.initial()) {
on<FetchProfileTabInfo>((event, emit) async {
final crowdActionsOrFailure =
Expand Down
10 changes: 0 additions & 10 deletions lib/domain/crowdaction/i_crowdaction_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,6 @@ abstract class ICrowdActionRepository {
Future<Either<CrowdActionFailure, List<CrowdAction>>>
getSpotlightCrowdActions();

Future<Either<CrowdActionFailure, Unit>> subscribeToCrowdAction(
CrowdAction crowdAction,
List<String> commitments,
String? password,
);

Future<Either<CrowdActionFailure, Unit>> unsubscribeFromCrowdAction(
CrowdAction crowdAction,
);

Future<Either<CrowdActionFailure, List<CrowdAction>>>
getCrowdActionsForUser();
}
1 change: 0 additions & 1 deletion lib/domain/user/user.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ class User with _$User {

bool get isAnonymous => this == anonymous;

/// TODO: Refactor after MVP to only neccessary fields (use profile microservice for details)
const factory User({
required String id,
required Future<String?> Function([bool forceRefresh]) getIdToken,
Expand Down
72 changes: 0 additions & 72 deletions lib/infrastructure/crowdaction/crowdaction_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -91,78 +91,6 @@ class CrowdActionRepository implements ICrowdActionRepository {
}
}

@override
Future<Either<CrowdActionFailure, Unit>> subscribeToCrowdAction(
CrowdAction crowdAction,
List<String> commitments,
String? password,
) async {
try {
final user = (await _authRepository.getSignedInUser())
.getOrElse(() => throw NotAuthenticatedError());
final tokenId = await user.getIdToken();

final uri = Uri.parse(
'${await _settingsRepository.baseApiEndpointUrl}/v1/participations',
);

final response = await _client.post(
uri,
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer $tokenId'
},
body: jsonEncode({
'crowdActionId': crowdAction.id,
'password': password,
'commitmentOptions': commitments
}),
);

if (response.statusCode == 200) {
return right(unit);
} else {
return left(const CrowdActionFailure.networkRequestFailed());
}
} catch (e) {
return left(const CrowdActionFailure.networkRequestFailed());
}
}

@override
Future<Either<CrowdActionFailure, Unit>> unsubscribeFromCrowdAction(
CrowdAction crowdAction,
) async {
try {
final user = (await _authRepository.getSignedInUser())
.getOrElse(() => throw NotAuthenticatedError());
final tokenId = await user.getIdToken();

final uri = Uri.parse(
'${await _settingsRepository.baseApiEndpointUrl}/v1/participations',
);

final response = await _client.post(
uri,
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer $tokenId'
},
body: jsonEncode({
'crowdActionId': crowdAction.id,
}),
);

if (response.statusCode == 200) {
return right(unit);
} else {
return left(const CrowdActionFailure.networkRequestFailed());
}
} catch (e) {
return left(const CrowdActionFailure.networkRequestFailed());
}
}

@override
Future<Either<CrowdActionFailure, List<CrowdAction>>>
getSpotlightCrowdActions() async {
Expand Down
2 changes: 0 additions & 2 deletions lib/presentation/core/collaction_icons.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
import 'package:flutter/widgets.dart';

class CollactionIcons {
const CollactionIcons._();

static const _kFontFam = 'CollactionIcons';

static const IconData eye = IconData(0xe800, fontFamily: _kFontFam);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ class WithdrawParticipation extends StatelessWidget {
fontSize: 28,
),
),

const SizedBox(
height: 20,
),
Expand All @@ -105,10 +104,6 @@ class WithdrawParticipation extends StatelessWidget {
margin: EdgeInsets.zero,
),
const SizedBox(height: 20),
//TODO - Add message to change commitments const Divider(),
// const SizedBox(
// height: 15,
// ),
],
),
);
Expand Down
1 change: 0 additions & 1 deletion lib/presentation/profile/profile_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,6 @@ class _UserProfilePageState extends State<UserProfilePage> {
),
],
] else ...[
// TODO only for MVP (remove later)
const SizedBox(height: 40),
PillButton(
text: 'Sign in',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ import 'package:collaction_app/domain/auth/i_auth_repository.dart';
import 'package:collaction_app/domain/user/i_avatar_repository.dart';
import 'package:collaction_app/domain/user/i_user_repository.dart';
import 'package:dartz/dartz.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mocktail/mocktail.dart';
// ignore: depend_on_referenced_packages
import 'package:test/test.dart';

class MockAuthRepository extends Mock implements IAuthRepository {}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import 'package:bloc_test/bloc_test.dart';
import 'package:collaction_app/application/settings/build_information/build_information_bloc.dart';

import 'package:collaction_app/domain/settings/build_information.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mocktail/mocktail.dart';

import '../../domain/core/i_settings_repository.mocks.dart';

void main() {
group('Testing Build Information BLoC', () {
late final SettingsRepositoryMock settingsRepository;
late final BuildInformationBloc bloc;

setUpAll(() {
settingsRepository = SettingsRepositoryMock();
bloc = BuildInformationBloc(settingsRepository);
});

test('Initial state', () {
expect(bloc.state, const BuildInformationState.loading());
});

blocTest(
'Fetch',
build: () => bloc,
act: (bloc) {
when(() => settingsRepository.getBuildInformation())
.thenAnswer((_) => Future.value(tBuildInformation));

bloc.add(BuildInformationEvent.fetch());
},
expect: () => [
const BuildInformationState.loading(),
BuildInformationState.fetched(tBuildInformation),
],
);
});
}

final tBuildInformation = BuildInformation(version: '1.0.0', buildNumber: '1');
49 changes: 34 additions & 15 deletions test/application/contact_form/contact_form_test.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:bloc_test/bloc_test.dart';
import 'package:collaction_app/application/contact_form/contact_form_bloc.dart';
import 'package:collaction_app/domain/contact_form/contact_failures.dart';
import 'package:dartz/dartz.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mocktail/mocktail.dart';
Expand All @@ -11,20 +12,38 @@ void main() {
expect(tContactFormBloc.state, const ContactFormState.initial());
});

{
when(() => tContactFormApi.sendContactFormContents(tContactFormDtoS))
.thenAnswer((_) => Future.value(right(unit)));
blocTest(
'Testing submit event success',
build: () => ContactFormBloc(tContactFormApi),
act: (ContactFormBloc bloc) {
bloc.add(ContactFormEvent.submitted(tContactFormDtoS));
},
expect: () => [
const ContactFormState.submitting(),
const ContactFormState.submissionSuccessful(),
],
);
}
blocTest(
'Testing submit event success',
build: () => ContactFormBloc(tContactFormApi),
act: (ContactFormBloc bloc) {
when(() => tContactFormApi.sendContactFormContents(tContactFormDtoS))
.thenAnswer((_) => Future.value(right(unit)));

bloc.add(ContactFormEvent.submitted(tContactFormDtoS));
},
expect: () => [
const ContactFormState.submitting(),
const ContactFormState.submissionSuccessful(),
],
);

blocTest(
'Testing submit event failure',
build: () => ContactFormBloc(tContactFormApi),
act: (ContactFormBloc bloc) {
when(() => tContactFormApi.sendContactFormContents(tContactFormDtoS))
.thenAnswer(
(_) => Future.value(
left(ContactFailure.serverError()),
),
);

bloc.add(ContactFormEvent.submitted(tContactFormDtoS));
},
expect: () => [
const ContactFormState.submitting(),
const ContactFormState.failed('Submission failed'),
],
);
});
}
69 changes: 69 additions & 0 deletions test/application/username/username_bloc_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import 'package:bloc_test/bloc_test.dart';
import 'package:collaction_app/application/username/username_bloc.dart';
import 'package:collaction_app/domain/user/profile_failure.dart';

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

import '../../test_utilities.dart';

void main() {
group('Testing Username BLoC', () {
late final MockProfileRepository profileRepository;
late UsernameBloc bloc;

setUpAll(() {
profileRepository = MockProfileRepository();
bloc = UsernameBloc(profileRepository);
});

setUp(() {
// Reset BLOC
bloc = UsernameBloc(profileRepository);
});

test('Initial state', () {
expect(bloc.state, const UsernameState.initial());
});

blocTest(
'Update username success',
build: () => bloc,
act: (bloc) {
when(() => profileRepository.updateUsername(
firstName: tFirstName, lastName: tLastName)).thenAnswer(
(_) => Future.value(right(unit)),
);

bloc.add(UsernameEvent.updateUsername(
firstName: tFirstName, lastName: tLastName));
},
expect: () => [
const UsernameState.updateInProgress(),
UsernameState.updateSuccess('$tFirstName $tLastName'),
],
);

blocTest(
'Update username failure',
build: () => bloc,
act: (bloc) {
when(() => profileRepository.updateUsername(
firstName: tFirstName, lastName: tLastName)).thenAnswer(
(_) => Future.value(left(ProfileFailure.unexpected())),
);

bloc.add(UsernameEvent.updateUsername(
firstName: tFirstName, lastName: tLastName));
},
expect: () => [
const UsernameState.updateInProgress(),
const UsernameState.updateFailure(),
],
);
});
}

final tFirstName = "Mathias";
final tLastName = "M";
Loading

0 comments on commit 7fa9ecc

Please sign in to comment.