-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TW-2046 Added unit tests for compositeNameVaidator and verifyNameInte…
…ractor
- Loading branch information
1 parent
d6ba8e5
commit c95ec5f
Showing
10 changed files
with
182 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import 'package:dartz/dartz.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/exception/verify_name_exception.dart'; | ||
import 'package:fluffychat/domain/model/verification/new_name_request.dart'; | ||
import 'package:fluffychat/domain/model/verification/validator.dart'; | ||
|
||
class EmptyNameValidator extends Validator<NewNameRequest> { | ||
@override | ||
Either<Failure, Success> validate(NewNameRequest value) { | ||
if (value.value == null || value.value!.isEmpty) { | ||
return const Left<Failure, Success>( | ||
VerifyNameFailure(EmptyNameException()), | ||
); | ||
} else { | ||
return Right<Failure, Success>(VerifyNameSuccessViewState()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
test/domain/model/verification/composite_name_validator_test.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Right>()); | ||
expect(result, Right<Failure, Success>(VerifyNameSuccessViewState())); | ||
}); | ||
|
||
test( | ||
'applies multiple validators and returns failure if any validator fails', | ||
() { | ||
final newNameRequest = NewNameRequest('InvalidName'); | ||
|
||
when(mockValidator1.validate(newNameRequest)) | ||
.thenReturn(Right(VerifyNameSuccessViewState())); | ||
when(mockValidator2.validate(newNameRequest)) | ||
.thenReturn(const Left(VerifyNameFailure("Invalid name"))); | ||
|
||
final compositeValidator = | ||
CompositeNameValidator([mockValidator1, mockValidator2]); | ||
|
||
final result = compositeValidator.validate(newNameRequest); | ||
|
||
expect(result, isA<Left>()); | ||
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(VerifyNameSuccessViewState())); | ||
when(mockValidator2.validate(newNameRequest)) | ||
.thenReturn(Right(VerifyNameSuccessViewState())); | ||
|
||
final compositeValidator = | ||
CompositeNameValidator([mockValidator1, mockValidator2]); | ||
|
||
final result = compositeValidator.validate(newNameRequest); | ||
|
||
expect(result, isA<Right>()); | ||
verify(mockValidator1.validate(newNameRequest)).called(1); | ||
verify(mockValidator2.validate(newNameRequest)).called(1); | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(VerifyNameSuccessViewState())); | ||
when(mockValidator2.validate(any)) | ||
.thenReturn(Right(VerifyNameSuccessViewState())); | ||
|
||
final result = | ||
interactor.execute(newName, [mockValidator1, mockValidator2]); | ||
|
||
expect(result, isA<Right>()); | ||
expect(result, Right<Failure, Success>(VerifyNameSuccessViewState())); | ||
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(VerifyNameSuccessViewState())); | ||
when(mockValidator2.validate(any)) | ||
.thenReturn(const Left(VerifyNameFailure('Name is not valid'))); | ||
|
||
final result = | ||
interactor.execute(newName, [mockValidator1, mockValidator2]); | ||
|
||
expect(result, isA<Left>()); | ||
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<Left>()); | ||
expect((result as Left).value, isA<VerifyNameFailure>()); | ||
}); | ||
}); | ||
} |