Skip to content

Commit

Permalink
TW-2046 Added unit tests for compositeNameVaidator and verifyNameInte…
Browse files Browse the repository at this point in the history
…ractor
  • Loading branch information
KhaledNjim committed Nov 14, 2024
1 parent 5f610de commit 8f9dd2e
Show file tree
Hide file tree
Showing 2 changed files with 145 additions and 0 deletions.
73 changes: 73 additions & 0 deletions test/domain/model/verification/composite_name_validator_test.dart
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>(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<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(VerifyNameViewState()));
when(mockValidator2.validate(newNameRequest))
.thenReturn(Right(VerifyNameViewState()));

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);
});
});
}
72 changes: 72 additions & 0 deletions test/domain/usecase/verify_name_interactor_test.dart
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(VerifyNameViewState()));
when(mockValidator2.validate(any))
.thenReturn(Right(VerifyNameViewState()));

final result =
interactor.execute(newName, [mockValidator1, mockValidator2]);

expect(result, isA<Right>());
expect(result, Right<Failure, Success>(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<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>());
});
});
}

0 comments on commit 8f9dd2e

Please sign in to comment.