Skip to content

Commit

Permalink
Update test according new real-time update changes
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-pratik-k committed Jul 21, 2023
1 parent c75f96c commit bbc4376
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 185 deletions.
5 changes: 4 additions & 1 deletion lib/data/bloc/user_state/user_state_controller_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ class UserStateControllerBloc
if (employee != null &&
space != null &&
employee.status == EmployeeStatus.active) {
add(UpdateUserData(employee: employee, space: space));
if (employee != _userStateNotifier.employee ||
space != _userStateNotifier.currentSpace) {
add(UpdateUserData(employee: employee, space: space));
}
return const UserControllerState(
userState: UserState.authenticated);
} else {
Expand Down
147 changes: 82 additions & 65 deletions test/unit_test/bloc/user_state/user_state_controller_bloc_test.dart
Original file line number Diff line number Diff line change
@@ -1,33 +1,32 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
import 'package:projectunity/data/Repo/employee_repo.dart';
import 'package:projectunity/data/bloc/user_state/user_controller_state.dart';
import 'package:projectunity/data/bloc/user_state/user_state_controller_bloc.dart';
import 'package:projectunity/data/bloc/user_state/user_state_controller_event.dart';
import 'package:projectunity/data/model/employee/employee.dart';
import 'package:projectunity/data/model/space/space.dart';
import 'package:projectunity/data/provider/user_state.dart';
import 'package:projectunity/data/services/employee_service.dart';
import 'package:projectunity/data/services/space_service.dart';

import 'user_state_controller_bloc_test.mocks.dart';

@GenerateMocks([EmployeeService, SpaceService, UserStateNotifier])
@GenerateMocks([EmployeeRepo, SpaceService, UserStateNotifier])
void main() {
late UserStateControllerBloc bloc;
late EmployeeService employeeService;
late EmployeeRepo employeeRepo;
late SpaceService spaceService;
late UserStateNotifier userStateNotifier;

final employee = Employee(
uid: 'uid',
name: 'Andrew jhone',
email: '[email protected]',
role: Role.admin,
status: EmployeeStatus.active,
dateOfJoining: DateTime(2000));




final Space space = Space(
id: 'space_id',
name: 'Google',
Expand All @@ -36,72 +35,90 @@ void main() {
ownerIds: [employee.uid]);

final Space newSpace = Space(
id: 'space)id',
id: 'new_space_id',
name: 'Alphabet',
createdAt: DateTime.fromMillisecondsSinceEpoch(20),
paidTimeOff: 20,
ownerIds: [employee.uid]);

setUp(() {
employeeService = MockEmployeeService();
spaceService = MockSpaceService();
userStateNotifier = MockUserStateNotifier();
bloc = UserStateControllerBloc(
employeeService, userStateNotifier, spaceService);
when(userStateNotifier.currentSpaceId).thenReturn(space.id);
when(userStateNotifier.userUID).thenReturn(employee.uid);
});
group('User state controller test', () {
setUp(() {
employeeRepo = MockEmployeeRepo();
spaceService = MockSpaceService();
userStateNotifier = MockUserStateNotifier();
bloc = UserStateControllerBloc(
employeeRepo, userStateNotifier, spaceService);
when(userStateNotifier.currentSpaceId).thenReturn(space.id);
when(userStateNotifier.employeeId).thenReturn(employee.uid);
});

test('Should emit initial state as default state of bloc', () {
expect(bloc.state, const UserControllerState());
});
test('Should emit initial state as default state of bloc', () {
expect(bloc.state, const UserControllerState());
});

test(
'Fetch data of user and space from firestore and update it on CheckUserStatusEvent',
() async {
when(userStateNotifier.currentSpace)
.thenReturn(space);
when(userStateNotifier.employee)
.thenReturn(employee);
bloc.add(CheckUserStatus());
when(employeeService.getEmployee(employee.uid))
.thenAnswer((_) async => employee);
when(spaceService.getSpace(space.id)).thenAnswer((_) async => newSpace);
expectLater(bloc.stream,
emits(const UserControllerState(userState: UserState.authenticated)));
await untilCalled(userStateNotifier.setEmployeeWithSpace(
space: newSpace, spaceUser: employee, redirect: false));
verify(userStateNotifier.setEmployeeWithSpace(
space: newSpace, spaceUser: employee,redirect: false))
.called(1);
});
test(
'Fetch data of user and space from firestore and if value found null then remove user from space',
() async {
bloc.add(CheckUserStatus());
when(employeeService.getEmployee(employee.uid))
.thenAnswer((_) async => employee);
when(spaceService.getSpace(space.id)).thenAnswer((_) async => null);
expectLater(bloc.stream,
emits(const UserControllerState(userState: UserState.unauthenticated)));
});
test(
'Fetch data of user and space from firestore and update it on CheckUserStatusEvent',
() async {
when(spaceService.getSpace(space.id)).thenAnswer((_) async => newSpace);
when(employeeRepo.memberDetails(employee.uid))
.thenAnswer((_) => Stream.value(employee));
when(userStateNotifier.currentSpace).thenReturn(space);
when(userStateNotifier.employee).thenReturn(employee);
bloc.add(CheckUserStatus());
expectLater(bloc.stream,
emits(const UserControllerState(userState: UserState.authenticated)));
await untilCalled(userStateNotifier.setEmployeeWithSpace(
space: newSpace, spaceUser: employee, redirect: false));
verify(userStateNotifier.setEmployeeWithSpace(
space: newSpace, spaceUser: employee, redirect: false))
.called(1);
});

test(
'Fetch data of user and space from firestore and if exception is thrown then emits state as disable user',
() async {
bloc.add(CheckUserStatus());
when(employeeService.getEmployee(employee.uid)).thenThrow(Exception());
when(spaceService.getSpace(space.id)).thenAnswer((_) async => space);
expectLater(bloc.stream,
emits(const UserControllerState(userState: UserState.unauthenticated)));
});
test(
'Clear data of user on ClearDataClearDataForDisableUser event when user status is disable',
() async {
when(userStateNotifier.state).thenReturn(UserState.update);
bloc.add(ClearDataForDisableUser());
await untilCalled(userStateNotifier.removeEmployeeWithSpace());
verify(userStateNotifier.removeEmployeeWithSpace()).called(1);
expectLater(userStateNotifier.state, UserState.update);
test(
'Fetch data of user and space from firestore and if value found null then remove user from space',
() async {
bloc.add(CheckUserStatus());
when(employeeRepo.memberDetails(employee.uid))
.thenAnswer((_) => Stream.value(employee));
when(spaceService.getSpace(space.id)).thenAnswer((_) async => null);
expectLater(
bloc.stream,
emits(
const UserControllerState(userState: UserState.unauthenticated)));
});

test(
'Fetch data of user and space from firestore and if error is emit then emits state as disable user',
() async {
bloc.add(CheckUserStatus());
when(employeeRepo.memberDetails(employee.uid))
.thenAnswer((_) => Stream.error('error'));
when(spaceService.getSpace(space.id)).thenAnswer((_) async => space);
expectLater(
bloc.stream,
emits(
const UserControllerState(userState: UserState.unauthenticated)));
});

test(
'Fetch data of user and space from firestore and if exception is thrown then emits state as disable user',
() async {
bloc.add(CheckUserStatus());
when(employeeRepo.memberDetails(employee.uid))
.thenThrow(Exception('error'));
when(spaceService.getSpace(space.id)).thenAnswer((_) async => space);
expectLater(
bloc.stream,
emits(
const UserControllerState(userState: UserState.unauthenticated)));
});

test(
'Clear data of user on ClearDataClearDataForDisableUser event when user status is disable',
() async {
bloc.add(ClearDataForDisableUser());
await untilCalled(userStateNotifier.removeEmployeeWithSpace());
verify(userStateNotifier.removeEmployeeWithSpace()).called(1);
});
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import 'package:projectunity/data/model/account/account.dart' as _i9;
import 'package:projectunity/data/model/employee/employee.dart' as _i4;
import 'package:projectunity/data/model/space/space.dart' as _i3;
import 'package:projectunity/data/provider/user_state.dart' as _i8;
import 'package:projectunity/data/services/employee_service.dart' as _i5;
import 'package:projectunity/data/Repo/employee_repo.dart' as _i5;
import 'package:projectunity/data/services/space_service.dart' as _i7;

// ignore_for_file: type=lint
Expand Down Expand Up @@ -57,146 +57,46 @@ class _FakeEmployee_2 extends _i1.SmartFake implements _i4.Employee {
);
}

/// A class which mocks [EmployeeService].
/// A class which mocks [EmployeeRepo].
///
/// See the documentation for Mockito's code generation for more information.
class MockEmployeeService extends _i1.Mock implements _i5.EmployeeService {
MockEmployeeService() {
class MockEmployeeRepo extends _i1.Mock implements _i5.EmployeeRepo {
MockEmployeeRepo() {
_i1.throwOnMissingStub(this);
}

@override
_i2.FirebaseFirestore get fireStore => (super.noSuchMethod(
Invocation.getter(#fireStore),
returnValue: _FakeFirebaseFirestore_0(
this,
Invocation.getter(#fireStore),
),
) as _i2.FirebaseFirestore);
@override
_i6.Stream<List<_i4.Employee>> employees(String? spaceId) =>
(super.noSuchMethod(
Invocation.method(
#employees,
[spaceId],
),
_i6.Stream<List<_i4.Employee>> get employees => (super.noSuchMethod(
Invocation.getter(#employees),
returnValue: _i6.Stream<List<_i4.Employee>>.empty(),
) as _i6.Stream<List<_i4.Employee>>);
@override
_i6.Future<void> addEmployeeBySpaceId({
required _i4.Employee? employee,
required String? spaceId,
}) =>
(super.noSuchMethod(
Invocation.method(
#addEmployeeBySpaceId,
[],
{
#employee: employee,
#spaceId: spaceId,
},
),
returnValue: _i6.Future<void>.value(),
returnValueForMissingStub: _i6.Future<void>.value(),
) as _i6.Future<void>);
@override
_i6.Future<_i4.Employee?> getEmployeeBySpaceId({
required String? userId,
required String? spaceId,
}) =>
(super.noSuchMethod(
Invocation.method(
#getEmployeeBySpaceId,
[],
{
#userId: userId,
#spaceId: spaceId,
},
),
returnValue: _i6.Future<_i4.Employee?>.value(),
) as _i6.Future<_i4.Employee?>);
@override
_i6.Future<List<_i4.Employee>> getEmployees() => (super.noSuchMethod(
Invocation.method(
#getEmployees,
[],
),
returnValue: _i6.Future<List<_i4.Employee>>.value(<_i4.Employee>[]),
) as _i6.Future<List<_i4.Employee>>);
@override
_i6.Future<_i4.Employee?> getEmployee(String? id) => (super.noSuchMethod(
Invocation.method(
#getEmployee,
[id],
),
returnValue: _i6.Future<_i4.Employee?>.value(),
) as _i6.Future<_i4.Employee?>);
@override
_i6.Future<bool> hasUser(String? email) => (super.noSuchMethod(
Invocation.method(
#hasUser,
[email],
),
returnValue: _i6.Future<bool>.value(false),
) as _i6.Future<bool>);
_i6.Stream<List<_i4.Employee>> get activeEmployees => (super.noSuchMethod(
Invocation.getter(#activeEmployees),
returnValue: _i6.Stream<List<_i4.Employee>>.empty(),
) as _i6.Stream<List<_i4.Employee>>);
@override
_i6.Future<void> addEmployee(_i4.Employee? employee) => (super.noSuchMethod(
_i6.Stream<_i4.Employee?> memberDetails(String? uid) => (super.noSuchMethod(
Invocation.method(
#addEmployee,
[employee],
#memberDetails,
[uid],
),
returnValue: _i6.Future<void>.value(),
returnValueForMissingStub: _i6.Future<void>.value(),
) as _i6.Future<void>);
returnValue: _i6.Stream<_i4.Employee?>.empty(),
) as _i6.Stream<_i4.Employee?>);
@override
_i6.Future<void> updateEmployeeDetails({required _i4.Employee? employee}) =>
(super.noSuchMethod(
_i6.Future<void> reset() => (super.noSuchMethod(
Invocation.method(
#updateEmployeeDetails,
#reset,
[],
{#employee: employee},
),
returnValue: _i6.Future<void>.value(),
returnValueForMissingStub: _i6.Future<void>.value(),
) as _i6.Future<void>);
@override
_i6.Future<void> changeEmployeeRoleType(
String? id,
_i4.Role? role,
) =>
(super.noSuchMethod(
Invocation.method(
#changeEmployeeRoleType,
[
id,
role,
],
),
returnValue: _i6.Future<void>.value(),
returnValueForMissingStub: _i6.Future<void>.value(),
) as _i6.Future<void>);
@override
_i6.Future<void> deleteEmployee(String? id) => (super.noSuchMethod(
_i6.Future<void> dispose() => (super.noSuchMethod(
Invocation.method(
#deleteEmployee,
[id],
),
returnValue: _i6.Future<void>.value(),
returnValueForMissingStub: _i6.Future<void>.value(),
) as _i6.Future<void>);
@override
_i6.Future<void> changeAccountStatus({
required String? id,
required _i4.EmployeeStatus? status,
}) =>
(super.noSuchMethod(
Invocation.method(
#changeAccountStatus,
#dispose,
[],
{
#id: id,
#status: status,
},
),
returnValue: _i6.Future<void>.value(),
returnValueForMissingStub: _i6.Future<void>.value(),
Expand Down

0 comments on commit bbc4376

Please sign in to comment.