100 lines
3.4 KiB
Dart

// Copyright (C) 2022 WYATT GROUP
// Please see the AUTHORS file for details.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
import 'package:bloc_test/bloc_test.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mocktail/mocktail.dart';
import 'package:wyatt_authentication_bloc/wyatt_authentication_bloc.dart';
class MockAuthenticationRepository extends Mock
implements AuthenticationRepositoryInterface {}
class MockUser extends Mock implements UserInterface {}
void main() {
group('AuthenticationCubit', () {
final MockUser user = MockUser();
late AuthenticationRepositoryInterface authenticationRepository;
setUp(() {
authenticationRepository = MockAuthenticationRepository();
when(() => authenticationRepository.user).thenAnswer(
(_) => const Stream.empty(),
);
when(
() => authenticationRepository.currentUser,
).thenReturn(const UserFirebase.empty());
});
test('initial state is unknown', () {
expect(
AuthenticationCubit(authenticationRepository: authenticationRepository)
.state,
const AuthenticationState.unknown(),
);
});
group('UserChanged', () {
blocTest<AuthenticationCubit, AuthenticationState>(
'emits authenticated when user is not empty',
setUp: () {
when(() => user.isNotEmpty).thenReturn(true);
when(() => authenticationRepository.user).thenAnswer(
(_) => Stream.value(user),
);
},
build: () => AuthenticationCubit(
authenticationRepository: authenticationRepository,
)..init(),
seed: () => const AuthenticationState.unknown(),
expect: () => [AuthenticationState.authenticated(user, null)],
);
blocTest<AuthenticationCubit, AuthenticationState>(
'emits unauthenticated when user is empty',
setUp: () {
when(() => authenticationRepository.user).thenAnswer(
(_) => Stream.value(const UserFirebase.empty()),
);
},
build: () => AuthenticationCubit(
authenticationRepository: authenticationRepository,
)..init(),
seed: () => const AuthenticationState.unknown(),
expect: () => [const AuthenticationState.unauthenticated()],
);
});
group('LogoutRequested', () {
blocTest<AuthenticationCubit, AuthenticationState>(
'invokes signOut',
setUp: () {
when(
() => authenticationRepository.signOut(),
).thenAnswer((_) async {});
},
build: () => AuthenticationCubit(
authenticationRepository: authenticationRepository,
),
act: (AuthenticationCubit cubit) => cubit.logOut(),
verify: (_) {
verify(() => authenticationRepository.signOut()).called(1);
},
);
});
});
}