diff --git a/test/domain/model/verification/composite_name_validator_test.dart b/test/domain/model/verification/composite_name_validator_test.dart new file mode 100644 index 000000000..8bf81d99e --- /dev/null +++ b/test/domain/model/verification/composite_name_validator_test.dart @@ -0,0 +1,73 @@ +import 'package:dartz/dartz.dart'; +import 'package:fluffychat/domain/model/verification/composite_name_validator.dart'; +import 'package:fluffychat/domain/model/verification/validator.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:mockito/annotations.dart'; +import 'package:mockito/mockito.dart'; +import 'package:fluffychat/app_state/failure.dart'; +import 'package:fluffychat/app_state/success.dart'; +import 'package:fluffychat/domain/app_state/validator/verify_name_view_state.dart'; +import 'package:fluffychat/domain/model/verification/new_name_request.dart'; +import 'composite_name_validator_test.mocks.dart'; + +@GenerateMocks([Validator]) +void main() { + group('CompositeNameValidator', () { + late CompositeNameValidator compositeValidator; + late MockValidator mockValidator1; + late MockValidator mockValidator2; + setUp(() { + compositeValidator = CompositeNameValidator([]); + mockValidator1 = MockValidator(); + mockValidator2 = MockValidator(); + }); + test('returns VerifyNameViewState when list of validators is empty', () { + final newNameRequest = NewNameRequest('ValidName'); + + final result = compositeValidator.validate(newNameRequest); + + expect(result, isA()); + expect(result, Right(VerifyNameViewState())); + }); + + test( + 'applies multiple validators and returns failure if any validator fails', + () { + final newNameRequest = NewNameRequest('InvalidName'); + + when(mockValidator1.validate(newNameRequest)) + .thenReturn(Right(VerifyNameViewState())); + when(mockValidator2.validate(newNameRequest)) + .thenReturn(const Left(VerifyNameFailure("Invalid name"))); + + final compositeValidator = + CompositeNameValidator([mockValidator1, mockValidator2]); + + final result = compositeValidator.validate(newNameRequest); + + expect(result, isA()); + verify(mockValidator1.validate(newNameRequest)).called(1); + verify(mockValidator2.validate(newNameRequest)).called(1); + }); + + test('returns success if all validators pass', () { + final mockValidator1 = MockValidator(); + final mockValidator2 = MockValidator(); + final newNameRequest = NewNameRequest('ValidName'); + + when(mockValidator1.validate(newNameRequest)) + .thenReturn(Right(VerifyNameViewState())); + when(mockValidator2.validate(newNameRequest)) + .thenReturn(Right(VerifyNameViewState())); + + final compositeValidator = + CompositeNameValidator([mockValidator1, mockValidator2]); + + final result = compositeValidator.validate(newNameRequest); + + expect(result, isA()); + verify(mockValidator1.validate(newNameRequest)).called(1); + verify(mockValidator2.validate(newNameRequest)).called(1); + }); + }); +} diff --git a/test/domain/usecase/verify_name_interactor_test.dart b/test/domain/usecase/verify_name_interactor_test.dart new file mode 100644 index 000000000..c31dbabb2 --- /dev/null +++ b/test/domain/usecase/verify_name_interactor_test.dart @@ -0,0 +1,72 @@ +import 'package:dartz/dartz.dart'; +import 'package:fluffychat/domain/usecase/verify_name_interactor.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:mockito/mockito.dart'; +import 'package:fluffychat/app_state/failure.dart'; +import 'package:fluffychat/app_state/success.dart'; +import 'package:fluffychat/domain/app_state/validator/verify_name_view_state.dart'; + +import '../model/verification/composite_name_validator_test.mocks.dart'; + +void main() { + group('VerifyNameInteractor', () { + late MockValidator mockValidator1; + late MockValidator mockValidator2; + late VerifyNameInteractor interactor; + setUp(() { + mockValidator1 = MockValidator(); + mockValidator2 = MockValidator(); + interactor = VerifyNameInteractor(); + }); + test('returns VerifyNameViewState success when all validators pass', () { + const newName = 'ValidName'; + + when(mockValidator1.validate(any)) + .thenReturn(Right(VerifyNameViewState())); + when(mockValidator2.validate(any)) + .thenReturn(Right(VerifyNameViewState())); + + final result = + interactor.execute(newName, [mockValidator1, mockValidator2]); + + expect(result, isA()); + expect(result, Right(VerifyNameViewState())); + verify(mockValidator1.validate(any)).called(1); + verify(mockValidator2.validate(any)).called(1); + }); + + test('returns failure when one of the validators fails', () { + final mockValidator1 = MockValidator(); + final mockValidator2 = MockValidator(); + final interactor = VerifyNameInteractor(); + const newName = 'InvalidName'; + + when(mockValidator1.validate(any)) + .thenReturn(Right(VerifyNameViewState())); + when(mockValidator2.validate(any)) + .thenReturn(const Left(VerifyNameFailure('Name is not valid'))); + + final result = + interactor.execute(newName, [mockValidator1, mockValidator2]); + + expect(result, isA()); + verify(mockValidator1.validate(any)).called(1); + verify(mockValidator2.validate(any)).called(1); + }); + + test('returns VerifyNameFailure when an exception occurs', () { + final mockValidator = MockValidator(); + final interactor = VerifyNameInteractor(); + const newName = 'ValidName'; + + // Mock a validator to throw an exception + when(mockValidator.validate(any)) + .thenThrow(Exception('Validation failed')); + + final result = interactor.execute(newName, [mockValidator]); + + expect(result, isA()); + expect((result as Left).value, isA()); + }); + }); +}